XMLUtils.java revision f1e82b18ba6b6dd5dfc2569f2a83040b923233ca
/**
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the License). You may not use this file except in
* compliance with the License.
*
* You can obtain a copy of the License at
* See the License for the specific language governing
* permission and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* Header Notice in each file and include the License file
* at opensso/legal/CDDLv1.0.txt.
* If applicable, add the following below the CDDL Header,
* with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* $Id: XMLUtils.java,v 1.15 2009/10/19 18:19:20 asyhuang Exp $
*
*/
/**
* Portions Copyrighted 2011-2014 ForgeRock AS
*/
/**
* This class contains utilities to parse XML documents
*/
public class XMLUtils {
.unmodifiableMap(new HashMap());
// property to see if XML document validating is needed. The validating
// is turned on only if the value for com.iplanet.am.util.xml.validating
// property is set to "on" and value for com.iplanet.services.debug.level
// property is set to "warning" or "message".
private static boolean validating = false;
private static String ATTR_BASE64_ENCODED =
"com_sun_identity_opensso_base64_encoded";
private static int ATTR_BASE64_ENCODED_LENGTH =
private static Pattern invalidXMLChars =
static {
try {
) {
validating = true;
}
} catch (Exception e) {
// ignore since there is not debug class here
}
}
/**
* Size of document builder cache.
*/
private static final int DOCBUILDER_CACHE_SIZE =
/**
* Size of the SAXParser cache. Defaults to same size as document builder cache.
*/
private static final int SAXPARSER_CACHE_SIZE =
private static final int TRANSFORMER_FACTORY_CACHE_SIZE =
/**
* Provider for DocumentBuilder instances. Caches in an LRU cache per thread.
*/
private static final DocumentBuilderProvider DOCUMENT_BUILDER_PROVIDER =
/**
* Provider for SAXParser instances. Caches in a per-thread LRU cache.
*/
private static final SAXParserProvider SAX_PARSER_PROVIDER = Providers.saxParserProvider(SAXPARSER_CACHE_SIZE);
/**
* Provider for TransformerFactory instances. Caches in a per-thread LRU cache.
*/
private static final TransformerFactoryProvider TRANSFORMER_FACTORY_PROVIDER =
public String getATTR_VALUE_PAIR_NODE() {
return ATTR_VALUE_PAIR_NODE;
}
public static boolean isValidating() {
return validating;
}
/**
* Converts the XML document from a String format to DOM Document format.
*
* @param xmlString
* is the XML document in a String format
* @param debug
* is the debug object used for logging debug info
* @return Document is the DOM object obtained by converting the String XML
* Returns null if xmlString is null. Returns null if there are any
* parser errores.
*/
return null;
}
try {
.getBytes("UTF-8"));
} catch (UnsupportedEncodingException uee) {
uee);
}
return null;
}
}
/**
* Converts the XML document from an input stream to DOM Document format.
*
* @param is
* is the InputStream that contains XML document
* @return Document is the DOM object obtained by parsing the input stream.
* Returns null if there are any parser errores.
*/
try {
// Assign new debug object
} catch (ParserConfigurationException pe) {
}
}
try {
if (documentBuilder == null) {
}
return null;
}
debug));
}
} catch (Exception e) {
// Since there may potentially be several invalid XML documents
// that are mostly client-side errors, only a warning is logged for
// efficiency reasons.
}
return null;
}
}
/**
* This method parse an Attributes tag, DTD for Attribute is as follows.
*
* <pre>
* < !-- This DTD defines the DPro Attribute tag.
* Unique Declaration name for DOCTYPE tag:
* "Directory Pro 5.0 Attributes DTD"
* -->
* < !ELEMENT Attributes (Attribute)+>
* < !ELEMENT Attribute EMPTY>
* < !ATTLIST Attribute
* name NMTOKEN #REQUIRED
* >
* </pre>
*
* @param n
* Node
* @return Set Set of the attribute names
*/
// get Attribute node list
if (numAttributes == 0) {
return null;
}
for (int l = 0; l < numAttributes; l++) {
// get next attribute
// need error handling
continue;
}
}
return set;
}
/**
* @param parentNode
* is the element tag that contains all the AttirbuteValuePair
* tags as children
* @return Map Returns the AV pairs in a Map where each entry in the Map is
* an AV pair. The key is the attribute name and the value is a Set
* of String objects.
*/
if (keyValueSize <= 0) {
return EMPTY_MAP;
}
for (int l = 0; l < keyValueSize; l++) {
continue;
}
if (keyValueEntrySize < 2) {
// TODO: More error handling required later for missing
// 'Attribute' or 'Value' tags.
continue;
}
// Since Attribute tag is always the first leaf node as per the DTD, and values can one or more,
// Attribute tag can be parsed first and then iterate over the values, if any.
for (int i = 0; i < keyValueEntrySize; i++) {
break;
}
}
// Now parse the Value tags. If there are not 'Value' tags, ignore this key
// TODO: More error handling required later for zero 'Value' tags.
for (int m = 0; m < keyValueEntrySize; m++) {
// TODO: Error handling required here
continue;
}
}
if (firstChild != null) {
}
}
}
// No 'Value' tags found. So ignore this key.
// TODO: More error handling required later for zero
// 'Value' tags.
continue;
}
}
}
}
}
/**
* Obtains a new instance of a DOM Document object
*
* @return a new instance of a DOM Document object
* @exception Exception
* if an error occurs while constructing a new document
*/
}
try {
return doc;
} catch (SAXParseException pe) {
} catch (SAXException sax) {
} catch (ParserConfigurationException pc) {
} catch (IOException ioe) {
}
}
return (null);
}
return (node);
}
return (null);
}
/**
* Checks if a node has a child of ELEMENT type.
*
* @param node
* a node
* @return true if the node has a child of ELEMENT type
*/
for (int i = 0; i < length; i++) {
return true;
}
}
return false;
}
/**
* Returns a child node that has the given node name and give attribute name
* and value.
*/
return (node);
}
}
return (null);
}
}
}
return (retVal);
}
/**
* Gets the value of an element. This method returns a concatenated String
* from all its TEXT children.
*
* @param element
* a DOM tree element.
* @return A String that contained in its TEXT children; or null if an error
* occurred.
*/
return null;
}
for (int i = 0; i < length; i++) {
}
}
}
/**
* Gets the children value of an element. This method returns a
* concatenated String from all its children.
* @param element a DOM tree element.
* @return A String that contained in its TEXT children;
* or null if an error occurred.
*/
return null;
}
for (int i = 0; i < length; i++) {
}
}
/**
* Gets the value of an element. This method returns a concatenated String
* from all its TEXT children.
*
* @param element
* a DOM tree element.
* @return A String that contained in its TEXT children; or null if an error
* occurred or the input contain non Node.TEXT_NODE node.
*/
return null;
}
} else {
return null;
}
}
}
return (null);
return (null);
return (value.getNodeValue());
}
/**
* Gets attribute value of a node.
*
* @param node
* a node
* @param namespaceURI
* attribute namespace URI
* @param attrName
* attribute name
* @return attribute value
*/
return null;
}
return null;
}
return value.getNodeValue();
}
/**
* Method to get Values within AttributeValuePair as a java.util.Set
*/
return (getAttributeValuePair(node, true));
}
/**
* Method to get Values within AttributeValuePair as a java.util.Set
* If <class>unescape<class> is set to false, xml escaped chars will not
* be unescaped.
*/
return (null);
}
}
}
return (retVal);
}
/**
* Method to get the value of "Value" node
*/
return (getValueOfValueNode(n, true));
}
/**
* Method to get the value of "Value" node
* If <class>unescape<class> is set to false, xml escaped chars will not
* be unescaped.
*/
}
/**
* Method to get the value of "Value" node
*/
return (getValueOfValueNodeNoTrim(n, true));
}
/**
* Method to get the value of "Value" node
* If <class>unescape<class> is set to false, xml escaped chars will not
* be unescaped.
*/
}
} else {
}
}
}
/**
* This method searches children of Element element for element with tagName
* and namespaceURI nsName. It searchs one level down only.
*
* @param element
* The root element
* @param nsName
* NamespaceURI
* @param tagName
* A String representing the name of the tag to be searched for.
* @return A List of elements that meet the criterial.
*/
public static List getElementsByTagNameNS1(
) {
for (int i = 0; i < length; i++) {
}
}
}
return list;
}
/**
* Print SAML Attribute Element and replace its prefix with the input
* prefix.
*
* @param node
* A DOM tree Node
* @param prefix
* A String representing the new prefix
* @return An xml String representation of the DOM tree.
*/
return null;
}
for (int i = 0; i < length; i++) {
// xml.append(normalize(attr.getNodeValue()));
}
for (int i = 0; i < len; i++) {
}
}
}
/**
* Print a Node tree recursively using UTF-8 encoding.
*
* @param node
* A DOM tree Node
* @return An xml String representation of the DOM tree.
*/
}
/**
* Prints a Node tree recursively.
* @param node A DOM tree Node
* @param encoding character encoding
* @return An xml String representation of the DOM tree.
*/
return null;
}
try {
} catch (Exception e) {
return null;
}
}
/**
* Returns unescape special character text.
*
* @param text String to be unescaped.
* @return unescape special character text.
*/
return text;
}
/**
* Removes invalid XML characters from the input text and then replaces XML special character <code>&</code>,
* <code><</code>, <code>></code>, <code>"</code>, <code>'</code> with corresponding entity references.
*
* @param text The input that needs to be escaped. May be null.
* @return String with the special characters replaced with entity references. May be null.
*/
return text;
}
switch (c) {
case '&':
break;
case '<':
break;
case '>':
break;
case '\"':
break;
case '\'':
break;
case '\n':
break;
case '\r':
break;
default:
}
}
}
}
/**
* Remove invalid XML characters from a string.
* @param text the text to cleanse.
* @return cleansed text or the original string if it is null or empty
*/
return text;
}
}
return set;
}
if (invalidXMLCharExists(st)) {
debug.warning("XMLUtils.encodeAttributeSet invalid XML characters get Base64 encoded to be : " + st);
}
}
}
return newSet;
}
return set;
}
}
}
return newSet;
}
if (c == '\u0000') {
}
return st;
}
/**
* Provides a secure DocumentBuilder implementation, which is protected against
* different types of entity expansion attacks and makes sure that only locally
* available DTDs can be referenced within the XML document.
* @param validating Whether the returned DocumentBuilder should validate input.
* @return A secure DocumentBuilder instance.
* @throws ParserConfigurationException In case xerces does not support one
* of the required features.
*/
public static DocumentBuilder getSafeDocumentBuilder(boolean validating) throws ParserConfigurationException {
}
/**
* Provides a secure SAXParser instance, which is protected against different
* types of entity expension, DoS attacks and makes sure that only locally
* available DTDs can be referenced within the XML document.
* @param validating Whether the returned DocumentBuilder should validate input.
* @return A secure SAXParser instance.
* @throws ParserConfigurationException In case Xerces does not support one of
* the required features.
* @throws SAXException In case Xerces does not support one of the required
* features.
*/
public static SAXParser getSafeSAXParser(boolean validating) throws ParserConfigurationException, SAXException {
}
/**
* Provides a cached {@link TransformerFactory} instance for the current thread.
*
* @return A cached {@link TransformerFactory} instance.
*/
public static TransformerFactory getTransformerFactory() {
}
/**
* Creates a SAXSource instance based on the incoming InputSource, which
* later on can be safely used by JAXB unmarshalling. The SAXSource will be
* protected against different types of entity expansion, DoS attacks and
* makes sure that only locally available DTDs can be referenced within the
* XML document.
* @param source The InputSource to be unmarshalled by JAXB
* @return A safe SAXSource instance
* @throws JAXBException In case an error occurs while creating the SAXSource
*/
try {
//Let's convert the exception to a JAXBException, so the unmarshalling
//codes can handle the failure.
}
}
}
class ValidationErrorHandler implements ErrorHandler {
}
}
}
}
}
}
}
}