1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/**
6 * Licensed to the Apache Software Foundation (ASF) under one
7 * or more contributor license agreements. See the NOTICE file
8 * distributed with this work for additional information
9 * regarding copyright ownership. The ASF licenses this file
10 * to you under the Apache License, Version 2.0 (the
11 * "License"); you may not use this file except in compliance
12 * with the License. You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing,
17 * software distributed under the License is distributed on an
18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
19 * KIND, either express or implied. See the License for the
20 * specific language governing permissions and limitations
21 * under the License.
22 */
23/*
24 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
25 */
26/*
27 * $Id: Utils.java 1197150 2011-11-03 14:34:57Z coheigea $
28 */
29package org.jcp.xml.dsig.internal.dom;
30
31import java.io.ByteArrayOutputStream;
32import java.io.InputStream;
33import java.io.IOException;
34import java.util.*;
35import javax.xml.crypto.XMLCryptoContext;
36import org.w3c.dom.NamedNodeMap;
37import org.w3c.dom.Node;
38
39/**
40 * Miscellaneous static utility methods for use in JSR 105 RI.
41 *
42 * @author Sean Mullan
43 */
44public final class Utils {
45
46    private Utils() {}
47
48    public static byte[] readBytesFromStream(InputStream is)
49        throws IOException
50    {
51        ByteArrayOutputStream baos = new ByteArrayOutputStream();
52        byte[] buf = new byte[1024];
53        while (true) {
54            int read = is.read(buf);
55            if (read == -1) { // EOF
56                break;
57            }
58            baos.write(buf, 0, read);
59            if (read < 1024) {
60                break;
61            }
62        }
63        return baos.toByteArray();
64    }
65
66    /**
67     * Converts an Iterator to a Set of Nodes, according to the XPath
68     * Data Model.
69     *
70     * @param i the Iterator
71     * @return the Set of Nodes
72     */
73    static Set<Node> toNodeSet(Iterator<?> i) {
74        Set<Node> nodeSet = new HashSet<Node>();
75        while (i.hasNext()) {
76            Node n = (Node)i.next();
77            nodeSet.add(n);
78            // insert attributes nodes to comply with XPath
79            if (n.getNodeType() == Node.ELEMENT_NODE) {
80                NamedNodeMap nnm = n.getAttributes();
81                for (int j = 0, length = nnm.getLength(); j < length; j++) {
82                    nodeSet.add(nnm.item(j));
83                }
84            }
85        }
86        return nodeSet;
87    }
88
89    /**
90     * Returns the ID from a same-document URI (ex: "#id")
91     */
92    public static String parseIdFromSameDocumentURI(String uri) {
93        if (uri.length() == 0) {
94            return null;
95        }
96        String id = uri.substring(1);
97        if (id != null && id.startsWith("xpointer(id(")) {
98            int i1 = id.indexOf('\'');
99            int i2 = id.indexOf('\'', i1+1);
100            id = id.substring(i1+1, i2);
101        }
102        return id;
103    }
104
105    /**
106     * Returns true if uri is a same-document URI, false otherwise.
107     */
108    public static boolean sameDocumentURI(String uri) {
109        return (uri != null && (uri.length() == 0 || uri.charAt(0) == '#'));
110    }
111
112    static boolean secureValidation(XMLCryptoContext xc) {
113        if (xc == null) {
114            return false;
115        }
116        return getBoolean(xc, "org.jcp.xml.dsig.secureValidation");
117    }
118
119    private static boolean getBoolean(XMLCryptoContext xc, String name) {
120        Boolean value = (Boolean)xc.getProperty(name);
121        return (value != null && value.booleanValue());
122    }
123}
124