Author: anil.saldhana(a)jboss.com
Date: 2009-05-29 16:58:27 -0400 (Fri, 29 May 2009)
New Revision: 545
Modified:
identity-federation/trunk/jboss-identity-fed-core/src/main/java/org/jboss/identity/federation/core/saml/v2/util/DocumentUtil.java
Log:
new methods
Modified:
identity-federation/trunk/jboss-identity-fed-core/src/main/java/org/jboss/identity/federation/core/saml/v2/util/DocumentUtil.java
===================================================================
---
identity-federation/trunk/jboss-identity-fed-core/src/main/java/org/jboss/identity/federation/core/saml/v2/util/DocumentUtil.java 2009-05-29
20:56:55 UTC (rev 544)
+++
identity-federation/trunk/jboss-identity-fed-core/src/main/java/org/jboss/identity/federation/core/saml/v2/util/DocumentUtil.java 2009-05-29
20:58:27 UTC (rev 545)
@@ -42,10 +42,13 @@
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
+import javax.xml.xpath.XPathException;
+import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
@@ -55,8 +58,31 @@
* @since Jan 14, 2009
*/
public class DocumentUtil
-{
+{
+ private static Logger log = Logger.getLogger(DocumentUtil.class);
+
+
/**
+ * Check whether a node belongs to a document
+ * @param doc
+ * @param node
+ * @return
+ */
+ public static boolean containsNode(Document doc, Node node)
+ {
+ if(node.getNodeType() == Node.ELEMENT_NODE)
+ {
+ Element elem = (Element) node;
+ NodeList nl = doc.getElementsByTagNameNS(elem.getNamespaceURI(),
elem.getLocalName());
+ if(nl != null && nl.getLength() > 0)
+ return true;
+ else
+ return false;
+ }
+ throw new UnsupportedOperationException();
+ }
+
+ /**
* Create a new document
* @return
* @throws ParserConfigurationException
@@ -191,9 +217,92 @@
ByteArrayInputStream bis = new ByteArrayInputStream(baos.toByteArray());
return bis;
- }
+ }
/**
+ * Stream a DOM Node as a String
+ * @param node
+ * @return
+ * @throws TransformerFactoryConfigurationError
+ * @throws TransformerException
+ */
+ public static String getNodeAsString(Node node)
+ throws TransformerFactoryConfigurationError, TransformerException
+ {
+ Source source = new DOMSource(node);
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ Result streamResult = new StreamResult(baos);
+ // Write the DOM document to the stream
+ Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
+ transformer.transform(source, streamResult);
+
+ return new String(baos.toByteArray());
+ }
+
+ /**
+ * Given a document, return a Node with the given node name
+ * and an attribute with a particular attribute value
+ * @param document
+ * @param nsURI
+ * @param nodeName
+ * @param attributeName
+ * @param attributeValue
+ * @return
+ * @throws XPathException
+ * @throws TransformerFactoryConfigurationError
+ * @throws TransformerException
+ */
+ public static Node getNodeWithAttribute(Document document, final String nsURI,
+ String nodeName,
+ String attributeName, String attributeValue) throws XPathException,
+ TransformerFactoryConfigurationError, TransformerException
+ {
+ NodeList nl = document.getElementsByTagNameNS(nsURI, nodeName);
+ int len = nl != null ? nl.getLength() : 0;
+
+ for (int i = 0; i < len; i++)
+ {
+ Node n = nl.item(i);
+ if(n.getNodeType() != Node.ELEMENT_NODE)
+ continue;
+ Element el = (Element) n;
+ String attrValue = el.getAttributeNS(nsURI, attributeName);
+ if(attributeValue.equals(attrValue))
+ return el;
+ //Take care of attributes with null NS
+ attrValue = el.getAttribute(attributeName);
+ if(attributeValue.equals(attrValue))
+ return el;
+ }
+ return null;
+ }
+
+ /**
+ * Log the nodes in the document
+ * @param doc
+ */
+ public static void logNodes(Document doc)
+ {
+ visit(doc, 0);
+ }
+
+ private static void visit(Node node, int level)
+ {
+ // Visit each child
+ NodeList list = node.getChildNodes();
+ for (int i=0; i<list.getLength(); i++)
+ {
+ // Get child node
+ Node childNode = list.item(i);
+ log.trace("Node="+ childNode.getNamespaceURI()+
"::"+childNode.getLocalName());
+ // Visit child node
+ visit(childNode, level+1);
+ }
+ }
+
+ /**
* Create a namespace aware Document builder factory
* @return
*/
Show replies by date