| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.resource; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.expr.XPathContext; |
| 12 |
import net.sf.saxon.functions.URIQueryParameters; |
| 13 |
import net.sf.saxon.lib.ParseOptions; |
| 14 |
import net.sf.saxon.lib.Resource; |
| 15 |
import net.sf.saxon.lib.ResourceFactory; |
| 16 |
import net.sf.saxon.om.Item; |
| 17 |
import net.sf.saxon.om.NodeInfo; |
| 18 |
import net.sf.saxon.trans.XPathException; |
| 19 |
|
| 20 |
import javax.xml.transform.Source; |
| 21 |
import javax.xml.transform.stream.StreamSource; |
| 22 |
import java.io.IOException; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class XmlResource implements Resource { |
| 30 |
private Source source; |
| 31 |
private ParseOptions options; |
| 32 |
private NodeInfo doc; |
| 33 |
private Configuration config; |
| 34 |
private int onError = URIQueryParameters.ON_ERROR_FAIL; |
| 35 |
|
| 36 |
public final static ResourceFactory FACTORY = new ResourceFactory() { |
| 37 |
public Resource makeResource(Configuration config, String resourceURI, String contentType, AbstractResourceCollection.InputDetails details) throws XPathException { |
| 38 |
Source source = new StreamSource(details.inputStream, resourceURI); |
| 39 |
ParseOptions options = details.parseOptions; |
| 40 |
if (options == null) { |
| 41 |
options = config.getParseOptions(); |
| 42 |
} |
| 43 |
return new XmlResource(config, source, options, details.onError); |
| 44 |
} |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public XmlResource(NodeInfo doc) { |
| 54 |
this.config = doc.getConfiguration(); |
| 55 |
this.doc = doc; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public XmlResource(Configuration config, NodeInfo doc) { |
| 66 |
this.config = config; |
| 67 |
this.doc = doc; |
| 68 |
if (config != doc.getConfiguration()) { |
| 69 |
throw new IllegalArgumentException("Supplied node belongs to wrong configuration"); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public XmlResource(Configuration config, Source source, ParseOptions options, int onError) { |
| 85 |
this.config = config; |
| 86 |
this.source = source; |
| 87 |
this.options = options; |
| 88 |
this.onError = onError; |
| 89 |
} |
| 90 |
|
| 91 |
public String getResourceURI() { |
| 92 |
if (doc == null) { |
| 93 |
return source.getSystemId(); |
| 94 |
} else { |
| 95 |
return doc.getSystemId(); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public Item getItem(XPathContext context) throws XPathException { |
| 107 |
if (doc == null) { |
| 108 |
try { |
| 109 |
doc = config.buildDocumentTree(source, options).getRootNode(); |
| 110 |
} catch (XPathException e) { |
| 111 |
if (onError == URIQueryParameters.ON_ERROR_FAIL) { |
| 112 |
XPathException e2 = new XPathException("collection(): failed to parse XML file " + source.getSystemId() + ": " + e.getMessage(), |
| 113 |
e.getErrorCodeLocalPart()); |
| 114 |
throw e2; |
| 115 |
} else if (onError == URIQueryParameters.ON_ERROR_WARNING) { |
| 116 |
context.getController().warning("collection(): failed to parse XML file " + source.getSystemId() + ": " + e.getMessage(), e.getErrorCodeLocalPart(), null); |
| 117 |
} else { |
| 118 |
return null; |
| 119 |
} |
| 120 |
} |
| 121 |
if (source instanceof StreamSource && ((StreamSource)source).getInputStream() != null) { |
| 122 |
try { |
| 123 |
((StreamSource) source).getInputStream().close(); |
| 124 |
} catch (IOException e) { |
| 125 |
|
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
return doc; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public String getContentType() { |
| 139 |
return "application/xml"; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
} |