001: // PublicId.java - Information about public identifiers
002:
003: /*
004: * Copyright 2001-2004 The Apache Software Foundation or its licensors,
005: * as applicable.
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019:
020: package com.sun.org.apache.xml.internal.resolver.helpers;
021:
022: /**
023: * Static methods for dealing with public identifiers.
024: *
025: * <p>This class defines a set of static methods that can be called
026: * to handle public identifiers.</p>
027: *
028: * @author Norman Walsh
029: * <a href="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
030: *
031: * @version 1.0
032: */
033: public abstract class PublicId {
034: protected PublicId() {
035: }
036:
037: /**
038: * Normalize a public identifier.
039: *
040: * <p>Public identifiers must be normalized according to the following
041: * rules before comparisons between them can be made:</p>
042: *
043: * <ul>
044: * <li>Whitespace characters are normalized to spaces (e.g., line feeds,
045: * tabs, etc. become spaces).</li>
046: * <li>Leading and trailing whitespace is removed.</li>
047: * <li>Multiple internal whitespaces are normalized to a single
048: * space.</li>
049: * </ul>
050: *
051: * <p>This method is declared static so that other classes
052: * can use it directly.</p>
053: *
054: * @param publicId The unnormalized public identifier.
055: *
056: * @return The normalized identifier.
057: */
058: public static String normalize(String publicId) {
059: String normal = publicId.replace('\t', ' ');
060: normal = normal.replace('\r', ' ');
061: normal = normal.replace('\n', ' ');
062: normal = normal.trim();
063:
064: int pos;
065:
066: while ((pos = normal.indexOf(" ")) >= 0) {
067: normal = normal.substring(0, pos)
068: + normal.substring(pos + 1);
069: }
070:
071: return normal;
072: }
073:
074: /**
075: * Encode a public identifier as a "publicid" URN.
076: *
077: * <p>This method is declared static so that other classes
078: * can use it directly.</p>
079: *
080: * @param publicId The unnormalized public identifier.
081: *
082: * @return The normalized identifier.
083: */
084: public static String encodeURN(String publicId) {
085: String urn = PublicId.normalize(publicId);
086:
087: urn = PublicId.stringReplace(urn, "%", "%25");
088: urn = PublicId.stringReplace(urn, ";", "%3B");
089: urn = PublicId.stringReplace(urn, "'", "%27");
090: urn = PublicId.stringReplace(urn, "?", "%3F");
091: urn = PublicId.stringReplace(urn, "#", "%23");
092: urn = PublicId.stringReplace(urn, "+", "%2B");
093: urn = PublicId.stringReplace(urn, " ", "+");
094: urn = PublicId.stringReplace(urn, "::", ";");
095: urn = PublicId.stringReplace(urn, ":", "%3A");
096: urn = PublicId.stringReplace(urn, "//", ":");
097: urn = PublicId.stringReplace(urn, "/", "%2F");
098:
099: return "urn:publicid:" + urn;
100: }
101:
102: /**
103: * Decode a "publicid" URN into a public identifier.
104: *
105: * <p>This method is declared static so that other classes
106: * can use it directly.</p>
107: *
108: * @param urn The urn:publicid: URN
109: *
110: * @return The normalized identifier.
111: */
112: public static String decodeURN(String urn) {
113: String publicId = "";
114:
115: if (urn.startsWith("urn:publicid:")) {
116: publicId = urn.substring(13);
117: } else {
118: return urn;
119: }
120:
121: publicId = PublicId.stringReplace(publicId, "%2F", "/");
122: publicId = PublicId.stringReplace(publicId, ":", "//");
123: publicId = PublicId.stringReplace(publicId, "%3A", ":");
124: publicId = PublicId.stringReplace(publicId, ";", "::");
125: publicId = PublicId.stringReplace(publicId, "+", " ");
126: publicId = PublicId.stringReplace(publicId, "%2B", "+");
127: publicId = PublicId.stringReplace(publicId, "%23", "#");
128: publicId = PublicId.stringReplace(publicId, "%3F", "?");
129: publicId = PublicId.stringReplace(publicId, "%27", "'");
130: publicId = PublicId.stringReplace(publicId, "%3B", ";");
131: publicId = PublicId.stringReplace(publicId, "%25", "%");
132:
133: return publicId;
134: }
135:
136: /**
137: * Replace one string with another.
138: *
139: */
140: private static String stringReplace(String str, String oldStr,
141: String newStr) {
142:
143: String result = "";
144: int pos = str.indexOf(oldStr);
145:
146: // System.out.println(str + ": " + oldStr + " => " + newStr);
147:
148: while (pos >= 0) {
149: // System.out.println(str + " (" + pos + ")");
150: result += str.substring(0, pos);
151: result += newStr;
152: str = str.substring(pos + 1);
153:
154: pos = str.indexOf(oldStr);
155: }
156:
157: return result + str;
158: }
159: }
|