1 19 20 package org.apache.cayenne.xml; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import javax.xml.parsers.DocumentBuilder ; 27 import javax.xml.parsers.DocumentBuilderFactory ; 28 import javax.xml.parsers.ParserConfigurationException ; 29 30 import org.apache.commons.collections.Predicate; 31 import org.apache.cayenne.CayenneRuntimeException; 32 import org.w3c.dom.CharacterData ; 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 37 43 class XMLUtil { 44 45 static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss zzz"; 47 48 static DocumentBuilderFactory sharedFactory; 49 50 53 static DocumentBuilder newBuilder() throws CayenneRuntimeException { 54 if (sharedFactory == null) { 55 sharedFactory = DocumentBuilderFactory.newInstance(); 56 } 57 58 try { 59 return sharedFactory.newDocumentBuilder(); 60 } 61 catch (ParserConfigurationException e) { 62 throw new CayenneRuntimeException("Can't create DocumentBuilder", e); 63 } 64 } 65 66 69 static List replaceParent(Node oldParent, Node newParent) { 70 71 List children = XMLUtil.getChildren(oldParent); 72 73 Iterator it = children.iterator(); 74 while (it.hasNext()) { 75 Element child = (Element ) it.next(); 76 oldParent.removeChild(child); 77 newParent.appendChild(child); 78 } 79 80 return children; 81 } 82 83 86 static String getText(Node node) { 87 88 NodeList nodes = node.getChildNodes(); 89 int len = nodes.getLength(); 90 91 if (len == 0) { 92 return null; 93 } 94 95 StringBuffer text = new StringBuffer (); 96 for (int i = 0; i < len; i++) { 97 Node child = nodes.item(i); 98 99 if (child instanceof CharacterData ) { 100 text.append(((CharacterData ) child).getData()); 101 } 102 } 103 104 return text.length() == 0 ? null : text.toString(); 105 } 106 107 110 static Element getChild(Node node, final String name) { 111 Predicate p = new Predicate() { 112 113 public boolean evaluate(Object object) { 114 if (object instanceof Element ) { 115 Element e = (Element ) object; 116 return name.equals(e.getNodeName()); 117 } 118 119 return false; 120 } 121 }; 122 123 return (Element ) firstMatch(node.getChildNodes(), p); 124 } 125 126 129 static List getChildren(Node node, final String name) { 130 Predicate p = new Predicate() { 131 132 public boolean evaluate(Object object) { 133 if (object instanceof Element ) { 134 Element e = (Element ) object; 135 return name.equals(e.getNodeName()); 136 } 137 138 return false; 139 } 140 }; 141 142 return allMatches(node.getChildNodes(), p); 143 } 144 145 148 static List getChildren(Node node) { 149 Predicate p = new Predicate() { 150 151 public boolean evaluate(Object object) { 152 return object instanceof Element ; 153 } 154 }; 155 156 return allMatches(node.getChildNodes(), p); 157 } 158 159 private static Node firstMatch(NodeList list, Predicate predicate) { 160 int len = list.getLength(); 161 162 for (int i = 0; i < len; i++) { 163 Node node = list.item(i); 164 if (predicate.evaluate(node)) { 165 return node; 166 } 167 } 168 169 return null; 170 } 171 172 private static List allMatches(NodeList list, Predicate predicate) { 173 int len = list.getLength(); 174 List children = new ArrayList (len); 175 176 for (int i = 0; i < len; i++) { 177 Node node = list.item(i); 178 if (predicate.evaluate(node)) { 179 children.add(node); 180 } 181 } 182 183 return children; 184 } 185 } 186 | Popular Tags |