Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / xml / transform / XSLURIResolver.java
blob6a49caab458832b310eb0f8dca0394957ecfa087
1 /* XSLURIResolver.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.xml.transform;
40 import java.io.File;
41 import java.io.InputStream;
42 import java.io.IOException;
43 import java.io.Reader;
44 import java.net.MalformedURLException;
45 import java.net.URL;
46 import java.net.URLConnection;
47 import java.util.HashMap;
48 import java.util.Map;
49 import javax.xml.parsers.DocumentBuilder;
50 import javax.xml.parsers.DocumentBuilderFactory;
51 import javax.xml.transform.ErrorListener;
52 import javax.xml.transform.Source;
53 import javax.xml.transform.TransformerException;
54 import javax.xml.transform.URIResolver;
55 import javax.xml.transform.dom.DOMSource;
56 import javax.xml.transform.stream.StreamSource;
57 import org.w3c.dom.Node;
58 import org.xml.sax.InputSource;
59 import org.xml.sax.SAXException;
60 import gnu.xml.dom.ls.ReaderInputStream;
62 /**
63 * URI resolver for XSLT.
64 * This resolver parses external entities into DOMSources. It
65 * maintains a cache of URIs to DOMSources to avoid expensive re-parsing.
67 * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
69 class XSLURIResolver
70 implements URIResolver
73 Map lastModifiedCache = new HashMap();
74 Map nodeCache = new HashMap();
75 DocumentBuilder builder;
76 URIResolver userResolver;
77 ErrorListener userListener;
79 void setUserResolver(URIResolver userResolver)
81 this.userResolver = userResolver;
84 void setUserListener(ErrorListener userListener)
86 this.userListener = userListener;
89 /**
90 * Clear the cache.
92 void flush()
94 lastModifiedCache.clear();
95 nodeCache.clear();
98 public Source resolve(String href, String base)
99 throws TransformerException
101 Source source = null;
102 if (userResolver != null)
104 source = userResolver.resolve(base, href);
106 return resolveDOM(source, href, base);
109 DOMSource resolveDOM(Source source, String base, String href)
110 throws TransformerException
112 if (source != null && source instanceof DOMSource)
114 return (DOMSource) source;
116 String systemId = (source == null) ? null : source.getSystemId();
117 long lastModified = 0L, lastLastModified = 0L;
121 URL url = resolveURL(systemId, base, href);
122 Node node = null;
123 InputStream in = null;
124 if (source instanceof StreamSource)
126 StreamSource ss = (StreamSource) source;
127 in = ss.getInputStream();
128 if (in == null)
130 Reader reader = ss.getReader();
131 if (reader != null)
133 in = new ReaderInputStream(reader);
137 if (in == null)
139 if (url != null)
141 systemId = url.toString();
142 node = (Node) nodeCache.get(systemId);
143 // Is the resource up to date?
144 URLConnection conn = url.openConnection();
145 Long llm = (Long) lastModifiedCache.get(systemId);
146 if (llm != null)
148 lastLastModified = llm.longValue();
149 conn.setIfModifiedSince(lastLastModified);
151 conn.connect();
152 lastModified = conn.getLastModified();
153 if (node != null &&
154 lastModified > 0L &&
155 lastModified <= lastLastModified)
157 // Resource unchanged
158 return new DOMSource(node, systemId);
160 else
162 // Resource new or modified
163 in = conn.getInputStream();
164 nodeCache.put(systemId, node);
165 lastModifiedCache.put(systemId, new Long(lastModified));
168 else
170 throw new TransformerException("can't resolve URL: " +
171 systemId);
174 InputSource input = new InputSource(in);
175 input.setSystemId(systemId);
176 DocumentBuilder builder = getDocumentBuilder();
177 node = builder.parse(input);
178 return new DOMSource(node, systemId);
180 catch (IOException e)
182 throw new TransformerException(e);
184 catch (SAXException e)
186 throw new TransformerException(e);
190 URL resolveURL(String systemId, String base, String href)
191 throws IOException
193 URL url = null;
196 if (systemId != null)
200 url = new URL(systemId);
202 catch (MalformedURLException e)
204 // Try building from base + href
207 if (url == null)
209 if (base != null)
211 URL baseURL = new URL(base);
212 url = new URL(baseURL, href);
214 else if (href != null)
216 url = new URL(href);
218 else
220 // See below
221 throw new MalformedURLException(systemId);
224 return url;
226 catch (MalformedURLException e)
228 // Fall back to local filesystem
229 File file = null;
230 if (href == null)
232 href = systemId;
234 if (base != null)
236 int lsi = base.lastIndexOf(File.separatorChar);
237 if (lsi != -1 && lsi < base.length() - 1)
239 base = base.substring(0, lsi);
241 File baseFile = new File(base);
242 file = new File(baseFile, href);
244 else if (href != null)
246 file = new File(href);
248 return (file == null) ? null : file.toURL();
252 DocumentBuilder getDocumentBuilder()
253 throws TransformerException
257 if (builder == null)
259 DocumentBuilderFactory factory =
260 DocumentBuilderFactory.newInstance();
261 factory.setNamespaceAware(true);
262 factory.setExpandEntityReferences(true);
263 builder = factory.newDocumentBuilder();
265 if (userResolver != null)
267 builder.setEntityResolver(new URIResolverEntityResolver(userResolver));
269 if (userListener != null)
271 builder.setErrorHandler(new ErrorListenerErrorHandler(userListener));
273 return builder;
275 catch (Exception e)
277 throw new TransformerException(e);