src/share/classes/org/jcp/xml/dsig/internal/dom/Utils.java

Print this page


   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2005 The Apache Software Foundation.
   7  *
   8  *  Licensed under the Apache License, Version 2.0 (the "License");
   9  *  you may not use this file except in compliance with the License.
  10  *  You may obtain a copy of the License at


  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  *  Unless required by applicable law or agreed to in writing, software
  15  *  distributed under the License is distributed on an "AS IS" BASIS,
  16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  *  See the License for the specific language governing permissions and
  18  *  limitations under the License.
  19  *
  20  */
  21 /*
  22  * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
  23  */
  24 /*
  25  * $Id: Utils.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
  26  */
  27 package org.jcp.xml.dsig.internal.dom;
  28 
  29 import java.io.ByteArrayOutputStream;
  30 import java.io.InputStream;
  31 import java.io.IOException;
  32 import java.util.*;
  33 import javax.xml.crypto.XMLCryptoContext;
  34 import org.w3c.dom.NamedNodeMap;
  35 import org.w3c.dom.Node;
  36 
  37 /**
  38  * Miscellaneous static utility methods for use in JSR 105 RI.
  39  *
  40  * @author Sean Mullan
  41  */
  42 public final class Utils {
  43 
  44     private Utils() {}
  45 
  46     public static byte[] readBytesFromStream(InputStream is)
  47         throws IOException {

  48         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  49         byte[] buf = new byte[1024];
  50         while (true) {
  51             int read = is.read(buf);
  52             if (read == -1) { // EOF
  53                 break;
  54             }
  55             baos.write(buf, 0, read);
  56             if (read < 1024) {
  57                 break;
  58             }
  59         }
  60         return baos.toByteArray();
  61     }
  62 
  63     /**
  64      * Converts an Iterator to a Set of Nodes, according to the XPath
  65      * Data Model.
  66      *
  67      * @param i the Iterator
  68      * @return the Set of Nodes
  69      */
  70     static Set toNodeSet(Iterator i) {
  71         Set nodeSet = new HashSet();
  72         while (i.hasNext()) {
  73             Node n = (Node) i.next();
  74             nodeSet.add(n);
  75             // insert attributes nodes to comply with XPath
  76             if (n.getNodeType() == Node.ELEMENT_NODE) {
  77                 NamedNodeMap nnm = n.getAttributes();
  78                 for (int j = 0, length = nnm.getLength(); j < length; j++) {
  79                     nodeSet.add(nnm.item(j));
  80                 }
  81             }
  82         }
  83         return nodeSet;
  84     }
  85 
  86     /**
  87      * Returns the ID from a same-document URI (ex: "#id")
  88      */
  89     public static String parseIdFromSameDocumentURI(String uri) {
  90         if (uri.length() == 0) {
  91             return null;
  92         }
  93         String id = uri.substring(1);


   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, 2008, Oracle and/or its affiliates. All rights reserved.
  25  */
  26 /*
  27  * $Id: Utils.java 1197150 2011-11-03 14:34:57Z coheigea $
  28  */
  29 package org.jcp.xml.dsig.internal.dom;
  30 
  31 import java.io.ByteArrayOutputStream;
  32 import java.io.InputStream;
  33 import java.io.IOException;
  34 import java.util.*;
  35 import javax.xml.crypto.XMLCryptoContext;
  36 import org.w3c.dom.NamedNodeMap;
  37 import org.w3c.dom.Node;
  38 
  39 /**
  40  * Miscellaneous static utility methods for use in JSR 105 RI.
  41  *
  42  * @author Sean Mullan
  43  */
  44 public 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<Node> i) {
  74         Set<Node> nodeSet = new HashSet<Node>();
  75         while (i.hasNext()) {
  76             Node n = 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);