jdk/src/share/classes/org/jcp/xml/dsig/internal/dom/Utils.java
author xdono
Mon, 15 Dec 2008 16:55:25 -0800
changeset 1639 a97859015238
parent 1337 e8d6cef36199
child 5506 202f599c92aa
permissions -rw-r--r--
6785258: Update copyright year Summary: Update copyright for files that have been modified starting July 2008 to Dec 2008 Reviewed-by: katleman, ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     2
 * reserved comment block
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     6
 * Copyright 2005 The Apache Software Foundation.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     7
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     8
 *  Licensed under the Apache License, Version 2.0 (the "License");
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
     9
 *  you may not use this file except in compliance with the License.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    10
 *  You may obtain a copy of the License at
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    11
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    13
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    14
 *  Unless required by applicable law or agreed to in writing, software
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    15
 *  distributed under the License is distributed on an "AS IS" BASIS,
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    16
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    17
 *  See the License for the specific language governing permissions and
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    18
 *  limitations under the License.
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    19
 *
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    20
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    21
/*
1639
a97859015238 6785258: Update copyright year
xdono
parents: 1337
diff changeset
    22
 * Copyright 2005-2008 Sun Microsystems, Inc. All rights reserved.
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
 */
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    24
/*
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    25
 * $Id: Utils.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package org.jcp.xml.dsig.internal.dom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import org.w3c.dom.NamedNodeMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import org.w3c.dom.Node;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Miscellaneous static utility methods for use in JSR 105 RI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Sean Mullan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public final class Utils {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private Utils() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public static byte[] readBytesFromStream(InputStream is)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        byte[] buf = new byte[1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        while (true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            int read = is.read(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            if (read == -1) { // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            baos.write(buf, 0, read);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            if (read < 1024) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Converts an Iterator to a Set of Nodes, according to the XPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Data Model.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param i the Iterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @return the Set of Nodes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    static Set toNodeSet(Iterator i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        Set nodeSet = new HashSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        while (i.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            Node n = (Node) i.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            nodeSet.add(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            // insert attributes nodes to comply with XPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (n.getNodeType() == Node.ELEMENT_NODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                NamedNodeMap nnm = n.getAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                for (int j = 0, length = nnm.getLength(); j < length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    nodeSet.add(nnm.item(j));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        return nodeSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Returns the ID from a same-document URI (ex: "#id")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public static String parseIdFromSameDocumentURI(String uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (uri.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        String id = uri.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (id != null && id.startsWith("xpointer(id(")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            int i1 = id.indexOf('\'');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            int i2 = id.indexOf('\'', i1+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            id = id.substring(i1+1, i2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        return id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Returns true if uri is a same-document URI, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public static boolean sameDocumentURI(String uri) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        return (uri != null && (uri.length() == 0 || uri.charAt(0) == '#'));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}