com.sun.org.apache.xml.internal.resolver.helpers.PublicId Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaxp-ri Show documentation
Show all versions of jaxp-ri Show documentation
Java API for XML Processing Reference Implementation
The newest version!
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can
* obtain a copy of the License at
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
* or packager/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at packager/legal/LICENSE.txt.
*
* GPL Classpath Exception:
* Oracle designates this particular file as subject to the "Classpath"
* exception as provided by Oracle in the GPL Version 2 section of the License
* file that accompanied this code.
*
* Modifications:
* If applicable, add the following below the License Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyright [year] [name of copyright owner]"
*
* Contributor(s):
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// PublicId.java - Information about public identifiers
package com.sun.org.apache.xml.internal.resolver.helpers;
/**
* 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
* [email protected]
*
* @version 1.0
*/
public abstract class PublicId {
protected 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.
*/
public static String normalize(String publicId) {
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;
}
/**
* 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.
*/
public static String encodeURN(String publicId) {
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;
}
/**
* 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.
*/
public static String decodeURN(String urn) {
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;
}
/**
* Replace one string with another.
*
*/
private static String stringReplace(String str,
String oldStr,
String newStr) {
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;
}
}