root/src/jomdoc/trunk/src/jomdoc/org/omdoc/jomdoc/util/resolver/AbstractJOMDocURIResolver.java

Revision 2231, 6.9 KB (checked in by vzholudev, 19 months ago)

Minor fix

Line 
1/*
2 * JOMDoc - A Java library for OMDoc documents (http://omdoc.org/jomdoc).
3 *
4 * Original author    Dimitar Misev <d.misev@jacobs-university.de>
5 * Web                http://kwarc.info/dmisev/
6 * Created            Feb 5, 2010, 4:19:28 PM
7 *
8 * Filename           $Id$
9 * Revision           $Revision$
10 * Last modified on   $Date$
11 *               by   $Author$
12 *
13 * Copyright (C) 2007,2008 the KWARC group (http://kwarc.info)
14 * Licensed under the GNU Public License v3 (GPL3).
15 * For other licensing contact Michael Kohlhase <m.kohlhase@jacobs-university.de>
16 */
17package org.omdoc.jomdoc.util.resolver;
18
19import com.thaiopensource.validation.LSInputImpl;
20import java.io.InputStream;
21import java.net.URI;
22import java.net.URISyntaxException;
23import java.util.Arrays;
24import java.util.HashSet;
25import java.util.Set;
26import javax.xml.transform.Source;
27import javax.xml.transform.TransformerException;
28import javax.xml.transform.stream.StreamSource;
29import nu.xom.Document;
30import nu.xom.Element;
31import org.apache.log4j.Logger;
32import org.omdoc.jomdoc.lang.OMDocDocument;
33import org.omdoc.jomdoc.lang.OMDocElement;
34import org.omdoc.jomdoc.parser.Parser;
35import org.omdoc.jomdoc.util.err.ErrorUtil;
36import org.omdoc.jomdoc.util.etc.IOUtil;
37import org.omdoc.jomdoc.util.etc.Log;
38import org.omdoc.jomdoc.util.types.Pair;
39import org.omdoc.jomdoc.util.xml.RelaxNGValidator;
40import org.omdoc.jomdoc.util.xml.XMLUtil;
41import org.omdoc.jomdoc.util.xml.XSLTUtil;
42import org.w3c.dom.ls.LSInput;
43
44/**
45 * This is a default implementation of {@link JOMDocURIResolver}. Users that want
46 * to use their own URIResolver in JOMDoc will typically extend from this class,
47 * specifying supported URI schemes with {@link #AbstractJOMDocURIResolver(String[])}.
48 * <p>
49 *
50 * @author <a href="mailto:d.misev@jacobs-university.de">Dimitar Misev</a>
51 */
52public abstract class AbstractJOMDocURIResolver implements JOMDocURIResolver {
53
54    static Logger log = Logger.getLogger(AbstractJOMDocURIResolver.class);
55
56    protected Set<String> supportedSchemas;
57
58    protected AbstractJOMDocURIResolver(String... supportedSchemas) {
59        if (supportedSchemas == null) {
60            throw new IllegalArgumentException("A resolver must support at least one scheme");
61        }
62        this.supportedSchemas = new HashSet<String>();
63        this.supportedSchemas.addAll(Arrays.asList(supportedSchemas));
64    }
65
66    public Source resolve(String href, String base) throws TransformerException {
67        if (!isSchemaSupported(href, base, true)) {
68            return null;
69        }
70        Pair<? extends InputStream, URI> p = null;
71        try {
72             p = resolveInputStream(href, base);
73        } catch (ResolveException ex) {
74            log.warn(Log.array("Error resolving resource during transformation: " + href, ErrorUtil.errorMessage(ex)));
75        }
76        if (p != null && p.fst != null && p.snd != null) {
77            return new StreamSource(p.fst, p.snd.toString());
78        } else {
79            return null;
80        }
81    }
82
83    public Element resolveElement(String href, String base) throws ResolveException {
84        Pair<String, String> p = URIUtil.splitURI(href);
85        Document doc = resolveDocument(p.fst, base);
86        if (doc == null) {
87            return null;
88        }
89        Element root = doc.getRootElement();
90        if (p.snd == null) {
91            return root;
92        } else {
93            return XMLUtil.childWithId(root, p.snd);
94        }
95    }
96
97    public final OMDocDocument resolveOMDocDocument(String href, String base) throws ResolveException {
98        Document doc = resolveDocument(href, base);
99        if (doc == null) {
100            return null;
101        }
102        try {
103            return Parser.parse(doc);
104        } catch (URISyntaxException ex) {
105            throw new ResolveException(ex);
106        } catch (Exception ex) {
107            throw new ResolveException("Error parsing XOM Document to OMDocDocument.", ex);
108        }
109    }
110
111    public final OMDocElement resolveOMDocElement(String href, String base) throws ResolveException {
112        Element e = resolveElement(href, base);
113        if (e == null) {
114            return null;
115        }
116        try {
117            return (OMDocElement) Parser.parse(e);
118        } catch (URISyntaxException ex) {
119            throw new ResolveException(ex);
120        } catch (Exception ex) {
121            throw new ResolveException("Error parsing XOM Element to OMDocElement.", ex);
122        }
123    }
124
125    public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
126        LSInput ret = new LSInputImpl();
127        ret.setPublicId(publicId);
128        try {
129            // resolve with default
130            InputStream is = RelaxNGValidator.class.getClassLoader().getResourceAsStream(RelaxNGValidator.SCHEMA_DIR + systemId);
131            ret.setSystemId(systemId);
132            if (is == null) {
133                // if it fails use resolveInputStream
134                Pair<? extends InputStream, URI> res = null;
135                if (baseURI != null && (!baseURI.contains(":") || !isSchemaSupported(baseURI))) {
136                    baseURI = "file:" + baseURI;
137                }
138                res = resolveInputStream(systemId, baseURI);
139                if (res == null) {
140                    return ret;
141                }
142                is = res.fst;
143                if (res.snd != null) {
144                    ret.setBaseURI(res.snd.toString());
145                    // the scheme must be removed, doesn't work otherwise
146                    ret.setSystemId(IOUtil.removeScheme(res.snd.toString()));
147                }
148            }
149            ret.setByteStream(is);
150        } catch (Exception ex) {
151            log.warn(Log.array("Resolve error in RelaxNG validation", ex));
152        }
153        return ret;
154    }
155
156    public boolean isSchemaSupported(String uri) {
157        String schema = IOUtil.getScheme(uri);
158        if (schema == null) {
159            return false;
160        }
161        return supportedSchemas.contains(schema);
162    }
163
164    public boolean isSchemaSupported(String href, String base, boolean print) {
165        URI absURI = null;
166        try {
167            absURI = URIUtil.toAbsoluteURI(href, base);
168        } catch (ResolveException e) {
169            log.warn(String.format("Exception while resolving href %s against base $b", href, base));
170            return false;
171        }
172        return isSchemaSupported(absURI.toString());
173
174//        if (base != null)
175//            if (!isSchemaSupported(base)) {
176//                if (print) {
177//                    log.warn(Log.array("URI schema of " + base + "\nis not supported by this URI resolver"));
178//                }
179//                return false;
180//            } else {
181//                return true;
182//            }
183//        if (!isSchemaSupported(href)) {
184//            if (print) {
185//                log.warn(Log.array("URI schema of " + href + "\nis not supported by this URI resolver"));
186//            }
187//            return false;
188//        }
189//        return true;
190    }
191}
Note: See TracBrowser for help on using the browser.