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.xml.schema.reader;
36  
37  import java.io.Reader;
38  
39  import org.exolab.castor.net.URILocation;
40  import org.exolab.castor.net.util.URIUtils;
41  import org.exolab.castor.xml.schema.Schema;
42  
43  
44  /**
45   * An implementation of URILocation for applications that need to resolve an XML Schema in a
46   * non-standard way, such as a Schema embedded in another XML document, or a Schema created
47   * in-memory, etc.
48   *
49   * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
50   * @version $Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
51   */
52  public final class SchemaLocation extends URILocation {
53  
54    private String _documentBase = null;
55    private String _absoluteURI = null;
56    private String _relativeURI = null;
57  
58  
59    /**
60     * A reference to an alread loaded schema
61     */
62    private Schema _schema = null;
63  
64  
65    /**
66     * Creates a new SchemaLocation
67     *
68     * @param schema the Schema that represents the resource at identified by this URILocation
69     * @param href the absolute URL for the resource identified by this URILocation.
70     */
71    public SchemaLocation(Schema schema, String href) {
72      if (schema == null)
73        throw new IllegalStateException("argument 'schema' must not be null.");
74  
75      _schema = schema;
76  
77      if (href != null) {
78        _absoluteURI = URIUtils.resolveAsString(href, null);
79      }
80    } // -- SchemaLocation
81  
82    /**
83     * Returns the absolute URI for this URILocation
84     *
85     * @return the absolute URI for this URILocation
86     * @see #getRelativeURI
87     * @see #getBaseURI
88     **/
89    public String getAbsoluteURI() {
90      return _absoluteURI;
91    } // -- getAbsoluteURI
92  
93    /**
94     * Returns the base location of this URILocation. If this URILocation is an URL, the base location
95     * will be equivalent to the document base for the URL.
96     *
97     * @return the base location of this URILocation
98     * @see #getAbsoluteURI
99     * @see #getRelativeURI
100    **/
101   public String getBaseURI() {
102     if (_documentBase == null) {
103       if (_absoluteURI != null) {
104         _documentBase = URIUtils.getDocumentBase(_absoluteURI);
105       }
106     }
107     return _documentBase;
108   } // -- getBaseURI
109 
110   /**
111    * Returns a Reader for the resource represented by this URILocation.
112    *
113    * Note: This method always returns null for this URILocation
114    *
115    * @return a Reader for the resource represented by this URILocation
116    * @exception java.io.FileNotFoundException
117    * @exception java.io.IOException
118    **/
119   public Reader getReader() throws java.io.IOException {
120     return null; // -- Not Supported by this URILocation
121   } // -- getReader
122 
123   /**
124    * Returns the relative URI for this URILocation
125    *
126    * @return the relative URI for this URILocation
127    * @see #getAbsoluteURI
128    * @see #getBaseURI
129    **/
130   public String getRelativeURI() {
131 
132     if (_relativeURI == null) {
133       if (_absoluteURI != null) {
134         int idx = getBaseURI().length();
135         _relativeURI = _absoluteURI.substring(idx);
136       }
137     }
138 
139     return _relativeURI;
140 
141   } // -- getRelativeURI
142 
143   /**
144    * Returns the Schema for this SchemaLocation, or null if this SchemaLocation was not constructed
145    * with a Schema object.
146    *
147    * @return the Schema for this SchemaLocation, or null if no Schema object was set.
148    */
149   public Schema getSchema() {
150     return _schema;
151   } // -- getSchema
152 
153   /**
154    * Returns the String representation of this URILocation.
155    *
156    * @return the String representation of this URILocation
157    **/
158   public String toString() {
159     if (_absoluteURI != null)
160       return _absoluteURI;
161     return "URILocation: " + _schema.toString();
162   }
163 
164 } // -- SchemaLocation