src/java.xml.crypto/share/classes/com/sun/org/apache/xml/internal/security/encryption/AbstractSerializer.java
author redestad
Sat, 09 Dec 2017 03:33:39 +0100
changeset 49283 a14ede52a278
parent 47216 71c04702a3d5
permissions -rw-r--r--
8193128: Reduce number of implementation classes returned by List/Set/Map.of() 8191418: List.of().indexOf(null) doesn't throw NullPointerException Reviewed-by: smarks, jrose, martin, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
     1
/*
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
     2
 * reserved comment block
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
     4
 */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
     5
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
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:
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:
diff changeset
     8
 * distributed with this work for additional information
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
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:
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:
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:
diff changeset
    12
 * with the License. You may obtain a copy of the License at
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    13
 *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    14
 * http://www.apache.org/licenses/LICENSE-2.0
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    15
 *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
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:
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:
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:
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:
diff changeset
    20
 * specific language governing permissions and limitations
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    21
 * under the License.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    22
 */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    23
package com.sun.org.apache.xml.internal.security.encryption;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    24
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    25
import java.io.ByteArrayOutputStream;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    26
import java.io.IOException;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    27
import java.io.OutputStreamWriter;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    28
import java.io.UnsupportedEncodingException;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    29
import java.util.HashMap;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    30
import java.util.Map;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    31
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    32
import com.sun.org.apache.xml.internal.security.c14n.Canonicalizer;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    33
import org.w3c.dom.Element;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    34
import org.w3c.dom.NamedNodeMap;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    35
import org.w3c.dom.Node;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    36
import org.w3c.dom.NodeList;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    37
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    38
/**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    39
 * Converts <code>String</code>s into <code>Node</code>s and visa versa.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    40
 *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    41
 * An abstract class for common Serializer functionality
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    42
 */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    43
public abstract class AbstractSerializer implements Serializer {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    44
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    45
    protected Canonicalizer canon;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    46
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    47
    public void setCanonicalizer(Canonicalizer canon) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    48
        this.canon = canon;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    49
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    50
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    51
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    52
     * Returns a <code>String</code> representation of the specified
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    53
     * <code>Element</code>.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    54
     * <p/>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    55
     * Refer also to comments about setup of format.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    56
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    57
     * @param element the <code>Element</code> to serialize.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    58
     * @return the <code>String</code> representation of the serilaized
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    59
     *   <code>Element</code>.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    60
     * @throws Exception
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    61
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    62
    public String serialize(Element element) throws Exception {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    63
        return canonSerialize(element);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    64
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    65
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    66
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    67
     * Returns a <code>byte[]</code> representation of the specified
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    68
     * <code>Element</code>.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    69
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    70
     * @param element the <code>Element</code> to serialize.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    71
     * @return the <code>byte[]</code> representation of the serilaized
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    72
     *   <code>Element</code>.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    73
     * @throws Exception
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    74
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    75
    public byte[] serializeToByteArray(Element element) throws Exception {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    76
        return canonSerializeToByteArray(element);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    77
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    78
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    79
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    80
     * Returns a <code>String</code> representation of the specified
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    81
     * <code>NodeList</code>.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    82
     * <p/>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    83
     * This is a special case because the NodeList may represent a
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    84
     * <code>DocumentFragment</code>. A document fragment may be a
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    85
     * non-valid XML document (refer to appropriate description of
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    86
     * W3C) because it my start with a non-element node, e.g. a text
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    87
     * node.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    88
     * <p/>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    89
     * The methods first converts the node list into a document fragment.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    90
     * Special care is taken to not destroy the current document, thus
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    91
     * the method clones the nodes (deep cloning) before it appends
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    92
     * them to the document fragment.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    93
     * <p/>
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    94
     * Refer also to comments about setup of format.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    95
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    96
     * @param content the <code>NodeList</code> to serialize.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    97
     * @return the <code>String</code> representation of the serialized
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    98
     *   <code>NodeList</code>.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
    99
     * @throws Exception
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   100
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   101
    public String serialize(NodeList content) throws Exception {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   102
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   103
        canon.setWriter(baos);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   104
        canon.notReset();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   105
        for (int i = 0; i < content.getLength(); i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   106
            canon.canonicalizeSubtree(content.item(i));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   107
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   108
        String ret = baos.toString("UTF-8");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   109
        baos.reset();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   110
        return ret;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   111
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   112
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   113
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   114
     * Returns a <code>byte[]</code> representation of the specified
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   115
     * <code>NodeList</code>.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   116
     *
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   117
     * @param content the <code>NodeList</code> to serialize.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   118
     * @return the <code>byte[]</code> representation of the serialized
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   119
     *   <code>NodeList</code>.
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   120
     * @throws Exception
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   121
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   122
    public byte[] serializeToByteArray(NodeList content) throws Exception {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   123
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   124
        canon.setWriter(baos);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   125
        canon.notReset();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   126
        for (int i = 0; i < content.getLength(); i++) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   127
            canon.canonicalizeSubtree(content.item(i));
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   128
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   129
        return baos.toByteArray();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   130
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   131
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   132
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   133
     * Use the Canonicalizer to serialize the node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   134
     * @param node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   135
     * @return the canonicalization of the node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   136
     * @throws Exception
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   137
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   138
    public String canonSerialize(Node node) throws Exception {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   139
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   140
        canon.setWriter(baos);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   141
        canon.notReset();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   142
        canon.canonicalizeSubtree(node);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   143
        String ret = baos.toString("UTF-8");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   144
        baos.reset();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   145
        return ret;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   146
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   147
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   148
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   149
     * Use the Canonicalizer to serialize the node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   150
     * @param node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   151
     * @return the (byte[]) canonicalization of the node
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   152
     * @throws Exception
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   153
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   154
    public byte[] canonSerializeToByteArray(Node node) throws Exception {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   155
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   156
        canon.setWriter(baos);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   157
        canon.notReset();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   158
        canon.canonicalizeSubtree(node);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   159
        return baos.toByteArray();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   160
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   161
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   162
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   163
     * @param source
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   164
     * @param ctx
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   165
     * @return the Node resulting from the parse of the source
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   166
     * @throws XMLEncryptionException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   167
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   168
    public abstract Node deserialize(String source, Node ctx) throws XMLEncryptionException;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   169
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   170
    /**
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   171
     * @param source
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   172
     * @param ctx
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   173
     * @return the Node resulting from the parse of the source
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   174
     * @throws XMLEncryptionException
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   175
     */
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   176
    public abstract Node deserialize(byte[] source, Node ctx) throws XMLEncryptionException;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   177
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   178
    protected static byte[] createContext(byte[] source, Node ctx) throws XMLEncryptionException {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   179
        // Create the context to parse the document against
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   180
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   181
        try {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   182
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(byteArrayOutputStream, "UTF-8");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   183
            outputStreamWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><dummy");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   184
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   185
            // Run through each node up to the document node and find any xmlns: nodes
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   186
            Map<String, String> storedNamespaces = new HashMap<String, String>();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   187
            Node wk = ctx;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   188
            while (wk != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   189
                NamedNodeMap atts = wk.getAttributes();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   190
                if (atts != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   191
                    for (int i = 0; i < atts.getLength(); ++i) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   192
                        Node att = atts.item(i);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   193
                        String nodeName = att.getNodeName();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   194
                        if ((nodeName.equals("xmlns") || nodeName.startsWith("xmlns:"))
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   195
                                && !storedNamespaces.containsKey(att.getNodeName())) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   196
                            outputStreamWriter.write(" ");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   197
                            outputStreamWriter.write(nodeName);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   198
                            outputStreamWriter.write("=\"");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   199
                            outputStreamWriter.write(att.getNodeValue());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   200
                            outputStreamWriter.write("\"");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   201
                            storedNamespaces.put(nodeName, att.getNodeValue());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   202
                        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   203
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   204
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   205
                wk = wk.getParentNode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   206
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   207
            outputStreamWriter.write(">");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   208
            outputStreamWriter.flush();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   209
            byteArrayOutputStream.write(source);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   210
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   211
            outputStreamWriter.write("</dummy>");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   212
            outputStreamWriter.close();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   213
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   214
            return byteArrayOutputStream.toByteArray();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   215
        } catch (UnsupportedEncodingException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   216
            throw new XMLEncryptionException("empty", e);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   217
        } catch (IOException e) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   218
            throw new XMLEncryptionException("empty", e);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   219
        }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   220
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   221
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   222
    protected static String createContext(String source, Node ctx) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   223
        // Create the context to parse the document against
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   224
        StringBuilder sb = new StringBuilder();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   225
        sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><dummy");
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   226
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   227
        // Run through each node up to the document node and find any xmlns: nodes
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   228
        Map<String, String> storedNamespaces = new HashMap<String, String>();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   229
        Node wk = ctx;
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   230
        while (wk != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   231
            NamedNodeMap atts = wk.getAttributes();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   232
            if (atts != null) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   233
                for (int i = 0; i < atts.getLength(); ++i) {
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   234
                    Node att = atts.item(i);
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   235
                    String nodeName = att.getNodeName();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   236
                    if ((nodeName.equals("xmlns") || nodeName.startsWith("xmlns:"))
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   237
                        && !storedNamespaces.containsKey(att.getNodeName())) {
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   238
                        sb.append(' ').append(nodeName).append("=\"")
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   239
                                .append(att.getNodeValue()).append('"');
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   240
                        storedNamespaces.put(nodeName, att.getNodeValue());
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   241
                    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   242
                }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   243
            }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   244
            wk = wk.getParentNode();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   245
        }
27957
24b4e6082f19 8055723: Replace concat String to append in StringBuilder parameters (dev)
weijun
parents: 25859
diff changeset
   246
        sb.append('>').append(source).append("</dummy>");
18780
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   247
        return sb.toString();
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   248
    }
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   249
f47b920867e7 8011547: Update XML Signature implementation to Apache Santuario 1.5.4
mullan
parents:
diff changeset
   250
}