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