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