XMLUtils.java revision 5c099afa7c9361afc2f4477fec0e3018588d7840
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster/**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * The contents of this file are subject to the terms
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * of the Common Development and Distribution License
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * (the License). You may not use this file except in
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * compliance with the License.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * You can obtain a copy of the License at
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * https://opensso.dev.java.net/public/CDDLv1.0.html or
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * opensso/legal/CDDLv1.0.txt
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * See the License for the specific language governing
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * permission and limitations under the License.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * When distributing Covered Code, include this CDDL
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Header Notice in each file and include the License file
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * at opensso/legal/CDDLv1.0.txt.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * If applicable, add the following below the CDDL Header,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * with the fields enclosed by brackets [] replaced by
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * your own identifying information:
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * "Portions Copyrighted [year] [name of copyright owner]"
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * $Id: XMLUtils.java,v 1.15 2009/10/19 18:19:20 asyhuang Exp $
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster/*
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Portions Copyrighted 2011 ForgeRock AS
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterpackage com.sun.identity.shared.xml;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport com.sun.identity.shared.Constants;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport com.sun.identity.shared.configuration.SystemPropertiesManager;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport com.sun.identity.shared.datastruct.OrderedSet;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport com.sun.identity.shared.debug.Debug;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport com.sun.identity.shared.encode.Base64;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.io.ByteArrayInputStream;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.io.ByteArrayOutputStream;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.io.IOException;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.io.InputStream;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.io.UnsupportedEncodingException;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.ArrayList;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.Collections;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.HashMap;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.HashSet;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.Iterator;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.List;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.Map;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.Set;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.regex.Pattern;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport java.util.regex.Matcher;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport javax.xml.parsers.DocumentBuilder;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport javax.xml.parsers.DocumentBuilderFactory;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport javax.xml.parsers.ParserConfigurationException;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport javax.xml.transform.Transformer;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport javax.xml.transform.TransformerFactory;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport javax.xml.transform.dom.DOMSource;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport javax.xml.transform.stream.StreamResult;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.w3c.dom.Attr;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.w3c.dom.Document;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.w3c.dom.Element;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.w3c.dom.NamedNodeMap;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.w3c.dom.Node;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.w3c.dom.NodeList;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.w3c.dom.Text;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.xml.sax.ErrorHandler;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.xml.sax.SAXException;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterimport org.xml.sax.SAXParseException;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster/**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This class contains utilities to parse XML documents
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterpublic class XMLUtils {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static final Map EMPTY_MAP = Collections
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster .unmodifiableMap(new HashMap());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // property to see if XML document validating is needed. The validating
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // is turned on only if the value for com.iplanet.am.util.xml.validating
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // property is set to "on" and value for com.iplanet.services.debug.level
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // property is set to "warning" or "message".
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static boolean validating = false;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static String ATTR_BASE64_ENCODED =
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster "com_sun_identity_opensso_base64_encoded";
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static int ATTR_BASE64_ENCODED_LENGTH =
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ATTR_BASE64_ENCODED.length();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static Pattern invalidXMLChars =
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Pattern.compile("[\u0000-\u0008\u000b-\u001f\ufffe\uffff]");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster static {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster try {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String xmlVal = SystemPropertiesManager.get(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Constants.XML_VALIDATING, "off");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String debugLevel = SystemPropertiesManager.get(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Constants.SERVICES_DEBUG_LEVEL, "error");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (xmlVal.trim().equalsIgnoreCase("on") &&
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster (debugLevel.trim().equalsIgnoreCase("warning") ||
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debugLevel.trim().equalsIgnoreCase("message"))
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster validating = true;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (Exception e) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // ignore since there is not debug class here
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public String getATTR_VALUE_PAIR_NODE() {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return ATTR_VALUE_PAIR_NODE;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Converts the XML document from a String format to DOM Document format.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param xmlString
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * is the XML document in a String format
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param debug
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * is the debug object used for logging debug info
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return Document is the DOM object obtained by converting the String XML
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns null if xmlString is null. Returns null if there are any
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * parser errores.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Document toDOMDocument(String xmlString, Debug debug) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((xmlString == null) || (xmlString.length() == 0)) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster try {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ByteArrayInputStream is = new ByteArrayInputStream(xmlString
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster .getBytes("UTF-8"));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return toDOMDocument(is, debug);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (UnsupportedEncodingException uee) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (debug != null && debug.warningEnabled()) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.warning("Can't parse the XML document:\n" + xmlString,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster uee);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Converts the XML document from an input stream to DOM Document format.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param is
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * is the InputStream that contains XML document
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return Document is the DOM object obtained by parsing the input stream.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns null if there are any parser errores.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Document toDOMDocument(InputStream is, Debug debug) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /*
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Constructing a DocumentBuilderFactory for every call is less
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * expensive than a synchronizing a single instance of the factory and
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * obtaining the builder
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster DocumentBuilderFactory dbFactory = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster try {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // Assign new debug object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster dbFactory = DocumentBuilderFactory.newInstance();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster dbFactory.setValidating(validating);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster dbFactory.setNamespaceAware(true);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (Exception e) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (debug != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.error("XMLUtils.DocumentBuilder init failed", e);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster try {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster DocumentBuilder documentBuilder = dbFactory.newDocumentBuilder();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (documentBuilder == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (debug != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.error("XMLUtils.toDOM : null builder instance");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster documentBuilder.setEntityResolver(new XMLHandler());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (debug != null && debug.warningEnabled()) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster documentBuilder.setErrorHandler(new ValidationErrorHandler(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return documentBuilder.parse(is);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (Exception e) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // Since there may potentially be several invalid XML documents
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // that are mostly client-side errors, only a warning is logged for
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // efficiency reasons.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (debug != null && debug.warningEnabled()) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.warning("Can't parse the XML document", e);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This method parse an Attributes tag, DTD for Attribute is as follows.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * <pre>
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * &lt; !-- This DTD defines the DPro Attribute tag.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Unique Declaration name for DOCTYPE tag:
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * &quot;Directory Pro 5.0 Attributes DTD&quot;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * --&gt;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * &lt; !ELEMENT Attributes (Attribute)+&gt;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * &lt; !ELEMENT Attribute EMPTY&gt;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * &lt; !ATTLIST Attribute
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * name NMTOKEN #REQUIRED
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * &gt;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * </pre>
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param n
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return Set Set of the attribute names
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Set parseAttributesTag(Node n) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // get Attribute node list
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList attributes = n.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster final int numAttributes = attributes.getLength();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (numAttributes == 0) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set set = new HashSet();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int l = 0; l < numAttributes; l++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // get next attribute
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node attr = attributes.item(l);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((attr.getNodeType() != Node.ELEMENT_NODE)
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster && !attr.getNodeName().equals("Attribute")) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // need error handling
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster continue;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster set.add(((Element) attr).getAttribute("name"));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return set;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param parentNode
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * is the element tag that contains all the AttirbuteValuePair
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * tags as children
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return Map Returns the AV pairs in a Map where each entry in the Map is
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * an AV pair. The key is the attribute name and the value is a Set
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * of String objects.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Map parseAttributeValuePairTags(Node parentNode) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList avList = parentNode.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Map map = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int numAVPairs = avList.getLength();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (numAVPairs <= 0) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return EMPTY_MAP;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int l = 0; l < numAVPairs; l++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node avPair = avList.item(l);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((avPair.getNodeType() != Node.ELEMENT_NODE) ||
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster !avPair.getNodeName().equals("AttributeValuePair")
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster continue;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList leafNodeList = avPair.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster long numLeafNodes = leafNodeList.getLength();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (numLeafNodes < 2) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // TODO: More error handling required later for missing
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // 'Attribute' or
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // 'Value' tags.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster continue;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String key = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set values = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // Since Attribute tag is always the first leaf node as per the
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // DTD,and
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // values can one or more, Attribute tag can be parsed first and
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // then
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // iterate over the values, if any.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node attributeNode = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < numLeafNodes; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster attributeNode = leafNodeList.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((attributeNode.getNodeType() == Node.ELEMENT_NODE)
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster && (attributeNode.getNodeName().equals("Attribute"))) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster i = (int) numLeafNodes;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } else {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster continue;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster key = ((Element) attributeNode).getAttribute("name");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // Now parse the Value tags. If there are not 'Value' tags, ignore
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // this key
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // TODO: More error handling required later for zero 'Value' tags.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int m = 0; m < numLeafNodes; m++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node valueNode = leafNodeList.item(m);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((valueNode.getNodeType() != Node.ELEMENT_NODE)
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster || !valueNode.getNodeName().equals("Value")) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // TODO: Error handling required here
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster continue;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (values == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster values = new HashSet();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node fchild = (Text) valueNode.getFirstChild();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (fchild != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String value = fchild.getNodeValue();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (value != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster values.add(value.trim());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (values == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // No 'Value' tags found. So ignore this key.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // TODO: More error handling required later for zero
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // 'Value'tags.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster continue;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (map == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster map = new HashMap();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set oldValues = (Set)map.get(key);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (oldValues != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster values.addAll(oldValues);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster map.put(key, values);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // now reset values to prepare for the next AV pair.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster values = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (map == null) ? EMPTY_MAP : map;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Obtains a new instance of a DOM Document object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return a new instance of a DOM Document object
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @exception Exception
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * if an error occurs while constructing a new document
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Document newDocument() throws ParserConfigurationException {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster dbFactory.setNamespaceAware(true);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster dbFactory.setValidating(validating);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return dbFactory.newDocumentBuilder().newDocument();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Document getXMLDocument(InputStream in) throws Exception {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster try {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster DocumentBuilderFactory factory = DocumentBuilderFactory
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster .newInstance();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster factory.setValidating(validating);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster DocumentBuilder builder = factory.newDocumentBuilder();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Document doc = builder.parse(in);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (doc);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (SAXParseException pe) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String msg = "\n" + pe.getMessage() + "\n";
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Object params[] = { new Integer(pe.getLineNumber()) };
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster throw (new Exception(msg + "XMLUtils.parser_error" + params));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (SAXException sax) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Object params[] = { new String(sax.getMessage()) };
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster throw (new Exception("XMLUtils.exception_message" + params));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (ParserConfigurationException pc) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Object params[] = { new String(pc.getMessage()) };
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster throw (new Exception("XMLUtils.invalid_xml_document" + params));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (IOException ioe) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Object params[] = { new String(ioe.getMessage()) };
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster throw (new Exception("XMLUtils.invalid_input_stream" + params));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Node getRootNode(Document doc, String nodeName) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList nodes = doc.getElementsByTagName(nodeName);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (nodes == null || nodes.getLength() == 0)
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (null);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (nodes.item(0));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Node getChildNode(Node parentNode, String childName) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList children = parentNode.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < children.getLength(); i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node node = children.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (node.getNodeName().equalsIgnoreCase(childName))
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (node);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (null);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Checks if a node has a child of ELEMENT type.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * a node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return true if the node has a child of ELEMENT type
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static boolean hasElementChild(Node node) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList nl = node.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node child = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int length = nl.getLength();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < length; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster child = nl.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (child.getNodeType() == Node.ELEMENT_NODE) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return true;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return false;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns a child node that has the given node name and give attribute name
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * and value.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Node getNamedChildNode(Node parentNode, String childNodeName,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String attrName, String attrValue) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList children = parentNode.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < children.getLength(); i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node node = children.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (childNodeName.equalsIgnoreCase(node.getNodeName())) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (getNodeAttributeValue(node, attrName).equalsIgnoreCase(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster attrValue))
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (node);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (null);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Set getChildNodes(Node parentNode, String childName) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set retVal = new HashSet();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList children = parentNode.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < children.getLength(); i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node node = children.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (node.getNodeName().equalsIgnoreCase(childName)) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster retVal.add(node);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (retVal);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the value of an element. This method returns a concatenated String
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * from all its TEXT children.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param element
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * a DOM tree element.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return A String that contained in its TEXT children; or null if an error
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * occurred.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getElementValue(Element element) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (element == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StringBuffer sb = new StringBuffer(1000);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList nl = element.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node child = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int length = nl.getLength();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < length; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster child = nl.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (child.getNodeType() == Node.TEXT_NODE) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append(child.getNodeValue());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return sb.toString().trim();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the children value of an element. This method returns a
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * concatenated String from all its children.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param element a DOM tree element.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return A String that contained in its TEXT children;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * or null if an error occurred.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getChildrenValue(Element element) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (element == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StringBuffer sb = new StringBuffer(1000);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList nl = element.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int length = nl.getLength();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < length; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append(XMLUtils.print(nl.item(i)));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return sb.toString().trim();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets the value of an element. This method returns a concatenated String
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * from all its TEXT children.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param element
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * a DOM tree element.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return A String that contained in its TEXT children; or null if an error
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * occurred or the input contain non Node.TEXT_NODE node.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getElementString(Element element) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (element == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StringBuffer sb = new StringBuffer(1000);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList nl = element.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node child = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0, length = nl.getLength(); i < length; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster child = nl.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (child.getNodeType() == Node.TEXT_NODE) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append(child.getNodeValue());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } else {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return sb.toString().trim();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getNodeAttributeValue(Node node, String attrName) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NamedNodeMap attrs = node.getAttributes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (attrs == null)
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (null);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node value = attrs.getNamedItem(attrName);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (value == null)
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (null);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (value.getNodeValue());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Gets attribute value of a node.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * a node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param namespaceURI
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * attribute namespace URI
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param attrName
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * attribute name
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return attribute value
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getNodeAttributeValueNS(Node node,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String namespaceURI, String attrName) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NamedNodeMap attrs = node.getAttributes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (attrs == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node value = attrs.getNamedItemNS(namespaceURI, attrName);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (value == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return value.getNodeValue();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Method to get Values within AttributeValuePair as a java.util.Set
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Set getAttributeValuePair(Node node) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (getAttributeValuePair(node, true));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Method to get Values within AttributeValuePair as a java.util.Set
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * If <class>unescape<class> is set to false, xml escaped chars will not
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * be unescaped.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Set getAttributeValuePair(Node node, boolean unescape) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (!node.getNodeName().equals(ATTR_VALUE_PAIR_NODE)) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (null);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set retVal = new OrderedSet();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList children = node.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < children.getLength(); i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node n = children.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (n.getNodeName().equalsIgnoreCase(VALUE_NODE)) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster retVal.add(getValueOfValueNode(n, unescape));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (retVal);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Method to get the value of "Value" node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getValueOfValueNode(Node n) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (getValueOfValueNode(n, true));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Method to get the value of "Value" node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * If <class>unescape<class> is set to false, xml escaped chars will not
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * be unescaped.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getValueOfValueNode(Node n, boolean unescape) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return getValueOfValueNodeNoTrim(n, unescape).trim();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Method to get the value of "Value" node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getValueOfValueNodeNoTrim(Node n) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return (getValueOfValueNodeNoTrim(n, true));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Method to get the value of "Value" node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * If <class>unescape<class> is set to false, xml escaped chars will not
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * be unescaped.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String getValueOfValueNodeNoTrim(Node n, boolean unescape) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList textNodes = n.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node textNode;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StringBuffer value = new StringBuffer("");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int j = 0; j < textNodes.getLength(); j++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster textNode = textNodes.item(j);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String text = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (textNode.getNodeType() == Node.TEXT_NODE) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster text = textNode.getNodeValue();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } else if (textNode.getNodeType() == Node.ELEMENT_NODE) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster text = print(textNode);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (text != null && unescape) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster value.append(unescapeSpecialCharacters(text));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } else {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster value.append(text);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return value.toString();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * This method searches children of Element element for element with tagName
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * and namespaceURI nsName. It searchs one level down only.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param element
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * The root element
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param nsName
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * NamespaceURI
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param tagName
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * A String representing the name of the tag to be searched for.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return A List of elements that meet the criterial.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static List getElementsByTagNameNS1(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Element element,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String nsName,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String tagName
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster List list = new ArrayList();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (element != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList nl = element.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int length = nl.getLength();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < length; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Node child = nl.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String childName = child.getLocalName();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String childNS = child.getNamespaceURI();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((childName != null) && (childName.equals(tagName))
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster && (childNS != null) && (childNS.equals(nsName))) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster list.add(child);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return list;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Print SAML Attribute Element and replace its prefix with the input
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * prefix.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * A DOM tree Node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param prefix
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * A String representing the new prefix
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return An xml String representation of the DOM tree.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String printAttributeValue(Element node, String prefix) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (node == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StringBuffer xml = new StringBuffer(100);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append('<');
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append(prefix).append(node.getLocalName());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NamedNodeMap attrs = node.getAttributes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int length = attrs.getLength();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < length; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Attr attr = (Attr) attrs.item(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append(' ');
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append(attr.getNodeName());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append("=\"");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster // xml.append(normalize(attr.getNodeValue()));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append(attr.getNodeValue());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append('"');
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append('>');
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster NodeList children = node.getChildNodes();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (children != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int len = children.getLength();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (int i = 0; i < len; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append(print(children.item(i)));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append("</");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append(prefix).append(node.getLocalName());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster xml.append('>');
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return xml.toString();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Print a Node tree recursively using UTF-8 encoding.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * A DOM tree Node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return An xml String representation of the DOM tree.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String print(Node node) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return print(node, "UTF-8");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Prints a Node tree recursively.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param node A DOM tree Node
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param encoding character encoding
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return An xml String representation of the DOM tree.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String print(Node node, String encoding) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (node == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster try {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster TransformerFactory tFactory =
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster TransformerFactory.newInstance();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Transformer transformer = tFactory.newTransformer();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster transformer.setOutputProperty("omit-xml-declaration", "yes");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster transformer.setOutputProperty("encoding", encoding);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster DOMSource source = new DOMSource(node);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ByteArrayOutputStream os = new ByteArrayOutputStream(2000);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StreamResult result = new StreamResult(os);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster transformer.transform(source, result);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return os.toString(encoding);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster } catch (Exception e) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Returns unescape special character text.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @param text String to be unescaped.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return unescape special character text.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String unescapeSpecialCharacters(String text) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster text = text.replaceAll("&amp;", "&");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster text = text.replaceAll("&lt;", "<");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster text = text.replaceAll("&gt;", ">");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster text = text.replaceAll("&quot;", "\"");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster text = text.replaceAll("&apos;", "'");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster text = text.replaceAll("&#xA;", "\n");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster text = text.replaceAll("&#xD;", "\r");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return text;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster /**
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * Replaces XML special character <code>&</code>, <code>&lt;</code>,
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * <code>&gt;</code>, <code>"</code>, <code>'</code> with
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * corresponding entity references.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster *
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * @return String with the special characters replaced with entity
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster * references.
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster */
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String escapeSpecialCharacters(String text) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String escapedText = text;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster StringBuffer sb = null;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int len = 0;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (text != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster len = text.length();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int i = 0;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster boolean specialCharacterFound = false;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (; i < len; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster char c = text.charAt(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (c == '&' || c == '<' || c == '>' || c == '\'' || c == '\"'
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster || c == '\n' || c == '\r') {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster specialCharacterFound = true;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster break;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (specialCharacterFound) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb = new StringBuffer();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append(text.substring(0, i));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster for (; i < len; i++) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster char c = text.charAt(i);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster switch (c) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster case '&':
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append("&amp;");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster break;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster case '<':
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append("&lt;");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster break;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster case '>':
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append("&gt;");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster break;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster case '\"':
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append("&quot;");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster break;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster case '\'':
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append("&apos;");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster break;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster case '\n':
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append("&#xA;");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster break;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster case '\r':
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append("&#xD;");
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster break;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster default:
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster sb.append(c);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster escapedText = sb.toString();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return escapedText;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static boolean invalidXMLCharExists(String st) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Matcher matcher = invalidXMLChars.matcher(st);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return matcher.find();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Set encodeAttributeSet(Set set, Debug debug) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (set == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return set;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set newSet = new HashSet();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Iterator iter = set.iterator();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster while(iter.hasNext()) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Object obj = iter.next();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String st = (String)obj;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (invalidXMLCharExists(st)) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster st = Base64.encode(st.getBytes());
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((debug != null) && (debug.warningEnabled())) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.warning("XMLUtils.encodeAttributeSet invalid XML characters get Base64 encoded to be : " + st);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster st = ATTR_BASE64_ENCODED + st;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster newSet.add(st);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return newSet;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static Set decodeAttributeSet(Set set) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (set == null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return set;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Set newSet = new HashSet();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Iterator iter = set.iterator();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster while(iter.hasNext()) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster Object obj = iter.next();
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster String st = (String)obj;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (st.startsWith(ATTR_BASE64_ENCODED)) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster st = new String(Base64.decode(
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster st.substring(ATTR_BASE64_ENCODED_LENGTH)));
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster newSet.add(st);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return newSet;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public static String removeNullCharAtEnd(String st) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster int index = st.length() - 1;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster char c = st.charAt(index);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (c == '\u0000') {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return st.substring(0, index);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster return st;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static String ATTR_VALUE_PAIR_NODE = "AttributeValuePair";
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private static String VALUE_NODE = "Value";
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster}
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Fosterclass ValidationErrorHandler implements ErrorHandler {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster private Debug debug;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster ValidationErrorHandler(Debug tmpDebug) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug = tmpDebug;
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public void fatalError(SAXParseException spe) throws SAXParseException {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (debug != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.error("XMLUtils.fatalError", spe);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public void error(SAXParseException spe) throws SAXParseException {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if (debug != null) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.warning("XMLUtils.error", spe);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster public void warning(SAXParseException spe) throws SAXParseException {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster if ((debug != null) && (debug.warningEnabled())) {
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster debug.warning("XMLUtils.warning", spe);
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster }
5c099afa7c9361afc2f4477fec0e3018588d7840Allan Foster}