1 58 package org.krysalis.barcode.tools; 59 60 import org.apache.avalon.framework.configuration.Configuration; 61 import org.apache.avalon.framework.configuration.DefaultConfiguration; 62 import org.w3c.dom.Attr ; 63 import org.w3c.dom.Document ; 64 import org.w3c.dom.DocumentFragment ; 65 import org.w3c.dom.Element ; 66 import org.w3c.dom.NamedNodeMap ; 67 import org.w3c.dom.Node ; 68 import org.w3c.dom.Text ; 69 70 75 public class ConfigurationUtil { 76 77 80 protected ConfigurationUtil() { 81 throw new UnsupportedOperationException (); 82 } 83 84 89 public static Configuration buildConfiguration(Node node) { 90 return processNode(node); 91 } 92 93 private static Element findDocumentElement(Document document) { 94 Node nd = null; 96 for (int i = 0; i < document.getChildNodes().getLength(); i++) { 97 nd = document.getChildNodes().item(i); 98 if (nd.getNodeType() == Node.ELEMENT_NODE) { 99 return (Element )nd; 100 } 101 } 102 return null; 103 } 104 105 private static DefaultConfiguration processNode(Node node) { 106 if (node.getNodeType() == Node.ELEMENT_NODE) { 107 return processElement((Element )node); 108 } else if (node.getNodeType() == Node.DOCUMENT_NODE) { 109 return processElement(findDocumentElement((Document )node)); 110 } else if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE) { 111 DocumentFragment df = (DocumentFragment )node; 112 return processNode(df.getFirstChild()); 113 } else { 114 return null; 115 } 116 } 117 118 private static DefaultConfiguration processElement(Element el) { 119 DefaultConfiguration cfg = new DefaultConfiguration(el.getLocalName()); 120 NamedNodeMap atts = el.getAttributes(); 121 for (int i = 0; i < atts.getLength(); i++) { 122 Attr attr = (Attr )atts.item(i); 123 cfg.setAttribute(attr.getName(), attr.getValue()); 124 } 125 for (int i = 0; i < el.getChildNodes().getLength(); i++) { 126 Node node = el.getChildNodes().item(i); 127 if (node.getNodeType() == Node.ATTRIBUTE_NODE) { 128 Attr attr = (Attr )node; 129 cfg.setAttribute(attr.getName(), attr.getNodeValue()); 130 } else if (node.getNodeType() == Node.ELEMENT_NODE) { 131 cfg.addChild(processElement((Element )node)); 132 } else if (node.getNodeType() == Node.TEXT_NODE) { 133 String s = cfg.getValue("") + ((Text )node).getData(); 134 cfg.setValue(s.trim()); 135 } else { 136 } 138 } 139 return cfg; 140 } 141 142 } 143 | Popular Tags |