| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.lib; |
| 9 |
|
| 10 |
import net.sf.saxon.Configuration; |
| 11 |
import net.sf.saxon.Platform; |
| 12 |
import net.sf.saxon.Version; |
| 13 |
import net.sf.saxon.event.FilterFactory; |
| 14 |
import net.sf.saxon.event.IDFilter; |
| 15 |
import net.sf.saxon.event.ProxyReceiver; |
| 16 |
import net.sf.saxon.event.Receiver; |
| 17 |
import net.sf.saxon.functions.EncodeForUri; |
| 18 |
import net.sf.saxon.functions.ResolveURI; |
| 19 |
import net.sf.saxon.functions.URIQueryParameters; |
| 20 |
import net.sf.saxon.om.SpaceStrippingRule; |
| 21 |
import net.sf.saxon.resource.BinaryResource; |
| 22 |
import net.sf.saxon.resource.DataURIScheme; |
| 23 |
import net.sf.saxon.resource.UnparsedTextResource; |
| 24 |
import net.sf.saxon.trans.Err; |
| 25 |
import net.sf.saxon.trans.Maker; |
| 26 |
import net.sf.saxon.trans.NonDelegatingURIResolver; |
| 27 |
import net.sf.saxon.trans.XPathException; |
| 28 |
import org.xml.sax.InputSource; |
| 29 |
import org.xml.sax.XMLReader; |
| 30 |
|
| 31 |
import javax.xml.transform.Source; |
| 32 |
import javax.xml.transform.sax.SAXSource; |
| 33 |
import java.io.ByteArrayInputStream; |
| 34 |
import java.io.InputStream; |
| 35 |
import java.net.URI; |
| 36 |
import java.net.URISyntaxException; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class StandardURIResolver implements NonDelegatingURIResolver { |
| 54 |
|
| 55 |
private Configuration config = null; |
| 56 |
protected boolean recognizeQueryParameters = false; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public StandardURIResolver() { |
| 65 |
this(null); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public StandardURIResolver( Configuration config) { |
| 76 |
this.config = config; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public void setRecognizeQueryParameters(boolean recognize) { |
| 87 |
recognizeQueryParameters = recognize; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public boolean queryParametersAreRecognized() { |
| 97 |
return recognizeQueryParameters; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
protected Platform getPlatform() { |
| 107 |
return Version.platform; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public void setConfiguration(Configuration config) { |
| 117 |
this.config = config; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public Configuration getConfiguration() { |
| 127 |
return config; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
public Source resolve(String href, String base) |
| 141 |
throws XPathException { |
| 142 |
|
| 143 |
if (config != null && config.isTiming()) { |
| 144 |
assert config != null; |
| 145 |
config.getStandardErrorOutput().println("URIResolver.resolve href=\"" + href + "\" base=\"" + base + "\""); |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
String relativeURI = href; |
| 150 |
String id = null; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
int hash = href.indexOf('#'); |
| 157 |
if (hash >= 0) { |
| 158 |
relativeURI = href.substring(0, hash); |
| 159 |
id = href.substring(hash + 1); |
| 160 |
|
| 161 |
} |
| 162 |
|
| 163 |
URIQueryParameters params = null; |
| 164 |
URI uri; |
| 165 |
URI relative; |
| 166 |
try { |
| 167 |
relativeURI = ResolveURI.escapeSpaces(relativeURI); |
| 168 |
relative = new URI(relativeURI); |
| 169 |
} catch (URISyntaxException err) { |
| 170 |
throw new XPathException("Invalid relative URI " + Err.wrap(relativeURI), err); |
| 171 |
} |
| 172 |
|
| 173 |
String query = relative.getQuery(); |
| 174 |
if (query != null && recognizeQueryParameters) { |
| 175 |
params = new URIQueryParameters(query, config); |
| 176 |
int q = relativeURI.indexOf('?'); |
| 177 |
relativeURI = relativeURI.substring(0, q); |
| 178 |
} |
| 179 |
|
| 180 |
Source source = null; |
| 181 |
if (recognizeQueryParameters && relativeURI.endsWith(".ptree")) { |
| 182 |
source = getPTreeSource(relativeURI, base); |
| 183 |
} |
| 184 |
|
| 185 |
if (source == null) { |
| 186 |
try { |
| 187 |
uri = ResolveURI.makeAbsolute(relativeURI, base); |
| 188 |
} catch (URISyntaxException err) { |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
String expandedBase = ResolveURI.tryToExpand(base); |
| 193 |
if (!expandedBase.equals(base)) { |
| 194 |
return resolve(href, expandedBase); |
| 195 |
} |
| 196 |
|
| 197 |
throw new XPathException("Invalid URI " + Err.wrap(relativeURI) + " - base " + Err.wrap(base), err); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
final String uriString = uri.toString(); |
| 204 |
EncodeForUri.checkPercentEncoding(uriString); |
| 205 |
|
| 206 |
|
| 207 |
if ("data".equals(uri.getScheme())) { |
| 208 |
Resource resource; |
| 209 |
try { |
| 210 |
resource = DataURIScheme.decode(uri); |
| 211 |
} catch (IllegalArgumentException e) { |
| 212 |
throw new XPathException("Invalid URI using 'data' scheme: " + e.getMessage()); |
| 213 |
} |
| 214 |
if (resource instanceof BinaryResource) { |
| 215 |
byte[] contents = ((BinaryResource)resource).getData(); |
| 216 |
InputSource is = new InputSource(new ByteArrayInputStream(contents)); |
| 217 |
source = new SAXSource(is); |
| 218 |
source.setSystemId(uriString); |
| 219 |
} else { |
| 220 |
assert resource instanceof UnparsedTextResource; |
| 221 |
InputStream is = ((UnparsedTextResource)resource).getInputStream(); |
| 222 |
source = new SAXSource(new InputSource(is)); |
| 223 |
source.setSystemId(uriString); |
| 224 |
} |
| 225 |
} else { |
| 226 |
source = new SAXSource(); |
| 227 |
setSAXInputSource((SAXSource) source, uriString); |
| 228 |
} |
| 229 |
|
| 230 |
if (params != null) { |
| 231 |
Maker<XMLReader> parser = params.getXMLReaderMaker(); |
| 232 |
if (parser != null) { |
| 233 |
((SAXSource) source).setXMLReader(parser.make()); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
if (((SAXSource) source).getXMLReader() == null) { |
| 238 |
if (config == null) { |
| 239 |
try { |
| 240 |
((SAXSource) source).setXMLReader(Version.platform.loadParser()); |
| 241 |
} catch (Exception err) { |
| 242 |
throw new XPathException(err); |
| 243 |
} |
| 244 |
} else { |
| 245 |
|
| 246 |
|
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if (params != null) { |
| 252 |
SpaceStrippingRule stripSpace = params.getSpaceStrippingRule(); |
| 253 |
source = AugmentedSource.makeAugmentedSource(source); |
| 254 |
((AugmentedSource) source).getParseOptions().setSpaceStrippingRule(stripSpace); |
| 255 |
} |
| 256 |
|
| 257 |
if (id != null) { |
| 258 |
final String idFinal = id; |
| 259 |
FilterFactory factory = new FilterFactory() { |
| 260 |
public ProxyReceiver makeFilter(Receiver next) { |
| 261 |
return new IDFilter(next, idFinal); |
| 262 |
} |
| 263 |
}; |
| 264 |
source = AugmentedSource.makeAugmentedSource(source); |
| 265 |
((AugmentedSource) source).addFilter(factory); |
| 266 |
} |
| 267 |
|
| 268 |
if (params != null) { |
| 269 |
Integer validation = params.getValidationMode(); |
| 270 |
if (validation != null) { |
| 271 |
source = AugmentedSource.makeAugmentedSource(source); |
| 272 |
((AugmentedSource) source).setSchemaValidationMode(validation); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
if (params != null) { |
| 277 |
Boolean xinclude = params.getXInclude(); |
| 278 |
if (xinclude != null) { |
| 279 |
source = AugmentedSource.makeAugmentedSource(source); |
| 280 |
((AugmentedSource) source).setXIncludeAware(xinclude.booleanValue()); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
return source; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
protected Source getPTreeSource(String href, String base) throws XPathException { |
| 296 |
throw new XPathException("PTree files can only be read using a Saxon-EE configuration"); |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
protected void setSAXInputSource(SAXSource source, String uriString) { |
| 314 |
if (uriString.startsWith("classpath:") && uriString.length() > 10) { |
| 315 |
InputStream is = getConfiguration().getDynamicLoader().getResourceAsStream(uriString.substring(10)); |
| 316 |
if (is != null) { |
| 317 |
source.setInputSource(new InputSource(is)); |
| 318 |
source.setSystemId(uriString); |
| 319 |
return; |
| 320 |
} |
| 321 |
} |
| 322 |
source.setInputSource(new InputSource(uriString)); |
| 323 |
source.setSystemId(uriString); |
| 324 |
} |
| 325 |
|
| 326 |
} |
| 327 |
|