KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > loader > XMLUtil


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.loader;
6
7 import com.opensymphony.workflow.util.XMLizable;
8
9 import org.w3c.dom.Element JavaDoc;
10 import org.w3c.dom.Node JavaDoc;
11 import org.w3c.dom.NodeList JavaDoc;
12
13 import java.io.PrintWriter JavaDoc;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.List JavaDoc;
17
18
19 /**
20  *
21  *
22  * @author <a HREF="mailto:plightbo@.com">Pat Lightbody</a>
23  */

24 public class XMLUtil {
25     //~ Methods ////////////////////////////////////////////////////////////////
26

27     public static Element JavaDoc getChildElement(Element JavaDoc parent, String JavaDoc childName) {
28         NodeList JavaDoc children = parent.getChildNodes();
29         int size = children.getLength();
30
31         for (int i = 0; i < size; i++) {
32             Node JavaDoc node = children.item(i);
33
34             if (node.getNodeType() == Node.ELEMENT_NODE) {
35                 Element JavaDoc element = (Element JavaDoc) node;
36
37                 if (childName.equals(element.getNodeName())) {
38                     return element;
39                 }
40             }
41         }
42
43         return null;
44     }
45
46     public static List JavaDoc getChildElements(Element JavaDoc parent, String JavaDoc childName) {
47         NodeList JavaDoc children = parent.getChildNodes();
48         List JavaDoc list = new ArrayList JavaDoc();
49         int size = children.getLength();
50
51         for (int i = 0; i < size; i++) {
52             Node JavaDoc node = children.item(i);
53
54             if (node.getNodeType() == Node.ELEMENT_NODE) {
55                 Element JavaDoc element = (Element JavaDoc) node;
56
57                 if (childName.equals(element.getNodeName())) {
58                     list.add(element);
59                 }
60             }
61         }
62
63         return list;
64     }
65
66     public static String JavaDoc getChildText(Element JavaDoc parent, String JavaDoc childName) {
67         Element JavaDoc child = getChildElement(parent, childName);
68
69         if (child == null) {
70             return null;
71         }
72
73         return getText(child);
74     }
75
76     public static String JavaDoc getText(Element JavaDoc node) {
77         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
78         NodeList JavaDoc list = node.getChildNodes();
79
80         for (int i = 0; i < list.getLength(); i++) {
81             Node JavaDoc child = list.item(i);
82
83             switch (child.getNodeType()) {
84             case Node.CDATA_SECTION_NODE:
85             case Node.TEXT_NODE:
86                 sb.append(child.getNodeValue());
87             }
88         }
89
90         return sb.toString();
91     }
92
93     public static String JavaDoc encode(Object JavaDoc string) {
94         if (string == null) {
95             return "";
96         }
97
98         char[] chars = string.toString().toCharArray();
99         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
100
101         for (int i = 0; i < chars.length; i++) {
102             switch (chars[i]) {
103             case '&':
104                 out.append("&amp;");
105
106                 break;
107
108             case '<':
109                 out.append("&lt;");
110
111                 break;
112
113             case '>':
114                 out.append("&gt;");
115
116                 break;
117
118             case '\"':
119                 out.append("&quot;");
120
121                 break;
122
123             default:
124                 out.append(chars[i]);
125             }
126         }
127
128         return out.toString();
129     }
130
131     public static void printIndent(PrintWriter JavaDoc out, int indent) {
132         for (int i = 0; i < indent; i++) {
133             out.print(XMLizable.INDENT);
134         }
135     }
136 }
137
Popular Tags