FileDocCategorySizeDatePackage
PublicId.javaAPI DocJava SE 6 API4799Tue Jun 10 00:23:00 BST 2008com.sun.org.apache.xml.internal.resolver.helpers

PublicId

public abstract class PublicId extends Object
Static methods for dealing with public identifiers.

This class defines a set of static methods that can be called to handle public identifiers.

author
Norman Walsh Norman.Walsh@Sun.COM
version
1.0

Fields Summary
Constructors Summary
protected PublicId()

 
Methods Summary
public static java.lang.StringdecodeURN(java.lang.String urn)
Decode a "publicid" URN into a public identifier.

This method is declared static so that other classes can use it directly.

param
urn The urn:publicid: URN
return
The normalized identifier.

    String publicId = "";

    if (urn.startsWith("urn:publicid:")) {
      publicId = urn.substring(13);
    } else {
      return urn;
    }

    publicId = PublicId.stringReplace(publicId, "%2F", "/");
    publicId = PublicId.stringReplace(publicId, ":", "//");
    publicId = PublicId.stringReplace(publicId, "%3A", ":");
    publicId = PublicId.stringReplace(publicId, ";", "::");
    publicId = PublicId.stringReplace(publicId, "+", " ");
    publicId = PublicId.stringReplace(publicId, "%2B", "+");
    publicId = PublicId.stringReplace(publicId, "%23", "#");
    publicId = PublicId.stringReplace(publicId, "%3F", "?");
    publicId = PublicId.stringReplace(publicId, "%27", "'");
    publicId = PublicId.stringReplace(publicId, "%3B", ";");
    publicId = PublicId.stringReplace(publicId, "%25", "%");

    return publicId;
    
public static java.lang.StringencodeURN(java.lang.String publicId)
Encode a public identifier as a "publicid" URN.

This method is declared static so that other classes can use it directly.

param
publicId The unnormalized public identifier.
return
The normalized identifier.

    String urn = PublicId.normalize(publicId);

    urn = PublicId.stringReplace(urn, "%", "%25");
    urn = PublicId.stringReplace(urn, ";", "%3B");
    urn = PublicId.stringReplace(urn, "'", "%27");
    urn = PublicId.stringReplace(urn, "?", "%3F");
    urn = PublicId.stringReplace(urn, "#", "%23");
    urn = PublicId.stringReplace(urn, "+", "%2B");
    urn = PublicId.stringReplace(urn, " ", "+");
    urn = PublicId.stringReplace(urn, "::", ";");
    urn = PublicId.stringReplace(urn, ":", "%3A");
    urn = PublicId.stringReplace(urn, "//", ":");
    urn = PublicId.stringReplace(urn, "/", "%2F");

    return "urn:publicid:" + urn;
  
public static java.lang.Stringnormalize(java.lang.String publicId)
Normalize a public identifier.

Public identifiers must be normalized according to the following rules before comparisons between them can be made:

  • Whitespace characters are normalized to spaces (e.g., line feeds, tabs, etc. become spaces).
  • Leading and trailing whitespace is removed.
  • Multiple internal whitespaces are normalized to a single space.

This method is declared static so that other classes can use it directly.

param
publicId The unnormalized public identifier.
return
The normalized identifier.

    String normal = publicId.replace('\t", ' ");
    normal = normal.replace('\r", ' ");
    normal = normal.replace('\n", ' ");
    normal = normal.trim();

    int pos;

    while ((pos = normal.indexOf("  ")) >= 0) {
      normal = normal.substring(0, pos) + normal.substring(pos+1);
    }

    return normal;
  
private static java.lang.StringstringReplace(java.lang.String str, java.lang.String oldStr, java.lang.String newStr)
Replace one string with another.


    String result = "";
    int pos = str.indexOf(oldStr);

    //    System.out.println(str + ": " + oldStr + " => " + newStr);

    while (pos >= 0) {
      //      System.out.println(str + " (" + pos + ")");
      result += str.substring(0, pos);
      result += newStr;
      str = str.substring(pos+1);

      pos = str.indexOf(oldStr);
    }

    return result + str;