KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > resolver > helpers > PublicId


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

3 /*
4  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
5  * as applicable.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.sun.org.apache.xml.internal.resolver.helpers;
21
22 /**
23  * Static methods for dealing with public identifiers.
24  *
25  * <p>This class defines a set of static methods that can be called
26  * to handle public identifiers.</p>
27  *
28  * @author Norman Walsh
29  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
30  *
31  * @version 1.0
32  */

33 public abstract class PublicId {
34   protected PublicId() { }
35
36   /**
37    * Normalize a public identifier.
38    *
39    * <p>Public identifiers must be normalized according to the following
40    * rules before comparisons between them can be made:</p>
41    *
42    * <ul>
43    * <li>Whitespace characters are normalized to spaces (e.g., line feeds,
44    * tabs, etc. become spaces).</li>
45    * <li>Leading and trailing whitespace is removed.</li>
46    * <li>Multiple internal whitespaces are normalized to a single
47    * space.</li>
48    * </ul>
49    *
50    * <p>This method is declared static so that other classes
51    * can use it directly.</p>
52    *
53    * @param publicId The unnormalized public identifier.
54    *
55    * @return The normalized identifier.
56    */

57   public static String JavaDoc normalize(String JavaDoc publicId) {
58     String JavaDoc normal = publicId.replace('\t', ' ');
59     normal = normal.replace('\r', ' ');
60     normal = normal.replace('\n', ' ');
61     normal = normal.trim();
62
63     int pos;
64
65     while ((pos = normal.indexOf(" ")) >= 0) {
66       normal = normal.substring(0, pos) + normal.substring(pos+1);
67     }
68
69     return normal;
70   }
71
72   /**
73    * Encode a public identifier as a "publicid" URN.
74    *
75    * <p>This method is declared static so that other classes
76    * can use it directly.</p>
77    *
78    * @param publicId The unnormalized public identifier.
79    *
80    * @return The normalized identifier.
81    */

82   public static String JavaDoc encodeURN(String JavaDoc publicId) {
83     String JavaDoc urn = PublicId.normalize(publicId);
84
85     urn = PublicId.stringReplace(urn, "%", "%25");
86     urn = PublicId.stringReplace(urn, ";", "%3B");
87     urn = PublicId.stringReplace(urn, "'", "%27");
88     urn = PublicId.stringReplace(urn, "?", "%3F");
89     urn = PublicId.stringReplace(urn, "#", "%23");
90     urn = PublicId.stringReplace(urn, "+", "%2B");
91     urn = PublicId.stringReplace(urn, " ", "+");
92     urn = PublicId.stringReplace(urn, "::", ";");
93     urn = PublicId.stringReplace(urn, ":", "%3A");
94     urn = PublicId.stringReplace(urn, "//", ":");
95     urn = PublicId.stringReplace(urn, "/", "%2F");
96
97     return "urn:publicid:" + urn;
98   }
99
100   /**
101    * Decode a "publicid" URN into a public identifier.
102    *
103    * <p>This method is declared static so that other classes
104    * can use it directly.</p>
105    *
106    * @param urn The urn:publicid: URN
107    *
108    * @return The normalized identifier.
109    */

110   public static String JavaDoc decodeURN(String JavaDoc urn) {
111     String JavaDoc publicId = "";
112
113     if (urn.startsWith("urn:publicid:")) {
114       publicId = urn.substring(13);
115     } else {
116       return urn;
117     }
118
119     publicId = PublicId.stringReplace(publicId, "%2F", "/");
120     publicId = PublicId.stringReplace(publicId, ":", "//");
121     publicId = PublicId.stringReplace(publicId, "%3A", ":");
122     publicId = PublicId.stringReplace(publicId, ";", "::");
123     publicId = PublicId.stringReplace(publicId, "+", " ");
124     publicId = PublicId.stringReplace(publicId, "%2B", "+");
125     publicId = PublicId.stringReplace(publicId, "%23", "#");
126     publicId = PublicId.stringReplace(publicId, "%3F", "?");
127     publicId = PublicId.stringReplace(publicId, "%27", "'");
128     publicId = PublicId.stringReplace(publicId, "%3B", ";");
129     publicId = PublicId.stringReplace(publicId, "%25", "%");
130
131     return publicId;
132     }
133
134   /**
135    * Replace one string with another.
136    *
137    */

138   private static String JavaDoc stringReplace(String JavaDoc str,
139                       String JavaDoc oldStr,
140                       String JavaDoc newStr) {
141
142     String JavaDoc result = "";
143     int pos = str.indexOf(oldStr);
144
145     // System.out.println(str + ": " + oldStr + " => " + newStr);
146

147     while (pos >= 0) {
148       // System.out.println(str + " (" + pos + ")");
149
result += str.substring(0, pos);
150       result += newStr;
151       str = str.substring(pos+1);
152
153       pos = str.indexOf(oldStr);
154     }
155
156     return result + str;
157   }
158 }
159
Popular Tags