1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xml.utils;
  58. import javax.xml.transform.TransformerException;
  59. import org.apache.xml.utils.URI;
  60. import org.apache.xml.utils.URI.MalformedURIException;
  61. import java.io.*;
  62. import java.lang.StringBuffer;
  63. /**
  64. * <meta name="usage" content="internal"/>
  65. * This class is used to resolve relative URIs and SystemID
  66. * strings into absolute URIs.
  67. *
  68. * <p>This is a generic utility for resolving URIs, other than the
  69. * fact that it's declared to throw TransformerException. Please
  70. * see code comments for details on how resolution is performed.</p>
  71. */
  72. public class SystemIDResolver
  73. {
  74. /**
  75. * Get absolute URI from a given relative URI.
  76. *
  77. * <p>The URI is resolved relative to the system property "user.dir"
  78. * if it is available; if not (i.e. in an Applet perhaps which
  79. * throws SecurityException) then it is currently resolved
  80. * relative to "" or a blank string. Also replaces all
  81. * backslashes with forward slashes.</p>
  82. *
  83. * @param uri Relative URI to resolve
  84. *
  85. * @return Resolved absolute URI or the input relative URI if
  86. * it could not be resolved.
  87. */
  88. public static String getAbsoluteURIFromRelative(String uri)
  89. {
  90. String curdir = "";
  91. try {
  92. curdir = System.getProperty("user.dir");
  93. }
  94. catch (SecurityException se) {}// user.dir not accessible from applet
  95. if (null != curdir)
  96. {
  97. String base;
  98. if (curdir.startsWith(File.separator))
  99. base = "file://" + curdir;
  100. else
  101. base = "file:///" + curdir;
  102. if (uri != null)
  103. // Note: this should arguably stick in a '/' forward
  104. // slash character instead of the file separator,
  105. // since we're effectively assuming it's a hierarchical
  106. // URI and adding in the abs_path separator -sc
  107. uri = base + System.getProperty("file.separator") + uri;
  108. else
  109. uri = base + System.getProperty("file.separator");
  110. }
  111. if (null != uri && (uri.indexOf('\\') > -1))
  112. uri = uri.replace('\\', '/');
  113. return uri;
  114. }
  115. /**
  116. * Take a SystemID string and try and turn it into a good absolute URL.
  117. *
  118. * @param urlString url A URL string, which may be relative or absolute.
  119. *
  120. * @return The resolved absolute URI
  121. * @throws TransformerException thrown if the string can't be turned into a URL.
  122. */
  123. public static String getAbsoluteURI(String url)
  124. throws TransformerException
  125. {
  126. if (url.startsWith(".."))
  127. url = new File(url).getAbsolutePath();
  128. if (url.startsWith(File.separator))
  129. {
  130. // If the url starts with a path separator, we assume it's
  131. // a reference to a file: scheme (why do we do this? -sc)
  132. url = "file://" + url;
  133. }
  134. else if (url.indexOf(':') < 0)
  135. {
  136. // If the url does not have a colon: character (which
  137. // separates the scheme part from the rest) then it
  138. // must be a relative one, so go get an absolute one -sc
  139. url = getAbsoluteURIFromRelative(url);
  140. }
  141. // Bugzilla#5701: the below else if is incorrect, if you read
  142. // section 5.2 of RFC 2396. If the url did start with file:,
  143. // it implies we should assume it's absolute and be done
  144. // with resolving. Note that I'm not even sure why we put
  145. // in the second check for '/' anyways -sc
  146. //else if (url.startsWith("file:") && url.charAt(5) != '/')
  147. //{
  148. // url = getAbsoluteURIFromRelative(url.substring(5));
  149. //}
  150. // Bugzilla#5701 comment out code end
  151. return url;
  152. }
  153. /**
  154. * Take a SystemID string and try and turn it into a good absolute URL.
  155. *
  156. * @param urlString SystemID string
  157. * @param base Base URI to use to resolve the given systemID
  158. *
  159. * @return The resolved absolute URI
  160. * @throws TransformerException thrown if the string can't be turned into a URL.
  161. */
  162. public static String getAbsoluteURI(String urlString, String base)
  163. throws TransformerException
  164. {
  165. boolean isAbsouteUrl = false;
  166. boolean needToResolve = false;
  167. // Bugzilla#5701: the below if is incorrect, if you read
  168. // section 5.2 of RFC 2396. If the url did start with file:,
  169. // it implies we should assume it's absolute and be done
  170. // with resolving. Note that I'm not even sure why we put
  171. // in the second check for '/' anyways -sc
  172. //if(urlString.startsWith("file:") && urlString.charAt(5) != '/')
  173. //{
  174. // needToResolve = true;
  175. //}
  176. //else if (urlString.indexOf(':') > 0)
  177. // Bugzilla#5701 comment out code end
  178. if (urlString.indexOf(':') > 0)
  179. {
  180. // If there is a colon to separate the scheme from the rest,
  181. // it should be an absolute URL
  182. isAbsouteUrl = true;
  183. }
  184. else if (urlString.startsWith(File.separator))
  185. {
  186. // If the url starts with a path separator, we assume it's
  187. // a reference to a file: scheme (why do we do this? -sc)
  188. urlString = "file://" + urlString;
  189. isAbsouteUrl = true;
  190. }
  191. if ((!isAbsouteUrl) && ((null == base)
  192. || (base.indexOf(':') < 0)))
  193. {
  194. if (base != null && base.startsWith(File.separator))
  195. base = "file://" + base;
  196. else
  197. base = getAbsoluteURIFromRelative(base);
  198. }
  199. // bit of a hack here. Need to talk to URI person to see if this can be fixed.
  200. if ((null != base) && needToResolve)
  201. {
  202. if(base.equals(urlString))
  203. {
  204. base = "";
  205. }
  206. else
  207. {
  208. urlString = urlString.substring(5);
  209. isAbsouteUrl = false;
  210. }
  211. }
  212. // This is probably a bad idea, we should at least check for quotes...
  213. if (null != base && (base.indexOf('\\') > -1))
  214. base = base.replace('\\', '/');
  215. if (null != urlString && (urlString.indexOf('\\') > -1))
  216. urlString = urlString.replace('\\', '/');
  217. URI uri;
  218. try
  219. {
  220. if ((null == base) || (base.length() == 0) || (isAbsouteUrl))
  221. {
  222. uri = new URI(urlString);
  223. }
  224. else
  225. {
  226. URI baseURI = new URI(base);
  227. uri = new URI(baseURI, urlString);
  228. }
  229. }
  230. catch (MalformedURIException mue)
  231. {
  232. throw new TransformerException(mue);
  233. }
  234. String uriStr = uri.toString();
  235. // Not so sure if this is good. But, for now, I'll try it. We really must
  236. // make sure the return from this function is a URL!
  237. if((Character.isLetter(uriStr.charAt(0)) && (uriStr.charAt(1) == ':')
  238. && (uriStr.charAt(2) == '/') && (uriStr.length() == 3 || uriStr.charAt(3) != '/'))
  239. || ((uriStr.charAt(0) == '/') && (uriStr.length() == 1 || uriStr.charAt(1) != '/')))
  240. {
  241. uriStr = "file:///"+uriStr;
  242. }
  243. return uriStr;
  244. }
  245. }