[utilities COMMIT] /java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/ElementSupport.java
noreply at shibboleth.net
noreply at shibboleth.net
Thu May 2 00:40:17 EDT 2013
Author: dfisher
Date: Thu May 2 00:40:17 2013
New Revision: 397
URL: http://svn.shibboleth.net/view/utilities?rev=397&view=rev
Log:
Add convenience method for getting the first child element.
Fix null check bug in #getChildElements.
Modified:
java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/ElementSupport.java
Modified: java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/ElementSupport.java
URL: http://svn.shibboleth.net/view/utilities/java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/ElementSupport.java?rev=397&r1=396&r2=397&view=diff
==============================================================================
--- java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/ElementSupport.java (original)
+++ java-support/trunk/src/main/java/net/shibboleth/utilities/java/support/xml/ElementSupport.java Thu May 2 00:40:17 2013
@@ -127,7 +127,8 @@
Constraint.isNotNull(document, "Document may not be null");
final String trimmedLocalName =
- Constraint.isNotNull(StringSupport.trimOrNull(localName), "Element local name may not be null or empty");
+ Constraint
+ .isNotNull(StringSupport.trimOrNull(localName), "Element local name may not be null or empty");
String qualifiedName;
final String trimmedPrefix = StringSupport.trimOrNull(prefix);
@@ -152,7 +153,7 @@
if (root == null) {
return Collections.emptyList();
}
-
+
final ArrayList<Element> children = new ArrayList<Element>();
Element childNode = getFirstChildElement(root);
@@ -174,11 +175,28 @@
* @return list of child elements, never null
*/
@Nonnull public static List<Element> getChildElements(@Nullable final Node root, @Nullable final QName name) {
- if (name == null || name == null) {
+ if (root == null || name == null) {
return Collections.emptyList();
}
return getChildElementsByTagNameNS(root, name.getNamespaceURI(), name.getLocalPart());
+ }
+
+ /**
+ * Returns the first child element of the supplied element with the supplied name if it exists. Otherwise null. See
+ * {@link #getChildElements(Node, QName)}.
+ *
+ * @param root element to parse child elements
+ * @param name name of the child elements to parse
+ *
+ * @return first child element or null
+ */
+ @Nullable public static Element getFirstChildElement(@Nullable final Node root, @Nullable final QName name) {
+ final List<Element> elements = ElementSupport.getChildElements(root, name);
+ if (elements.size() > 0) {
+ return elements.get(0);
+ }
+ return null;
}
/**
@@ -259,16 +277,16 @@
}
return null;
}
-
- /**
- * Gets the text content for this Element only. Whereas {@link Node#getTextContent()} will return all text for this
- * element and all children, this just grabs the text for this element (which may be spread over multiple lines).
+
+ /**
+ * Gets the text content for this Element only. Whereas {@link Node#getTextContent()} will return all text for this
+ * element and all children, this just grabs the text for this element (which may be spread over multiple lines).
*
* @param element The element to look at.
* @return The text content, or "" if there is none, never null.
*
*/
- @Nonnull public static String getElementContentAsString(@Nullable final Element element){
+ @Nonnull public static String getElementContentAsString(@Nullable final Element element) {
if (element == null) {
return "";
}
@@ -287,7 +305,7 @@
}
node = node.getNextSibling();
}
-
+
return builder.toString();
}
@@ -327,9 +345,10 @@
if (valueComponents.length == 1) {
result = QNameSupport.constructQName(element.lookupNamespaceURI(null), valueComponents[0], null);
} else if (valueComponents.length == 2) {
- result = QNameSupport.constructQName(element.lookupNamespaceURI(valueComponents[0]), valueComponents[1],
- valueComponents[0]);
- }
+ result =
+ QNameSupport.constructQName(element.lookupNamespaceURI(valueComponents[0]), valueComponents[1],
+ valueComponents[0]);
+ }
return result;
}
@@ -380,7 +399,7 @@
}
elements.add(e);
-
+
e = getNextSiblingElement(e);
}
More information about the commits
mailing list