|
Java example source code file (XMLUtils.java)
The XMLUtils.java Java example source code/* * reserved comment block * DO NOT REMOVE OR ALTER! */ /** * 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. */ package com.sun.org.apache.xml.internal.security.utils; import java.io.IOException; import java.io.OutputStream; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException; import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer; import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.ProcessingInstruction; import org.w3c.dom.Text; /** * DOM and XML accessibility and comfort functions. * * @author Christian Geuer-Pollmann */ public class XMLUtils { private static boolean ignoreLineBreaks = AccessController.doPrivileged(new PrivilegedAction<Boolean>() { public Boolean run() { return Boolean.valueOf(Boolean.getBoolean ("com.sun.org.apache.xml.internal.security.ignoreLineBreaks")); } }).booleanValue(); private static volatile String dsPrefix = "ds"; private static volatile String ds11Prefix = "dsig11"; private static volatile String xencPrefix = "xenc"; private static volatile String xenc11Prefix = "xenc11"; /** {@link org.apache.commons.logging} logging facility */ private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(XMLUtils.class.getName()); /** * Constructor XMLUtils * */ private XMLUtils() { // we don't allow instantiation } /** * Set the prefix for the digital signature namespace * @param prefix the new prefix for the digital signature namespace */ public static void setDsPrefix(String prefix) { dsPrefix = prefix; } /** * Set the prefix for the digital signature 1.1 namespace * @param prefix the new prefix for the digital signature 1.1 namespace */ public static void setDs11Prefix(String prefix) { ds11Prefix = prefix; } /** * Set the prefix for the encryption namespace * @param prefix the new prefix for the encryption namespace */ public static void setXencPrefix(String prefix) { xencPrefix = prefix; } /** * Set the prefix for the encryption namespace 1.1 * @param prefix the new prefix for the encryption namespace 1.1 */ public static void setXenc11Prefix(String prefix) { xenc11Prefix = prefix; } public static Element getNextElement(Node el) { Node node = el; while ((node != null) && (node.getNodeType() != Node.ELEMENT_NODE)) { node = node.getNextSibling(); } return (Element)node; } /** * @param rootNode * @param result * @param exclude * @param com whether comments or not */ public static void getSet(Node rootNode, Set<Node> result, Node exclude, boolean com) { if ((exclude != null) && isDescendantOrSelf(exclude, rootNode)) { return; } getSetRec(rootNode, result, exclude, com); } @SuppressWarnings("fallthrough") private static void getSetRec(final Node rootNode, final Set<Node> result, final Node exclude, final boolean com) { if (rootNode == exclude) { return; } switch (rootNode.getNodeType()) { case Node.ELEMENT_NODE: result.add(rootNode); Element el = (Element)rootNode; if (el.hasAttributes()) { NamedNodeMap nl = el.getAttributes(); for (int i = 0;i < nl.getLength(); i++) { result.add(nl.item(i)); } } //no return keep working case Node.DOCUMENT_NODE: for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) { if (r.getNodeType() == Node.TEXT_NODE) { result.add(r); while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) { r = r.getNextSibling(); } if (r == null) { return; } } getSetRec(r, result, exclude, com); } return; case Node.COMMENT_NODE: if (com) { result.add(rootNode); } return; case Node.DOCUMENT_TYPE_NODE: return; default: result.add(rootNode); } } /** * Outputs a DOM tree to an {@link OutputStream}. * * @param contextNode root node of the DOM tree * @param os the {@link OutputStream} */ public static void outputDOM(Node contextNode, OutputStream os) { XMLUtils.outputDOM(contextNode, os, false); } /** * Outputs a DOM tree to an {@link OutputStream}. <I>If an Exception is * thrown during execution, it's StackTrace is output to System.out, but the * Exception is not re-thrown.</I> * * @param contextNode root node of the DOM tree * @param os the {@link OutputStream} * @param addPreamble */ public static void outputDOM(Node contextNode, OutputStream os, boolean addPreamble) { try { if (addPreamble) { os.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes("UTF-8")); } os.write(Canonicalizer.getInstance( Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(contextNode) ); } catch (IOException ex) { if (log.isLoggable(java.util.logging.Level.FINE)) { log.log(java.util.logging.Level.FINE, ex.getMessage(), ex); } } catch (InvalidCanonicalizerException ex) { if (log.isLoggable(java.util.logging.Level.FINE)) { log.log(java.util.logging.Level.FINE, ex.getMessage(), ex); } } catch (CanonicalizationException ex) { if (log.isLoggable(java.util.logging.Level.FINE)) { log.log(java.util.logging.Level.FINE, ex.getMessage(), ex); } } } /** * Serializes the <CODE>contextNode into the OutputStream, but * suppresses all Exceptions</I>. * <BR /> * NOTE: <I>This should only be used for debugging purposes, * NOT in a production environment; this method ignores all exceptions, * so you won't notice if something goes wrong. If you're asking what is to * be used in a production environment, simply use the code inside the * <code>try{} statement, but handle the Exceptions appropriately. * * @param contextNode * @param os */ public static void outputDOMc14nWithComments(Node contextNode, OutputStream os) { try { os.write(Canonicalizer.getInstance( Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(contextNode) ); } catch (IOException ex) { if (log.isLoggable(java.util.logging.Level.FINE)) { log.log(java.util.logging.Level.FINE, ex.getMessage(), ex); } // throw new RuntimeException(ex.getMessage()); } catch (InvalidCanonicalizerException ex) { if (log.isLoggable(java.util.logging.Level.FINE)) { log.log(java.util.logging.Level.FINE, ex.getMessage(), ex); } // throw new RuntimeException(ex.getMessage()); } catch (CanonicalizationException ex) { if (log.isLoggable(java.util.logging.Level.FINE)) { log.log(java.util.logging.Level.FINE, ex.getMessage(), ex); } // throw new RuntimeException(ex.getMessage()); } } /** * Method getFullTextChildrenFromElement * * @param element * @return the string of children */ public static String getFullTextChildrenFromElement(Element element) { StringBuilder sb = new StringBuilder(); Node child = element.getFirstChild(); while (child != null) { if (child.getNodeType() == Node.TEXT_NODE) { sb.append(((Text)child).getData()); } child = child.getNextSibling(); } return sb.toString(); } /** * Creates an Element in the XML Signature specification namespace. * * @param doc the factory Document * @param elementName the local name of the Element * @return the Element */ public static Element createElementInSignatureSpace(Document doc, String elementName) { if (doc == null) { throw new RuntimeException("Document is null"); } if ((dsPrefix == null) || (dsPrefix.length() == 0)) { return doc.createElementNS(Constants.SignatureSpecNS, elementName); } return doc.createElementNS(Constants.SignatureSpecNS, dsPrefix + ":" + elementName); } /** * Creates an Element in the XML Signature 1.1 specification namespace. * * @param doc the factory Document * @param elementName the local name of the Element * @return the Element */ public static Element createElementInSignature11Space(Document doc, String elementName) { if (doc == null) { throw new RuntimeException("Document is null"); } if ((ds11Prefix == null) || (ds11Prefix.length() == 0)) { return doc.createElementNS(Constants.SignatureSpec11NS, elementName); } return doc.createElementNS(Constants.SignatureSpec11NS, ds11Prefix + ":" + elementName); } /** * Creates an Element in the XML Encryption specification namespace. * * @param doc the factory Document * @param elementName the local name of the Element * @return the Element */ public static Element createElementInEncryptionSpace(Document doc, String elementName) { if (doc == null) { throw new RuntimeException("Document is null"); } if ((xencPrefix == null) || (xencPrefix.length() == 0)) { return doc.createElementNS(EncryptionConstants.EncryptionSpecNS, elementName); } return doc.createElementNS( EncryptionConstants.EncryptionSpecNS, xencPrefix + ":" + elementName ); } /** * Creates an Element in the XML Encryption 1.1 specification namespace. * * @param doc the factory Document * @param elementName the local name of the Element * @return the Element */ public static Element createElementInEncryption11Space(Document doc, String elementName) { if (doc == null) { throw new RuntimeException("Document is null"); } if ((xenc11Prefix == null) || (xenc11Prefix.length() == 0)) { return doc.createElementNS(EncryptionConstants.EncryptionSpec11NS, elementName); } return doc.createElementNS( EncryptionConstants.EncryptionSpec11NS, xenc11Prefix + ":" + elementName ); } /** * Returns true if the element is in XML Signature namespace and the local * name equals the supplied one. * * @param element * @param localName * @return true if the element is in XML Signature namespace and the local name equals * the supplied one */ public static boolean elementIsInSignatureSpace(Element element, String localName) { if (element == null){ return false; } return Constants.SignatureSpecNS.equals(element.getNamespaceURI()) && element.getLocalName().equals(localName); } /** * Returns true if the element is in XML Signature 1.1 namespace and the local * name equals the supplied one. * * @param element * @param localName * @return true if the element is in XML Signature namespace and the local name equals * the supplied one */ public static boolean elementIsInSignature11Space(Element element, String localName) { if (element == null) { return false; } return Constants.SignatureSpec11NS.equals(element.getNamespaceURI()) && element.getLocalName().equals(localName); } /** * Returns true if the element is in XML Encryption namespace and the local * name equals the supplied one. * * @param element * @param localName * @return true if the element is in XML Encryption namespace and the local name * equals the supplied one */ public static boolean elementIsInEncryptionSpace(Element element, String localName) { if (element == null){ return false; } return EncryptionConstants.EncryptionSpecNS.equals(element.getNamespaceURI()) && element.getLocalName().equals(localName); } /** * Returns true if the element is in XML Encryption 1.1 namespace and the local * name equals the supplied one. * * @param element * @param localName * @return true if the element is in XML Encryption 1.1 namespace and the local name * equals the supplied one */ public static boolean elementIsInEncryption11Space(Element element, String localName) { if (element == null){ return false; } return EncryptionConstants.EncryptionSpec11NS.equals(element.getNamespaceURI()) && element.getLocalName().equals(localName); } /** * This method returns the owner document of a particular node. * This method is necessary because it <I>always returns a * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null * if the {@link Node} is a {@link Document}. * * @param node * @return the owner document of the node */ public static Document getOwnerDocument(Node node) { if (node.getNodeType() == Node.DOCUMENT_NODE) { return (Document) node; } try { return node.getOwnerDocument(); } catch (NullPointerException npe) { throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0") + " Original message was \"" + npe.getMessage() + "\""); } } /** * This method returns the first non-null owner document of the Nodes in this Set. * This method is necessary because it <I>always returns a * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null * if the {@link Node} is a {@link Document}. * * @param xpathNodeSet * @return the owner document */ public static Document getOwnerDocument(Set<Node> xpathNodeSet) { NullPointerException npe = null; for (Node node : xpathNodeSet) { int nodeType = node.getNodeType(); if (nodeType == Node.DOCUMENT_NODE) { return (Document) node; } try { if (nodeType == Node.ATTRIBUTE_NODE) { return ((Attr)node).getOwnerElement().getOwnerDocument(); } return node.getOwnerDocument(); } catch (NullPointerException e) { npe = e; } } throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0") + " Original message was \"" + (npe == null ? "" : npe.getMessage()) + "\""); } /** * Method createDSctx * * @param doc * @param prefix * @param namespace * @return the element. */ public static Element createDSctx(Document doc, String prefix, String namespace) { if ((prefix == null) || (prefix.trim().length() == 0)) { throw new IllegalArgumentException("You must supply a prefix"); } Element ctx = doc.createElementNS(null, "namespaceContext"); ctx.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix.trim(), namespace); return ctx; } /** * Method addReturnToElement * * @param e */ public static void addReturnToElement(Element e) { if (!ignoreLineBreaks) { Document doc = e.getOwnerDocument(); e.appendChild(doc.createTextNode("\n")); } } public static void addReturnToElement(Document doc, HelperNodeList nl) { if (!ignoreLineBreaks) { nl.appendChild(doc.createTextNode("\n")); } } public static void addReturnBeforeChild(Element e, Node child) { if (!ignoreLineBreaks) { Document doc = e.getOwnerDocument(); e.insertBefore(doc.createTextNode("\n"), child); } } /** * Method convertNodelistToSet * * @param xpathNodeSet * @return the set with the nodelist */ public static Set<Node> convertNodelistToSet(NodeList xpathNodeSet) { if (xpathNodeSet == null) { return new HashSet<Node>(); } int length = xpathNodeSet.getLength(); Set<Node> set = new HashSet Other Java examples (source code examples)Here is a short list of links related to this Java XMLUtils.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.