001: /*
002: * Licensed under the X license (see http://www.x.org/terms.htm)
003: */
004: package org.ofbiz.minerva.pool.xml;
005:
006: import java.lang.reflect.*;
007: import java.util.*;
008:
009: import org.w3c.dom.*;
010:
011: /**
012: * Utility functions for parsing XML (DOM).
013: *
014: * @author Aaron Mulder (ammulder@alumni.princeton.edu)
015: */
016: public abstract class XMLUtils {
017:
018: public static String getChildText(Element parent, String name) {
019: Element e = getChildByName(parent, name);
020: if (e == null)
021: return "";
022: return getText(e);
023: }
024:
025: public static String getText(Element e) {
026: NodeList nl = e.getChildNodes();
027: int max = nl.getLength();
028: for (int i = 0; i < max; i++) {
029: Node n = nl.item(i);
030: if (n.getNodeType() == Node.TEXT_NODE) {
031: return n.getNodeValue();
032: }
033: }
034: return "";
035: }
036:
037: public static Element getChildByName(Element e, String name) {
038: Element[] list = getChildrenByName(e, name);
039: if (list.length == 0)
040: return null;
041: if (list.length > 1)
042: throw new IllegalStateException("Too many (" + list.length
043: + ") '" + name + "' elements found!");
044: return list[0];
045: }
046:
047: public static Element[] getChildrenByName(Element e, String name) {
048: NodeList nl = e.getChildNodes();
049: int max = nl.getLength();
050: LinkedList list = new LinkedList();
051: for (int i = 0; i < max; i++) {
052: Node n = nl.item(i);
053: if (n.getNodeType() == Node.ELEMENT_NODE
054: && n.getNodeName().equals(name)) {
055: list.add(n);
056: }
057: }
058: return (Element[]) list.toArray(new Element[list.size()]);
059: }
060:
061: public static String[] splitOnWhitespace(String source) {
062: int pos = -1;
063: LinkedList list = new LinkedList();
064: int max = source.length();
065: for (int i = 0; i < max; i++) {
066: char c = source.charAt(i);
067: if (Character.isWhitespace(c)) {
068: if (i - pos > 1)
069: list.add(source.substring(pos + 1, i));
070: pos = i;
071: }
072: }
073: return (String[]) list.toArray(new String[list.size()]);
074: }
075:
076: public static Element createChild(Document doc, Element root,
077: String name) {
078: Element elem = doc.createElement(name);
079: root.appendChild(elem);
080: return elem;
081: }
082:
083: public static void createChildText(Document doc, Element elem,
084: String name, String value) {
085: Element child = doc.createElement(name);
086: child.appendChild(doc
087: .createTextNode(value == null ? "" : value));
088: elem.appendChild(child);
089: }
090:
091: public static void createOptionalChildText(Document doc,
092: Element elem, String name, String value) {
093: if (value == null || value.length() == 0)
094: return;
095: Element child = doc.createElement(name);
096: child.appendChild(doc.createTextNode(value));
097: elem.appendChild(child);
098: }
099:
100: public static Map getProperties(Element root) {
101: Map map = new HashMap();
102: Element[] list = getChildrenByName(root, "property");
103: for (int i = 0; i < list.length; i++) {
104: String name = list[i].getAttribute("name");
105: String type = list[i].getAttribute("type");
106: String valueString = getText(list[i]);
107: try {
108: Class cls = Class.forName(type);
109: Constructor con = cls
110: .getConstructor(new Class[] { String.class });
111: Object value = con
112: .newInstance(new Object[] { valueString });
113: map.put(name, value);
114: } catch (Exception e) {
115: System.out.println("Unable to parse property '" + name
116: + "'='" + valueString + "': " + e);
117: }
118: }
119: return map;
120: }
121:
122: public static void applyProperties(Object o, Element root) {
123: Map map = getProperties(root);
124: Iterator it = map.keySet().iterator();
125: Field[] fields = o.getClass().getFields();
126: Method[] methods = o.getClass().getMethods();
127: while (it.hasNext()) {
128: String name = (String) it.next();
129: Object value = map.get(name);
130: try {
131: for (int i = 0; i < fields.length; i++) {
132: if (fields[i].getName().equalsIgnoreCase(name)
133: && isTypeMatch(fields[i].getType(), value
134: .getClass())) {
135: fields[i].set(o, value);
136: System.out.println("Set field "
137: + fields[i].getName() + "=" + value);
138: break;
139: }
140: }
141: for (int i = 0; i < methods.length; i++) {
142: if (methods[i].getName().equalsIgnoreCase(
143: "set" + name)
144: && methods[i].getParameterTypes().length == 1
145: && isTypeMatch(methods[i]
146: .getParameterTypes()[0], value
147: .getClass())) {
148: methods[i].invoke(o, new Object[] { value });
149: System.out.println("Set method "
150: + methods[i].getName() + "=" + value);
151: break;
152: }
153: }
154: } catch (Exception e) {
155: System.out.println("Unable to apply property '" + name
156: + "': " + e);
157: }
158: }
159: }
160:
161: private static boolean isTypeMatch(Class one, Class two) {
162: if (one.equals(two))
163: return true;
164: if (one.isPrimitive()) {
165: if (one.getName().equals("int")
166: && two.getName().equals("java.lang.Integer"))
167: return true;
168: if (one.getName().equals("long")
169: && two.getName().equals("java.lang.Long"))
170: return true;
171: if (one.getName().equals("float")
172: && two.getName().equals("java.lang.Float"))
173: return true;
174: if (one.getName().equals("double")
175: && two.getName().equals("java.lang.Double"))
176: return true;
177: if (one.getName().equals("char")
178: && two.getName().equals("java.lang.Character"))
179: return true;
180: if (one.getName().equals("byte")
181: && two.getName().equals("java.lang.Byte"))
182: return true;
183: if (one.getName().equals("short")
184: && two.getName().equals("java.lang.Short"))
185: return true;
186: if (one.getName().equals("boolean")
187: && two.getName().equals("java.lang.Boolean"))
188: return true;
189: }
190: return false;
191: }
192: }
|