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.URILocation;
38 import java.io.*;
39
40 /**
41 * An implementation of URILocation
42 *
43 * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
44 * @version $Revision$ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
45 **/
46 public final class URILocationImpl extends URILocation {
47
48 private String _documentBase = null;
49 private String _absoluteURI = null;
50 private String _relativeURI = null;
51
52
53 private Reader _reader = null;
54 private InputStream _is = null;
55
56
57 /**
58 * Creates a new URILocationImpl
59 **/
60 public URILocationImpl(String href) {
61 this(href, null);
62 } // -- URILocationImpl
63
64 /**
65 * Creates a new URILocationImpl
66 **/
67 public URILocationImpl(String href, String documentBase) {
68 if (href == null)
69 throw new IllegalStateException("href must not be null");
70 _absoluteURI = URIUtils.resolveAsString(href, documentBase);
71 } // -- URILocationImpl
72
73
74 /**
75 * Creates a new URILocationImpl
76 **/
77 public URILocationImpl(Reader reader, String href) {
78 this(href, null);
79 _reader = reader;
80 } // -- URILocationImpl
81
82 /**
83 * Creates a new URILocationImpl
84 **/
85 public URILocationImpl(InputStream is, String href) {
86 this(href, null);
87 _is = is;
88 } // -- URILocationImpl
89
90
91
92 /**
93 * Returns the absolute URI for this URILocation
94 *
95 * @return the absolute URI for this URILocation
96 * @see #getRelativeURI
97 * @see #getBaseURI
98 **/
99 public String getAbsoluteURI() {
100 return _absoluteURI;
101 } // -- getAbsoluteURI
102
103 /**
104 * Returns the base location of this URILocation. If this URILocation is an URL, the base location
105 * will be equivalent to the document base for the URL.
106 *
107 * @return the base location of this URILocation
108 * @see #getAbsoluteURI
109 * @see #getRelativeURI
110 **/
111 public String getBaseURI() {
112 if (_documentBase == null)
113 _documentBase = URIUtils.getDocumentBase(_absoluteURI);
114 return _documentBase;
115 } // -- getBaseURI
116
117 /**
118 * Returns a Reader for the resource represented by this URILocation.
119 *
120 * @return a Reader for the resource represented by this URILocation
121 * @exception java.io.FileNotFoundException
122 * @exception java.io.IOException
123 **/
124 public Reader getReader() throws java.io.IOException {
125
126 if (_reader != null)
127 return _reader;
128 if (_is != null)
129 return new InputStreamReader(_is);
130
131 return URIUtils.getReader(_absoluteURI, null);
132 } // -- getReader
133
134 /**
135 * Returns the relative URI for this URILocation
136 *
137 * @return the relative URI for this URILocation
138 * @see #getAbsoluteURI
139 * @see #getBaseURI
140 **/
141 public String getRelativeURI() {
142
143 if (_relativeURI == null) {
144 int idx = getBaseURI().length();
145 _relativeURI = _absoluteURI.substring(idx);
146 }
147
148 return _relativeURI;
149
150 } // -- getRelativeURI
151
152 /**
153 * Returns the String representation of this URILocation.
154 *
155 * @return the String representation of this URILocation
156 **/
157 public String toString() {
158 return getAbsoluteURI();
159 }
160
161 } // -- URILocationImpl