001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsUriSplitter.java,v $
003: * Date : $Date: 2008-02-27 12:05:36 $
004: * Version: $Revision: 1.6 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.util;
033:
034: import java.net.URI;
035:
036: /**
037: * Splits an URI String into separate components.<p>
038: *
039: * An URI is splitted into a <code>prefix</code>, a <code>anchor</code> and a <code>query</code> part.
040: *
041: * @author Alexander Kandzior
042: *
043: * @version $Revision: 1.6 $
044: */
045: public class CmsUriSplitter {
046:
047: /** Empty (non null) StringBuffer constant. */
048: private static final StringBuffer EMPTY_BUFFER = new StringBuffer(0);
049:
050: /** The anchor part of the uri, for example <code>someanchor</code>. */
051: private String m_anchor;
052:
053: /** Indicates if 'strict' URI parsing did produce an error. */
054: private boolean m_errorFree;
055:
056: /** The prefix part of the uri, for example <code>http://www.opencms.org/some/path/</code>. */
057: private String m_prefix;
058:
059: /** The query part of the uri, for example <code>a=b&c=d</code>. */
060: private String m_query;
061:
062: /** The original URI String that was split. */
063: private String m_uri;
064:
065: /**
066: * Creates a splitted URI using the default (not strict) parsing mode.<p>
067: *
068: * @param uri the URI to split
069: */
070: public CmsUriSplitter(String uri) {
071:
072: this (uri, false);
073: }
074:
075: /**
076: * Creates a splitted URI using the given parsing mode.<p>
077: *
078: * Using 'strict' parsing mode, all requirements for an URI are checked.
079: * If 'strict' is set to <code>false</code>, then only some simple parsing rules are applied,
080: * in which case the result may not be 100% valid (but still usable).
081: * If 'strict' parsing generates an error, then simple parsing is used as a fallback.<p>
082: *
083: * @param uri the URI to split
084: * @param strict if <code>true</code>, then 'strict' parsing mode is used, otherwise a relaxed URI parsing is done
085: */
086: public CmsUriSplitter(String uri, boolean strict) {
087:
088: m_uri = uri;
089: m_errorFree = true;
090:
091: if (strict) {
092:
093: // use strict parsing
094: try {
095: URI u = new URI(uri);
096: m_prefix = ((u.getScheme() != null) ? u.getScheme()
097: + ":" : "")
098: + u.getRawSchemeSpecificPart();
099: m_anchor = u.getRawFragment();
100: m_query = u.getRawQuery();
101: if (m_prefix != null) {
102: int i = m_prefix.indexOf('?');
103: if (i != -1) {
104: m_query = m_prefix.substring(i + 1);
105: m_prefix = m_prefix.substring(0, i);
106: }
107: }
108: if (m_anchor != null) {
109: int i = m_anchor.indexOf('?');
110: if (i != -1) {
111: m_query = m_anchor.substring(i + 1);
112: m_anchor = m_anchor.substring(0, i);
113: }
114: }
115: } catch (Exception exc) {
116: // may be thrown by URI constructor if uri is invalid
117: strict = false;
118: m_errorFree = false;
119: }
120: }
121:
122: if ((!strict) && (uri != null)) {
123:
124: // use simple parsing
125: StringBuffer prefix = new StringBuffer(uri.length());
126: StringBuffer anchor = EMPTY_BUFFER;
127: StringBuffer query = EMPTY_BUFFER;
128:
129: int len = uri.length();
130: int cur = 0;
131:
132: for (int i = 0; i < len; i++) {
133: char c = uri.charAt(i);
134: if (c == '#') {
135: // start of anchor
136: cur = 1;
137: anchor = new StringBuffer(uri.length());
138: continue;
139: }
140: if (c == '?') {
141: // start of query
142: cur = 2;
143: // ensure a duplicate query part is 'flushed' (same behaviour as strict parser)
144: query = new StringBuffer(uri.length());
145: continue;
146: }
147: switch (cur) {
148: case 1:
149: // append to anchor
150: anchor.append(c);
151: break;
152: case 2:
153: // append to query
154: query.append(c);
155: break;
156: default:
157: // append to prefix
158: prefix.append(c);
159: break;
160: }
161: }
162:
163: if (prefix.length() > 0) {
164: m_prefix = prefix.toString();
165: }
166: if (anchor.length() > 0) {
167: m_anchor = anchor.toString();
168: }
169: if (query.length() > 0) {
170: m_query = query.toString();
171: }
172: }
173: }
174:
175: /**
176: * @see java.lang.Object#equals(java.lang.Object)
177: */
178: public boolean equals(Object obj) {
179:
180: if (obj == this ) {
181: return true;
182: }
183: if (obj instanceof CmsUriSplitter) {
184: CmsUriSplitter other = (CmsUriSplitter) obj;
185: if (((m_prefix == null) && (other.m_prefix != null))
186: && (!other.m_prefix.equals(m_prefix))) {
187: return false;
188: }
189: if (((m_anchor == null) && (other.m_anchor != null))
190: && (!other.m_anchor.equals(m_anchor))) {
191: return false;
192: }
193: if (((m_query == null) && (other.m_query != null))
194: && (!other.m_query.equals(m_query))) {
195: return false;
196: }
197: return true;
198: }
199: return false;
200: }
201:
202: /**
203: * Returns the anchor part of the uri, for example <code>someanchor</code>,
204: * or <code>null</code> if no anchor is available.<p>
205: *
206: * @return the anchor part of the uri
207: */
208: public String getAnchor() {
209:
210: return m_anchor;
211: }
212:
213: /**
214: * Returns the prefix part of the uri, for example <code>http://www.opencms.org/some/path/</code>,
215: * or <code>null</code> if no prefix is available.<p>
216: *
217: * @return the prefix part of the uri
218: */
219: public String getPrefix() {
220:
221: return m_prefix;
222: }
223:
224: /**
225: * Returns the query part of the uri, for example <code>a=b&c=d</code>,
226: * or <code>null</code> if no query is available.<p>
227: *
228: * @return the query part of the uri
229: */
230: public String getQuery() {
231:
232: return m_query;
233: }
234:
235: /**
236: * Returns the URI String passed to this URI splitter.<p>
237: *
238: * @return the URI String passed to this URI splitter
239: */
240: public String getUri() {
241:
242: return m_uri;
243: }
244:
245: /**
246: * @see java.lang.Object#hashCode()
247: */
248: public int hashCode() {
249:
250: int hashCode = 0;
251: if (m_prefix != null) {
252: hashCode += m_prefix.hashCode();
253: }
254: if (m_anchor != null) {
255: hashCode += m_anchor.hashCode();
256: }
257: if (m_query != null) {
258: hashCode += m_query.hashCode();
259: }
260: return hashCode;
261: }
262:
263: /**
264: * Returns <code>true</code> if the URI was parsed error free in 'strict' mode,
265: * or if the simple mode was used.<p>
266: *
267: * @return <code>true</code> if the URI was parsed error free in 'strict' mode,
268: * or if the simple mode was used
269: */
270: public boolean isErrorFree() {
271:
272: return m_errorFree;
273: }
274: }
|