1 24 package org.riotfamily.common.xml; 25 26 import java.io.IOException ; 27 import java.io.StringReader ; 28 import java.io.StringWriter ; 29 import java.util.Iterator ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 33 import javax.xml.parsers.DocumentBuilder ; 34 import javax.xml.parsers.DocumentBuilderFactory ; 35 import javax.xml.parsers.ParserConfigurationException ; 36 import javax.xml.transform.OutputKeys ; 37 import javax.xml.transform.Result ; 38 import javax.xml.transform.Source ; 39 import javax.xml.transform.Transformer ; 40 import javax.xml.transform.TransformerException ; 41 import javax.xml.transform.TransformerFactory ; 42 import javax.xml.transform.dom.DOMSource ; 43 import javax.xml.transform.stream.StreamResult ; 44 45 import org.riotfamily.common.util.FormatUtils; 46 import org.springframework.beans.BeanWrapper; 47 import org.springframework.beans.BeanWrapperImpl; 48 import org.springframework.beans.MutablePropertyValues; 49 import org.springframework.beans.PropertyValue; 50 import org.springframework.beans.PropertyValues; 51 import org.springframework.beans.factory.BeanFactory; 52 import org.springframework.util.Assert; 53 import org.springframework.util.StringUtils; 54 import org.w3c.dom.Attr ; 55 import org.w3c.dom.Document ; 56 import org.w3c.dom.Element ; 57 import org.w3c.dom.NamedNodeMap ; 58 import org.w3c.dom.Node ; 59 import org.w3c.dom.NodeList ; 60 import org.xml.sax.InputSource ; 61 import org.xml.sax.SAXException ; 62 63 67 public class XmlUtils { 68 69 private static final String PROPERTY_NAME = "name"; 70 71 private static final String PROPERTY_VALUE = "value"; 72 73 private static final String PROPERTY_REF = "ref"; 74 75 public static String getLocalName(Node node) { 76 String name = node.getLocalName(); 77 return name != null ? name : node.getNodeName(); 78 } 79 80 public static List getChildElements(Element ele) { 81 NodeList nl = ele.getChildNodes(); 82 List childEles = new LinkedList (); 83 for (int i = 0; i < nl.getLength(); i++) { 84 Node node = nl.item(i); 85 if (node instanceof Element ) { 86 childEles.add(node); 87 } 88 } 89 return childEles; 90 } 91 92 public static List getChildElementsByRegex(Element ele, String pattern) { 93 NodeList nl = ele.getChildNodes(); 94 List childEles = new LinkedList (); 95 for (int i = 0; i < nl.getLength(); i++) { 96 Node node = nl.item(i); 97 if (node instanceof Element && getLocalName(node).matches(pattern)) { 98 childEles.add(node); 99 } 100 } 101 return childEles; 102 } 103 104 public static Element getFirstChildElement(Element parent) { 105 Node child = parent.getFirstChild(); 106 while (child != null) { 107 if (child instanceof Element ) { 108 return (Element ) child; 109 } 110 child = child.getNextSibling(); 111 } 112 return null; 113 } 114 115 public static Element getFirstChildByTagName(Element ele, String tagName) { 116 NodeList nl = ele.getChildNodes(); 117 for (int i = 0; i < nl.getLength(); i++) { 118 Node node = nl.item(i); 119 if (tagName.equals(getLocalName(node))) { 120 return (Element ) node; 121 } 122 } 123 return null; 124 } 125 126 public static Element getFirstChildByRegex(Element ele, String pattern) { 127 NodeList nl = ele.getChildNodes(); 128 for (int i = 0; i < nl.getLength(); i++) { 129 Node node = nl.item(i); 130 if (node instanceof Element && getLocalName(node).matches(pattern)) { 131 return (Element ) node; 132 } 133 } 134 return null; 135 } 136 137 public static Element getNextSiblingElement(Node node) { 138 node = node.getNextSibling(); 139 while (node != null) { 140 if (node.getNodeType() == Node.ELEMENT_NODE) { 141 return (Element ) node; 142 } 143 node = node.getNextSibling(); 144 } 145 return null; 146 } 147 148 public static String getAttribute(Element ele, String name) { 149 Assert.hasText(name, "An attribute name must be specified"); 150 Attr attr = ele.getAttributeNode(name); 151 return attr != null ? attr.getNodeValue() : null; 152 } 153 154 public static Integer getIntegerAttribute(Element ele, String name) { 155 String s = getAttribute(ele, name); 156 if (s != null) { 157 try { 158 return new Integer (s); 159 } 160 catch (NumberFormatException e) { 161 } 162 } 163 return null; 164 } 165 166 public static int getIntAttribute(Element ele, String name, 167 int defaultValue) { 168 169 String s = getAttribute(ele, name); 170 if (s != null) { 171 try { 172 return Integer.parseInt(s); 173 } 174 catch (NumberFormatException e) { 175 } 176 } 177 return defaultValue; 178 } 179 180 public static boolean getBooleanAttribute(Element ele, String name) { 181 String s = getAttribute(ele, name); 182 return Boolean.valueOf(s).booleanValue(); 183 } 184 185 public static void populate(Object bean, NamedNodeMap attributes) { 186 BeanWrapper beanWrapper = new BeanWrapperImpl(bean); 187 beanWrapper.setPropertyValues(getPropertyValues(attributes)); 188 } 189 190 public static PropertyValues getPropertyValues(NamedNodeMap attributes) { 191 MutablePropertyValues pvs = new MutablePropertyValues(); 192 if (attributes != null) { 193 int length = attributes.getLength(); 194 for (int i = 0; i < length; i++) { 195 Attr attr = (Attr ) attributes.item(i); 196 String property = FormatUtils.xmlToCamelCase(attr.getName()); 197 pvs.addPropertyValue(property, attr.getValue()); 198 } 199 } 200 return pvs; 201 } 202 203 public static void populate(Object bean, Element element, 204 String [] attributeNames) { 205 206 populate(bean, element, attributeNames, null); 207 } 208 209 public static void populate(Object bean, Element element, 210 String [] attributeNames, BeanFactory beanFactory) { 211 212 BeanWrapper beanWrapper = new BeanWrapperImpl(bean); 213 beanWrapper.setPropertyValues(getPropertyValues( 214 element, attributeNames, beanFactory)); 215 } 216 217 public static PropertyValues getPropertyValues(Element element, 218 String [] attributeNames, BeanFactory beanFactory) { 219 220 MutablePropertyValues pvs = new MutablePropertyValues(); 221 for (int i = 0; i < attributeNames.length; i++) { 222 PropertyValue pv = getPropertyValue(element, 223 attributeNames[i], beanFactory); 224 225 if (pv != null) { 226 pvs.addPropertyValue(pv); 227 } 228 } 229 return pvs; 230 } 231 232 public static PropertyValue getPropertyValue(Element element, String attr, 233 BeanFactory beanFactory) { 234 235 String property = null; 236 int i = attr.indexOf('='); 237 if (i != -1) { 238 property = attr.substring(0, i); 239 attr = attr.substring(i + 1); 240 } 241 242 boolean beanRef = attr.charAt(0) == '@'; 243 if (beanRef) { 244 Assert.notNull(beanFactory, "A BeanFactory must be passed in " + 245 "order to resolve references"); 246 247 attr = attr.substring(1); 248 } 249 250 if (property == null) { 251 property = FormatUtils.xmlToCamelCase(attr); 252 } 253 254 String value = element.getAttribute(attr); 255 if (StringUtils.hasLength(value)) { 256 if (beanRef) { 257 return new PropertyValue(property, beanFactory.getBean(value)); 258 } 259 return new PropertyValue(property, value); 260 } 261 return null; 262 } 263 264 public static void populate(Object bean, List elements, 265 BeanFactory beanFactory) { 266 267 BeanWrapper beanWrapper = new BeanWrapperImpl(bean); 268 Iterator it = elements.iterator(); 269 while (it.hasNext()) { 270 Element ele = (Element ) it.next(); 271 String name = getAttribute(ele, PROPERTY_NAME); 272 Object value = getAttribute(ele, PROPERTY_VALUE); 273 if (value == null) { 274 String beanId = getAttribute(ele, PROPERTY_REF); 275 if (beanId != null) { 276 Assert.notNull(beanFactory, 277 "A BeanFactory must be supplied in order to set " + 278 "properties by reference."); 279 280 value = beanFactory.getBean(beanId); 281 } 282 } 283 beanWrapper.setPropertyValue(name, value); 284 } 285 } 286 287 public static Document createDocument() { 288 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 289 try { 290 return dbf.newDocumentBuilder().newDocument(); 291 } 292 catch (ParserConfigurationException e) { 293 throw new RuntimeException (e); 294 } 295 } 296 297 public static void removeNode(Node node) { 298 if (node != null) { 299 Node parent = node.getParentNode(); 300 if (parent != null) { 301 parent.removeChild(node); 302 } 303 } 304 } 305 306 public static void importAndAppend(Node node, Node parent) { 307 parent.appendChild(parent.getOwnerDocument().importNode(node, true)); 308 } 309 310 public static void removeAllChildNodes(Node node) { 311 while (node.hasChildNodes()) { 312 node.removeChild(node.getLastChild()); 313 } 314 } 315 316 public static void setTextValue(Element element, String value) { 317 removeAllChildNodes(element); 318 if (value != null) { 319 Node text = element.getOwnerDocument().createTextNode(value); 320 element.appendChild(text); 321 } 322 } 323 324 public static Document parse(String xml) { 325 try { 326 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 327 dbf.setNamespaceAware(true); 328 DocumentBuilder parser = dbf.newDocumentBuilder(); 329 return parser.parse(new InputSource (new StringReader (xml))); 330 } 331 catch (ParserConfigurationException e) { 332 throw new RuntimeException (e); 333 } 334 catch (IOException e) { 335 throw new RuntimeException (e); 336 } 337 catch (SAXException e) { 338 throw new RuntimeException (e); 339 } 340 } 341 342 public static String serialize(Node node) { 343 try { 344 TransformerFactory tf = TransformerFactory.newInstance(); 345 Transformer transformer = tf.newTransformer(); 346 transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 347 Source source = new DOMSource (node); 348 StringWriter sw = new StringWriter (); 349 Result result = new StreamResult (sw); 350 transformer.transform(source, result); 351 return sw.toString(); 352 } 353 catch (TransformerException e) { 354 throw new RuntimeException (e); 355 } 356 } 357 358 } 359 | Popular Tags |