001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.loader;
006:
007: import com.opensymphony.workflow.util.XMLizable;
008:
009: import org.w3c.dom.Element;
010: import org.w3c.dom.Node;
011: import org.w3c.dom.NodeList;
012:
013: import java.io.PrintWriter;
014:
015: import java.util.ArrayList;
016: import java.util.List;
017:
018: /**
019: *
020: *
021: * @author <a href="mailto:plightbo@.com">Pat Lightbody</a>
022: */
023: public class XMLUtil {
024: //~ Methods ////////////////////////////////////////////////////////////////
025:
026: public static Element getChildElement(Element parent,
027: String childName) {
028: NodeList children = parent.getChildNodes();
029: int size = children.getLength();
030:
031: for (int i = 0; i < size; i++) {
032: Node node = children.item(i);
033:
034: if (node.getNodeType() == Node.ELEMENT_NODE) {
035: Element element = (Element) node;
036:
037: if (childName.equals(element.getNodeName())) {
038: return element;
039: }
040: }
041: }
042:
043: return null;
044: }
045:
046: public static List getChildElements(Element parent, String childName) {
047: NodeList children = parent.getChildNodes();
048: List list = new ArrayList();
049: int size = children.getLength();
050:
051: for (int i = 0; i < size; i++) {
052: Node node = children.item(i);
053:
054: if (node.getNodeType() == Node.ELEMENT_NODE) {
055: Element element = (Element) node;
056:
057: if (childName.equals(element.getNodeName())) {
058: list.add(element);
059: }
060: }
061: }
062:
063: return list;
064: }
065:
066: public static String getChildText(Element parent, String childName) {
067: Element child = getChildElement(parent, childName);
068:
069: if (child == null) {
070: return null;
071: }
072:
073: return getText(child);
074: }
075:
076: public static String getText(Element node) {
077: StringBuffer sb = new StringBuffer();
078: NodeList list = node.getChildNodes();
079:
080: for (int i = 0; i < list.getLength(); i++) {
081: Node child = list.item(i);
082:
083: switch (child.getNodeType()) {
084: case Node.CDATA_SECTION_NODE:
085: case Node.TEXT_NODE:
086: sb.append(child.getNodeValue());
087: }
088: }
089:
090: return sb.toString();
091: }
092:
093: public static String encode(Object string) {
094: if (string == null) {
095: return "";
096: }
097:
098: char[] chars = string.toString().toCharArray();
099: StringBuffer out = new StringBuffer();
100:
101: for (int i = 0; i < chars.length; i++) {
102: switch (chars[i]) {
103: case '&':
104: out.append("&");
105:
106: break;
107:
108: case '<':
109: out.append("<");
110:
111: break;
112:
113: case '>':
114: out.append(">");
115:
116: break;
117:
118: case '\"':
119: out.append(""");
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 out, int indent) {
132: for (int i = 0; i < indent; i++) {
133: out.print(XMLizable.INDENT);
134: }
135: }
136: }
|