Author: julien(a)jboss.com
Date: 2008-06-02 13:09:40 -0400 (Mon, 02 Jun 2008)
New Revision: 10904
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/xml/XMLTools.java
Log:
improve xml stuff
Modified:
modules/common/trunk/common/src/main/java/org/jboss/portal/common/xml/XMLTools.java
===================================================================
---
modules/common/trunk/common/src/main/java/org/jboss/portal/common/xml/XMLTools.java 2008-06-02
13:53:29 UTC (rev 10903)
+++
modules/common/trunk/common/src/main/java/org/jboss/portal/common/xml/XMLTools.java 2008-06-02
17:09:40 UTC (rev 10904)
@@ -48,6 +48,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
+import java.util.Set;
/**
* Utilities for dealing with XML.
@@ -429,39 +430,134 @@
*/
public static List<Element> getChildren(Element element, String uri, String
name) throws IllegalArgumentException
{
- if (element == null)
+ return getChildren(element, byName(uri, name));
+ }
+
+ /**
+ * <p>Return all the children of the given node that match the provided
filter.</p>
+ *
+ * <p>The resulting element collection can be safely modified.</p>
+ *
+ * @param node the parent element
+ * @param filter the filter
+ * @return a list of elements
+ * @throws IllegalArgumentException if the element is null
+ */
+ @SuppressWarnings("unchecked")
+ public static <T extends Node> List<T> getChildren(Node node,
Filter<T> filter) throws IllegalArgumentException
+ {
+ if (node == null)
{
- throw new IllegalArgumentException("No element found");
+ throw new IllegalArgumentException("No node provided");
}
- ArrayList<Element> result = new ArrayList<Element>();
- NodeList list = element.getChildNodes();
+ if (filter == null)
+ {
+ throw new IllegalArgumentException("No filter provided");
+ }
+
+ //
+ ArrayList result = new ArrayList();
+
+ //
+ NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); i++)
{
- Node node = list.item(i);
- if (node.getNodeType() == Node.ELEMENT_NODE)
+ Node child = list.item(i);
+
+ //
+ Class<T> nodeType = filter.getNodeClass();
+
+ //
+ if (nodeType.isInstance(child))
{
- Element childElt = (Element)node;
+ T typedChild = nodeType.cast(child);
//
+ if (filter.accept(typedChild))
+ {
+ result.add(child);
+ }
+ }
+ }
+
+ // It is fine
+ return result;
+ }
+
+ public static interface Filter<N extends Node>
+ {
+
+ Class<N> getNodeClass();
+
+ boolean accept(N node);
+ }
+
+ public static Filter<Element> byName(final String uri, final Set<String>
names)
+ {
+ return new Filter<Element>()
+ {
+ public Class<Element> getNodeClass()
+ {
+ return Element.class;
+ }
+
+ public boolean accept(Element element)
+ {
if (uri == null)
{
- if (name == null || childElt.getTagName().equals(name))
+ if (names.contains(element.getTagName()))
{
- result.add(childElt);
+ return true;
}
}
- else if (uri.equals(childElt.getNamespaceURI()))
+ else if (uri.equals(element.getNamespaceURI()))
{
- if (name == null || childElt.getLocalName().equals(name))
+ if (names.contains(element.getLocalName()))
{
- result.add(childElt);
+ return true;
}
}
+
+ //
+ return false;
}
- }
- return result;
+ };
+
}
+ public static Filter<Element> byName(final String uri, final String name)
+ {
+ return new Filter<Element>()
+ {
+ public Class<Element> getNodeClass()
+ {
+ return Element.class;
+ }
+
+ public boolean accept(Element element)
+ {
+ if (uri == null)
+ {
+ if (name == null || element.getTagName().equals(name))
+ {
+ return true;
+ }
+ }
+ else if (uri.equals(element.getNamespaceURI()))
+ {
+ if (name == null || element.getLocalName().equals(name))
+ {
+ return true;
+ }
+ }
+
+ //
+ return false;
+ }
+ };
+
+ }
+
public static Properties loadXMLProperties(Element propertiesElt)
{
if (propertiesElt == null)
Show replies by date