001: /**
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 2002 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: URILocationImpl.java 5951 2006-05-30 22:18:48Z bsnyder $
044: */package org.exolab.castor.net.util;
045:
046: import org.exolab.castor.net.URILocation;
047: import java.io.*;
048:
049: /**
050: * An implementation of URILocation
051: *
052: * @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
053: * @version $Revision: 5951 $ $Date: 2005-03-05 06:42:06 -0700 (Sat, 05 Mar 2005) $
054: **/
055: public final class URILocationImpl extends URILocation {
056:
057: private String _documentBase = null;
058: private String _absoluteURI = null;
059: private String _relativeURI = null;
060:
061: private Reader _reader = null;
062: private InputStream _is = null;
063:
064: /**
065: * Creates a new URILocationImpl
066: **/
067: public URILocationImpl(String href) {
068: this (href, null);
069: } //-- URILocationImpl
070:
071: /**
072: * Creates a new URILocationImpl
073: **/
074: public URILocationImpl(String href, String documentBase) {
075: if (href == null)
076: throw new IllegalStateException("href must not be null");
077: _absoluteURI = URIUtils.resolveAsString(href, documentBase);
078: } //-- URILocationImpl
079:
080: /**
081: * Creates a new URILocationImpl
082: **/
083: public URILocationImpl(Reader reader, String href) {
084: this (href, null);
085: _reader = reader;
086: } //-- URILocationImpl
087:
088: /**
089: * Creates a new URILocationImpl
090: **/
091: public URILocationImpl(InputStream is, String href) {
092: this (href, null);
093: _is = is;
094: } //-- URILocationImpl
095:
096: /**
097: * Returns the absolute URI for this URILocation
098: *
099: * @return the absolute URI for this URILocation
100: * @see #getRelativeURI
101: * @see #getBaseURI
102: **/
103: public String getAbsoluteURI() {
104: return _absoluteURI;
105: } //-- getAbsoluteURI
106:
107: /**
108: * Returns the base location of this URILocation.
109: * If this URILocation is an URL, the base location
110: * will be equivalent to the document base for the URL.
111: *
112: * @return the base location of this URILocation
113: * @see #getAbsoluteURI
114: * @see #getRelativeURI
115: **/
116: public String getBaseURI() {
117: if (_documentBase == null)
118: _documentBase = URIUtils.getDocumentBase(_absoluteURI);
119: return _documentBase;
120: } //-- getBaseURI
121:
122: /**
123: * Returns a Reader for the resource represented
124: * by this URILocation.
125: *
126: * @return a Reader for the resource represented by
127: * this URILocation
128: * @exception java.io.FileNotFoundException
129: * @exception java.io.IOException
130: **/
131: public Reader getReader() throws java.io.IOException {
132:
133: if (_reader != null)
134: return _reader;
135: if (_is != null)
136: return new InputStreamReader(_is);
137:
138: return URIUtils.getReader(_absoluteURI, null);
139: } //-- getReader
140:
141: /**
142: * Returns the relative URI for this URILocation
143: *
144: * @return the relative URI for this URILocation
145: * @see #getAbsoluteURI
146: * @see #getBaseURI
147: **/
148: public String getRelativeURI() {
149:
150: if (_relativeURI == null) {
151: int idx = getBaseURI().length();
152: _relativeURI = _absoluteURI.substring(idx);
153: }
154:
155: return _relativeURI;
156:
157: } //-- getRelativeURI
158:
159: /**
160: * Returns the String representation of
161: * this URILocation.
162: *
163: * @return the String representation of this URILocation
164: **/
165: public String toString() {
166: return getAbsoluteURI();
167: }
168:
169: } //-- URILocationImpl
|