|
|
|
|
|
*/ |
|
/* |
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
|
* contributor license agreements. See the NOTICE file distributed with |
|
* this work for additional information regarding copyright ownership. |
|
* The ASF licenses this file to You under the Apache License, Version 2.0 |
|
* (the "License"); you may not use this file except in compliance with |
|
* the License. You may obtain a copy of the License at |
|
* |
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
*/ |
|
|
|
package com.sun.org.apache.xpath.internal; |
|
|
|
import com.sun.org.apache.xml.internal.dtm.DTM; |
|
import com.sun.org.apache.xml.internal.utils.SystemIDResolver; |
|
import java.io.IOException; |
|
import java.util.ArrayList; |
|
import java.util.List; |
|
import javax.xml.transform.Source; |
|
import javax.xml.transform.SourceLocator; |
|
import javax.xml.transform.TransformerException; |
|
import javax.xml.transform.URIResolver; |
|
import javax.xml.transform.sax.SAXSource; |
|
import javax.xml.transform.stream.StreamSource; |
|
import org.xml.sax.XMLReader; |
|
import org.xml.sax.helpers.XMLReaderFactory; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
@SuppressWarnings("deprecation") |
|
public class SourceTreeManager |
|
{ |
|
|
|
|
|
private List<SourceTree> m_sourceTree = new ArrayList<>(); |
|
|
|
|
|
|
|
|
|
*/ |
|
public void reset() |
|
{ |
|
m_sourceTree = new ArrayList<>(); |
|
} |
|
|
|
|
|
URIResolver m_uriResolver; |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void setURIResolver(URIResolver resolver) |
|
{ |
|
m_uriResolver = resolver; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public URIResolver getURIResolver() |
|
{ |
|
return m_uriResolver; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public String findURIFromDoc(int owner) |
|
{ |
|
int n = m_sourceTree.size(); |
|
|
|
for (int i = 0; i < n; i++) |
|
{ |
|
SourceTree sTree = m_sourceTree.get(i); |
|
|
|
if (owner == sTree.m_root) |
|
return sTree.m_url; |
|
} |
|
|
|
return null; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public Source resolveURI( |
|
String base, String urlString, SourceLocator locator) |
|
throws TransformerException, IOException |
|
{ |
|
|
|
Source source = null; |
|
|
|
if (null != m_uriResolver) |
|
{ |
|
source = m_uriResolver.resolve(urlString, base); |
|
} |
|
|
|
if (null == source) |
|
{ |
|
String uri = SystemIDResolver.getAbsoluteURI(urlString, base); |
|
|
|
source = new StreamSource(uri); |
|
} |
|
|
|
return source; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void removeDocumentFromCache(int n) |
|
{ |
|
if(DTM.NULL ==n) |
|
return; |
|
for(int i=m_sourceTree.size()-1;i>=0;--i) |
|
{ |
|
SourceTree st= m_sourceTree.get(i); |
|
if(st!=null && st.m_root==n) |
|
{ |
|
m_sourceTree.remove(i); |
|
return; |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void putDocumentInCache(int n, Source source) |
|
{ |
|
|
|
int cachedNode = getNode(source); |
|
|
|
if (DTM.NULL != cachedNode) |
|
{ |
|
if (!(cachedNode == n)) |
|
throw new RuntimeException( |
|
"Programmer's Error! " |
|
+ "putDocumentInCache found reparse of doc: " |
|
+ source.getSystemId()); |
|
return; |
|
} |
|
if (null != source.getSystemId()) |
|
{ |
|
m_sourceTree.add(new SourceTree(n, source.getSystemId())); |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public int getNode(Source source) |
|
{ |
|
|
|
// if (source instanceof DOMSource) |
|
// return ((DOMSource) source).getNode(); |
|
|
|
|
|
String url = source.getSystemId(); |
|
|
|
if (null == url) |
|
return DTM.NULL; |
|
|
|
int n = m_sourceTree.size(); |
|
|
|
|
|
for (int i = 0; i < n; i++) |
|
{ |
|
SourceTree sTree = m_sourceTree.get(i); |
|
|
|
// System.out.println("getNode - url: "+url); |
|
|
|
if (url.equals(sTree.m_url)) |
|
return sTree.m_root; |
|
} |
|
|
|
|
|
return DTM.NULL; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public int getSourceTree( |
|
String base, String urlString, SourceLocator locator, XPathContext xctxt) |
|
throws TransformerException |
|
{ |
|
|
|
|
|
try |
|
{ |
|
Source source = this.resolveURI(base, urlString, locator); |
|
|
|
|
|
return getSourceTree(source, locator, xctxt); |
|
} |
|
catch (IOException ioe) |
|
{ |
|
throw new TransformerException(ioe.getMessage(), locator, ioe); |
|
} |
|
|
|
/* catch (TransformerException te) |
|
{ |
|
throw new TransformerException(te.getMessage(), locator, te); |
|
}*/ |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public int getSourceTree(Source source, SourceLocator locator, XPathContext xctxt) |
|
throws TransformerException |
|
{ |
|
|
|
int n = getNode(source); |
|
|
|
if (DTM.NULL != n) |
|
return n; |
|
|
|
n = parseToNode(source, locator, xctxt); |
|
|
|
if (DTM.NULL != n) |
|
putDocumentInCache(n, source); |
|
|
|
return n; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public int parseToNode(Source source, SourceLocator locator, XPathContext xctxt) |
|
throws TransformerException |
|
{ |
|
|
|
try |
|
{ |
|
Object xowner = xctxt.getOwnerObject(); |
|
DTM dtm; |
|
if(null != xowner && xowner instanceof com.sun.org.apache.xml.internal.dtm.DTMWSFilter) |
|
{ |
|
dtm = xctxt.getDTM(source, false, |
|
(com.sun.org.apache.xml.internal.dtm.DTMWSFilter)xowner, false, true); |
|
} |
|
else |
|
{ |
|
dtm = xctxt.getDTM(source, false, null, false, true); |
|
} |
|
return dtm.getDocument(); |
|
} |
|
catch (Exception e) |
|
{ |
|
|
|
throw new TransformerException(e.getMessage(), locator, e); |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public static XMLReader getXMLReader(Source inputSource, SourceLocator locator) |
|
throws TransformerException |
|
{ |
|
|
|
try |
|
{ |
|
XMLReader reader = (inputSource instanceof SAXSource) |
|
? ((SAXSource) inputSource).getXMLReader() : null; |
|
|
|
if (null == reader) |
|
{ |
|
try { |
|
javax.xml.parsers.SAXParserFactory factory= |
|
javax.xml.parsers.SAXParserFactory.newInstance(); |
|
factory.setNamespaceAware( true ); |
|
javax.xml.parsers.SAXParser jaxpParser= |
|
factory.newSAXParser(); |
|
reader=jaxpParser.getXMLReader(); |
|
|
|
} catch( javax.xml.parsers.ParserConfigurationException ex ) { |
|
throw new org.xml.sax.SAXException( ex ); |
|
} catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { |
|
throw new org.xml.sax.SAXException( ex1.toString() ); |
|
} catch( NoSuchMethodError ex2 ) { |
|
} |
|
catch (AbstractMethodError ame){} |
|
if(null == reader) |
|
reader = XMLReaderFactory.createXMLReader(); |
|
} |
|
|
|
try |
|
{ |
|
reader.setFeature("http://xml.org/sax/features/namespace-prefixes", |
|
true); |
|
} |
|
catch (org.xml.sax.SAXException se) |
|
{ |
|
|
|
// What can we do? |
|
// TODO: User diagnostics. |
|
} |
|
|
|
return reader; |
|
} |
|
catch (org.xml.sax.SAXException se) |
|
{ |
|
throw new TransformerException(se.getMessage(), locator, se); |
|
} |
|
} |
|
} |