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)
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., 59 Temple Place, Suite 330, Boston, MA
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
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
;
41 import java
.io
.InputStream
;
42 import java
.io
.IOException
;
43 import java
.io
.Reader
;
44 import java
.net
.MalformedURLException
;
46 import java
.net
.URLConnection
;
47 import java
.util
.HashMap
;
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
;
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>
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
;
94 lastModifiedCache
.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
);
123 InputStream in
= null;
124 if (source
instanceof StreamSource
)
126 StreamSource ss
= (StreamSource
) source
;
127 in
= ss
.getInputStream();
130 Reader reader
= ss
.getReader();
133 in
= new ReaderInputStream(reader
);
137 if (in
== null && url
!= null)
139 systemId
= url
.toString();
140 node
= (Node
) nodeCache
.get(systemId
);
141 // Is the resource up to date?
142 URLConnection conn
= url
.openConnection();
143 Long llm
= (Long
) lastModifiedCache
.get(systemId
);
146 lastLastModified
= llm
.longValue();
147 conn
.setIfModifiedSince(lastLastModified
);
150 lastModified
= conn
.getLastModified();
153 lastModified
<= lastLastModified
)
155 // Resource unchanged
156 return new DOMSource(node
, systemId
);
160 // Resource new or modified
161 in
= conn
.getInputStream();
162 nodeCache
.put(systemId
, node
);
163 lastModifiedCache
.put(systemId
, new Long(lastModified
));
166 InputSource input
= new InputSource(in
);
167 input
.setSystemId(systemId
);
168 DocumentBuilder builder
= getDocumentBuilder();
169 node
= builder
.parse(input
);
170 return new DOMSource(node
, systemId
);
172 catch (IOException e
)
174 throw new TransformerException(e
);
176 catch (SAXException e
)
178 throw new TransformerException(e
);
182 URL
resolveURL(String systemId
, String base
, String href
)
188 if (systemId
!= null)
192 url
= new URL(systemId
);
194 catch (MalformedURLException e
)
196 // Try building from base + href
203 URL baseURL
= new URL(base
);
204 url
= new URL(baseURL
, href
);
206 else if (href
!= null)
213 catch (MalformedURLException e
)
215 // Fall back to local filesystem
223 int lsi
= base
.lastIndexOf(File
.separatorChar
);
224 if (lsi
!= -1 && lsi
< base
.length() - 1)
226 base
= base
.substring(0, lsi
);
228 File baseFile
= new File(base
);
229 file
= new File(baseFile
, href
);
231 else if (href
!= null)
233 file
= new File(href
);
235 return (file
== null) ?
null : file
.toURL();
239 DocumentBuilder
getDocumentBuilder()
240 throws TransformerException
246 DocumentBuilderFactory factory
=
247 DocumentBuilderFactory
.newInstance();
248 factory
.setNamespaceAware(true);
249 factory
.setExpandEntityReferences(true);
250 builder
= factory
.newDocumentBuilder();
252 if (userResolver
!= null)
254 builder
.setEntityResolver(new URIResolverEntityResolver(userResolver
));
256 if (userListener
!= null)
258 builder
.setErrorHandler(new ErrorListenerErrorHandler(userListener
));
264 throw new TransformerException(e
);