List of usage examples for org.w3c.dom Node getNamespaceURI
public String getNamespaceURI();
null
if it is unspecified (see ). From source file:Main.java
/** * Checks if two Nodes are Similar<br> * <p/>//from w w w .j ava 2 s . co m * Compares Nodes just by their Name, Type, local name and Namespace generically */ public static boolean nodesSimilar(Node node1, Node node2, boolean ignoreWhitespace) { if ((node1 == null) || (node2 == null)) return false; /* if (node1.getNodeType() == node2.getNodeType() && (areNullorEqual(node1.getLocalName(), node2.getLocalName(), ignoreWhitespace, false)) && (areNullorEqual(node1.getNamespaceURI(), node2.getNamespaceURI(), ignoreWhitespace, false)) && (areNullorEqual(node1.getNodeName(), node2.getNodeName(), ignoreWhitespace, false))) return true; */ if (areNonNullAndEqual(node1.getNamespaceURI(), node2.getNamespaceURI())) { if (node1.getNodeType() == node2.getNodeType() && (areNullorEqual(node1.getLocalName(), node2.getLocalName(), ignoreWhitespace, false))) return true; } else if ((node1.getNamespaceURI() == null) && (node2.getNamespaceURI() == null)) { if (node1.getNodeType() == node2.getNodeType() && (areNullorEqual(node1.getNodeName(), node2.getNodeName(), ignoreWhitespace, false))) return true; } return false; }
From source file:com.ibm.soatf.component.soap.builder.SchemaUtils.java
/** * Returns a map mapping urls to corresponding XmlSchema XmlObjects for the * specified wsdlUrl// www . ja v a 2s . com */ public static void getSchemas(final String wsdlUrl, Map<String, XmlObject> existing, SchemaLoader loader, String tns) { if (existing.containsKey(wsdlUrl)) { return; } log.debug("Getting schema " + wsdlUrl); ArrayList<?> errorList = new ArrayList<>(); Map<String, XmlObject> result = new HashMap<>(); boolean common = false; try { XmlOptions options = new XmlOptions(); options.setCompileNoValidation(); options.setSaveUseOpenFrag(); options.setErrorListener(errorList); options.setSaveSyntheticDocumentElement(new QName(Constants.XSD_NS, "schema")); XmlObject xmlObject = loader.loadXmlObject(wsdlUrl, options); if (xmlObject == null) throw new Exception("Failed to load schema from [" + wsdlUrl + "]"); Document dom = (Document) xmlObject.getDomNode(); Node domNode = dom.getDocumentElement(); // is this an xml schema? if (domNode.getLocalName().equals("schema") && Constants.XSD_NS.equals(domNode.getNamespaceURI())) { // set targetNamespace (this happens if we are following an include // statement) if (tns != null) { Element elm = ((Element) domNode); if (!elm.hasAttribute("targetNamespace")) { common = true; elm.setAttribute("targetNamespace", tns); } // check for namespace prefix for targetNamespace NamedNodeMap attributes = elm.getAttributes(); int c = 0; for (; c < attributes.getLength(); c++) { Node item = attributes.item(c); if (item.getNodeName().equals("xmlns")) break; if (item.getNodeValue().equals(tns) && item.getNodeName().startsWith("xmlns")) break; } if (c == attributes.getLength()) elm.setAttribute("xmlns", tns); } if (common && !existing.containsKey(wsdlUrl + "@" + tns)) result.put(wsdlUrl + "@" + tns, xmlObject); else result.put(wsdlUrl, xmlObject); } else { existing.put(wsdlUrl, null); XmlObject[] schemas = xmlObject .selectPath("declare namespace s='" + Constants.XSD_NS + "' .//s:schema"); for (int i = 0; i < schemas.length; i++) { XmlCursor xmlCursor = schemas[i].newCursor(); String xmlText = xmlCursor.getObject().xmlText(options); // schemas[i] = XmlObject.Factory.parse( xmlText, options ); schemas[i] = XmlUtils.createXmlObject(xmlText, options); schemas[i].documentProperties().setSourceName(wsdlUrl); result.put(wsdlUrl + "@" + (i + 1), schemas[i]); } XmlObject[] wsdlImports = xmlObject .selectPath("declare namespace s='" + Constants.WSDL11_NS + "' .//s:import/@location"); for (int i = 0; i < wsdlImports.length; i++) { String location = ((SimpleValue) wsdlImports[i]).getStringValue(); if (location != null) { if (!location.startsWith("file:") && location.indexOf("://") == -1) location = joinRelativeUrl(wsdlUrl, location); getSchemas(location, existing, loader, null); } } XmlObject[] wadl10Imports = xmlObject.selectPath( "declare namespace s='" + Constants.WADL10_NS + "' .//s:grammars/s:include/@href"); for (int i = 0; i < wadl10Imports.length; i++) { String location = ((SimpleValue) wadl10Imports[i]).getStringValue(); if (location != null) { if (!location.startsWith("file:") && location.indexOf("://") == -1) location = joinRelativeUrl(wsdlUrl, location); getSchemas(location, existing, loader, null); } } XmlObject[] wadlImports = xmlObject.selectPath( "declare namespace s='" + Constants.WADL11_NS + "' .//s:grammars/s:include/@href"); for (int i = 0; i < wadlImports.length; i++) { String location = ((SimpleValue) wadlImports[i]).getStringValue(); if (location != null) { if (!location.startsWith("file:") && location.indexOf("://") == -1) location = joinRelativeUrl(wsdlUrl, location); getSchemas(location, existing, loader, null); } } } existing.putAll(result); XmlObject[] schemas = result.values().toArray(new XmlObject[result.size()]); for (int c = 0; c < schemas.length; c++) { xmlObject = schemas[c]; XmlObject[] schemaImports = xmlObject .selectPath("declare namespace s='" + Constants.XSD_NS + "' .//s:import/@schemaLocation"); for (int i = 0; i < schemaImports.length; i++) { String location = ((SimpleValue) schemaImports[i]).getStringValue(); Element elm = ((Attr) schemaImports[i].getDomNode()).getOwnerElement(); if (location != null && !defaultSchemas.containsKey(elm.getAttribute("namespace"))) { if (!location.startsWith("file:") && location.indexOf("://") == -1) location = joinRelativeUrl(wsdlUrl, location); getSchemas(location, existing, loader, null); } } XmlObject[] schemaIncludes = xmlObject .selectPath("declare namespace s='" + Constants.XSD_NS + "' .//s:include/@schemaLocation"); for (int i = 0; i < schemaIncludes.length; i++) { String location = ((SimpleValue) schemaIncludes[i]).getStringValue(); if (location != null) { String targetNS = getTargetNamespace(xmlObject); if (!location.startsWith("file:") && location.indexOf("://") == -1) location = joinRelativeUrl(wsdlUrl, location); getSchemas(location, existing, loader, targetNS); } } } } catch (Exception e) { throw new SoapBuilderException(e); } }
From source file:com.centeractive.ws.legacy.SchemaUtils.java
/** * Returns a map mapping urls to corresponding XmlSchema XmlObjects for the specified wsdlUrl *///w ww. java 2s . c o m public static void getSchemas(String wsdlUrl, Map<String, XmlObject> existing, SchemaLoader loader, String tns) { if (existing.containsKey(wsdlUrl)) { return; } log.debug("Getting schema " + wsdlUrl); ArrayList<?> errorList = new ArrayList<Object>(); Map<String, XmlObject> result = new HashMap<String, XmlObject>(); boolean common = false; try { XmlOptions options = new XmlOptions(); options.setCompileNoValidation(); options.setSaveUseOpenFrag(); options.setErrorListener(errorList); options.setSaveSyntheticDocumentElement(new QName(Constants.XSD_NS, "schema")); XmlObject xmlObject = loader.loadXmlObject(wsdlUrl, options); if (xmlObject == null) { throw new Exception("Failed to load schema from [" + wsdlUrl + "]"); } Document dom = (Document) xmlObject.getDomNode(); Node domNode = dom.getDocumentElement(); // is this an xml schema? if (domNode.getLocalName().equals("schema") && Constants.XSD_NS.equals(domNode.getNamespaceURI())) { // set targetNamespace (this happens if we are following an include // statement) if (tns != null) { Element elm = ((Element) domNode); if (!elm.hasAttribute("targetNamespace")) { common = true; elm.setAttribute("targetNamespace", tns); } // check for namespace prefix for targetNamespace NamedNodeMap attributes = elm.getAttributes(); int c = 0; for (; c < attributes.getLength(); c++) { Node item = attributes.item(c); if (item.getNodeName().equals("xmlns")) { break; } if (item.getNodeValue().equals(tns) && item.getNodeName().startsWith("xmlns")) { break; } } if (c == attributes.getLength()) { elm.setAttribute("xmlns", tns); } } if (common && !existing.containsKey(wsdlUrl + "@" + tns)) { result.put(wsdlUrl + "@" + tns, xmlObject); } else { result.put(wsdlUrl, xmlObject); } } else { existing.put(wsdlUrl, null); XmlObject[] schemas = xmlObject .selectPath("declare namespace s='" + Constants.XSD_NS + "' .//s:schema"); for (int i = 0; i < schemas.length; i++) { XmlCursor xmlCursor = schemas[i].newCursor(); String xmlText = xmlCursor.getObject().xmlText(options); // schemas[i] = XmlObject.Factory.parse( xmlText, options ); schemas[i] = XmlUtils.createXmlObject(xmlText, options); schemas[i].documentProperties().setSourceName(wsdlUrl); result.put(wsdlUrl + "@" + (i + 1), schemas[i]); } XmlObject[] wsdlImports = xmlObject .selectPath("declare namespace s='" + Constants.WSDL11_NS + "' .//s:import/@location"); for (int i = 0; i < wsdlImports.length; i++) { String location = ((SimpleValue) wsdlImports[i]).getStringValue(); if (location != null) { if (!location.startsWith("file:") && location.indexOf("://") == -1) { location = joinRelativeUrl(wsdlUrl, location); } getSchemas(location, existing, loader, null); } } XmlObject[] wadl10Imports = xmlObject.selectPath( "declare namespace s='" + Constants.WADL10_NS + "' .//s:grammars/s:include/@href"); for (int i = 0; i < wadl10Imports.length; i++) { String location = ((SimpleValue) wadl10Imports[i]).getStringValue(); if (location != null) { if (!location.startsWith("file:") && location.indexOf("://") == -1) { location = joinRelativeUrl(wsdlUrl, location); } getSchemas(location, existing, loader, null); } } XmlObject[] wadlImports = xmlObject.selectPath( "declare namespace s='" + Constants.WADL11_NS + "' .//s:grammars/s:include/@href"); for (int i = 0; i < wadlImports.length; i++) { String location = ((SimpleValue) wadlImports[i]).getStringValue(); if (location != null) { if (!location.startsWith("file:") && location.indexOf("://") == -1) { location = joinRelativeUrl(wsdlUrl, location); } getSchemas(location, existing, loader, null); } } } existing.putAll(result); XmlObject[] schemas = result.values().toArray(new XmlObject[result.size()]); for (int c = 0; c < schemas.length; c++) { xmlObject = schemas[c]; XmlObject[] schemaImports = xmlObject .selectPath("declare namespace s='" + Constants.XSD_NS + "' .//s:import/@schemaLocation"); for (int i = 0; i < schemaImports.length; i++) { String location = ((SimpleValue) schemaImports[i]).getStringValue(); Element elm = ((Attr) schemaImports[i].getDomNode()).getOwnerElement(); if (location != null && !defaultSchemas.containsKey(elm.getAttribute("namespace"))) { if (!location.startsWith("file:") && location.indexOf("://") == -1) { location = joinRelativeUrl(wsdlUrl, location); } getSchemas(location, existing, loader, null); } } XmlObject[] schemaIncludes = xmlObject .selectPath("declare namespace s='" + Constants.XSD_NS + "' .//s:include/@schemaLocation"); for (int i = 0; i < schemaIncludes.length; i++) { String location = ((SimpleValue) schemaIncludes[i]).getStringValue(); if (location != null) { String targetNS = getTargetNamespace(xmlObject); if (!location.startsWith("file:") && location.indexOf("://") == -1) { location = joinRelativeUrl(wsdlUrl, location); } getSchemas(location, existing, loader, targetNS); } } } } catch (Exception e) { throw new SoapBuilderException(e); } }
From source file:eu.semaine.util.XMLTool.java
/** * Get the direct child of node that is an element with the given * tag name and namespace.//w ww. ja v a 2 s . com * @param node * @param childName * @param childNamespace * @return the child element, or null if there is no such child. */ public static Element getChildElementByTagNameNS(Node node, String childName, String childNamespace) { NodeList nl = node.getChildNodes(); for (int i = 0, max = nl.getLength(); i < max; i++) { Node n = nl.item(i); if (n instanceof Element && n.getNodeName().equals(childName) && n.getNamespaceURI().equals(childNamespace)) { return (Element) n; } } return null; }
From source file:eu.semaine.util.XMLTool.java
/** * Get the direct child of node that is an element with the given * local name and namespace./* ww w .j a va 2s . com*/ * @param node * @param childName the child's local name, without a namespace prefix. * @param childNamespace * @return the child element, or null if there is no such child. */ public static Element getChildElementByLocalNameNS(Node node, String childName, String childNamespace) { NodeList nl = node.getChildNodes(); for (int i = 0, max = nl.getLength(); i < max; i++) { Node n = nl.item(i); if (n instanceof Element && n.getLocalName().equals(childName) && n.getNamespaceURI().equals(childNamespace)) { return (Element) n; } } return null; }
From source file:cz.muni.fi.mir.mathmlunificator.MathMLUnificator.java
/** * <p>//from w w w . jav a 2 s .c o m * Test whether the given node is unified math node. The node is considered * to be unified math node if * </p> * <ol> * <li>the node is not <code>null</code>,</li> * <li>the node is math node,</li> * <li>the node has current unification level attribute (see * {@link Constants#UNIFIED_MATHML_LEVEL_ATTR}) in MathML Unification XML * namespace (see {@link Constants#UNIFIED_MATHML_NS}) with valid value, * i.e. integer greather than 0.</li> * </ol> * <p> * The method is XML namespace aware and expects the node DOM to be build as * XML aware (see * {@link javax.xml.parsers.DocumentBuilderFactory#setNamespaceAware(boolean)}). * </p> * <p> * In case the input DOM was created as namespace unaware or the input XML * document does not correctly use namespaces the method tries to fall back * to element-plain-name-only math node detection. The element is considered * to be math element if * <ul> * <li>the element is in MathML namespace XML namespace * {@code http://www.w3.org/1998/Math/MathML} (see * {@link Constants#MATHML_NS}),</li> * <li>or the local name of the element is {@code <math>} (see * {@link Constants#UNIFIED_MATHML_ROOT_ELEM}) without any namespace * definition. * </ul> * </p> * * @param node The node to test. * @return <code>true</code> if the above description is fulfilled, * <code>false</code> otherwise. */ public static boolean isUnifiedMathNode(Node node) { // Test the nodes is element if (node != null && node.getNodeType() == Node.ELEMENT_NODE) { // Test the node is math node in MathML namesapce or at least with corrent name if no namesapce is defined at all if ((node.getNamespaceURI() == null && node.getNodeName().equals(MATHML_ROOT_ELEM)) || (node.getNamespaceURI() != null && node.getNamespaceURI().equals(MATHML_NS))) { // Test presence of unification level attribute Node uniLevel = node.getAttributes().getNamedItemNS(UNIFIED_MATHML_NS, UNIFIED_MATHML_LEVEL_ATTR); if (uniLevel != null && uniLevel.getNodeType() == Node.ATTRIBUTE_NODE) { Integer value; try { value = Integer.parseInt(((Attr) uniLevel).getTextContent()); } catch (NumberFormatException ex) { return false; } if (value >= 0) { return true; } } } } return false; }
From source file:eu.semaine.util.XMLTool.java
/** * Get a list of all direct children with the given tag name and namespace. * Whereas getChildElementByTagNameNS() returns the single first child, * this method returns all the children that match. * @param node//from w w w. ja v a 2 s. c o m * @param childName * @param childNamespace * @return a list containing the children that match, or an empty list if none match. * @throws MessageFormatException */ public static List<Element> getChildrenByTagNameNS(Node node, String childName, String childNamespace) { List<Element> list = new ArrayList<Element>(); Element e = getChildElementByTagNameNS(node, childName, childNamespace); if (e != null) { list.add(e); Node n = e; while ((n = n.getNextSibling()) != null) { if (n.getNodeType() == Node.ELEMENT_NODE && isSameNamespace(n.getNamespaceURI(), childNamespace) && n.getNodeName().equals(childName)) { list.add((Element) n); } } } return list; }
From source file:eu.semaine.util.XMLTool.java
/** * Get a list of all direct children with the given local name and namespace. * Whereas getChildElementByTagNameNS() returns the single first child, * this method returns all the children that match. * @param node/* w w w .ja v a 2s . c o m*/ * @param childName the child's local name * @param childNamespace * @return a list containing the children that match, or an empty list if none match. * @throws MessageFormatException */ public static List<Element> getChildrenByLocalNameNS(Node node, String childName, String childNamespace) { List<Element> list = new ArrayList<Element>(); Element e = getChildElementByLocalNameNS(node, childName, childNamespace); if (e != null) { list.add(e); Node n = e; while ((n = n.getNextSibling()) != null) { if (n.getNodeType() == Node.ELEMENT_NODE && isSameNamespace(n.getNamespaceURI(), childNamespace) && n.getLocalName().equals(childName)) { list.add((Element) n); } } } return list; }
From source file:be.fedict.eid.dss.spi.utils.XAdESUtils.java
/** * Finds a XAdES named unsigned signature property as DOM element. * <p/>/*from w w w. j a va 2 s .co m*/ * Working DOM based is required if you want to be able to find the next * sibling at the DOM level. JAXB does not expose this properly. * * @param qualifyingPropertiesElement * the XAdES qualifying properties DOM element. * @param localName * @return * @throws XAdESValidationException */ public static Element findUnsignedSignaturePropertyElement(Element qualifyingPropertiesElement, String localName) throws XAdESValidationException { NodeList unsignedSignaturePropertiesNodeList = qualifyingPropertiesElement .getElementsByTagNameNS(XAdESUtils.XADES_132_NS_URI, "UnsignedSignatureProperties"); if (unsignedSignaturePropertiesNodeList.getLength() == 0) { throw new XAdESValidationException("UnsignedSignatureProperties node not present"); } Node unsignedSignaturePropertiesNode = unsignedSignaturePropertiesNodeList.item(0); NodeList childNodes = unsignedSignaturePropertiesNode.getChildNodes(); int childNodesCount = childNodes.getLength(); for (int idx = 0; idx < childNodesCount; idx++) { Node childNode = childNodes.item(idx); if (Node.ELEMENT_NODE != childNode.getNodeType()) { continue; } Element childElement = (Element) childNode; if (!XAdESUtils.XADES_132_NS_URI.equals(childNode.getNamespaceURI())) { continue; } String actualLocalName = childNode.getLocalName(); if (localName.equals(actualLocalName)) { return childElement; } } return null; }
From source file:Main.java
/** * Convert the given Node to an XML String. * <p>/*from ww w . j ava2 s.c o m*/ * This method is a simplified version of... * <p> * <code> * ByteArrayOutputStream out = new ByteArrayOutputStream();<br/> * javax.xml.Transformer transformer = TransformerFactory.newInstance().newTransformer();<br/> * transformer.transform( new DOMSource( node ), new StreamResult( out ));<br/> * return out.toString(); * </code> * <p> * ...but not all platforms (eg. Android) support <code>javax.xml.transform.Transformer</code>. * * @param indent * how much to indent the output. -1 for no indent. */ private static String nodeToString(Node node, int indent) { // Text nodes if (node == null) { return null; } if (!(node instanceof Element)) { String value = node.getNodeValue(); if (value == null) { return null; } return escapeForXml(value.trim()); } // (use StringBuffer for J2SE 1.4 compatibility) StringBuffer buffer = new StringBuffer(); // Open tag indent(buffer, indent); String nodeName = escapeForXml(node.getNodeName()); buffer.append("<"); buffer.append(nodeName); // Changing namespace String namespace = node.getNamespaceURI(); Node parentNode = node.getParentNode(); if (namespace != null && (parentNode == null || !namespace.equals(parentNode.getNamespaceURI()))) { buffer.append(" xmlns=\""); buffer.append(namespace); buffer.append("\""); } // Attributes NamedNodeMap attributes = node.getAttributes(); // Always put name first for easy unit tests Node name = attributes.getNamedItem("name"); if (name != null) { buffer.append(" name=\""); buffer.append(escapeForXml(name.getNodeValue())); buffer.append("\""); } for (int loop = 0; loop < attributes.getLength(); loop++) { Node attribute = attributes.item(loop); String attributeName = attribute.getNodeName(); // (I'm a bit surprised xmlns is an attribute - is that a bug?) if ("xmlns".equals(attributeName)) { continue; } // (always put name first for easy unit tests) if ("name".equals(attributeName)) { continue; } buffer.append(" "); buffer.append(escapeForXml(attributeName)); buffer.append("=\""); buffer.append(escapeForXml(attribute.getNodeValue())); buffer.append("\""); } // Children (if any) NodeList children = node.getChildNodes(); int length = children.getLength(); if (length == 0) { buffer.append("/>"); } else { buffer.append(">"); int nextIndent = indent; if (indent != -1) { nextIndent++; } for (int loop = 0; loop < length; loop++) { Node childNode = children.item(loop); if (indent != -1 && childNode instanceof Element) { buffer.append("\n"); } buffer.append(nodeToString(childNode, nextIndent)); } if (indent != -1 && buffer.charAt(buffer.length() - 1) == '>') { buffer.append("\n"); indent(buffer, indent); } // Close tag buffer.append("</"); buffer.append(nodeName); buffer.append(">"); } return buffer.toString(); }