KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > util > CmsUriSplitter


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/util/CmsUriSplitter.java,v $
3  * Date : $Date: 2006/03/27 14:52:41 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (C) 2002 - 2005 Alkacon Software (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.util;
33
34 import java.net.URI JavaDoc;
35
36 /**
37  * Splits an URI String into separate components.<p>
38  *
39  * An URI is splitted into a <code>prefix</code>, a <code>anchor</code> and a <code>query</code> part.
40  *
41  * @author Alexander Kandzior
42  *
43  * @version $Revision: 1.3 $
44  */

45 public class CmsUriSplitter {
46
47     /** The anchor part of the uri, for example <code>someanchor</code>. */
48     private String JavaDoc m_anchor;
49
50     /** Indicates if 'strict' URI parsing did produce an error. */
51     private boolean m_errorFree;
52
53     /** The prefix part of the uri, for example <code>http://www.opencms.org/some/path/</code>. */
54     private String JavaDoc m_prefix;
55
56     /** The query part of the uri, for example <code>a=b&c=d</code>. */
57     private String JavaDoc m_query;
58
59     /** The original URI String that was split. */
60     private String JavaDoc m_uri;
61
62     /**
63      * Creates a splitted URI using the default (not strict) parsing mode.<p>
64      *
65      * @param uri the URI to split
66      */

67     public CmsUriSplitter(String JavaDoc uri) {
68
69         this(uri, false);
70     }
71
72     /**
73      * Creates a splitted URI using the given parsing mode.<p>
74      *
75      * Using 'strict' parsing mode, all requirements for an URI are checked.
76      * If 'strict' is set to <code>false</code>, then only some simple parsing rules are applied,
77      * in which case the result may not be 100% valid (but still usable).
78      * If 'strict' parsing generates an error, then simple parsing is used as a fallback.<p>
79      *
80      * @param uri the URI to split
81      * @param strict if <code>true</code>, then 'strict' parsing mode is used, otherwise a relaxed URI parsing is done
82      */

83     public CmsUriSplitter(String JavaDoc uri, boolean strict) {
84
85         m_uri = uri;
86         m_errorFree = true;
87
88         if (strict) {
89
90             // use strict parsing
91
try {
92                 URI JavaDoc u = new URI JavaDoc(uri);
93                 m_prefix = ((u.getScheme() != null) ? u.getScheme() + ":" : "") + u.getRawSchemeSpecificPart();
94                 m_anchor = u.getRawFragment();
95                 m_query = u.getRawQuery();
96                 if (m_prefix != null) {
97                     int i = m_prefix.indexOf('?');
98                     if (i != -1) {
99                         m_query = m_prefix.substring(i + 1);
100                         m_prefix = m_prefix.substring(0, i);
101                     }
102                 }
103                 if (m_anchor != null) {
104                     int i = m_anchor.indexOf('?');
105                     if (i != -1) {
106                         m_query = m_anchor.substring(i + 1);
107                         m_anchor = m_anchor.substring(0, i);
108                     }
109                 }
110             } catch (Exception JavaDoc exc) {
111                 // may be thrown by URI constructor if uri is invalid
112
strict = false;
113                 m_errorFree = false;
114             }
115         }
116
117         if ((!strict) && (uri != null)) {
118
119             // use simple parsing
120
StringBuffer JavaDoc prefix = new StringBuffer JavaDoc(uri.length());
121             StringBuffer JavaDoc anchor = null;
122             StringBuffer JavaDoc query = null;
123
124             int len = uri.length();
125             int cur = 0;
126
127             for (int i = 0; i < len; i++) {
128
129                 char c = uri.charAt(i);
130                 if (c == '#') {
131                     // start of anchor
132
cur = 1;
133                     anchor = new StringBuffer JavaDoc(uri.length());
134                     continue;
135                 }
136                 if (c == '?') {
137                     // start of query
138
cur = 2;
139                     // ensure a duplicate query part is 'flushed' (same behaviour as strict parser)
140
query = new StringBuffer JavaDoc(uri.length());
141                     continue;
142                 }
143                 switch (cur) {
144                     case 1:
145                         // append to anchor
146
anchor.append(c);
147                         break;
148                     case 2:
149                         // append to query
150
query.append(c);
151                         break;
152                     default:
153                         // append to prefix
154
prefix.append(c);
155                         break;
156                 }
157             }
158
159             if (prefix.length() > 0) {
160                 m_prefix = prefix.toString();
161             }
162             if ((anchor != null) && (anchor.length() > 0)) {
163                 m_anchor = anchor.toString();
164             }
165             if ((query != null) && (query.length() > 0)) {
166                 m_query = query.toString();
167             }
168         }
169     }
170
171     /**
172      * @see java.lang.Object#equals(java.lang.Object)
173      */

174     public boolean equals(Object JavaDoc obj) {
175
176         if (obj == this) {
177             return true;
178         }
179         if (obj instanceof CmsUriSplitter) {
180             CmsUriSplitter other = (CmsUriSplitter)obj;
181             if (((m_prefix == null) && (other.m_prefix != null)) && (!m_prefix.equals(other.m_prefix))) {
182                 return false;
183             }
184             if (((m_anchor == null) && (other.m_anchor != null)) && (!m_anchor.equals(other.m_anchor))) {
185                 return false;
186             }
187             if (((m_query == null) && (other.m_query != null)) && (!m_query.equals(other.m_query))) {
188                 return false;
189             }
190             return true;
191         }
192         return false;
193     }
194
195     /**
196      * Returns the anchor part of the uri, for example <code>someanchor</code>,
197      * or <code>null</code> if no anchor is available.<p>
198      *
199      * @return the anchor part of the uri
200      */

201     public String JavaDoc getAnchor() {
202
203         return m_anchor;
204     }
205
206     /**
207      * Returns the prefix part of the uri, for example <code>http://www.opencms.org/some/path/</code>,
208      * or <code>null</code> if no prefix is available.<p>
209      *
210      * @return the prefix part of the uri
211      */

212     public String JavaDoc getPrefix() {
213
214         return m_prefix;
215     }
216
217     /**
218      * Returns the query part of the uri, for example <code>a=b&c=d</code>,
219      * or <code>null</code> if no query is available.<p>
220      *
221      * @return the query part of the uri
222      */

223     public String JavaDoc getQuery() {
224
225         return m_query;
226     }
227
228     /**
229      * Returns the URI String passed to this URI splitter.<p>
230      *
231      * @return the URI String passed to this URI splitter
232      */

233     public String JavaDoc getUri() {
234
235         return m_uri;
236     }
237
238     /**
239      * @see java.lang.Object#hashCode()
240      */

241     public int hashCode() {
242
243         int hashCode = 0;
244         if (m_prefix != null) {
245             hashCode += m_prefix.hashCode();
246         }
247         if (m_anchor != null) {
248             hashCode += m_anchor.hashCode();
249         }
250         if (m_query != null) {
251             hashCode += m_query.hashCode();
252         }
253         return hashCode;
254     }
255
256     /**
257      * Returns <code>true</code> if the URI was parsed error free in 'strict' mode,
258      * or if the simple mode was used.<p>
259      *
260      * @return <code>true</code> if the URI was parsed error free in 'strict' mode,
261      * or if the simple mode was used
262      */

263     public boolean isErrorFree() {
264
265         return m_errorFree;
266     }
267 }
Popular Tags