jdk/src/share/classes/com/sun/org/apache/xml/internal/security/c14n/implementations/Canonicalizer20010315Excl.java
author mullan
Mon, 26 Sep 2011 17:20:45 -0700
changeset 10694 cf59e2badd14
parent 1337 e8d6cef36199
child 18780 f47b920867e7
permissions -rw-r--r--
7088502: Security libraries don't build with javac -Werror Summary: Changes to files in src/share/classes/com/sun/org/apache/xml/internal/security and its subpackages to remove warnings Reviewed-by: mullan Contributed-by: kurchi.subhra.hazra@oracle.com
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * Copyright 1999-2004 The Apache Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * use this file except in compliance with the License. You may obtain a copy of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * the License at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * http://www.apache.org/licenses/LICENSE-2.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * Unless required by applicable law or agreed to in writing, software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * License for the specific language governing permissions and limitations under
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.xml.internal.security.c14n.implementations;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
import java.io.IOException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.SortedSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.TreeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    29
import javax.xml.parsers.ParserConfigurationException;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import com.sun.org.apache.xml.internal.security.c14n.helper.C14nHelper;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.org.apache.xml.internal.security.signature.XMLSignatureInput;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import com.sun.org.apache.xml.internal.security.transforms.params.InclusiveNamespaces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import com.sun.org.apache.xml.internal.security.utils.Constants;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    36
import com.sun.org.apache.xml.internal.security.utils.XMLUtils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import org.w3c.dom.Attr;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    38
import org.w3c.dom.Document;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import org.w3c.dom.Element;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import org.w3c.dom.NamedNodeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import org.w3c.dom.Node;
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    42
import org.xml.sax.SAXException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * Implements &quot; <A
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * HREF="http://www.w3.org/TR/2002/REC-xml-exc-c14n-20020718/">Exclusive XML
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Canonicalization, Version 1.0 </A>&quot; <BR />
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Credits: During restructuring of the Canonicalizer framework, Ren??
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Kollmorgen from Software AG submitted an implementation of ExclC14n which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * fitted into the old architecture and which based heavily on my old (and slow)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * implementation of "Canonical XML". A big "thank you" to Ren?? for this.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <BR />
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <i>THIS </i> implementation is a complete rewrite of the algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Christian Geuer-Pollmann <geuerp@apache.org>
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    55
 * @version $Revision: 1.5 $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see <a href="http://www.w3.org/TR/2002/REC-xml-exc-c14n-20020718/ Exclusive#">
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *          XML Canonicalization, Version 1.0</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public abstract class Canonicalizer20010315Excl extends CanonicalizerBase {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
      * This Set contains the names (Strings like "xmlns" or "xmlns:foo") of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
      * the inclusive namespaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
    64
    TreeSet<String> _inclusiveNSSet = new TreeSet<String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static final String XMLNS_URI=Constants.NamespaceSpecNS;
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
    66
    final SortedSet<Attr> result = new TreeSet<Attr>(COMPARE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
         * Constructor Canonicalizer20010315Excl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
         * @param includeComments
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        public Canonicalizer20010315Excl(boolean includeComments) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                super(includeComments);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         * Method engineCanonicalizeSubTree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
         * @inheritDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
         * @param rootNode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
         * @throws CanonicalizationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        public byte[] engineCanonicalizeSubTree(Node rootNode)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        throws CanonicalizationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                return this.engineCanonicalizeSubTree(rootNode, "",null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * Method engineCanonicalizeSubTree
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         *  @inheritDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         * @param rootNode
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         * @param inclusiveNamespaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
         * @throws CanonicalizationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        public byte[] engineCanonicalizeSubTree(Node rootNode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        String inclusiveNamespaces) throws CanonicalizationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                return this.engineCanonicalizeSubTree(rootNode, inclusiveNamespaces,null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         * Method engineCanonicalizeSubTree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
         * @param rootNode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param inclusiveNamespaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param excl A element to exclude from the c14n process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
         * @return the rootNode c14n.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
         * @throws CanonicalizationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public byte[] engineCanonicalizeSubTree(Node rootNode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                        String inclusiveNamespaces,Node excl) throws CanonicalizationException {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   109
                        this._inclusiveNSSet = getInclusiveNameSpace(inclusiveNamespaces);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        return super.engineCanonicalizeSubTree(rootNode,excl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         * @param rootNode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
         * @param inclusiveNamespaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * @return the rootNode c14n.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         * @throws CanonicalizationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
         */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   119
        @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public byte[] engineCanonicalize(XMLSignatureInput rootNode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                        String inclusiveNamespaces) throws CanonicalizationException {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   122
                        this._inclusiveNSSet = getInclusiveNameSpace(inclusiveNamespaces);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        return super.engineCanonicalize(rootNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
         * Method handleAttributesSubtree
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
         * @inheritDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
         * @param E
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
         * @throws CanonicalizationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
         */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   132
        Iterator<Attr> handleAttributesSubtree(Element E,NameSpaceSymbTable ns)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                        throws CanonicalizationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // System.out.println("During the traversal, I encountered " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                // XMLUtils.getXPath(E));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                // result will contain the attrs which have to be outputted
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   137
                SortedSet<Attr> result = this.result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            result.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                NamedNodeMap attrs=null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                int attrsLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (E.hasAttributes()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            attrs = E.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                attrsLength = attrs.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                //The prefix visibly utilized(in the attribute or in the name) in the element
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   147
                SortedSet<String> visiblyUtilized = getNSSetClone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                for (int i = 0; i < attrsLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                        Attr N = (Attr) attrs.item(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   152
                        if (XMLNS_URI!=N.getNamespaceURI()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                //Not a namespace definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                //The Element is output element, add his prefix(if used) to visibyUtilized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                String prefix = N.getPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                if ( (prefix != null) && (!prefix.equals(XML) && !prefix.equals(XMLNS)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                                visiblyUtilized.add(prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                                //Add to the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                 result.add(N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   163
                        String NName=N.getLocalName();
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   164
                        String NNodeValue=N.getNodeValue();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        if (ns.addMapping(NName, NNodeValue,N)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                //New definition check if it is relative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (C14nHelper.namespaceIsRelative(NNodeValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    Object exArgs[] = {E.getTagName(), NName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                            N.getNodeValue()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    throw new CanonicalizationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                            "c14n.Canonicalizer.RelativeNamespace", exArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   176
                String prefix;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (E.getNamespaceURI() != null) {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   178
                        prefix = E.getPrefix();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        if ((prefix == null) || (prefix.length() == 0)) {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   180
                                prefix=XMLNS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                        }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   182
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                } else {
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   184
                        prefix=XMLNS;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   186
                visiblyUtilized.add(prefix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                //This can be optimezed by I don't have time
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   189
                Iterator<String> it=visiblyUtilized.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                while (it.hasNext()) {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   191
                        String s=it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                        Attr key=ns.getMapping(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                        if (key==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                        result.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                return result.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
         * Method engineCanonicalizeXPathNodeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
         * @inheritDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
         * @param xpathNodeSet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
         * @param inclusiveNamespaces
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
         * @throws CanonicalizationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
         */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   209
        public byte[] engineCanonicalizeXPathNodeSet(Set<Node> xpathNodeSet,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                        String inclusiveNamespaces) throws CanonicalizationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   212
                        this._inclusiveNSSet = getInclusiveNameSpace(inclusiveNamespaces);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                        return super.engineCanonicalizeXPathNodeSet(xpathNodeSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   217
    @SuppressWarnings("unchecked")
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   218
    private TreeSet<String> getInclusiveNameSpace(String inclusiveNameSpaces) {
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   219
        return (TreeSet<String>)InclusiveNamespaces.prefixStr2Set(inclusiveNameSpaces);
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   220
    }
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   221
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   222
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   223
    @SuppressWarnings("unchecked")
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   224
    private SortedSet<String> getNSSetClone() {
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   225
        return (SortedSet<String>) this._inclusiveNSSet.clone();
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   226
    }
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   227
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   228
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * @inheritDoc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
         * @param E
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * @throws CanonicalizationException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         */
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   234
        final Iterator<Attr> handleAttributes(Element E, NameSpaceSymbTable ns)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                        throws CanonicalizationException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                // result will contain the attrs which have to be outputted
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   237
                SortedSet<Attr> result = this.result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            result.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                NamedNodeMap attrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                int attrsLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (E.hasAttributes()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            attrs = E.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                attrsLength = attrs.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                //The prefix visibly utilized(in the attribute or in the name) in the element
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   246
                Set<String> visiblyUtilized =null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                //It's the output selected.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   248
                boolean isOutputElement=isVisibleDO(E,ns.getLevel())==1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if (isOutputElement) {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   250
                        visiblyUtilized =  getNSSetClone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                for (int i = 0; i < attrsLength; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                        Attr N = (Attr) attrs.item(i);
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   255
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   257
                        if (XMLNS_URI!=N.getNamespaceURI()) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   258
                                if ( !isVisible(N) )  {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   259
                                        //The node is not in the nodeset(if there is a nodeset)
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   260
                                        continue;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   261
                                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                                //Not a namespace definition.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                                if (isOutputElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                                        //The Element is output element, add his prefix(if used) to visibyUtilized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                                        String prefix = N.getPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                                        if ((prefix != null) && (!prefix.equals(XML) && !prefix.equals(XMLNS)) ){
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                                                        visiblyUtilized.add(prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                                        //Add to the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                                    result.add(N);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                        }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   274
                        String NName=N.getLocalName();
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   275
                        if (isOutputElement && !isVisible(N) && NName!=XMLNS) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   276
                        ns.removeMappingIfNotRender(NName);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   277
                        continue;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   278
                }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   279
                        String NNodeValue=N.getNodeValue();
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   280
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   281
                        if (!isOutputElement && isVisible(N) && _inclusiveNSSet.contains(NName) && !ns.removeMappingIfRender(NName)) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   282
                                Node n=ns.addMappingAndRender(NName,NNodeValue,N);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   283
                                if (n!=null) {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   284
                                                result.add((Attr)n);
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   285
                            if (C14nHelper.namespaceIsRelative(N)) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   286
                               Object exArgs[] = { E.getTagName(), NName, N.getNodeValue() };
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   287
                               throw new CanonicalizationException(
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   288
                                  "c14n.Canonicalizer.RelativeNamespace", exArgs);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   289
                           }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   290
                                 }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   291
                        }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   292
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                        if (ns.addMapping(NName, NNodeValue,N)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                //New definiton check if it is relative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                if (C14nHelper.namespaceIsRelative(NNodeValue)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    Object exArgs[] = {E.getTagName(), NName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                            N.getNodeValue()};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    throw new CanonicalizationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                            "c14n.Canonicalizer.RelativeNamespace", exArgs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                if (isOutputElement) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
           //The element is visible, handle the xmlns definition
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
           Attr xmlns = E.getAttributeNodeNS(XMLNS_URI, XMLNS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
           if ((xmlns!=null) &&  (!isVisible(xmlns))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
              //There is a definition but the xmlns is not selected by the xpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
              //then xmlns=""
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
              ns.addMapping(XMLNS,"",nullNode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                        if (E.getNamespaceURI() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                String prefix = E.getPrefix();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                                if ((prefix == null) || (prefix.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                                        visiblyUtilized.add(XMLNS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                                        visiblyUtilized.add( prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                                visiblyUtilized.add(XMLNS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                        //This can be optimezed by I don't have time
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                        //visiblyUtilized.addAll(this._inclusiveNSSet);
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   327
                        Iterator<String> it=visiblyUtilized.iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                        while (it.hasNext()) {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 1337
diff changeset
   329
                                String s=it.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                Attr key=ns.getMapping(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                if (key==null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                                result.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                return result.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   340
        void circumventBugIfNeeded(XMLSignatureInput input) throws CanonicalizationException, ParserConfigurationException, IOException, SAXException {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   341
                if (!input.isNeedsToBeExpanded() || _inclusiveNSSet.isEmpty())
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   342
                        return;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   343
                Document doc = null;
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   344
               if (input.getSubNode() != null) {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   345
                   doc=XMLUtils.getOwnerDocument(input.getSubNode());
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   346
               } else {
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   347
                   doc=XMLUtils.getOwnerDocument(input.getNodeSet());
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   348
               }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   349
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   350
                XMLUtils.circumventBug2650(doc);
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
   351
           }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
}