1 package net.sf.saxon.query; 2 import net.sf.saxon.StandardURIResolver; 3 import net.sf.saxon.trans.StaticError; 4 import net.sf.saxon.trans.XPathException; 5 6 import javax.xml.transform.stream.StreamSource ; 7 import java.io.BufferedInputStream ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.io.InputStreamReader ; 11 import java.net.URI ; 12 import java.net.URISyntaxException ; 13 import java.net.URL ; 14 import java.net.URLConnection ; 15 16 17 23 24 public class StandardModuleURIResolver implements ModuleURIResolver { 25 26 42 43 public StreamSource [] resolve(String moduleURI, String baseURI, String [] locations) throws XPathException { 44 if (locations.length == 0) { 45 StaticError err = new StaticError("Cannot locate module for namespace " + moduleURI); 46 err.setErrorCode("XQST0059"); 47 throw err; 48 } else { 49 StreamSource [] sources = new StreamSource [locations.length]; 51 for (int m=0; m<locations.length; m++) { 52 String href = locations[m]; 53 URI absoluteURI; 54 try { 55 absoluteURI = StandardURIResolver.makeAbsolute(href, baseURI); 56 } catch (URISyntaxException err) { 57 StaticError se = new StaticError("Cannot resolve relative URI " + href, err); 58 se.setErrorCode("XQST0059"); 59 throw se; 60 } 61 sources[m] = getQuerySource(absoluteURI); 62 } 63 return sources; 64 } 65 } 66 67 79 80 public static StreamSource getQuerySource(URI absoluteURI) 81 throws StaticError { 82 83 try { 84 InputStream is; 85 URL absoluteURL = absoluteURI.toURL(); 86 URLConnection connection = absoluteURL.openConnection(); 87 connection.connect(); 88 is = connection.getInputStream(); 89 90 if (!is.markSupported()) { 91 is = new BufferedInputStream (is); 92 } 93 94 String contentType; 96 String encoding = null; 97 98 if (!"file".equals(connection.getURL().getProtocol())) { 100 101 contentType = connection.getContentType(); 103 104 if (contentType != null) { 105 int pos = contentType.indexOf("charset"); 106 if (pos>=0) { 107 pos = contentType.indexOf('=', pos + 7); 108 if (pos>=0) { 109 contentType = contentType.substring(pos + 1); 110 } 111 if ((pos = contentType.indexOf(';')) > 0) { 112 contentType = contentType.substring(0, pos); 113 } 114 115 if ((pos = contentType.indexOf('(')) > 0) { 117 contentType = contentType.substring(0, pos); 118 } 119 if ((pos = contentType.indexOf('"')) > 0) { 121 contentType = contentType.substring(pos + 1, 122 contentType.indexOf('"', pos + 2)); 123 } 124 encoding = contentType.trim(); 125 } 126 } 127 } 128 StreamSource ss = new StreamSource (); 129 if (encoding == null) { 130 ss.setInputStream(is); 131 } else { 132 ss.setReader(new InputStreamReader (is, encoding)); 133 } 134 ss.setSystemId(absoluteURL.toString()); 135 return ss; 136 } catch (IOException err) { 137 StaticError se = new StaticError(err); 138 se.setErrorCode("XQST0059"); 139 throw se; 140 } 141 142 } 143 } 144 145 | Popular Tags |