djencks 2004/01/01 14:42:48
Modified: modules/core/src/java/org/apache/geronimo/xml/deployment
LoaderUtil.java
Log:
Geronimo-137, improved null/missing element handling
Revision Changes Path
1.10 +35 -24
incubator-geronimo/modules/core/src/java/org/apache/geronimo/xml/deployment/LoaderUtil.java
Index: LoaderUtil.java
===================================================================
RCS file:
/home/cvs/incubator-geronimo/modules/core/src/java/org/apache/geronimo/xml/deployment/LoaderUtil.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- LoaderUtil.java 18 Nov 2003 02:12:33 -0000 1.9
+++ LoaderUtil.java 1 Jan 2004 22:42:48 -0000 1.10
@@ -59,11 +59,11 @@
import java.io.IOException;
import java.io.Reader;
import java.util.LinkedList;
+
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
@@ -77,24 +77,26 @@
* @version $Revision$ $Date$
*/
public final class LoaderUtil {
- private static final Log log = LogFactory.getLog(LoaderUtil.class);
public static String getContent(Element element) {
+ if (element == null) {
+ return null;
+ }
LinkedList nodes = new LinkedList();
nodes.add(element);
StringBuffer buf = new StringBuffer(100);
while (!nodes.isEmpty()) {
Node node = (Node) nodes.removeFirst();
switch (node.getNodeType()) {
- case Node.ELEMENT_NODE:
- for (Node child = node.getLastChild(); child != null; child
= child.getPreviousSibling()) {
- nodes.addFirst(child);
- }
- break;
- case Node.TEXT_NODE:
- case Node.CDATA_SECTION_NODE:
- buf.append(node.getNodeValue());
- break;
+ case Node.ELEMENT_NODE:
+ for (Node child = node.getLastChild(); child != null;
child = child.getPreviousSibling()) {
+ nodes.addFirst(child);
+ }
+ break;
+ case Node.TEXT_NODE:
+ case Node.CDATA_SECTION_NODE:
+ buf.append(node.getNodeValue());
+ break;
}
}
String content = buf.toString().trim();
@@ -106,14 +108,16 @@
}
public static String getAttribute(Element element, String attribute) {
- if (element.hasAttribute(attribute)) {
+ if (element != null && element.hasAttribute(attribute)) {
return element.getAttribute(attribute);
- } else {
- return null;
}
+ return null;
}
public static Element getChild(Element element, String child) {
+ if (element == null || child == null) {
+ return null;
+ }
for (Node node = element.getFirstChild(); node != null; node =
node.getNextSibling()) {
if (node instanceof Element == false) {
continue;
@@ -127,21 +131,23 @@
}
/**
- * Gets an array of the direct children of this node with the specified
- * tag name
+ * @return an array of the direct children of this node with the
specified
+ * tag name; never null
*/
public static Element[] getChildren(Element root, String childName) {
+ if (root == null || childName == null) {
+ return new Element[0];
+ }
NodeList nl = root.getChildNodes();
int max = nl.getLength();
LinkedList list = new LinkedList();
- for(int i=0; i<max; i++) {
+ for (int i = 0; i < max; i++) {
Node n = nl.item(i);
- if(n.getNodeType() == Node.ELEMENT_NODE &&
- n.getLocalName().equals(childName)) {
+ if (n.getNodeType() == Node.ELEMENT_NODE &&
n.getLocalName().equals(childName)) {
list.add(n);
}
}
- return (Element[])list.toArray(new Element[list.size()]);
+ return (Element[]) list.toArray(new Element[list.size()]);
}
public static String getChildContent(Element element, String child) {
@@ -154,9 +160,12 @@
}
public static String[] getChildrenContent(Element element, String child)
{
+ if (element == null || child == null) {
+ return new String[0];
+ }
NodeList nodes = element.getElementsByTagName(child);
String[] result = new String[nodes.getLength()];
- for (int i=0; i < nodes.getLength(); i++) {
+ for (int i = 0; i < nodes.getLength(); i++) {
Element e = (Element) nodes.item(i);
result[i] = getContent(e);
}
@@ -184,7 +193,8 @@
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(true);
-
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
"http://www.w3.org/2001/XMLSchema");
+
factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
+ "http://www.w3.org/2001/XMLSchema");
try {
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new LocalEntityResolver());
@@ -194,3 +204,4 @@
}
}
}
+