1 16 19 package org.apache.xpath; 20 21 import java.io.IOException ; 22 import java.util.Vector ; 23 24 import javax.xml.transform.Source ; 25 import javax.xml.transform.SourceLocator ; 26 import javax.xml.transform.TransformerException ; 27 import javax.xml.transform.URIResolver ; 28 import javax.xml.transform.sax.SAXSource ; 29 import javax.xml.transform.stream.StreamSource ; 30 31 import org.apache.xml.dtm.DTM; 32 import org.apache.xml.utils.SystemIDResolver; 33 34 import org.xml.sax.XMLReader ; 35 import org.xml.sax.helpers.XMLReaderFactory ; 36 37 42 public class SourceTreeManager 43 { 44 45 46 private Vector m_sourceTree = new Vector (); 47 48 52 public void reset() 53 { 54 m_sourceTree = new Vector (); 55 } 56 57 58 URIResolver m_uriResolver; 59 60 66 public void setURIResolver(URIResolver resolver) 67 { 68 m_uriResolver = resolver; 69 } 70 71 77 public URIResolver getURIResolver() 78 { 79 return m_uriResolver; 80 } 81 82 88 public String findURIFromDoc(int owner) 89 { 90 int n = m_sourceTree.size(); 91 92 for (int i = 0; i < n; i++) 93 { 94 SourceTree sTree = (SourceTree) m_sourceTree.elementAt(i); 95 96 if (owner == sTree.m_root) 97 return sTree.m_url; 98 } 99 100 return null; 101 } 102 103 116 public Source resolveURI( 117 String base, String urlString, SourceLocator locator) 118 throws TransformerException , IOException 119 { 120 121 Source source = null; 122 123 if (null != m_uriResolver) 124 { 125 source = m_uriResolver.resolve(urlString, base); 126 } 127 128 if (null == source) 129 { 130 String uri = SystemIDResolver.getAbsoluteURI(urlString, base); 131 132 source = new StreamSource (uri); 133 } 134 135 return source; 136 } 137 138 143 public void removeDocumentFromCache(int n) 144 { 145 if(DTM.NULL ==n) 146 return; 147 for(int i=m_sourceTree.size()-1;i>=0;--i) 148 { 149 SourceTree st=(SourceTree)m_sourceTree.elementAt(i); 150 if(st!=null && st.m_root==n) 151 { 152 m_sourceTree.removeElementAt(i); 153 return; 154 } 155 } 156 } 157 158 159 160 167 public void putDocumentInCache(int n, Source source) 168 { 169 170 int cachedNode = getNode(source); 171 172 if (DTM.NULL != cachedNode) 173 { 174 if (!(cachedNode == n)) 175 throw new RuntimeException ( 176 "Programmer's Error! " 177 + "putDocumentInCache found reparse of doc: " 178 + source.getSystemId()); 179 return; 180 } 181 if (null != source.getSystemId()) 182 { 183 m_sourceTree.addElement(new SourceTree(n, source.getSystemId())); 184 } 185 } 186 187 194 public int getNode(Source source) 195 { 196 197 200 String url = source.getSystemId(); 202 203 if (null == url) 204 return DTM.NULL; 205 206 int n = m_sourceTree.size(); 207 208 for (int i = 0; i < n; i++) 210 { 211 SourceTree sTree = (SourceTree) m_sourceTree.elementAt(i); 212 213 if (url.equals(sTree.m_url)) 216 return sTree.m_root; 217 } 218 219 return DTM.NULL; 221 } 222 223 235 public int getSourceTree( 236 String base, String urlString, SourceLocator locator, XPathContext xctxt) 237 throws TransformerException 238 { 239 240 try 242 { 243 Source source = this.resolveURI(base, urlString, locator); 244 245 return getSourceTree(source, locator, xctxt); 247 } 248 catch (IOException ioe) 249 { 250 throw new TransformerException (ioe.getMessage(), locator, ioe); 251 } 252 253 257 } 258 259 270 public int getSourceTree(Source source, SourceLocator locator, XPathContext xctxt) 271 throws TransformerException 272 { 273 274 int n = getNode(source); 275 276 if (DTM.NULL != n) 277 return n; 278 279 n = parseToNode(source, locator, xctxt); 280 281 if (DTM.NULL != n) 282 putDocumentInCache(n, source); 283 284 return n; 285 } 286 287 298 public int parseToNode(Source source, SourceLocator locator, XPathContext xctxt) 299 throws TransformerException 300 { 301 302 try 303 { 304 Object xowner = xctxt.getOwnerObject(); 305 DTM dtm; 306 if(null != xowner && xowner instanceof org.apache.xml.dtm.DTMWSFilter) 307 { 308 dtm = xctxt.getDTM(source, false, 309 (org.apache.xml.dtm.DTMWSFilter)xowner, false, true); 310 } 311 else 312 { 313 dtm = xctxt.getDTM(source, false, null, false, true); 314 } 315 return dtm.getDocument(); 316 } 317 catch (Exception e) 318 { 319 throw new TransformerException (e.getMessage(), locator, e); 321 } 322 323 } 324 325 341 public static XMLReader getXMLReader(Source inputSource, SourceLocator locator) 342 throws TransformerException 343 { 344 345 try 346 { 347 XMLReader reader = (inputSource instanceof SAXSource ) 348 ? ((SAXSource ) inputSource).getXMLReader() : null; 349 350 if (null == reader) 351 { 352 try { 353 javax.xml.parsers.SAXParserFactory factory= 354 javax.xml.parsers.SAXParserFactory.newInstance(); 355 factory.setNamespaceAware( true ); 356 javax.xml.parsers.SAXParser jaxpParser= 357 factory.newSAXParser(); 358 reader=jaxpParser.getXMLReader(); 359 360 } catch( javax.xml.parsers.ParserConfigurationException ex ) { 361 throw new org.xml.sax.SAXException ( ex ); 362 } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { 363 throw new org.xml.sax.SAXException ( ex1.toString() ); 364 } catch( NoSuchMethodError ex2 ) { 365 } 366 catch (AbstractMethodError ame){} 367 if(null == reader) 368 reader = XMLReaderFactory.createXMLReader(); 369 } 370 371 try 372 { 373 reader.setFeature("http://xml.org/sax/features/namespace-prefixes", 374 true); 375 } 376 catch (org.xml.sax.SAXException se) 377 { 378 379 } 382 383 return reader; 384 } 385 catch (org.xml.sax.SAXException se) 386 { 387 throw new TransformerException (se.getMessage(), locator, se); 388 } 389 } 390 } 391 | Popular Tags |