2
|
1 |
/*
|
1337
|
2 |
* reserved comment block
|
|
3 |
* DO NOT REMOVE OR ALTER!
|
2
|
4 |
*/
|
|
5 |
/*
|
1337
|
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 |
/*
|
1639
|
22 |
* Copyright 2005-2008 Sun Microsystems, Inc. All rights reserved.
|
1337
|
23 |
*/
|
|
24 |
/*
|
|
25 |
* $Id: Utils.java,v 1.2 2008/07/24 15:20:32 mullan Exp $
|
2
|
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 org.w3c.dom.NamedNodeMap;
|
|
34 |
import org.w3c.dom.Node;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Miscellaneous static utility methods for use in JSR 105 RI.
|
|
38 |
*
|
|
39 |
* @author Sean Mullan
|
|
40 |
*/
|
|
41 |
public final class Utils {
|
|
42 |
|
|
43 |
private Utils() {}
|
|
44 |
|
|
45 |
public static byte[] readBytesFromStream(InputStream is)
|
|
46 |
throws IOException {
|
|
47 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
48 |
byte[] buf = new byte[1024];
|
|
49 |
while (true) {
|
|
50 |
int read = is.read(buf);
|
|
51 |
if (read == -1) { // EOF
|
|
52 |
break;
|
|
53 |
}
|
|
54 |
baos.write(buf, 0, read);
|
|
55 |
if (read < 1024) {
|
|
56 |
break;
|
|
57 |
}
|
|
58 |
}
|
|
59 |
return baos.toByteArray();
|
|
60 |
}
|
|
61 |
|
|
62 |
/**
|
|
63 |
* Converts an Iterator to a Set of Nodes, according to the XPath
|
|
64 |
* Data Model.
|
|
65 |
*
|
|
66 |
* @param i the Iterator
|
|
67 |
* @return the Set of Nodes
|
|
68 |
*/
|
|
69 |
static Set toNodeSet(Iterator i) {
|
|
70 |
Set nodeSet = new HashSet();
|
|
71 |
while (i.hasNext()) {
|
|
72 |
Node n = (Node) i.next();
|
|
73 |
nodeSet.add(n);
|
|
74 |
// insert attributes nodes to comply with XPath
|
|
75 |
if (n.getNodeType() == Node.ELEMENT_NODE) {
|
|
76 |
NamedNodeMap nnm = n.getAttributes();
|
|
77 |
for (int j = 0, length = nnm.getLength(); j < length; j++) {
|
|
78 |
nodeSet.add(nnm.item(j));
|
|
79 |
}
|
|
80 |
}
|
|
81 |
}
|
|
82 |
return nodeSet;
|
|
83 |
}
|
|
84 |
|
|
85 |
/**
|
|
86 |
* Returns the ID from a same-document URI (ex: "#id")
|
|
87 |
*/
|
|
88 |
public static String parseIdFromSameDocumentURI(String uri) {
|
|
89 |
if (uri.length() == 0) {
|
|
90 |
return null;
|
|
91 |
}
|
|
92 |
String id = uri.substring(1);
|
|
93 |
if (id != null && id.startsWith("xpointer(id(")) {
|
|
94 |
int i1 = id.indexOf('\'');
|
|
95 |
int i2 = id.indexOf('\'', i1+1);
|
|
96 |
id = id.substring(i1+1, i2);
|
|
97 |
}
|
|
98 |
return id;
|
|
99 |
}
|
|
100 |
|
|
101 |
/**
|
|
102 |
* Returns true if uri is a same-document URI, false otherwise.
|
|
103 |
*/
|
|
104 |
public static boolean sameDocumentURI(String uri) {
|
|
105 |
return (uri != null && (uri.length() == 0 || uri.charAt(0) == '#'));
|
|
106 |
}
|
|
107 |
}
|