View Javadoc
1   /**
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Intalio, Inc.  For written permission,
18   *    please contact info@exolab.org.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Intalio, Inc. Exolab is a registered
23   *    trademark of Intalio, Inc.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2002 (C) Intalio, Inc. All Rights Reserved.
42   *
43   * $Id$
44   */
45  package org.exolab.castor.net.util;
46  
47  import org.exolab.castor.net.URIResolver;
48  import org.exolab.castor.net.URIException;
49  import org.exolab.castor.net.URILocation;
50  
51  /**
52   * The default implementation of URIResolver
53   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
54   * @author <a href="mailto:blandin@intalio.com">Arnaud Blandin</a>
55   * @version $Revision$ $Date: 2003-03-03 02:57:21 -0700 (Mon, 03 Mar 2003) $
56  **/
57  public final class URIResolverImpl implements URIResolver {
58  
59      private static final String HTTP_PROTOCOL = "http:";
60      private static final String FILE_PROTOCOL = "file:";
61  
62      public URIResolverImpl() {
63          super();
64      } //-- URIResolver;
65  
66  	/**
67  	 * Resolves the given href and documentBase.
68       * The href can either be an absolute or a relative URI as defined by
69       * IETF RFC 2396:
70       * <ul>
71       *    <li><b>Absolute</b> URI: [scheme]:'//'[authority][absolute path]</li>
72       *    <li><b>Relative</b> URI: '//'[authority][absolute path] or [relative path]
73       *    or [absolute path].</li>
74       * </ul>
75       *
76       * <p><b>Note</b>:
77       * <ol>
78       *   <li>When [authority] is not used then '//' must not be used.</li>
79       *   <li>An [absolute path] begins by a '/' sign.</li>
80       * </ol>
81       * For instance the following URIs are valid:
82       * <blockquote>
83       *    <pre>
84       *        file:/c:/Program Files/MyApp/myresource.xml
85       *        file://usr/etc/myresource.xml
86       *        http://www.castor.org/index.html
87       *        ../relative/index.html
88       *    </pre>
89       * </blockquote>
90       * However, the following <b>won't</b> be valid:
91       * <blockquote>
92       *    <pre>
93       *        file://c:/Program Files/MyApp/myresource.xml
94       *    </pre>
95       * </blockquote>
96  	 *
97  	 * @return the URILocation for the URI
98  	**/
99  	public URILocation resolve(String href, String documentBase)
100 	    throws URIException
101 	{
102 	    URILocation uriLocation = null;
103         boolean stripHostSeparator = (java.io.File.separatorChar == '\\');
104         //--true if the path in the href is absolute
105         boolean absolute = false;
106 
107         //1--Is there  a scheme? If yes the URI is absolute.
108         //NOTE: only HTTP and File protocols are currently supported.
109         if ( href.startsWith(HTTP_PROTOCOL) || href.startsWith(FILE_PROTOCOL) ) {
110             absolute = true;
111         }
112         //2-- handle relative URIs: '//'<authority>[absolute path]
113         //or [relative path] or [absolute path]
114         if (!absolute) {
115             if (stripHostSeparator) {
116                 if (href.startsWith("//"))
117                     absolute = true;
118             } else if (href.startsWith("///")) {
119                 absolute = true;
120             }
121 
122         }
123 
124         //3--[relative path]
125         if (!absolute) {
126 
127              if (href.startsWith("./"))
128                 href = href.substring(2);
129 
130              //--resolve using the document base
131              if (documentBase != null) {
132 
133                 //--resolve the previous directory
134                 while (href.startsWith("../")) {
135                     href = href.substring(3);
136                     documentBase = documentBase.substring(0,documentBase.lastIndexOf('/'));
137                     documentBase = documentBase.substring(0,documentBase.lastIndexOf('/')+1);
138                 }
139              }//--documentBase != null
140         }
141 
142 	    try {
143             uriLocation = new URILocationImpl(href, documentBase);
144 	    }
145 	    catch (RuntimeException ex) {
146 	        throw new URIException(ex.getMessage(), ex);
147 	    }
148 	    return uriLocation;
149 	} //-- resolve
150 
151 	/**
152 	 * Resolves the given urn. An implementation of this
153 	 * method may return null if the URN could not be resolved.
154 	 *
155 	 * @return the URILocation for the URN
156 	 */
157 	public URILocation resolveURN(String urn)
158 	    throws URIException
159 	{
160 	    return null;
161 	} //-- resolveURN
162 
163 } //-- URIResolver