org.jcp.xml.dsig.internal.dom.DOMUtils Maven / Gradle / Ivy
The newest version!
/* */ package org.jcp.xml.dsig.internal.dom;
/* */
/* */ import com.sun.org.apache.xml.internal.security.utils.IdResolver;
/* */ import java.security.spec.AlgorithmParameterSpec;
/* */ import java.util.AbstractSet;
/* */ import java.util.Iterator;
/* */ import java.util.List;
/* */ import java.util.NoSuchElementException;
/* */ import java.util.Set;
/* */ import javax.xml.crypto.XMLCryptoContext;
/* */ import javax.xml.crypto.XMLStructure;
/* */ import javax.xml.crypto.dom.DOMStructure;
/* */ import javax.xml.crypto.dsig.spec.ExcC14NParameterSpec;
/* */ import javax.xml.crypto.dsig.spec.XPathFilter2ParameterSpec;
/* */ import javax.xml.crypto.dsig.spec.XPathFilterParameterSpec;
/* */ import javax.xml.crypto.dsig.spec.XPathType;
/* */ import javax.xml.crypto.dsig.spec.XSLTTransformParameterSpec;
/* */ import org.w3c.dom.Attr;
/* */ import org.w3c.dom.Document;
/* */ import org.w3c.dom.Element;
/* */ import org.w3c.dom.Node;
/* */ import org.w3c.dom.NodeList;
/* */
/* */ public class DOMUtils
/* */ {
/* */ public static Document getOwnerDocument(Node node)
/* */ {
/* 40 */ if (node.getNodeType() == 9) {
/* 41 */ return (Document)node;
/* */ }
/* 43 */ return node.getOwnerDocument();
/* */ }
/* */
/* */ public static Element createElement(Document doc, String tag, String nsURI, String prefix)
/* */ {
/* 59 */ String qName = prefix + ":" + tag;
/* 60 */ return doc.createElementNS(nsURI, qName);
/* */ }
/* */
/* */ public static void setAttribute(Element elem, String name, String value)
/* */ {
/* 72 */ if (value == null) return;
/* 73 */ elem.setAttributeNS(null, name, value);
/* */ }
/* */
/* */ public static void setAttributeID(Element elem, String name, String value)
/* */ {
/* 87 */ if (value == null) return;
/* 88 */ elem.setAttributeNS(null, name, value);
/* 89 */ IdResolver.registerElementById(elem, value);
/* */ }
/* */
/* */ public static Element getFirstChildElement(Node node)
/* */ {
/* 102 */ Node child = node.getFirstChild();
/* 103 */ while ((child != null) && (child.getNodeType() != 1)) {
/* 104 */ child = child.getNextSibling();
/* */ }
/* 106 */ return (Element)child;
/* */ }
/* */
/* */ public static Element getLastChildElement(Node node)
/* */ {
/* 119 */ Node child = node.getLastChild();
/* 120 */ while ((child != null) && (child.getNodeType() != 1)) {
/* 121 */ child = child.getPreviousSibling();
/* */ }
/* 123 */ return (Element)child;
/* */ }
/* */
/* */ public static Element getNextSiblingElement(Node node)
/* */ {
/* 136 */ Node sibling = node.getNextSibling();
/* 137 */ while ((sibling != null) && (sibling.getNodeType() != 1)) {
/* 138 */ sibling = sibling.getNextSibling();
/* */ }
/* 140 */ return (Element)sibling;
/* */ }
/* */
/* */ public static String getAttributeValue(Element elem, String name)
/* */ {
/* 158 */ Attr attr = elem.getAttributeNodeNS(null, name);
/* 159 */ return attr == null ? null : attr.getValue();
/* */ }
/* */
/* */ public static Set nodeSet(NodeList nl)
/* */ {
/* 170 */ return new NodeSet(nl);
/* */ }
/* */
/* */ public static String getNSPrefix(XMLCryptoContext context, String nsURI)
/* */ {
/* 209 */ if (context != null) {
/* 210 */ return context.getNamespacePrefix(nsURI, context.getDefaultNamespacePrefix());
/* */ }
/* */
/* 213 */ return null;
/* */ }
/* */
/* */ public static String getSignaturePrefix(XMLCryptoContext context)
/* */ {
/* 225 */ return getNSPrefix(context, "http://www.w3.org/2000/09/xmldsig#");
/* */ }
/* */
/* */ public static void removeAllChildren(Node node)
/* */ {
/* 234 */ NodeList children = node.getChildNodes();
/* 235 */ int i = 0; for (int length = children.getLength(); i < length; i++)
/* 236 */ node.removeChild(children.item(i));
/* */ }
/* */
/* */ public static boolean nodesEqual(Node thisNode, Node otherNode)
/* */ {
/* 244 */ if (thisNode == otherNode) {
/* 245 */ return true;
/* */ }
/* 247 */ if (thisNode.getNodeType() != otherNode.getNodeType()) {
/* 248 */ return false;
/* */ }
/* */
/* 251 */ return true;
/* */ }
/* */
/* */ public static void appendChild(Node parent, Node child)
/* */ {
/* 260 */ Document ownerDoc = getOwnerDocument(parent);
/* 261 */ if (child.getOwnerDocument() != ownerDoc)
/* 262 */ parent.appendChild(ownerDoc.importNode(child, true));
/* */ else
/* 264 */ parent.appendChild(child);
/* */ }
/* */
/* */ public static boolean paramsEqual(AlgorithmParameterSpec spec1, AlgorithmParameterSpec spec2)
/* */ {
/* 270 */ if (spec1 == spec2) {
/* 271 */ return true;
/* */ }
/* 273 */ if (((spec1 instanceof XPathFilter2ParameterSpec)) && ((spec2 instanceof XPathFilter2ParameterSpec)))
/* */ {
/* 275 */ return paramsEqual((XPathFilter2ParameterSpec)spec1, (XPathFilter2ParameterSpec)spec2);
/* */ }
/* */
/* 278 */ if (((spec1 instanceof ExcC14NParameterSpec)) && ((spec2 instanceof ExcC14NParameterSpec)))
/* */ {
/* 280 */ return paramsEqual((ExcC14NParameterSpec)spec1, (ExcC14NParameterSpec)spec2);
/* */ }
/* */
/* 283 */ if (((spec1 instanceof XPathFilterParameterSpec)) && ((spec2 instanceof XPathFilterParameterSpec)))
/* */ {
/* 285 */ return paramsEqual((XPathFilterParameterSpec)spec1, (XPathFilterParameterSpec)spec2);
/* */ }
/* */
/* 288 */ if (((spec1 instanceof XSLTTransformParameterSpec)) && ((spec2 instanceof XSLTTransformParameterSpec)))
/* */ {
/* 290 */ return paramsEqual((XSLTTransformParameterSpec)spec1, (XSLTTransformParameterSpec)spec2);
/* */ }
/* */
/* 293 */ return false;
/* */ }
/* */
/* */ private static boolean paramsEqual(XPathFilter2ParameterSpec spec1, XPathFilter2ParameterSpec spec2)
/* */ {
/* 299 */ List types = spec1.getXPathList();
/* 300 */ List otypes = spec2.getXPathList();
/* 301 */ int size = types.size();
/* 302 */ if (size != otypes.size()) {
/* 303 */ return false;
/* */ }
/* 305 */ for (int i = 0; i < size; i++) {
/* 306 */ XPathType type = (XPathType)types.get(i);
/* 307 */ XPathType otype = (XPathType)otypes.get(i);
/* 308 */ if ((!type.getExpression().equals(otype.getExpression())) || (type.getFilter() != otype.getFilter()))
/* */ {
/* 310 */ return false;
/* */ }
/* */ }
/* 313 */ return true;
/* */ }
/* */
/* */ private static boolean paramsEqual(ExcC14NParameterSpec spec1, ExcC14NParameterSpec spec2)
/* */ {
/* 318 */ return spec1.getPrefixList().equals(spec2.getPrefixList());
/* */ }
/* */
/* */ private static boolean paramsEqual(XPathFilterParameterSpec spec1, XPathFilterParameterSpec spec2)
/* */ {
/* 324 */ return spec1.getXPath().equals(spec2.getXPath());
/* */ }
/* */
/* */ private static boolean paramsEqual(XSLTTransformParameterSpec spec1, XSLTTransformParameterSpec spec2)
/* */ {
/* 330 */ XMLStructure ostylesheet = spec2.getStylesheet();
/* 331 */ if (!(ostylesheet instanceof DOMStructure)) {
/* 332 */ return false;
/* */ }
/* 334 */ Node ostylesheetElem = ((DOMStructure)ostylesheet).getNode();
/* */
/* 336 */ XMLStructure stylesheet = spec1.getStylesheet();
/* 337 */ Node stylesheetElem = ((DOMStructure)stylesheet).getNode();
/* */
/* 339 */ return nodesEqual(stylesheetElem, ostylesheetElem);
/* */ }
/* */
/* */ static class NodeSet extends AbstractSet
/* */ {
/* */ private NodeList nl;
/* */
/* */ public NodeSet(NodeList nl)
/* */ {
/* 176 */ this.nl = nl;
/* */ }
/* */ public int size() {
/* 179 */ return this.nl.getLength();
/* */ }
/* 181 */ public Iterator iterator() { return new Iterator() {
/* 182 */ int index = 0;
/* */
/* */ public void remove() {
/* 185 */ throw new UnsupportedOperationException();
/* */ }
/* */ public Object next() {
/* 188 */ if (!hasNext()) {
/* 189 */ throw new NoSuchElementException();
/* */ }
/* 191 */ return DOMUtils.NodeSet.this.nl.item(this.index++);
/* */ }
/* */ public boolean hasNext() {
/* 194 */ return this.index < DOMUtils.NodeSet.this.nl.getLength();
/* */ }
/* */ };
/* */ }
/* */ }
/* */ }
/* Location: E:\HYN\Java\trunk\ref\lib-dep\xmldsig\xmldsig.jar
* Qualified Name: org.jcp.xml.dsig.internal.dom.DOMUtils
* JD-Core Version: 0.6.2
*/