1 16 package scriptella.configuration; 17 18 import org.w3c.dom.Document ; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Node ; 21 import org.w3c.dom.NodeList ; 22 import scriptella.expression.PropertiesSubstitutor; 23 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 import java.util.Set ; 28 29 30 36 public class XmlElement { 37 private Element element; 38 private URL documentUrl; 39 private PropertiesSubstitutor substitutor; 40 41 public XmlElement(Element element, URL documentUrl, PropertiesSubstitutor substitutor) { 42 this.element = element; 43 this.documentUrl = documentUrl; 44 this.substitutor = substitutor; 45 } 46 47 public XmlElement(Element element, XmlElement parent) { 48 this(element, parent.documentUrl, parent.substitutor); 49 } 50 51 public String getTagName() { 52 return element.getTagName(); 53 } 54 55 public Element getElement() { 56 return element; 57 } 58 59 public URL getDocumentUrl() { 60 return documentUrl; 61 } 62 63 protected List <XmlElement> getChildren() { 64 return asList(element.getChildNodes()); 65 } 66 67 public String getXPath() { 68 List <String > l = new ArrayList <String >(); 69 Node cur = element; 70 StringBuilder tmp = new StringBuilder (); 71 72 while (!(cur instanceof Document )) { 73 int pos = 1; 74 Node sib = cur; 75 final String curTagName = ((Element ) cur).getTagName(); 76 77 while (sib != null) { 78 sib = sib.getPreviousSibling(); 79 80 if ((sib != null) && sib instanceof Element ) { 81 Element ee = (Element ) sib; 82 83 if (curTagName.equals(ee.getTagName())) { 84 pos++; 85 } 86 } 87 } 88 89 tmp.setLength(0); 90 tmp.append(curTagName); 91 cur = cur.getParentNode(); 92 if (!(cur instanceof Document )) { 93 tmp.append('['); 94 tmp.append(pos); 95 tmp.append(']'); 96 } 97 l.add(tmp.toString()); 98 99 } 100 101 StringBuilder res = new StringBuilder (100); 102 103 for (int i = l.size() - 1; i >= 0; i--) { 104 res.append('/'); 105 res.append(l.get(i)); 106 } 107 108 return res.toString(); 109 } 110 111 public List <XmlElement> getChildren(final String name) { 112 List <XmlElement> res = new ArrayList <XmlElement>(); 113 Node node = element.getFirstChild(); 114 115 while (node != null) { 116 if (node instanceof Element ) { 117 if (name.equals(((Element ) node).getTagName())) { 118 res.add(new XmlElement((Element ) node, this)); 119 } 120 } 121 122 node = node.getNextSibling(); 123 } 124 125 return res; 126 } 127 128 public List <XmlElement> getChildren(final Set <String > names) { 129 List <XmlElement> res = new ArrayList <XmlElement>(); 130 Node node = element.getFirstChild(); 131 132 while (node != null) { 133 if (node instanceof Element ) { 134 if (names.contains(((Element ) node).getTagName())) { 135 res.add(new XmlElement((Element ) node, this)); 136 } 137 } 138 139 node = node.getNextSibling(); 140 } 141 142 return res; 143 } 144 145 public XmlElement getChild(final String name) { 146 Node node = element.getFirstChild(); 147 148 while (node != null) { 149 if (node instanceof Element ) { 150 if (name.equals(((Element ) node).getTagName())) { 151 return new XmlElement((Element ) node, this); 152 } 153 } 154 155 node = node.getNextSibling(); 156 } 157 158 return null; 159 } 160 161 protected List <XmlElement> asList(final NodeList list) { 162 List <XmlElement> result = new ArrayList <XmlElement>(); 163 Node node = element.getFirstChild(); 164 165 while (node != null) { 166 if (node instanceof Element ) { 167 result.add(new XmlElement((Element ) node, this)); 168 } 169 170 node = node.getNextSibling(); 171 } 172 173 return result; 174 } 175 176 182 public String getAttribute(final String attribute) { 183 final String a = element.getAttribute(attribute); 184 185 return ((a != null) && (a.length() == 0)) ? null : expandProperties(a); 186 } 187 188 194 protected boolean getBooleanAttribute(final String attribute, 195 final boolean defaultValue) { 196 final String a = getAttribute(attribute); 197 198 if (a == null) { 199 return defaultValue; 200 } 201 202 if ("true".equalsIgnoreCase(a) || "1".equalsIgnoreCase(a) || 203 "on".equalsIgnoreCase(a) || "yes".equalsIgnoreCase(a)) { 204 return true; 205 } 206 207 if ("false".equalsIgnoreCase(a) || "0".equalsIgnoreCase(a) || 208 "off".equalsIgnoreCase(a) || "no".equalsIgnoreCase(a)) { 209 return false; 210 } 211 212 throw new ConfigurationException("Unrecognized value '" + a + 213 "' of boolean attribute " + attribute + 214 ". Valid values: yes/no, true/false, 1/0, on/off", this); 215 } 216 217 222 public String expandProperties(final String s) { 223 return substitutor.substitute(s); 224 } 225 226 } 227 | Popular Tags |