1 4 package org.ofbiz.minerva.pool.xml; 5 6 import java.lang.reflect.*; 7 import java.util.*; 8 9 import org.w3c.dom.*; 10 11 16 public abstract class XMLUtils { 17 18 public static String getChildText(Element parent, String name) { 19 Element e = getChildByName(parent, name); 20 if (e == null) 21 return ""; 22 return getText(e); 23 } 24 25 public static String getText(Element e) { 26 NodeList nl = e.getChildNodes(); 27 int max = nl.getLength(); 28 for (int i = 0; i < max; i++) { 29 Node n = nl.item(i); 30 if (n.getNodeType() == Node.TEXT_NODE) { 31 return n.getNodeValue(); 32 } 33 } 34 return ""; 35 } 36 37 public static Element getChildByName(Element e, String name) { 38 Element[] list = getChildrenByName(e, name); 39 if (list.length == 0) 40 return null; 41 if (list.length > 1) 42 throw new IllegalStateException ("Too many (" + list.length + ") '" + name + "' elements found!"); 43 return list[0]; 44 } 45 46 public static Element[] getChildrenByName(Element e, String name) { 47 NodeList nl = e.getChildNodes(); 48 int max = nl.getLength(); 49 LinkedList list = new LinkedList(); 50 for (int i = 0; i < max; i++) { 51 Node n = nl.item(i); 52 if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(name)) { 53 list.add(n); 54 } 55 } 56 return (Element[]) list.toArray(new Element[list.size()]); 57 } 58 59 public static String [] splitOnWhitespace(String source) { 60 int pos = -1; 61 LinkedList list = new LinkedList(); 62 int max = source.length(); 63 for (int i = 0; i < max; i++) { 64 char c = source.charAt(i); 65 if (Character.isWhitespace(c)) { 66 if (i - pos > 1) 67 list.add(source.substring(pos + 1, i)); 68 pos = i; 69 } 70 } 71 return (String []) list.toArray(new String [list.size()]); 72 } 73 74 public static Element createChild(Document doc, Element root, String name) { 75 Element elem = doc.createElement(name); 76 root.appendChild(elem); 77 return elem; 78 } 79 80 public static void createChildText(Document doc, Element elem, String name, String value) { 81 Element child = doc.createElement(name); 82 child.appendChild(doc.createTextNode(value == null ? "" : value)); 83 elem.appendChild(child); 84 } 85 86 public static void createOptionalChildText(Document doc, Element elem, String name, String value) { 87 if (value == null || value.length() == 0) 88 return; 89 Element child = doc.createElement(name); 90 child.appendChild(doc.createTextNode(value)); 91 elem.appendChild(child); 92 } 93 94 public static Map getProperties(Element root) { 95 Map map = new HashMap(); 96 Element[] list = getChildrenByName(root, "property"); 97 for (int i = 0; i < list.length; i++) { 98 String name = list[i].getAttribute("name"); 99 String type = list[i].getAttribute("type"); 100 String valueString = getText(list[i]); 101 try { 102 Class cls = Class.forName(type); 103 Constructor con = cls.getConstructor(new Class []{String .class}); 104 Object value = con.newInstance(new Object []{valueString}); 105 map.put(name, value); 106 } catch (Exception e) { 107 System.out.println("Unable to parse property '" + name + "'='" + valueString + "': " + e); 108 } 109 } 110 return map; 111 } 112 113 public static void applyProperties(Object o, Element root) { 114 Map map = getProperties(root); 115 Iterator it = map.keySet().iterator(); 116 Field[] fields = o.getClass().getFields(); 117 Method[] methods = o.getClass().getMethods(); 118 while (it.hasNext()) { 119 String name = (String ) it.next(); 120 Object value = map.get(name); 121 try { 122 for (int i = 0; i < fields.length; i++) { 123 if (fields[i].getName().equalsIgnoreCase(name) && isTypeMatch(fields[i].getType(), value.getClass())) { 124 fields[i].set(o, value); 125 System.out.println("Set field " + fields[i].getName() + "=" + value); 126 break; 127 } 128 } 129 for (int i = 0; i < methods.length; i++) { 130 if (methods[i].getName().equalsIgnoreCase("set" + name) && methods[i].getParameterTypes().length == 1 && isTypeMatch(methods[i].getParameterTypes()[0], value.getClass())) { 131 methods[i].invoke(o, new Object []{value}); 132 System.out.println("Set method " + methods[i].getName() + "=" + value); 133 break; 134 } 135 } 136 } catch (Exception e) { 137 System.out.println("Unable to apply property '" + name + "': " + e); 138 } 139 } 140 } 141 142 private static boolean isTypeMatch(Class one, Class two) { 143 if (one.equals(two)) 144 return true; 145 if (one.isPrimitive()) { 146 if (one.getName().equals("int") && two.getName().equals("java.lang.Integer")) 147 return true; 148 if (one.getName().equals("long") && two.getName().equals("java.lang.Long")) 149 return true; 150 if (one.getName().equals("float") && two.getName().equals("java.lang.Float")) 151 return true; 152 if (one.getName().equals("double") && two.getName().equals("java.lang.Double")) 153 return true; 154 if (one.getName().equals("char") && two.getName().equals("java.lang.Character")) 155 return true; 156 if (one.getName().equals("byte") && two.getName().equals("java.lang.Byte")) 157 return true; 158 if (one.getName().equals("short") && two.getName().equals("java.lang.Short")) 159 return true; 160 if (one.getName().equals("boolean") && two.getName().equals("java.lang.Boolean")) 161 return true; 162 } 163 return false; 164 } 165 } 166 | Popular Tags |