6
|
1 |
/*
|
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
|
4 |
*/
|
|
5 |
/*
|
|
6 |
* Copyright 1999-2004 The Apache Software Foundation.
|
|
7 |
*
|
|
8 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
9 |
* you may not use this file except in compliance with the License.
|
|
10 |
* You may obtain a copy of the License at
|
|
11 |
*
|
|
12 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
13 |
*
|
|
14 |
* Unless required by applicable law or agreed to in writing, software
|
|
15 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
16 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
17 |
* See the License for the specific language governing permissions and
|
|
18 |
* limitations under the License.
|
|
19 |
*/
|
|
20 |
/*
|
|
21 |
* $Id: SourceTreeManager.java,v 1.2.4.1 2005/09/10 18:14:09 jeffsuttor Exp $
|
|
22 |
*/
|
|
23 |
package com.sun.org.apache.xpath.internal;
|
|
24 |
|
|
25 |
import java.io.IOException;
|
|
26 |
import java.util.Vector;
|
|
27 |
|
|
28 |
import javax.xml.transform.Source;
|
|
29 |
import javax.xml.transform.SourceLocator;
|
|
30 |
import javax.xml.transform.TransformerException;
|
|
31 |
import javax.xml.transform.URIResolver;
|
|
32 |
import javax.xml.transform.sax.SAXSource;
|
|
33 |
import javax.xml.transform.stream.StreamSource;
|
|
34 |
|
|
35 |
import com.sun.org.apache.xml.internal.dtm.DTM;
|
|
36 |
import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
|
|
37 |
|
|
38 |
import org.xml.sax.XMLReader;
|
|
39 |
import org.xml.sax.helpers.XMLReaderFactory;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* This class bottlenecks all management of source trees. The methods
|
|
43 |
* in this class should allow easy garbage collection of source
|
|
44 |
* trees (not yet!), and should centralize parsing for those source trees.
|
|
45 |
*/
|
|
46 |
public class SourceTreeManager
|
|
47 |
{
|
|
48 |
|
|
49 |
/** Vector of SourceTree objects that this manager manages. */
|
|
50 |
private Vector m_sourceTree = new Vector();
|
|
51 |
|
|
52 |
/**
|
|
53 |
* Reset the list of SourceTree objects that this manager manages.
|
|
54 |
*
|
|
55 |
*/
|
|
56 |
public void reset()
|
|
57 |
{
|
|
58 |
m_sourceTree = new Vector();
|
|
59 |
}
|
|
60 |
|
|
61 |
/** The TrAX URI resolver used to obtain source trees. */
|
|
62 |
URIResolver m_uriResolver;
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Set an object that will be used to resolve URIs used in
|
|
66 |
* document(), etc.
|
|
67 |
* @param resolver An object that implements the URIResolver interface,
|
|
68 |
* or null.
|
|
69 |
*/
|
|
70 |
public void setURIResolver(URIResolver resolver)
|
|
71 |
{
|
|
72 |
m_uriResolver = resolver;
|
|
73 |
}
|
|
74 |
|
|
75 |
/**
|
|
76 |
* Get the object that will be used to resolve URIs used in
|
|
77 |
* document(), etc.
|
|
78 |
* @return An object that implements the URIResolver interface,
|
|
79 |
* or null.
|
|
80 |
*/
|
|
81 |
public URIResolver getURIResolver()
|
|
82 |
{
|
|
83 |
return m_uriResolver;
|
|
84 |
}
|
|
85 |
|
|
86 |
/**
|
|
87 |
* Given a document, find the URL associated with that document.
|
|
88 |
* @param owner Document that was previously processed by this liaison.
|
|
89 |
*
|
|
90 |
* @return The base URI of the owner argument.
|
|
91 |
*/
|
|
92 |
public String findURIFromDoc(int owner)
|
|
93 |
{
|
|
94 |
int n = m_sourceTree.size();
|
|
95 |
|
|
96 |
for (int i = 0; i < n; i++)
|
|
97 |
{
|
|
98 |
SourceTree sTree = (SourceTree) m_sourceTree.elementAt(i);
|
|
99 |
|
|
100 |
if (owner == sTree.m_root)
|
|
101 |
return sTree.m_url;
|
|
102 |
}
|
|
103 |
|
|
104 |
return null;
|
|
105 |
}
|
|
106 |
|
|
107 |
/**
|
|
108 |
* This will be called by the processor when it encounters
|
|
109 |
* an xsl:include, xsl:import, or document() function.
|
|
110 |
*
|
|
111 |
* @param base The base URI that should be used.
|
|
112 |
* @param urlString Value from an xsl:import or xsl:include's href attribute,
|
|
113 |
* or a URI specified in the document() function.
|
|
114 |
*
|
|
115 |
* @return a Source that can be used to process the resource.
|
|
116 |
*
|
|
117 |
* @throws IOException
|
|
118 |
* @throws TransformerException
|
|
119 |
*/
|
|
120 |
public Source resolveURI(
|
|
121 |
String base, String urlString, SourceLocator locator)
|
|
122 |
throws TransformerException, IOException
|
|
123 |
{
|
|
124 |
|
|
125 |
Source source = null;
|
|
126 |
|
|
127 |
if (null != m_uriResolver)
|
|
128 |
{
|
|
129 |
source = m_uriResolver.resolve(urlString, base);
|
|
130 |
}
|
|
131 |
|
|
132 |
if (null == source)
|
|
133 |
{
|
|
134 |
String uri = SystemIDResolver.getAbsoluteURI(urlString, base);
|
|
135 |
|
|
136 |
source = new StreamSource(uri);
|
|
137 |
}
|
|
138 |
|
|
139 |
return source;
|
|
140 |
}
|
|
141 |
|
|
142 |
/** JJK: Support <?xalan:doc_cache_off?> kluge in ElemForEach.
|
|
143 |
* TODO: This function is highly dangerous. Cache management must be improved.
|
|
144 |
*
|
|
145 |
* @param n The node to remove.
|
|
146 |
*/
|
|
147 |
public void removeDocumentFromCache(int n)
|
|
148 |
{
|
|
149 |
if(DTM.NULL ==n)
|
|
150 |
return;
|
|
151 |
for(int i=m_sourceTree.size()-1;i>=0;--i)
|
|
152 |
{
|
|
153 |
SourceTree st=(SourceTree)m_sourceTree.elementAt(i);
|
|
154 |
if(st!=null && st.m_root==n)
|
|
155 |
{
|
|
156 |
m_sourceTree.removeElementAt(i);
|
|
157 |
return;
|
|
158 |
}
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
/**
|
|
165 |
* Put the source tree root node in the document cache.
|
|
166 |
* TODO: This function needs to be a LOT more sophisticated.
|
|
167 |
*
|
|
168 |
* @param n The node to cache.
|
|
169 |
* @param source The Source object to cache.
|
|
170 |
*/
|
|
171 |
public void putDocumentInCache(int n, Source source)
|
|
172 |
{
|
|
173 |
|
|
174 |
int cachedNode = getNode(source);
|
|
175 |
|
|
176 |
if (DTM.NULL != cachedNode)
|
|
177 |
{
|
|
178 |
if (!(cachedNode == n))
|
|
179 |
throw new RuntimeException(
|
|
180 |
"Programmer's Error! "
|
|
181 |
+ "putDocumentInCache found reparse of doc: "
|
|
182 |
+ source.getSystemId());
|
|
183 |
return;
|
|
184 |
}
|
|
185 |
if (null != source.getSystemId())
|
|
186 |
{
|
|
187 |
m_sourceTree.addElement(new SourceTree(n, source.getSystemId()));
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
|
191 |
/**
|
|
192 |
* Given a Source object, find the node associated with it.
|
|
193 |
*
|
|
194 |
* @param source The Source object to act as the key.
|
|
195 |
*
|
|
196 |
* @return The node that is associated with the Source, or null if not found.
|
|
197 |
*/
|
|
198 |
public int getNode(Source source)
|
|
199 |
{
|
|
200 |
|
|
201 |
// if (source instanceof DOMSource)
|
|
202 |
// return ((DOMSource) source).getNode();
|
|
203 |
|
|
204 |
// TODO: Not sure if the BaseID is really the same thing as the ID.
|
|
205 |
String url = source.getSystemId();
|
|
206 |
|
|
207 |
if (null == url)
|
|
208 |
return DTM.NULL;
|
|
209 |
|
|
210 |
int n = m_sourceTree.size();
|
|
211 |
|
|
212 |
// System.out.println("getNode: "+n);
|
|
213 |
for (int i = 0; i < n; i++)
|
|
214 |
{
|
|
215 |
SourceTree sTree = (SourceTree) m_sourceTree.elementAt(i);
|
|
216 |
|
|
217 |
// System.out.println("getNode - url: "+url);
|
|
218 |
// System.out.println("getNode - sTree.m_url: "+sTree.m_url);
|
|
219 |
if (url.equals(sTree.m_url))
|
|
220 |
return sTree.m_root;
|
|
221 |
}
|
|
222 |
|
|
223 |
// System.out.println("getNode - returning: "+node);
|
|
224 |
return DTM.NULL;
|
|
225 |
}
|
|
226 |
|
|
227 |
/**
|
|
228 |
* Get the source tree from the a base URL and a URL string.
|
|
229 |
*
|
|
230 |
* @param base The base URI to use if the urlString is relative.
|
|
231 |
* @param urlString An absolute or relative URL string.
|
|
232 |
* @param locator The location of the caller, for diagnostic purposes.
|
|
233 |
*
|
|
234 |
* @return should be a non-null reference to the node identified by the
|
|
235 |
* base and urlString.
|
|
236 |
*
|
|
237 |
* @throws TransformerException If the URL can not resolve to a node.
|
|
238 |
*/
|
|
239 |
public int getSourceTree(
|
|
240 |
String base, String urlString, SourceLocator locator, XPathContext xctxt)
|
|
241 |
throws TransformerException
|
|
242 |
{
|
|
243 |
|
|
244 |
// System.out.println("getSourceTree");
|
|
245 |
try
|
|
246 |
{
|
|
247 |
Source source = this.resolveURI(base, urlString, locator);
|
|
248 |
|
|
249 |
// System.out.println("getSourceTree - base: "+base+", urlString: "+urlString+", source: "+source.getSystemId());
|
|
250 |
return getSourceTree(source, locator, xctxt);
|
|
251 |
}
|
|
252 |
catch (IOException ioe)
|
|
253 |
{
|
|
254 |
throw new TransformerException(ioe.getMessage(), locator, ioe);
|
|
255 |
}
|
|
256 |
|
|
257 |
/* catch (TransformerException te)
|
|
258 |
{
|
|
259 |
throw new TransformerException(te.getMessage(), locator, te);
|
|
260 |
}*/
|
|
261 |
}
|
|
262 |
|
|
263 |
/**
|
|
264 |
* Get the source tree from the input source.
|
|
265 |
*
|
|
266 |
* @param source The Source object that should identify the desired node.
|
|
267 |
* @param locator The location of the caller, for diagnostic purposes.
|
|
268 |
*
|
|
269 |
* @return non-null reference to a node.
|
|
270 |
*
|
|
271 |
* @throws TransformerException if the Source argument can't be resolved to
|
|
272 |
* a node.
|
|
273 |
*/
|
|
274 |
public int getSourceTree(Source source, SourceLocator locator, XPathContext xctxt)
|
|
275 |
throws TransformerException
|
|
276 |
{
|
|
277 |
|
|
278 |
int n = getNode(source);
|
|
279 |
|
|
280 |
if (DTM.NULL != n)
|
|
281 |
return n;
|
|
282 |
|
|
283 |
n = parseToNode(source, locator, xctxt);
|
|
284 |
|
|
285 |
if (DTM.NULL != n)
|
|
286 |
putDocumentInCache(n, source);
|
|
287 |
|
|
288 |
return n;
|
|
289 |
}
|
|
290 |
|
|
291 |
/**
|
|
292 |
* Try to create a DOM source tree from the input source.
|
|
293 |
*
|
|
294 |
* @param source The Source object that identifies the source node.
|
|
295 |
* @param locator The location of the caller, for diagnostic purposes.
|
|
296 |
*
|
|
297 |
* @return non-null reference to node identified by the source argument.
|
|
298 |
*
|
|
299 |
* @throws TransformerException if the source argument can not be resolved
|
|
300 |
* to a source node.
|
|
301 |
*/
|
|
302 |
public int parseToNode(Source source, SourceLocator locator, XPathContext xctxt)
|
|
303 |
throws TransformerException
|
|
304 |
{
|
|
305 |
|
|
306 |
try
|
|
307 |
{
|
|
308 |
Object xowner = xctxt.getOwnerObject();
|
|
309 |
DTM dtm;
|
|
310 |
if(null != xowner && xowner instanceof com.sun.org.apache.xml.internal.dtm.DTMWSFilter)
|
|
311 |
{
|
|
312 |
dtm = xctxt.getDTM(source, false,
|
|
313 |
(com.sun.org.apache.xml.internal.dtm.DTMWSFilter)xowner, false, true);
|
|
314 |
}
|
|
315 |
else
|
|
316 |
{
|
|
317 |
dtm = xctxt.getDTM(source, false, null, false, true);
|
|
318 |
}
|
|
319 |
return dtm.getDocument();
|
|
320 |
}
|
|
321 |
catch (Exception e)
|
|
322 |
{
|
|
323 |
//e.printStackTrace();
|
|
324 |
throw new TransformerException(e.getMessage(), locator, e);
|
|
325 |
}
|
|
326 |
|
|
327 |
}
|
|
328 |
|
|
329 |
/**
|
|
330 |
* This method returns the SAX2 parser to use with the InputSource
|
|
331 |
* obtained from this URI.
|
|
332 |
* It may return null if any SAX2-conformant XML parser can be used,
|
|
333 |
* or if getInputSource() will also return null. The parser must
|
|
334 |
* be free for use (i.e.
|
|
335 |
* not currently in use for another parse().
|
|
336 |
*
|
|
337 |
* @param inputSource The value returned from the URIResolver.
|
|
338 |
* @return a SAX2 XMLReader to use to resolve the inputSource argument.
|
|
339 |
* @param locator The location of the original caller, for diagnostic purposes.
|
|
340 |
*
|
|
341 |
* @throws TransformerException if the reader can not be created.
|
|
342 |
*/
|
|
343 |
public static XMLReader getXMLReader(Source inputSource, SourceLocator locator)
|
|
344 |
throws TransformerException
|
|
345 |
{
|
|
346 |
|
|
347 |
try
|
|
348 |
{
|
|
349 |
XMLReader reader = (inputSource instanceof SAXSource)
|
|
350 |
? ((SAXSource) inputSource).getXMLReader() : null;
|
|
351 |
|
|
352 |
if (null == reader)
|
|
353 |
{
|
|
354 |
try {
|
|
355 |
javax.xml.parsers.SAXParserFactory factory=
|
|
356 |
javax.xml.parsers.SAXParserFactory.newInstance();
|
|
357 |
factory.setNamespaceAware( true );
|
|
358 |
javax.xml.parsers.SAXParser jaxpParser=
|
|
359 |
factory.newSAXParser();
|
|
360 |
reader=jaxpParser.getXMLReader();
|
|
361 |
|
|
362 |
} catch( javax.xml.parsers.ParserConfigurationException ex ) {
|
|
363 |
throw new org.xml.sax.SAXException( ex );
|
|
364 |
} catch( javax.xml.parsers.FactoryConfigurationError ex1 ) {
|
|
365 |
throw new org.xml.sax.SAXException( ex1.toString() );
|
|
366 |
} catch( NoSuchMethodError ex2 ) {
|
|
367 |
}
|
|
368 |
catch (AbstractMethodError ame){}
|
|
369 |
if(null == reader)
|
|
370 |
reader = XMLReaderFactory.createXMLReader();
|
|
371 |
}
|
|
372 |
|
|
373 |
try
|
|
374 |
{
|
|
375 |
reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
|
|
376 |
true);
|
|
377 |
}
|
|
378 |
catch (org.xml.sax.SAXException se)
|
|
379 |
{
|
|
380 |
|
|
381 |
// What can we do?
|
|
382 |
// TODO: User diagnostics.
|
|
383 |
}
|
|
384 |
|
|
385 |
return reader;
|
|
386 |
}
|
|
387 |
catch (org.xml.sax.SAXException se)
|
|
388 |
{
|
|
389 |
throw new TransformerException(se.getMessage(), locator, se);
|
|
390 |
}
|
|
391 |
}
|
|
392 |
}
|