1 16 package org.apache.cocoon.forms.util; 17 18 import java.io.IOException ; 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.ListIterator ; 24 import java.util.Map ; 25 26 import org.apache.avalon.framework.service.ServiceException; 27 import org.apache.avalon.framework.service.ServiceManager; 28 import org.apache.cocoon.util.location.Location; 29 import org.apache.cocoon.util.location.LocationAttributes; 30 import org.apache.cocoon.xml.SaxBuffer; 31 import org.apache.cocoon.xml.dom.DOMBuilder; 32 import org.apache.cocoon.xml.dom.DOMStreamer; 33 import org.apache.commons.lang.BooleanUtils; 34 import org.apache.excalibur.xml.sax.SAXParser; 35 import org.apache.excalibur.xml.sax.XMLizable; 36 import org.w3c.dom.Attr ; 37 import org.w3c.dom.CDATASection ; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.NamedNodeMap ; 41 import org.w3c.dom.Node ; 42 import org.w3c.dom.NodeList ; 43 import org.w3c.dom.Text ; 44 import org.xml.sax.ContentHandler ; 45 import org.xml.sax.InputSource ; 46 import org.xml.sax.SAXException ; 47 import org.xml.sax.SAXNotSupportedException ; 48 49 62 public class DomHelper { 63 64 public static final String XMLNS_URI = "http://www.w3.org/2000/xmlns/"; 65 66 public static Location getLocationObject(Element element) { 67 return LocationAttributes.getLocation(element); 68 } 69 70 75 public static String getLocation(Element element) { 76 return LocationAttributes.getLocationString(element); 77 } 78 79 public static String getSystemIdLocation(Element element) { 80 return LocationAttributes.getURI(element); 81 } 82 83 public static int getLineLocation(Element element) { 84 return LocationAttributes.getLine(element); 85 } 86 87 public static int getColumnLocation(Element element) { 88 return LocationAttributes.getColumn(element); 89 } 90 91 95 public static Element [] getChildElements(Element element, String namespace) { 96 ArrayList elements = new ArrayList (); 97 NodeList nodeList = element.getChildNodes(); 98 for (int i = 0; i < nodeList.getLength(); i++) { 99 Node node = nodeList.item(i); 100 if (node instanceof Element 101 && namespace.equals(node.getNamespaceURI())) 102 elements.add(node); 103 } 104 return (Element [])elements.toArray(new Element [elements.size()]); 105 } 106 107 111 public static Element [] getChildElements(Element element, 112 String namespace, String localName) { 113 ArrayList elements = new ArrayList (); 114 NodeList nodeList = element.getChildNodes(); 115 for (int i = 0; i < nodeList.getLength(); i++) { 116 Node node = nodeList.item(i); 117 if (node instanceof Element 118 && namespace.equals(node.getNamespaceURI()) 119 && localName.equals(node.getLocalName())) { 120 elements.add(node); 121 } 122 } 123 return (Element [])elements.toArray(new Element [elements.size()]); 124 } 125 126 130 public static Element getChildElement(Element element, String namespace, 131 String localName) { 132 Element node = null; 133 try { 134 node = getChildElement(element, namespace, localName, false); 135 } catch (Exception e) { 136 node = null; 137 } 138 return node; 139 } 140 141 146 public static Element getChildElement(Element element, String namespace, 147 String localName, boolean required) throws Exception { 148 NodeList nodeList = element.getChildNodes(); 149 for (int i = 0; i < nodeList.getLength(); i++) { 150 Node node = nodeList.item(i); 151 if (node instanceof Element 152 && namespace.equals(node.getNamespaceURI()) 153 && localName.equals(node.getLocalName())) { 154 return (Element )node; 155 } 156 } 157 if (required) { 158 throw new Exception ("Missing element \"" + localName + 159 "\" as child of element \"" + element.getTagName() + 160 "\" at " + DomHelper.getLocation(element)); 161 } else { 162 return null; 163 } 164 } 165 166 170 public static String getAttribute(Element element, String attributeName) 171 throws Exception { 172 String attrValue = element.getAttribute(attributeName); 173 if (attrValue.equals("")) { 174 throw new Exception ("Missing attribute \"" + attributeName + 175 "\" on element \"" + element.getTagName() + 176 "\" at " + getLocation(element)); 177 } 178 return attrValue; 179 } 180 181 185 public static String getAttribute(Element element, String attributeName, 186 String defaultValue) { 187 String attrValue = element.getAttribute(attributeName); 188 if (attrValue.equals("")) { 189 return defaultValue; 190 } 191 return attrValue; 192 } 193 194 public static int getAttributeAsInteger(Element element, 195 String attributeName) throws Exception { 196 String attrValue = getAttribute(element, attributeName); 197 try { 198 return Integer.parseInt(attrValue); 199 } catch (NumberFormatException e) { 200 throw new Exception ("Cannot parse the value \"" + attrValue + 201 "\" as an integer in the attribute \"" + attributeName + 202 "\" on the element \"" + element.getTagName() + 203 "\" at " + getLocation(element)); 204 } 205 } 206 207 public static int getAttributeAsInteger(Element element, 208 String attributeName, int defaultValue) throws Exception { 209 String attrValue = element.getAttribute(attributeName); 210 if (attrValue.equals("")) { 211 return defaultValue; 212 } else { 213 try { 214 return Integer.parseInt(attrValue); 215 } catch (NumberFormatException e) { 216 throw new Exception ("Cannot parse the value \"" + attrValue + 217 "\" as an integer in the attribute \"" + 218 attributeName + "\" on the element \"" + 219 element.getTagName() + "\" at " + 220 getLocation(element)); 221 } 222 } 223 } 224 225 public static boolean getAttributeAsBoolean(Element element, 226 String attributeName, boolean defaultValue) { 227 String attrValue = element.getAttribute(attributeName); 228 Boolean result; 229 try { 230 result = BooleanUtils.toBooleanObject(attrValue, "true", "false", null); 231 } catch (IllegalArgumentException iae) { 232 result = null; 233 } 234 if (result != null) { 235 return result.booleanValue(); 236 } 237 try { 238 result = BooleanUtils.toBooleanObject(attrValue, "yes", "no", null); 239 } catch (IllegalArgumentException iae) { 240 result = null; 241 } 242 if (result != null) { 243 return result.booleanValue(); 244 } 245 return defaultValue; 246 } 247 248 public static String getElementText(Element element) { 249 StringBuffer value = new StringBuffer (); 250 NodeList nodeList = element.getChildNodes(); 251 for (int i = 0; i < nodeList.getLength(); i++) { 252 Node node = nodeList.item(i); 253 if (node instanceof Text || node instanceof CDATASection ) { 254 value.append(node.getNodeValue()); 255 } 256 } 257 return value.toString(); 258 } 259 260 267 public static XMLizable compileElementContent(Element element) { 268 LocationAttributes.remove(element, true); 270 271 SaxBuffer saxBuffer = new SaxBuffer(); 272 DOMStreamer domStreamer = new DOMStreamer(); 273 domStreamer.setContentHandler(saxBuffer); 274 275 NodeList childNodes = element.getChildNodes(); 276 for (int i = 0; i < childNodes.getLength(); i++) { 277 try { 278 domStreamer.stream(childNodes.item(i)); 279 } catch (SAXException e) { 280 throw new RuntimeException ( 283 "Error in DomHelper.compileElementContent: " + 284 e.toString()); 285 } 286 } 287 return saxBuffer; 288 } 289 290 298 public static Document parse(InputSource inputSource, ServiceManager manager) 299 throws SAXException , SAXNotSupportedException , IOException , ServiceException { 300 301 SAXParser parser = (SAXParser)manager.lookup(SAXParser.ROLE); 302 DOMBuilder builder = new DOMBuilder(); 303 304 ContentHandler locationHandler = new LocationAttributes.Pipe(builder); 306 307 try { 308 parser.parse(inputSource, locationHandler); 309 } finally { 310 manager.release(parser); 311 } 312 313 return builder.getDocument(); 314 } 315 316 public static Map getLocalNSDeclarations(Element elm) 317 { 318 return addLocalNSDeclarations(elm, null); 319 } 320 321 private static Map addLocalNSDeclarations(Element elm, Map nsDeclarations) 322 { 323 NamedNodeMap atts = elm.getAttributes(); 324 int attsSize = atts.getLength(); 325 326 for (int i = 0; i < attsSize; i++) 327 { 328 Attr attr = (Attr )atts.item(i); 329 if (XMLNS_URI.equals(attr.getNamespaceURI())) 330 { 331 String nsUri = attr.getValue(); 332 String pfx = attr.getLocalName(); 333 if (nsDeclarations == null) 334 nsDeclarations = new HashMap (); 335 nsDeclarations.put(nsUri, pfx); 336 } 337 } 338 return nsDeclarations; } 339 340 public static Map getInheritedNSDeclarations(Element elm) 341 { 342 List ancestorsAndSelf = new LinkedList (); 343 Element current = elm; 344 while (current != null) 345 { 346 ancestorsAndSelf.add(current); 347 Node parent = current.getParentNode(); 348 if (parent.getNodeType() == Node.ELEMENT_NODE) 349 current = (Element )parent; 350 else 351 current = null; 352 } 353 354 Map nsDeclarations = null; 355 ListIterator iter = ancestorsAndSelf.listIterator(ancestorsAndSelf.size()); 356 while (iter.hasPrevious()) 357 { 358 Element element = (Element ) iter.previous(); 359 nsDeclarations = addLocalNSDeclarations(element, nsDeclarations); 360 } 361 362 return nsDeclarations; 363 } 364 } 365 | Popular Tags |