bruno 2003/06/26 02:12:12
Modified: src/blocks/woody/java/org/apache/cocoon/woody/util
DomHelper.java
Log:
Added a few methods to get child elements
Revision Changes Path
1.3 +29 -0
cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/util/DomHelper.java
Index: DomHelper.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/woody/java/org/apache/cocoon/woody/util/DomHelper.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- DomHelper.java 24 Apr 2003 09:44:07 -0000 1.2
+++ DomHelper.java 26 Jun 2003 09:12:12 -0000 1.3
@@ -107,10 +107,26 @@
}
/**
+ * Returns all Element children of an Element that belong to the given
namespace and have the given local name.
+ */
+ public static Element[] getChildElements(Element element, String namespace,
String localName) {
+ ArrayList elements = new ArrayList();
+ NodeList nodeList = element.getChildNodes();
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Node node = nodeList.item(i);
+ if (node instanceof Element && namespace.equals(node.getNamespaceURI())
&& localName.equals(node.getLocalName()))
+ elements.add(node);
+ }
+ return (Element[])elements.toArray(new Element[0]);
+ }
+
+ /**
* Returns the first child element with the given namespace and localName, or
null
* if there is no such element.
*/
public static Element getChildElement(Element element, String namespace, String
localName) {
+ // note: instead of calling getChildElement(element, namespace, localName,
false),
+ // the code is duplicated here because this method does not throw an
exception
NodeList nodeList = element.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
@@ -118,6 +134,19 @@
return (Element)node;
}
return null;
+ }
+
+ public static Element getChildElement(Element element, String namespace, String
localName, boolean required) throws Exception {
+ NodeList nodeList = element.getChildNodes();
+ for (int i = 0; i < nodeList.getLength(); i++) {
+ Node node = nodeList.item(i);
+ if (node instanceof Element && namespace.equals(node.getNamespaceURI())
&& localName.equals(node.getLocalName()))
+ return (Element)node;
+ }
+ if (required)
+ throw new Exception("Missing element \"" + localName + "\" as child of
element \"" + element.getTagName() + "\" at " + DomHelper.getLocation(element));
+ else
+ return null;
}
/**