//************************************************************************** // Filename : WsTokenHelper.java, WSSE Toolkit 2005 // // Author : Denis Pilipchuk // // Purpose : This class holds a reference to the document which // is used to generate XML for tokens. // // Disclaimer: This software is provided "as-is" and does not imply // any warranty or responsibility. //**************************************************************************** package wsse.Toolkit.Tokens; // W3C DOM includes import org.w3c.dom.Element; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Text; import wsse.Toolkit.Utils.Xml.XmlDocHelper; import wsse.Toolkit.Utils.Xml.XmlDocException; import wsse.Toolkit.Utils.Xml.XmlID; import wsse.Toolkit.Utils.Xml.XmlQName; import wsse.Toolkit.Utils.Config.ConfigException; import wsse.Toolkit.Utils.Config.ConfigHelper; public class WsTokenHelper { public static XmlDocHelper GetDocHelper(String docBuilderFactoryImpl) throws XmlDocException { XmlDocHelper docHelper = null; try { // Use default config file ConfigHelper cfgHelper = ConfigHelper.GetInstance(null); // Get the requested DocBuilderFactory implementation (null means default) docHelper = cfgHelper.GetXmlFactory().GetDocHelper(docBuilderFactoryImpl); } catch(ConfigException ce) { throw new XmlDocException("Error creating xml doc implementation: " + docBuilderFactoryImpl, ce); } return docHelper; } public WsTokenHelper(XmlDocHelper docHelper) { if (null == docHelper) { throw new NullPointerException("Null document helper passed: " + this); } m_document = docHelper.GetDocument(); } public WsTokenHelper() throws XmlDocException { this(WsTokenHelper.GetDocHelper(null)); } /////////////////////////////////////////////////////////////////////////////////////////////////// // Convenience instance methods - related to a particular document public Element CreateXmlElement(XmlQName qName) { return m_document.createElementNS(qName.GetNS(),qName.GetPrefixedName()); } public Element CreateXmlElement(XmlQName qName, String value) { assert null != value : "Element's value can't be null"; Text textNode = m_document.createTextNode(value); Element element = m_document.createElementNS(qName.GetNS(),qName.GetPrefixedName()); element.appendChild(textNode); return element; } public Element EnsureElementOwner(Element element) { assert null != element : "Passed element can't be null"; Document docChild = element.getOwnerDocument(); // Need to import passed XML Element, if it was created by another document Element verifiedElement = docChild == m_document ? element : (Element)m_document.importNode(element, true); return verifiedElement; } public Document GetTokenDocument() { return m_document; } /////////////////////////////////////////////////////////////////////////////////////////////////// // Convenience static methods - not related to a particular document public static Element FindXmlElement(Element source, XmlQName qname, XmlID id) { if (null == source || null == qname) { throw new NullPointerException("Name and source parameters should be provided for the search"); } // First, check the source node itself Element retElement = qname.IsMatch(source) ? source : null; if (null == retElement) { NodeList nodes = source.getElementsByTagNameNS(qname.GetNS(), qname.GetLocalName()); // If ID was provided, use it in search as well (strict comparison). // Otherwise, employ lax comparison by name/ns only, i.e. use the first match if (null != id) { for (int i = 0; null == retElement && i < nodes.getLength(); i++) { Element testNode = (Element) nodes.item(i); String idAttr = testNode.getAttributeNS(id.GetNS(), id.GetTag()); retElement = null != idAttr && idAttr.equals(id.GetValue()) ? testNode : null; } } else { retElement = nodes.getLength() > 0 ? (Element) nodes.item(0) : null; } } return retElement; } public static Element[] FindXmlElements(Element source, XmlQName qname) { if (null == source || null == qname) { throw new NullPointerException("All parameters should be provided for the search"); } // Try to find the matching children NodeList nodes = source.getElementsByTagNameNS(qname.GetNS(),qname.GetLocalName()); // Also check the source node itself boolean sourceMatch = qname.IsMatch(source); Element[] elements = new Element[nodes.getLength() + (sourceMatch ? 1 : 0)]; for (int i = 0; i < nodes.getLength(); i++) { elements[i] = (Element) nodes.item(i); } if (sourceMatch) { elements[nodes.getLength()] = source; } return elements; } private Document m_document; }