KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > xml > catalog > helpers > PublicId


1 // PublicId.java - Information about public identifiers
2

3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgment may appear in the software itself,
26  * if and wherever such third-party acknowledgments normally appear.
27  *
28  * 4. The names "Apache" and "Apache Software Foundation" must
29  * not be used to endorse or promote products derived from this
30  * software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache",
34  * nor may "Apache" appear in their name, without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 package org.jboss.util.xml.catalog.helpers;
58
59 /**
60  * Static methods for dealing with public identifiers.
61  *
62  * <p>This class defines a set of static methods that can be called
63  * to handle public identifiers.</p>
64  *
65  * @author Norman Walsh
66  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
67  *
68  * @version 1.0
69  */

70 public abstract class PublicId {
71   protected PublicId() { }
72
73   /**
74    * Normalize a public identifier.
75    *
76    * <p>Public identifiers must be normalized according to the following
77    * rules before comparisons between them can be made:</p>
78    *
79    * <ul>
80    * <li>Whitespace characters are normalized to spaces (e.g., line feeds,
81    * tabs, etc. become spaces).</li>
82    * <li>Leading and trailing whitespace is removed.</li>
83    * <li>Multiple internal whitespaces are normalized to a single
84    * space.</li>
85    * </ul>
86    *
87    * <p>This method is declared static so that other classes
88    * can use it directly.</p>
89    *
90    * @param publicId The unnormalized public identifier.
91    *
92    * @return The normalized identifier.
93    */

94   public static String JavaDoc normalize(String JavaDoc publicId) {
95     String JavaDoc normal = publicId.replace('\t', ' ');
96     normal = normal.replace('\r', ' ');
97     normal = normal.replace('\n', ' ');
98     normal = normal.trim();
99
100     int pos;
101
102     while ((pos = normal.indexOf(" ")) >= 0) {
103       normal = normal.substring(0, pos) + normal.substring(pos+1);
104     }
105
106     return normal;
107   }
108
109   /**
110    * Encode a public identifier as a "publicid" URN.
111    *
112    * <p>This method is declared static so that other classes
113    * can use it directly.</p>
114    *
115    * @param publicId The unnormalized public identifier.
116    *
117    * @return The normalized identifier.
118    */

119   public static String JavaDoc encodeURN(String JavaDoc publicId) {
120     String JavaDoc urn = PublicId.normalize(publicId);
121
122     urn = PublicId.stringReplace(urn, "%", "%25");
123     urn = PublicId.stringReplace(urn, ";", "%3B");
124     urn = PublicId.stringReplace(urn, "'", "%27");
125     urn = PublicId.stringReplace(urn, "?", "%3F");
126     urn = PublicId.stringReplace(urn, "#", "%23");
127     urn = PublicId.stringReplace(urn, "+", "%2B");
128     urn = PublicId.stringReplace(urn, " ", "+");
129     urn = PublicId.stringReplace(urn, "::", ";");
130     urn = PublicId.stringReplace(urn, ":", "%3A");
131     urn = PublicId.stringReplace(urn, "//", ":");
132     urn = PublicId.stringReplace(urn, "/", "%2F");
133
134     return "urn:publicid:" + urn;
135   }
136
137   /**
138    * Decode a "publicid" URN into a public identifier.
139    *
140    * <p>This method is declared static so that other classes
141    * can use it directly.</p>
142    *
143    * @param publicId The unnormalized public identifier.
144    *
145    * @return The normalized identifier.
146    */

147   public static String JavaDoc decodeURN(String JavaDoc urn) {
148     String JavaDoc publicId = "";
149
150     if (urn.startsWith("urn:publicid:")) {
151       publicId = urn.substring(13);
152     } else {
153       return urn;
154     }
155
156     publicId = PublicId.stringReplace(publicId, "%2F", "/");
157     publicId = PublicId.stringReplace(publicId, ":", "//");
158     publicId = PublicId.stringReplace(publicId, "%3A", ":");
159     publicId = PublicId.stringReplace(publicId, ";", "::");
160     publicId = PublicId.stringReplace(publicId, "+", " ");
161     publicId = PublicId.stringReplace(publicId, "%2B", "+");
162     publicId = PublicId.stringReplace(publicId, "%23", "#");
163     publicId = PublicId.stringReplace(publicId, "%3F", "?");
164     publicId = PublicId.stringReplace(publicId, "%27", "'");
165     publicId = PublicId.stringReplace(publicId, "%3B", ";");
166     publicId = PublicId.stringReplace(publicId, "%25", "%");
167
168     return publicId;
169     }
170
171   /**
172    * Replace one string with another.
173    *
174    */

175   private static String JavaDoc stringReplace(String JavaDoc str,
176                       String JavaDoc oldStr,
177                       String JavaDoc newStr) {
178
179     String JavaDoc result = "";
180     int pos = str.indexOf(oldStr);
181
182     // System.out.println(str + ": " + oldStr + " => " + newStr);
183

184     while (pos >= 0) {
185       // System.out.println(str + " (" + pos + ")");
186
result += str.substring(0, pos);
187       result += newStr;
188       str = str.substring(pos+1);
189
190       pos = str.indexOf(oldStr);
191     }
192
193     return result + str;
194   }
195 }
196
Popular Tags