author | Stephen Talley <stephen.talley@oracle.com> |
Mon, 28 Mar 2011 10:53:34 -0400 | |
changeset 685 | 767674b0a2fb |
parent 219 | 57841c113efe |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
5 |
* Common Development and Distribution License (the "License"). |
|
6 |
* You may not use this file except in compliance with the License. |
|
7 |
* |
|
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
21 |
||
22 |
/* |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
23 |
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved. |
0 | 24 |
*/ |
25 |
||
219
57841c113efe
6788 package names should share o.o.o.vp prefix
Stephen Talley <stephen.talley@sun.com>
parents:
79
diff
changeset
|
26 |
package org.opensolaris.os.vp.util.misc; |
0 | 27 |
|
28 |
import java.util.NoSuchElementException; |
|
29 |
import org.w3c.dom.*; |
|
30 |
||
31 |
public class XMLUtil { |
|
32 |
public static String getTextContent(Element element) { |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
33 |
StringBuilder buffer = new StringBuilder(); |
0 | 34 |
|
35 |
element.normalize(); |
|
36 |
NodeList children = element.getChildNodes(); |
|
37 |
for (int i = 0, n = children.getLength(); i < n; i++) { |
|
38 |
Node child = children.item(i); |
|
39 |
if (child.getNodeType() == Node.TEXT_NODE) { |
|
40 |
buffer.append(child.getNodeValue()); |
|
41 |
} |
|
42 |
} |
|
43 |
||
44 |
return buffer.toString().trim(); |
|
45 |
} |
|
46 |
||
47 |
public static String getFullPath(Node node) { |
|
685
767674b0a2fb
18094 s/StringBuffer/StringBuilder/g
Stephen Talley <stephen.talley@oracle.com>
parents:
219
diff
changeset
|
48 |
StringBuilder buffer = new StringBuilder(); |
0 | 49 |
while (node != null) { |
50 |
buffer.insert(0, node.getNodeName()); |
|
51 |
char separator = '/'; |
|
52 |
if (node instanceof Attr) { |
|
53 |
separator = '@'; |
|
54 |
} |
|
55 |
buffer.insert(0, separator); |
|
56 |
node = node.getParentNode(); |
|
57 |
} |
|
58 |
return buffer.toString(); |
|
59 |
} |
|
60 |
||
61 |
public static String getAttribute(Element element, String name) { |
|
62 |
String value = element.getAttribute(name); |
|
79
d22c901edeff
3496 code cleanup/best practices/nits
Stephen Talley <stephen.talley@sun.com>
parents:
0
diff
changeset
|
63 |
if (value != null && !value.isEmpty()) { |
0 | 64 |
return value; |
65 |
} |
|
66 |
throw new NoSuchElementException(String.format( |
|
67 |
"Element <%s> has no \"%s\" attribute", element, name)); |
|
68 |
} |
|
69 |
||
70 |
public static boolean getBooleanAttribute(Element element, String name) { |
|
71 |
return Boolean.parseBoolean(getAttribute(element, name)); |
|
72 |
} |
|
73 |
||
74 |
public static int getIntegerAttribute(Element element, String name) { |
|
75 |
return Integer.parseInt(getAttribute(element, name)); |
|
76 |
} |
|
77 |
} |