1 package net.sf.saxon; 2 import net.sf.saxon.functions.URIQueryParameters; 3 import net.sf.saxon.trans.DynamicError; 4 import net.sf.saxon.trans.XPathException; 5 import net.sf.saxon.event.Stripper; 6 import net.sf.saxon.event.IDFilter; 7 import net.sf.saxon.om.AllElementStripper; 8 import org.xml.sax.InputSource ; 9 import org.xml.sax.XMLReader ; 10 11 import javax.xml.parsers.SAXParserFactory ; 12 import javax.xml.transform.Source ; 13 import javax.xml.transform.URIResolver ; 14 import javax.xml.transform.sax.SAXSource ; 15 import java.io.File ; 16 import java.io.Serializable ; 17 import java.net.MalformedURLException ; 18 import java.net.URI ; 19 import java.net.URISyntaxException ; 20 import java.net.URL ; 21 22 23 32 33 public class StandardURIResolver implements URIResolver , Serializable { 34 35 private Configuration config = null; 36 protected boolean recognizeQueryParameters = false; 37 38 41 public StandardURIResolver() { 42 this(null); 43 } 44 45 50 51 public StandardURIResolver(Configuration config) { 52 this.config = config; 53 } 54 55 60 61 public void setRecognizeQueryParameters(boolean recognize) { 62 recognizeQueryParameters = recognize; 63 } 64 65 69 70 public boolean queryParametersAreRecognized() { 71 return recognizeQueryParameters; 72 } 73 74 82 83 public Source resolve(String href, String base) 84 throws XPathException { 85 86 88 String relativeURI = href; 89 String id = null; 90 91 95 int hash = href.indexOf('#'); 96 if (hash>=0) { 97 relativeURI = href.substring(0, hash); 98 id = href.substring(hash+1); 99 } 101 102 URIQueryParameters params = null; 103 URI url; 104 URI relative; 105 try { 106 relativeURI = escapeSpaces(relativeURI); 107 relative = new URI (relativeURI); 108 } catch (URISyntaxException err) { 109 throw new DynamicError("Invalid relative URI " + Err.wrap(relativeURI), err); 110 } 111 112 String query = relative.getQuery(); 113 if (query != null && recognizeQueryParameters) { 114 params = new URIQueryParameters(query, config); 115 int q = relativeURI.indexOf('?'); 116 relativeURI = relativeURI.substring(0, q); 117 } 118 119 Source source = null; 120 if (recognizeQueryParameters && relativeURI.endsWith(".ptree")) { 121 source = getPTreeSource(relativeURI, base); 122 } 123 124 if (source == null) { 125 try { 126 url = makeAbsolute(relativeURI, base); 127 } catch (URISyntaxException err) { 128 String expandedBase = tryToExpand(base); 132 if (!expandedBase.equals(base)) { return resolve(href, expandedBase); 134 } 135 throw new DynamicError("Invalid URI " + Err.wrap(relativeURI) + " - base " + Err.wrap(base), err); 137 } 138 139 source = new SAXSource (); 140 ((SAXSource )source).setInputSource(new InputSource (url.toString())); 141 source.setSystemId(url.toString()); 142 143 if (params != null) { 144 XMLReader parser = params.getXMLReader(); 145 if (parser != null) { 146 ((SAXSource )source).setXMLReader(parser); 147 } 148 } 149 150 if (((SAXSource )source).getXMLReader() == null) { 151 if (config==null) { 152 try { 153 ((SAXSource )source).setXMLReader(SAXParserFactory.newInstance().newSAXParser().getXMLReader()); 154 } catch (Exception err) { 155 throw new DynamicError(err); 156 } 157 } else { 158 } 161 } 162 } 163 164 if (params != null) { 165 Boolean stripSpace = params.getStripSpace(); 166 if (stripSpace != null && stripSpace.booleanValue()) { 167 Stripper stripper = AllElementStripper.getInstance(); 168 stripper.setStripAll(); 169 source = AugmentedSource.makeAugmentedSource(source); 170 ((AugmentedSource)source).addFilter(stripper); 171 } 172 } 173 174 if (id != null) { 175 IDFilter filter = new IDFilter(id); 176 source = AugmentedSource.makeAugmentedSource(source); 177 ((AugmentedSource)source).addFilter(filter); 178 } 179 180 if (params != null) { 181 Integer validation = params.getValidationMode(); 182 if (validation != null) { 183 source = AugmentedSource.makeAugmentedSource(source); 184 ((AugmentedSource)source).setSchemaValidationMode(validation.intValue()); 185 } 186 } 187 188 return source; 189 } 190 191 194 195 public static URI makeAbsolute(String relativeURI, String base) throws DynamicError, URISyntaxException { 196 URI url; 197 relativeURI = escapeSpaces(relativeURI); 198 base = escapeSpaces(base); 199 try { 200 if (base==null) { 201 url = new URI (relativeURI); 202 if (!url.isAbsolute()) { 203 String expandedBase = tryToExpand(base); 204 if (!expandedBase.equals(base)) { return makeAbsolute(relativeURI, expandedBase); 206 } 207 } 208 } else { 210 URI baseURL = new URI (base); 212 url = (relativeURI.length()==0 ? 214 baseURL : 215 baseURL.resolve(relativeURI) 216 ); 217 } 231 } catch (IllegalArgumentException err0) { 232 throw new DynamicError("Invalid URI " + Err.wrap(relativeURI) + " - base " + Err.wrap(base)); 234 } 235 return url; 236 } 237 238 241 242 public static String escapeSpaces(String s) { 243 if (s == null) return s; 246 int i = s.indexOf(' '); 247 if (i < 0) { 248 return s; 249 } 250 return (i == 0 ? "" : s.substring(0, i)) 251 + "%20" 252 + (i == s.length()-1 ? "" : escapeSpaces(s.substring(i+1))); 253 } 254 258 259 public static String tryToExpand(String systemId) { 260 if (systemId==null) { 261 systemId = ""; 262 } 263 try { 264 new URL (systemId); 265 return systemId; } catch (MalformedURLException err) { 267 String dir; 268 try { 269 dir = System.getProperty("user.dir"); 270 } catch (Exception geterr) { 271 return systemId; 273 } 274 if (!(dir.endsWith("/") || systemId.startsWith("/"))) { 275 dir = dir + '/'; 276 } 277 278 try { 279 URL currentDirectoryURL = new File (dir).toURL(); 280 URL baseURL = new URL (currentDirectoryURL, systemId); 281 return baseURL.toString(); 283 } catch (MalformedURLException err2) { 284 return systemId; 286 } 287 } 288 } 289 290 293 294 protected Source getPTreeSource(String href, String base) throws XPathException { 295 return null; 296 } 297 298 } 299 300 | Popular Tags |