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

Revision 2291, 3.7 KB (checked in by dmisev, 17 months ago)

fix DocumentCache? clearing

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 6, 2010, 11:57:26 AM
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 java.io.IOException;
20import java.io.InputStream;
21import java.net.MalformedURLException;
22import java.net.URI;
23import javax.xml.transform.Source;
24import javax.xml.transform.TransformerException;
25import javax.xml.transform.stream.StreamSource;
26import nu.xom.Document;
27import nu.xom.ParsingException;
28import org.omdoc.jomdoc.util.etc.Log;
29import org.omdoc.jomdoc.util.types.Pair;
30import org.omdoc.jomdoc.util.xml.XMLUtil;
31import org.omdoc.jomdoc.util.xml.XSLTUtil;
32
33/**
34 * This is the default URI resolver used in JOMDoc. It provides resolving of local filesystem
35 * or http resources.
36 *
37 * @author <a href="mailto:d.misev@jacobs-university.de">Dimitar Misev</a>
38 */
39public class DefaultJOMDocURIResolver extends AbstractJOMDocURIResolver {
40
41    public DefaultJOMDocURIResolver() {
42        super("file", "http");
43    }
44
45    public DefaultJOMDocURIResolver(String... supportedSchemes) {
46        super(supportedSchemes);
47    }
48
49    public Pair<InputStream, URI> resolveInputStream(String href, String base) throws ResolveException {
50        if (!isSchemaSupported(href, base, true)) {
51            return null;
52        }
53        URI absolute = URIUtil.toAbsoluteURI(href, base);
54        try {
55            return Pair.of(absolute.toURL().openStream(), absolute);
56        } catch (MalformedURLException ex) {
57            throw new ResolveException("Error converting the given URI " + absolute + " to URL", ex);
58        } catch (IOException ex) {
59            throw new ResolveException("I/O error while opening stream to " + absolute, ex);
60        }
61    }
62
63    @Override
64    public Document resolveDocument(String href, String base) throws ResolveException {
65        Pair<InputStream, URI> res = resolveInputStream(href, base);
66        if (res == null) {
67            return null;
68        }
69        InputStream is = res.fst;
70        String uri = (res.snd == null) ? null : res.snd.toString();
71        try {
72            return XMLUtil.buildDocument(uri, is);
73        } catch (IOException ex) {
74            log.warn(Log.array("Error reading document at " + uri, ex));
75        } catch (ParsingException ex) {
76            log.warn(Log.array("Error parsing document at " + uri, ex));
77        }
78        return null;
79    }
80
81    @Override
82    public Source resolve(String href, String base) throws TransformerException {
83        if (!isSchemaSupported(href, base, true)) {
84            return null;
85        }
86        String absolute = null;
87        try {
88            absolute = URIUtil.toAbsoluteURI(href, base).getPath();
89//            log.warn("Href: " + href + "\nbase: " + base + "\nabsolute: " + absolute + "\n");
90            href = absolute.substring(1); // remove leading /
91        } catch (ResolveException ex) {
92            throw new TransformerException("Error resolving uri " + href + " to absolute, against base: " + base, ex);
93        }
94        InputStream is = null;
95        try {
96            is = XSLTUtil.class.getClassLoader().getResourceAsStream(href);
97        } catch (Exception ex) {
98//            log.warn("Error resolving resource during transformation: " + href);
99        }
100        if (is != null) {
101            return new StreamSource(is, absolute);
102        } else {
103            return null;
104        }
105    }
106
107}
Note: See TracBrowser for help on using the browser.