jdk/src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/utils/XMLUtils.java
author mullan
Wed, 11 Jun 2014 16:25:59 -0400
changeset 27081 1ceee8d3844d
parent 25859 3317bb8137f4
permissions -rw-r--r--
8038913: Bolster XML support Reviewed-by: xuelei, skoivu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
     6
 * Licensed to the Apache Software Foundation (ASF) under one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
     7
 * or more contributor license agreements. See the NOTICE file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
     9
 * regarding copyright ownership. The ASF licenses this file
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    10
 * to you under the Apache License, Version 2.0 (the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    11
 * "License"); you may not use this file except in compliance
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    12
 * with the License. You may obtain a copy of the License at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    16
 * Unless required by applicable law or agreed to in writing,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    17
 * software distributed under the License is distributed on an
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    18
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    19
 * KIND, either express or implied. See the License for the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    21
 * under the License.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
package com.sun.org.apache.xml.internal.security.utils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.OutputStream;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    27
import java.security.AccessController;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    28
import java.security.PrivilegedAction;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    29
import java.util.ArrayList;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.HashSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Iterator;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    32
import java.util.List;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import com.sun.org.apache.xml.internal.security.c14n.InvalidCanonicalizerException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import org.w3c.dom.Attr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import org.w3c.dom.Document;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import org.w3c.dom.NamedNodeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import org.w3c.dom.NodeList;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    44
import org.w3c.dom.ProcessingInstruction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import org.w3c.dom.Text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * DOM and XML accessibility and comfort functions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Christian Geuer-Pollmann
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class XMLUtils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    54
    private static boolean ignoreLineBreaks =
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    55
        AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    56
            public Boolean run() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    57
                return Boolean.valueOf(Boolean.getBoolean
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    58
                    ("com.sun.org.apache.xml.internal.security.ignoreLineBreaks"));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    59
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    60
        }).booleanValue();
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    61
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    62
    private static volatile String dsPrefix = "ds";
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    63
    private static volatile String ds11Prefix = "dsig11";
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    64
    private static volatile String xencPrefix = "xenc";
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    65
    private static volatile String xenc11Prefix = "xenc11";
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    66
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    67
    /** {@link org.apache.commons.logging} logging facility */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    68
    private static final java.util.logging.Logger log =
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    69
        java.util.logging.Logger.getLogger(XMLUtils.class.getName());
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    70
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    72
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    73
     * Constructor XMLUtils
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    74
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    75
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    76
    private XMLUtils() {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    77
        // we don't allow instantiation
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    78
    }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    79
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    80
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    81
     * Set the prefix for the digital signature namespace
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    82
     * @param prefix the new prefix for the digital signature namespace
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    83
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    84
     *    caller does not have permission to set the prefix
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    85
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    86
    public static void setDsPrefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    87
        JavaUtils.checkRegisterPermission();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    88
        dsPrefix = prefix;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    89
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    90
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
    91
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    92
     * Set the prefix for the digital signature 1.1 namespace
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    93
     * @param prefix the new prefix for the digital signature 1.1 namespace
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    94
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    95
     *    caller does not have permission to set the prefix
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    96
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    97
    public static void setDs11Prefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
    98
        JavaUtils.checkRegisterPermission();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
    99
        ds11Prefix = prefix;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   100
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   101
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   102
    /**
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   103
     * Set the prefix for the encryption namespace
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   104
     * @param prefix the new prefix for the encryption namespace
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   105
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   106
     *    caller does not have permission to set the prefix
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   107
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   108
    public static void setXencPrefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   109
        JavaUtils.checkRegisterPermission();
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   110
        xencPrefix = prefix;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   111
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   112
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   113
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   114
     * Set the prefix for the encryption namespace 1.1
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   115
     * @param prefix the new prefix for the encryption namespace 1.1
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   116
     * @throws SecurityException if a security manager is installed and the
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   117
     *    caller does not have permission to set the prefix
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   118
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   119
    public static void setXenc11Prefix(String prefix) {
27081
1ceee8d3844d 8038913: Bolster XML support
mullan
parents: 25859
diff changeset
   120
        JavaUtils.checkRegisterPermission();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   121
        xenc11Prefix = prefix;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   122
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   124
    public static Element getNextElement(Node el) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   125
        Node node = el;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   126
        while ((node != null) && (node.getNodeType() != Node.ELEMENT_NODE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   127
            node = node.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   128
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   129
        return (Element)node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   130
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   131
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   132
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   133
     * @param rootNode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   134
     * @param result
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   135
     * @param exclude
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   136
     * @param com whether comments or not
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   137
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   138
    public static void getSet(Node rootNode, Set<Node> result, Node exclude, boolean com) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   139
        if ((exclude != null) && isDescendantOrSelf(exclude, rootNode)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   140
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   141
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   142
        getSetRec(rootNode, result, exclude, com);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   143
    }
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   144
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   145
    @SuppressWarnings("fallthrough")
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   146
    private static void getSetRec(final Node rootNode, final Set<Node> result,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   147
                                final Node exclude, final boolean com) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   148
        if (rootNode == exclude) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   149
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   150
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   151
        switch (rootNode.getNodeType()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   152
        case Node.ELEMENT_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   153
            result.add(rootNode);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   154
            Element el = (Element)rootNode;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   155
            if (el.hasAttributes()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   156
                NamedNodeMap nl = el.getAttributes();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   157
                for (int i = 0;i < nl.getLength(); i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   158
                    result.add(nl.item(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   160
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   161
            //no return keep working
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   162
        case Node.DOCUMENT_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   163
            for (Node r = rootNode.getFirstChild(); r != null; r = r.getNextSibling()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   164
                if (r.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   165
                    result.add(r);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   166
                    while ((r != null) && (r.getNodeType() == Node.TEXT_NODE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   167
                        r = r.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   168
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   169
                    if (r == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   170
                        return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   171
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   172
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   173
                getSetRec(r, result, exclude, com);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   174
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   175
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   176
        case Node.COMMENT_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   177
            if (com) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   178
                result.add(rootNode);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   179
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   180
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   181
        case Node.DOCUMENT_TYPE_NODE:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   182
            return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   183
        default:
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   184
            result.add(rootNode);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   185
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   186
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   189
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   190
     * Outputs a DOM tree to an {@link OutputStream}.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   191
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   192
     * @param contextNode root node of the DOM tree
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   193
     * @param os the {@link OutputStream}
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   194
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   195
    public static void outputDOM(Node contextNode, OutputStream os) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   196
        XMLUtils.outputDOM(contextNode, os, false);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   197
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   199
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   200
     * Outputs a DOM tree to an {@link OutputStream}. <I>If an Exception is
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   201
     * thrown during execution, it's StackTrace is output to System.out, but the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   202
     * Exception is not re-thrown.</I>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   203
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   204
     * @param contextNode root node of the DOM tree
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   205
     * @param os the {@link OutputStream}
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   206
     * @param addPreamble
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   207
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   208
    public static void outputDOM(Node contextNode, OutputStream os, boolean addPreamble) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   209
        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   210
            if (addPreamble) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   211
                os.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n".getBytes("UTF-8"));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   212
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   214
            os.write(Canonicalizer.getInstance(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   215
                Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(contextNode)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   216
            );
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   217
        } catch (IOException ex) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   218
            if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   219
                log.log(java.util.logging.Level.FINE, ex.getMessage(), ex);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   220
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   221
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   222
        catch (InvalidCanonicalizerException ex) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   223
            if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   224
                log.log(java.util.logging.Level.FINE, ex.getMessage(), ex);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   225
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   226
        } catch (CanonicalizationException ex) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   227
            if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   228
                log.log(java.util.logging.Level.FINE, ex.getMessage(), ex);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   229
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   230
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   231
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   233
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   234
     * Serializes the <CODE>contextNode</CODE> into the OutputStream, <I>but
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   235
     * suppresses all Exceptions</I>.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   236
     * <BR />
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   237
     * NOTE: <I>This should only be used for debugging purposes,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   238
     * NOT in a production environment; this method ignores all exceptions,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   239
     * so you won't notice if something goes wrong. If you're asking what is to
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   240
     * be used in a production environment, simply use the code inside the
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   241
     * <code>try{}</code> statement, but handle the Exceptions appropriately.</I>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   242
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   243
     * @param contextNode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   244
     * @param os
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   245
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   246
    public static void outputDOMc14nWithComments(Node contextNode, OutputStream os) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   247
        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   248
            os.write(Canonicalizer.getInstance(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   249
                Canonicalizer.ALGO_ID_C14N_WITH_COMMENTS).canonicalizeSubtree(contextNode)
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   250
            );
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   251
        } catch (IOException ex) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   252
            if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   253
                log.log(java.util.logging.Level.FINE, ex.getMessage(), ex);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   254
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   255
            // throw new RuntimeException(ex.getMessage());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   256
        } catch (InvalidCanonicalizerException ex) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   257
            if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   258
                log.log(java.util.logging.Level.FINE, ex.getMessage(), ex);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   259
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   260
            // throw new RuntimeException(ex.getMessage());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   261
        } catch (CanonicalizationException ex) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   262
            if (log.isLoggable(java.util.logging.Level.FINE)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   263
                log.log(java.util.logging.Level.FINE, ex.getMessage(), ex);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   264
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   265
            // throw new RuntimeException(ex.getMessage());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   266
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   267
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   269
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   270
     * Method getFullTextChildrenFromElement
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   271
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   272
     * @param element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   273
     * @return the string of children
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   274
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   275
    public static String getFullTextChildrenFromElement(Element element) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   276
        StringBuilder sb = new StringBuilder();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   278
        Node child = element.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   279
        while (child != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   280
            if (child.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   281
                sb.append(((Text)child).getData());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   282
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   283
            child = child.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   284
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   286
        return sb.toString();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   287
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   289
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   290
     * Creates an Element in the XML Signature specification namespace.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   291
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   292
     * @param doc the factory Document
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   293
     * @param elementName the local name of the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   294
     * @return the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   295
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   296
    public static Element createElementInSignatureSpace(Document doc, String elementName) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   297
        if (doc == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   298
            throw new RuntimeException("Document is null");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   299
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   301
        if ((dsPrefix == null) || (dsPrefix.length() == 0)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   302
            return doc.createElementNS(Constants.SignatureSpecNS, elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   303
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   304
        return doc.createElementNS(Constants.SignatureSpecNS, dsPrefix + ":" + elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   305
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   307
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   308
     * Creates an Element in the XML Signature 1.1 specification namespace.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   309
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   310
     * @param doc the factory Document
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   311
     * @param elementName the local name of the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   312
     * @return the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   313
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   314
    public static Element createElementInSignature11Space(Document doc, String elementName) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   315
        if (doc == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   316
            throw new RuntimeException("Document is null");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   317
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   319
        if ((ds11Prefix == null) || (ds11Prefix.length() == 0)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   320
            return doc.createElementNS(Constants.SignatureSpec11NS, elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   321
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   322
        return doc.createElementNS(Constants.SignatureSpec11NS, ds11Prefix + ":" + elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   323
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   325
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   326
     * Creates an Element in the XML Encryption specification namespace.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   327
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   328
     * @param doc the factory Document
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   329
     * @param elementName the local name of the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   330
     * @return the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   331
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   332
    public static Element createElementInEncryptionSpace(Document doc, String elementName) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   333
        if (doc == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   334
            throw new RuntimeException("Document is null");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   335
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   337
        if ((xencPrefix == null) || (xencPrefix.length() == 0)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   338
            return doc.createElementNS(EncryptionConstants.EncryptionSpecNS, elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   339
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   340
        return
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   341
            doc.createElementNS(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   342
                EncryptionConstants.EncryptionSpecNS, xencPrefix + ":" + elementName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   343
            );
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   344
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   346
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   347
     * Creates an Element in the XML Encryption 1.1 specification namespace.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   348
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   349
     * @param doc the factory Document
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   350
     * @param elementName the local name of the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   351
     * @return the Element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   352
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   353
    public static Element createElementInEncryption11Space(Document doc, String elementName) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   354
        if (doc == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   355
            throw new RuntimeException("Document is null");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   356
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   357
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   358
        if ((xenc11Prefix == null) || (xenc11Prefix.length() == 0)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   359
            return doc.createElementNS(EncryptionConstants.EncryptionSpec11NS, elementName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   360
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   361
        return
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   362
            doc.createElementNS(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   363
                EncryptionConstants.EncryptionSpec11NS, xenc11Prefix + ":" + elementName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   364
            );
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   365
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   367
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   368
     * Returns true if the element is in XML Signature namespace and the local
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   369
     * name equals the supplied one.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   370
     *
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   371
     * @param element
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   372
     * @param localName
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   373
     * @return true if the element is in XML Signature namespace and the local name equals
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   374
     * the supplied one
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   375
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   376
    public static boolean elementIsInSignatureSpace(Element element, String localName) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   377
        if (element == null){
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   378
            return false;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   379
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   380
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   381
        return Constants.SignatureSpecNS.equals(element.getNamespaceURI())
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   382
            && element.getLocalName().equals(localName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   383
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   384
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   385
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   386
     * Returns true if the element is in XML Signature 1.1 namespace and the local
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   387
     * name equals the supplied one.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   388
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   389
     * @param element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   390
     * @param localName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   391
     * @return true if the element is in XML Signature namespace and the local name equals
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   392
     * the supplied one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   393
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   394
    public static boolean elementIsInSignature11Space(Element element, String localName) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   395
        if (element == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   396
            return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   397
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   399
        return Constants.SignatureSpec11NS.equals(element.getNamespaceURI())
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   400
            && element.getLocalName().equals(localName);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   401
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   402
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   403
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   404
     * Returns true if the element is in XML Encryption namespace and the local
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   405
     * name equals the supplied one.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   406
     *
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   407
     * @param element
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   408
     * @param localName
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   409
     * @return true if the element is in XML Encryption namespace and the local name
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   410
     * equals the supplied one
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   411
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   412
    public static boolean elementIsInEncryptionSpace(Element element, String localName) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   413
        if (element == null){
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   414
            return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   415
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   416
        return EncryptionConstants.EncryptionSpecNS.equals(element.getNamespaceURI())
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   417
            && element.getLocalName().equals(localName);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   418
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   420
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   421
     * Returns true if the element is in XML Encryption 1.1 namespace and the local
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   422
     * name equals the supplied one.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   423
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   424
     * @param element
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   425
     * @param localName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   426
     * @return true if the element is in XML Encryption 1.1 namespace and the local name
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   427
     * equals the supplied one
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   428
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   429
    public static boolean elementIsInEncryption11Space(Element element, String localName) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   430
        if (element == null){
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   431
            return false;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   432
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   433
        return EncryptionConstants.EncryptionSpec11NS.equals(element.getNamespaceURI())
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   434
            && element.getLocalName().equals(localName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   435
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   437
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   438
     * This method returns the owner document of a particular node.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   439
     * This method is necessary because it <I>always</I> returns a
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   440
     * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   441
     * if the {@link Node} is a {@link Document}.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   442
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   443
     * @param node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   444
     * @return the owner document of the node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   445
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   446
    public static Document getOwnerDocument(Node node) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   447
        if (node.getNodeType() == Node.DOCUMENT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   448
            return (Document) node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   449
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   450
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
            return node.getOwnerDocument();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   452
        } catch (NullPointerException npe) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                                           + " Original message was \""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                                           + npe.getMessage() + "\"");
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   456
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   457
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   460
     * This method returns the first non-null owner document of the Nodes in this Set.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * This method is necessary because it <I>always</I> returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * {@link Document}. {@link Node#getOwnerDocument} returns <CODE>null</CODE>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * if the {@link Node} is a {@link Document}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param xpathNodeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @return the owner document
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   468
    public static Document getOwnerDocument(Set<Node> xpathNodeSet) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   469
        NullPointerException npe = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   470
        for (Node node : xpathNodeSet) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   471
            int nodeType = node.getNodeType();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   472
            if (nodeType == Node.DOCUMENT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   473
                return (Document) node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   474
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   475
            try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   476
                if (nodeType == Node.ATTRIBUTE_NODE) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    return ((Attr)node).getOwnerElement().getOwnerDocument();
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   478
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   479
                return node.getOwnerDocument();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   480
            } catch (NullPointerException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   481
                npe = e;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   482
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   483
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   485
        throw new NullPointerException(I18n.translate("endorsed.jdk1.4.0")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                                       + " Original message was \""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                                       + (npe == null ? "" : npe.getMessage()) + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   490
    /**
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   491
     * Method createDSctx
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   492
     *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   493
     * @param doc
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   494
     * @param prefix
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   495
     * @param namespace
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   496
     * @return the element.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   497
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   498
    public static Element createDSctx(Document doc, String prefix, String namespace) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   499
        if ((prefix == null) || (prefix.trim().length() == 0)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   500
            throw new IllegalArgumentException("You must supply a prefix");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   501
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   502
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   503
        Element ctx = doc.createElementNS(null, "namespaceContext");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   504
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   505
        ctx.setAttributeNS(Constants.NamespaceSpecNS, "xmlns:" + prefix.trim(), namespace);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   506
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   507
        return ctx;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   508
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   510
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   511
     * Method addReturnToElement
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   512
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   513
     * @param e
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   514
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   515
    public static void addReturnToElement(Element e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   516
        if (!ignoreLineBreaks) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   517
            Document doc = e.getOwnerDocument();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   518
            e.appendChild(doc.createTextNode("\n"));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   519
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   520
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   522
    public static void addReturnToElement(Document doc, HelperNodeList nl) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   523
        if (!ignoreLineBreaks) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   524
            nl.appendChild(doc.createTextNode("\n"));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   525
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   526
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   527
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   528
    public static void addReturnBeforeChild(Element e, Node child) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   529
        if (!ignoreLineBreaks) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   530
            Document doc = e.getOwnerDocument();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   531
            e.insertBefore(doc.createTextNode("\n"), child);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   532
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   533
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   535
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   536
     * Method convertNodelistToSet
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   537
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   538
     * @param xpathNodeSet
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   539
     * @return the set with the nodelist
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   540
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   541
    public static Set<Node> convertNodelistToSet(NodeList xpathNodeSet) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   542
        if (xpathNodeSet == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   543
            return new HashSet<Node>();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   544
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   546
        int length = xpathNodeSet.getLength();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   547
        Set<Node> set = new HashSet<Node>(length);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   548
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   549
        for (int i = 0; i < length; i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   550
            set.add(xpathNodeSet.item(i));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   551
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   552
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   553
        return set;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   554
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   556
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   557
     * This method spreads all namespace attributes in a DOM document to their
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   558
     * children. This is needed because the XML Signature XPath transform
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   559
     * must evaluate the XPath against all nodes in the input, even against
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   560
     * XPath namespace nodes. Through a bug in XalanJ2, the namespace nodes are
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   561
     * not fully visible in the Xalan XPath model, so we have to do this by
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   562
     * hand in DOM spaces so that the nodes become visible in XPath space.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   563
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   564
     * @param doc
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   565
     * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   566
     * Namespace axis resolution is not XPath compliant </A>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   567
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   568
    public static void circumventBug2650(Document doc) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   570
        Element documentElement = doc.getDocumentElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   572
        // if the document element has no xmlns definition, we add xmlns=""
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   573
        Attr xmlnsAttr =
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   574
            documentElement.getAttributeNodeNS(Constants.NamespaceSpecNS, "xmlns");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   576
        if (xmlnsAttr == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   577
            documentElement.setAttributeNS(Constants.NamespaceSpecNS, "xmlns", "");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   578
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   580
        XMLUtils.circumventBug2650internal(doc);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   581
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   583
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   584
     * This is the work horse for {@link #circumventBug2650}.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   585
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   586
     * @param node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   587
     * @see <A HREF="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=2650">
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   588
     * Namespace axis resolution is not XPath compliant </A>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   589
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   590
    @SuppressWarnings("fallthrough")
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   591
    private static void circumventBug2650internal(Node node) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   592
        Node parent = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   593
        Node sibling = null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   594
        final String namespaceNs = Constants.NamespaceSpecNS;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   595
        do {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   596
            switch (node.getNodeType()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   597
            case Node.ELEMENT_NODE :
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   598
                Element element = (Element) node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   599
                if (!element.hasChildNodes()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   600
                    break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   602
                if (element.hasAttributes()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   603
                    NamedNodeMap attributes = element.getAttributes();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   604
                    int attributesLength = attributes.getLength();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   605
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   606
                    for (Node child = element.getFirstChild(); child!=null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   607
                        child = child.getNextSibling()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   609
                        if (child.getNodeType() != Node.ELEMENT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   610
                            continue;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   611
                        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   612
                        Element childElement = (Element) child;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   613
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   614
                        for (int i = 0; i < attributesLength; i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   615
                            Attr currentAttr = (Attr) attributes.item(i);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   616
                            if (!namespaceNs.equals(currentAttr.getNamespaceURI())) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                                continue;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   618
                            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   619
                            if (childElement.hasAttributeNS(namespaceNs,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   620
                                                            currentAttr.getLocalName())) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   621
                                continue;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   622
                            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   623
                            childElement.setAttributeNS(namespaceNs,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   624
                                                        currentAttr.getName(),
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   625
                                                        currentAttr.getNodeValue());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   627
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   629
            case Node.ENTITY_REFERENCE_NODE :
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   630
            case Node.DOCUMENT_NODE :
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   631
                parent = node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   632
                sibling = node.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   633
                break;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   634
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   635
            while ((sibling == null) && (parent != null)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   636
                sibling = parent.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   637
                parent = parent.getParentNode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   638
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   639
            if (sibling == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   640
                return;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   641
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   643
            node = sibling;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   644
            sibling = node.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   645
        } while (true);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   646
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   648
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   649
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   650
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   651
     * @param number
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   652
     * @return nodes with the constraint
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   653
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   654
    public static Element selectDsNode(Node sibling, String nodeName, int number) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   655
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   656
            if (Constants.SignatureSpecNS.equals(sibling.getNamespaceURI())
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   657
                && sibling.getLocalName().equals(nodeName)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   658
                if (number == 0){
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   659
                    return (Element)sibling;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   661
                number--;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   662
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   663
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
        return null;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   666
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   668
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   669
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   670
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   671
     * @param number
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   672
     * @return nodes with the constraint
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   673
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   674
    public static Element selectDs11Node(Node sibling, String nodeName, int number) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   675
        while (sibling != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   676
            if (Constants.SignatureSpec11NS.equals(sibling.getNamespaceURI())
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   677
                && sibling.getLocalName().equals(nodeName)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   678
                if (number == 0){
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   679
                    return (Element)sibling;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   680
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   681
                number--;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   682
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   683
            sibling = sibling.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   684
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   685
        return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   686
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   687
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   688
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   689
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   690
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   691
     * @param number
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   692
     * @return nodes with the constrain
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   693
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   694
    public static Element selectXencNode(Node sibling, String nodeName, int number) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   695
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   696
            if (EncryptionConstants.EncryptionSpecNS.equals(sibling.getNamespaceURI())
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   697
                && sibling.getLocalName().equals(nodeName)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   698
                if (number == 0){
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   699
                    return (Element)sibling;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   701
                number--;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   702
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   703
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        return null;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   706
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   708
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   709
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   710
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   711
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   712
     * @param number
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   713
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   714
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   715
    public static Text selectDsNodeText(Node sibling, String nodeName, int number) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   716
        Node n = selectDsNode(sibling,nodeName,number);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   717
        if (n == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   718
            return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   720
        n = n.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   721
        while (n != null && n.getNodeType() != Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   722
            n = n.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        return (Text)n;
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   725
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   727
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   728
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   729
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   730
     * @param number
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   731
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   732
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   733
    public static Text selectDs11NodeText(Node sibling, String nodeName, int number) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   734
        Node n = selectDs11Node(sibling,nodeName,number);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   735
        if (n == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   736
            return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   737
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   738
        n = n.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   739
        while (n != null && n.getNodeType() != Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   740
            n = n.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   741
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   742
        return (Text)n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   744
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   745
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   746
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   747
     * @param uri
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   748
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   749
     * @param number
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   750
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   751
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   752
    public static Text selectNodeText(Node sibling, String uri, String nodeName, int number) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   753
        Node n = selectNode(sibling,uri,nodeName,number);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   754
        if (n == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   755
            return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   756
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   757
        n = n.getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   758
        while (n != null && n.getNodeType() != Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   759
            n = n.getNextSibling();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   760
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   761
        return (Text)n;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   764
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   765
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   766
     * @param uri
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   767
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   768
     * @param number
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   769
     * @return nodes with the constrain
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   770
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   771
    public static Element selectNode(Node sibling, String uri, String nodeName, int number) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   772
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   773
            if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   774
                && sibling.getLocalName().equals(nodeName)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   775
                if (number == 0){
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   776
                    return (Element)sibling;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   778
                number--;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   779
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   780
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        return null;
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   783
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   784
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   785
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   786
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   787
     * @param nodeName
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   788
     * @return nodes with the constrain
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   789
     */
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   790
    public static Element[] selectDsNodes(Node sibling, String nodeName) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   791
        return selectNodes(sibling, Constants.SignatureSpecNS, nodeName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   792
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   793
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   794
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   795
     * @param sibling
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   796
     * @param nodeName
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   797
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   798
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   799
    public static Element[] selectDs11Nodes(Node sibling, String nodeName) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   800
        return selectNodes(sibling, Constants.SignatureSpec11NS, nodeName);
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   801
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   803
    /**
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   804
     * @param sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   805
     * @param uri
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   806
     * @param nodeName
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   807
     * @return nodes with the constraint
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   808
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   809
    public static Element[] selectNodes(Node sibling, String uri, String nodeName) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   810
        List<Element> list = new ArrayList<Element>();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   811
        while (sibling != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   812
            if (sibling.getNamespaceURI() != null && sibling.getNamespaceURI().equals(uri)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   813
                && sibling.getLocalName().equals(nodeName)) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   814
                list.add((Element)sibling);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   815
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   816
            sibling = sibling.getNextSibling();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   818
        return list.toArray(new Element[list.size()]);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   819
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   821
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   822
     * @param signatureElement
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   823
     * @param inputSet
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   824
     * @return nodes with the constrain
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   825
     */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   826
    public static Set<Node> excludeNodeFromSet(Node signatureElement, Set<Node> inputSet) {
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   827
        Set<Node> resultSet = new HashSet<Node>();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   828
        Iterator<Node> iterator = inputSet.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   830
        while (iterator.hasNext()) {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   831
            Node inputNode = iterator.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   833
            if (!XMLUtils.isDescendantOrSelf(signatureElement, inputNode)) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   834
                resultSet.add(inputNode);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            }
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   836
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   837
        return resultSet;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   838
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   840
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   841
     * Method getStrFromNode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   842
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   843
     * @param xpathnode
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   844
     * @return the string for the node.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   845
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   846
    public static String getStrFromNode(Node xpathnode) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   847
        if (xpathnode.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   848
            // we iterate over all siblings of the context node because eventually,
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   849
            // the text is "polluted" with pi's or comments
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   850
            StringBuilder sb = new StringBuilder();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   851
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   852
            for (Node currentSibling = xpathnode.getParentNode().getFirstChild();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   853
                currentSibling != null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   854
                currentSibling = currentSibling.getNextSibling()) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   855
                if (currentSibling.getNodeType() == Node.TEXT_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   856
                    sb.append(((Text) currentSibling).getData());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   857
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   858
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   859
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   860
            return sb.toString();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   861
        } else if (xpathnode.getNodeType() == Node.ATTRIBUTE_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   862
            return ((Attr) xpathnode).getNodeValue();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   863
        } else if (xpathnode.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   864
            return ((ProcessingInstruction) xpathnode).getNodeValue();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   865
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   867
        return null;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   868
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   870
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   871
     * Returns true if the descendantOrSelf is on the descendant-or-self axis
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   872
     * of the context node.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   873
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   874
     * @param ctx
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   875
     * @param descendantOrSelf
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   876
     * @return true if the node is descendant
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   877
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   878
    public static boolean isDescendantOrSelf(Node ctx, Node descendantOrSelf) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   879
        if (ctx == descendantOrSelf) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   880
            return true;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   881
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   883
        Node parent = descendantOrSelf;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   884
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   885
        while (true) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   886
            if (parent == null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   887
                return false;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   888
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   890
            if (parent == ctx) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   891
                return true;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   892
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   893
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   894
            if (parent.getNodeType() == Node.ATTRIBUTE_NODE) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   895
                parent = ((Attr) parent).getOwnerElement();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   896
            } else {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   897
                parent = parent.getParentNode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   898
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   899
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   900
    }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   901
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   902
    public static boolean ignoreLineBreaks() {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   903
        return ignoreLineBreaks;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   904
    }
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   905
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   906
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   907
     * Returns the attribute value for the attribute with the specified name.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   908
     * Returns null if there is no such attribute, or
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   909
     * the empty string if the attribute value is empty.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   910
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   911
     * <p>This works around a limitation of the DOM
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   912
     * <code>Element.getAttributeNode</code> method, which does not distinguish
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   913
     * between an unspecified attribute and an attribute with a value of
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   914
     * "" (it returns "" for both cases).
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   915
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   916
     * @param elem the element containing the attribute
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   917
     * @param name the name of the attribute
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   918
     * @return the attribute value (may be null if unspecified)
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   919
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   920
    public static String getAttributeValue(Element elem, String name) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   921
        Attr attr = elem.getAttributeNodeNS(null, name);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   922
        return (attr == null) ? null : attr.getValue();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   923
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   924
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   925
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   926
     * This method is a tree-search to help prevent against wrapping attacks. It checks that no
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   927
     * two Elements have ID Attributes that match the "value" argument, if this is the case then
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   928
     * "false" is returned. Note that a return value of "true" does not necessarily mean that
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   929
     * a matching Element has been found, just that no wrapping attack has been detected.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   930
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   931
    public static boolean protectAgainstWrappingAttack(Node startNode, String value) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   932
        Node startParent = startNode.getParentNode();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   933
        Node processedNode = null;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   934
        Element foundElement = null;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   935
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   936
        String id = value.trim();
23930
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18780
diff changeset
   937
        if (!id.isEmpty() && id.charAt(0) == '#') {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   938
            id = id.substring(1);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   939
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   940
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   941
        while (startNode != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   942
            if (startNode.getNodeType() == Node.ELEMENT_NODE) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   943
                Element se = (Element) startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   944
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   945
                NamedNodeMap attributes = se.getAttributes();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   946
                if (attributes != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   947
                    for (int i = 0; i < attributes.getLength(); i++) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   948
                        Attr attr = (Attr)attributes.item(i);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   949
                        if (attr.isId() && id.equals(attr.getValue())) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   950
                            if (foundElement == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   951
                                // Continue searching to find duplicates
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   952
                                foundElement = attr.getOwnerElement();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   953
                            } else {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   954
                                log.log(java.util.logging.Level.FINE, "Multiple elements with the same 'Id' attribute value!");
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   955
                                return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   956
                            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   957
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   958
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   959
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   960
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   961
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   962
            processedNode = startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   963
            startNode = startNode.getFirstChild();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   964
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   965
            // no child, this node is done.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   966
            if (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   967
                // close node processing, get sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   968
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   969
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   970
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   971
            // no more siblings, get parent, all children
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   972
            // of parent are processed.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   973
            while (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   974
                processedNode = processedNode.getParentNode();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   975
                if (processedNode == startParent) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   976
                    return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   977
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   978
                // close parent node processing (processed node now)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   979
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   980
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   981
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   982
        return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   983
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   984
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   985
    /**
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   986
     * This method is a tree-search to help prevent against wrapping attacks. It checks that no other
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   987
     * Element than the given "knownElement" argument has an ID attribute that matches the "value"
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   988
     * argument, which is the ID value of "knownElement". If this is the case then "false" is returned.
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   989
     */
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   990
    public static boolean protectAgainstWrappingAttack(
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   991
        Node startNode, Element knownElement, String value
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
   992
    ) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   993
        Node startParent = startNode.getParentNode();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   994
        Node processedNode = null;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   995
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   996
        String id = value.trim();
23930
0e5983df5018 8038184: XMLSignature throws StringIndexOutOfBoundsException if ID attribute value is empty String
mullan
parents: 18780
diff changeset
   997
        if (!id.isEmpty() && id.charAt(0) == '#') {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   998
            id = id.substring(1);
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
   999
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1000
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1001
        while (startNode != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1002
            if (startNode.getNodeType() == Node.ELEMENT_NODE) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1003
                Element se = (Element) startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1004
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1005
                NamedNodeMap attributes = se.getAttributes();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1006
                if (attributes != null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1007
                    for (int i = 0; i < attributes.getLength(); i++) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1008
                        Attr attr = (Attr)attributes.item(i);
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents: 18240
diff changeset
  1009
                        if (attr.isId() && id.equals(attr.getValue()) && se != knownElement) {
18240
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1010
                            log.log(java.util.logging.Level.FINE, "Multiple elements with the same 'Id' attribute value!");
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1011
                            return false;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1012
                        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1013
                    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1014
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1015
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1016
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1017
            processedNode = startNode;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1018
            startNode = startNode.getFirstChild();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1019
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1020
            // no child, this node is done.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1021
            if (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1022
                // close node processing, get sibling
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1023
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1024
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1025
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1026
            // no more siblings, get parent, all children
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1027
            // of parent are processed.
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1028
            while (startNode == null) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1029
                processedNode = processedNode.getParentNode();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1030
                if (processedNode == startParent) {
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1031
                    return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1032
                }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1033
                // close parent node processing (processed node now)
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1034
                startNode = processedNode.getNextSibling();
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1035
            }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1036
        }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1037
        return true;
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1038
    }
cda839ac048f 6741606: Integrate Apache Santuario
mullan
parents: 10694
diff changeset
  1039
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
}