List of usage examples for org.w3c.dom NamedNodeMap item
public Node item(int index);
index
th item in the map. From source file:com.example.soaplegacy.XmlUtils.java
public static String removeUnneccessaryNamespaces(String xml) { if (StringUtils.isBlank(xml)) { return xml; }/*from w w w. j ava 2 s .c om*/ XmlObject xmlObject = null; XmlCursor cursor = null; try { xmlObject = XmlObject.Factory.parse(xml); cursor = xmlObject.newCursor(); while (cursor.currentTokenType() != XmlCursor.TokenType.START && cursor.currentTokenType() != XmlCursor.TokenType.ENDDOC) { cursor.toNextToken(); } if (cursor.currentTokenType() == XmlCursor.TokenType.START) { Map<?, ?> nsMap = new HashMap<Object, Object>(); cursor.getAllNamespaces(nsMap); nsMap.remove(cursor.getDomNode().getPrefix()); NamedNodeMap attributes = cursor.getDomNode().getAttributes(); for (int c = 0; attributes != null && c < attributes.getLength(); c++) { nsMap.remove(attributes.item(c).getPrefix()); } if (cursor.toFirstChild()) { while (cursor.getDomNode() != xmlObject.getDomNode()) { attributes = cursor.getDomNode().getAttributes(); for (int c = 0; attributes != null && c < attributes.getLength(); c++) { nsMap.remove(attributes.item(c).getPrefix()); } nsMap.remove(cursor.getDomNode().getPrefix()); cursor.toNextToken(); } } xml = xmlObject.xmlText( new XmlOptions().setSaveOuter().setSavePrettyPrint().setSaveImplicitNamespaces(nsMap)); } } catch (XmlException e) { } finally { if (cursor != null) cursor.dispose(); } return xml; }
From source file:Main.java
public static void assertEquivalent(Node node, Node node2) { if (node == null) { throw new IllegalArgumentException("the first node to be compared is null"); }/*from w w w . ja va 2s . c om*/ if (node2 == null) { throw new IllegalArgumentException("the second node to be compared is null"); } if (!node.getNodeName().equals(node2.getNodeName())) { throw new IllegalArgumentException("nodes have different node names"); } int attrCount = 0; NamedNodeMap attrs = node.getAttributes(); if (attrs != null) { attrCount = attrs.getLength(); } int attrCount2 = 0; NamedNodeMap attrs2 = node2.getAttributes(); if (attrs2 != null) { attrCount2 = attrs2.getLength(); } if (attrCount != attrCount2) { throw new IllegalArgumentException("nodes hava a different number of attributes"); } outer: for (int i = 0; i < attrCount; i++) { Node n = attrs.item(i); String name = n.getNodeName(); String value = n.getNodeValue(); for (int j = 0; j < attrCount; j++) { Node n2 = attrs2.item(j); String name2 = n2.getNodeName(); String value2 = n2.getNodeValue(); if (name.equals(name2) && value.equals(value2)) { continue outer; } } throw new IllegalArgumentException("attribute " + name + "=" + value + " doesn't match"); } boolean hasChildren = node.hasChildNodes(); if (hasChildren != node2.hasChildNodes()) { throw new IllegalArgumentException("one node has children and the other doesn't"); } if (hasChildren) { NodeList nl = node.getChildNodes(); NodeList nl2 = node2.getChildNodes(); short[] toFilter = new short[] { Node.TEXT_NODE, Node.ATTRIBUTE_NODE, Node.COMMENT_NODE }; List nodes = filter(nl, toFilter); List nodes2 = filter(nl2, toFilter); int length = nodes.size(); if (length != nodes2.size()) { throw new IllegalArgumentException("nodes hava a different number of children"); } for (int i = 0; i < length; i++) { Node n = (Node) nodes.get(i); Node n2 = (Node) nodes2.get(i); assertEquivalent(n, n2); } } }
From source file:com.wavemaker.tools.pws.install.PwsInstall.java
private static Document insertEntryKey(Document doc, File[] runtimeJarFiles, File[] toolsJarFiles, String partnerName) throws IOException { NodeList beans_list = doc.getElementsByTagName("beans"); Node beans_node = beans_list.item(0); // First delete old entries NodeList beansChildren = beans_node.getChildNodes(); Node bean_node = null, prop_node = null, map_node = null; for (int i = 0; i < beansChildren.getLength(); i++) { if (!beansChildren.item(i).getNodeName().equals("bean")) { continue; }/*ww w . j av a 2 s . co m*/ bean_node = beansChildren.item(i); NodeList beanChildren = bean_node.getChildNodes(); for (int j = 0; j < beanChildren.getLength(); j++) { if (!beanChildren.item(j).getNodeName().equals("property")) { continue; } prop_node = beanChildren.item(j); break; } if (prop_node == null) { continue; } NodeList propChildren = prop_node.getChildNodes(); for (int k = 0; k < propChildren.getLength(); k++) { if (!propChildren.item(k).getNodeName().equals("map")) { continue; } map_node = propChildren.item(k); break; } if (map_node == null) { continue; } NodeList mapChildren = map_node.getChildNodes(); List<Node> oldEntryList = new ArrayList<Node>(); for (int l = 0; l < mapChildren.getLength(); l++) { if (!mapChildren.item(l).getNodeName().equals("entry")) { continue; } Node target = mapChildren.item(l); NamedNodeMap entry_attributes = target.getAttributes(); for (int m = 0; m < entry_attributes.getLength(); m++) { Node entryAttr = entry_attributes.item(m); if (entryAttr.getNodeName().equals("key") && entryAttr.getNodeValue().equals(partnerName)) { oldEntryList.add(target); break; } } } if (oldEntryList.size() > 0) { for (Node oldEntry : oldEntryList) { map_node.removeChild(oldEntry); } } // Now, add new entries NamedNodeMap bean_attributes = bean_node.getAttributes(); for (int m = 0; m < bean_attributes.getLength(); m++) { Node beanAttr = bean_attributes.item(m); if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsLoginManagerBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, LOGIN_MANAGER) || classExistsInJar(toolsJarFiles, partnerName, LOGIN_MANAGER)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + LOGIN_MANAGER); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsRestImporterBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, REST_IMPORTER) || classExistsInJar(toolsJarFiles, partnerName, REST_IMPORTER)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + REST_IMPORTER); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsRestWsdlGeneratorBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, REST_WSDL_GENERATOR) || classExistsInJar(toolsJarFiles, partnerName, REST_WSDL_GENERATOR)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + REST_WSDL_GENERATOR); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsServiceModifierBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, SERVICE_MODIFIER) || classExistsInJar(toolsJarFiles, partnerName, SERVICE_MODIFIER)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + SERVICE_MODIFIER); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsRestServiceGeneratorBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, REST_SERVICE_IMPORTER) || classExistsInJar(toolsJarFiles, partnerName, REST_SERVICE_IMPORTER)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + REST_SERVICE_IMPORTER); map_node.appendChild(newEntry); } break; } else if (beanAttr.getNodeName().equals("id") && beanAttr.getNodeValue().equals("pwsResponseProcessorBeanFactory")) { if (classExistsInJar(runtimeJarFiles, partnerName, RESPONSE_PROCESSOR) || classExistsInJar(toolsJarFiles, partnerName, RESPONSE_PROCESSOR)) { Element newEntry = doc.createElement("entry"); newEntry.setAttribute("key", partnerName); newEntry.setAttribute("value-ref", partnerName + RESPONSE_PROCESSOR); map_node.appendChild(newEntry); } break; } } } return doc; }
From source file:Main.java
/** * Convert the given Node to an XML String. * <p>/*from w w w . j ava 2s . 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(); }
From source file:com.esri.geoevent.solutions.adapter.cot.CoTUtilities.java
private static ArrayList<CoTTypeDef> typeBreakdown(Node n) { ArrayList<CoTTypeDef> hash = new ArrayList<CoTTypeDef>(); try {//w w w. j a v a 2s.c o m String name = n.getNodeName(); if (name.startsWith("#")) { // no match here return new ArrayList<CoTTypeDef>(); } StringBuffer sb = new StringBuffer(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { if (attrs.getLength() >= 3 && name.equals("cot")) { String zero = attrs.item(0).getNodeName(); String one = attrs.item(1).getNodeName(); String two = attrs.item(2).getNodeName(); // for some reason the attributes are not coming back in // order. make sure we get the ones we want int k = 0; int v = 1; if (zero.equals("cot")) { k = 0; } else if (one.equals("cot")) { k = 1; } else if (two.equals("cot")) { k = 2; } if (zero.equals("desc")) { v = 0; } else if (one.equals("desc")) { v = 1; } else if (two.equals("desc")) { v = 2; } hash.add(new CoTTypeDef("^" + attrs.item(k).getNodeValue() + "$", attrs.item(v).getNodeValue(), false)); } else if (attrs.getLength() == 2 && name.equals("cot")) { String zero = attrs.item(0).getNodeName(); // make sure we are grabbing the elements in the right order int k = 0; int v = 1; if (zero.equals("cot")) { k = 0; v = 1; } else { k = 1; v = 0; } hash.add(new CoTTypeDef("^" + attrs.item(k).getNodeValue() + "$", attrs.item(v).getNodeValue(), false)); } else if (attrs.getLength() == 2 && name.equals("weapon")) { String zero = attrs.item(0).getNodeName(); // make sure we are grabbing the elements in the right order int k = 0; int v = 1; if (zero.equals("cot")) { k = 0; v = 1; } else { k = 1; v = 0; } hash.add(new CoTTypeDef("^" + attrs.item(k).getNodeValue() + "$", attrs.item(v).getNodeValue(), false)); } else if (attrs.getLength() == 2 && name.equals("relation")) { String zero = attrs.item(0).getNodeName(); // make sure we are grabbing the elements in the right order int k = 0; int v = 1; if (zero.equals("cot")) { k = 0; v = 1; } else { k = 1; v = 0; } hash.add(new CoTTypeDef("^" + attrs.item(k).getNodeValue() + "$", attrs.item(v).getNodeValue(), false)); } else if (attrs.getLength() == 2 && name.equals("how")) { String zero = attrs.item(0).getNodeName(); // make sure we are grabbing the elements in the right order int k = 0; int v = 1; if (zero.equals("value")) { k = 0; v = 1; } else { k = 1; v = 0; } hash.add(new CoTTypeDef("^" + attrs.item(k).getNodeValue() + "$", attrs.item(v).getNodeValue(), false)); } else if (attrs.getLength() == 2 && name.equals("is")) { String zero = attrs.item(0).getNodeName(); // make sure we are grabbing the elements in the right order int k = 0; int v = 1; if (zero.equals("match")) { k = 0; v = 1; } else { k = 1; v = 0; } String s = attrs.item(v).getNodeValue(); if (!(s.equals("true") || s.equals("false") || s.equals("spare") || s.equals("any") || s.equals("atoms"))) { hash.add(new CoTTypeDef(attrs.item(k).getNodeValue(), attrs.item(v).getNodeValue(), true)); } } else if (attrs.getLength() == 2 && name.equals("how")) { String zero = attrs.item(0).getNodeName(); // make sure we are grabbing the elements in the right order int k = 0; int v = 1; if (zero.equals("value")) { k = 0; v = 1; } else { k = 1; v = 0; } String s = attrs.item(v).getNodeValue(); if (!(s.equals("true") || s.equals("false") || s.equals("spare") || s.equals("any") || s.equals("atoms"))) { hash.add(new CoTTypeDef(attrs.item(k).getNodeValue(), attrs.item(v).getNodeValue(), false)); } } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) { } else { } } else { boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { ArrayList<CoTTypeDef> childHash = typeBreakdown(children.item(i)); if (childHash.size() > 0) { hash.addAll(childHash); hasValidChildren = true; } } if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) { } } return hash; } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return hash; } }
From source file:microsoft.exchange.webservices.data.core.EwsServiceXmlWriter.java
/** * @param element DOM element/*from www . ja va 2 s . co m*/ * @param writer XML stream writer * @throws XMLStreamException the XML stream exception */ public static void addElement(Element element, XMLStreamWriter writer) throws XMLStreamException { String nameSpace = element.getNamespaceURI(); String prefix = element.getPrefix(); String localName = element.getLocalName(); if (prefix == null) { prefix = ""; } if (localName == null) { localName = element.getNodeName(); if (localName == null) { throw new IllegalStateException("Element's local name cannot be null!"); } } String decUri = writer.getNamespaceContext().getNamespaceURI(prefix); boolean declareNamespace = decUri == null || !decUri.equals(nameSpace); if (nameSpace == null || nameSpace.length() == 0) { writer.writeStartElement(localName); } else { writer.writeStartElement(prefix, localName, nameSpace); } NamedNodeMap attrs = element.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); String name = attr.getNodeName(); String attrPrefix = ""; int prefixIndex = name.indexOf(':'); if (prefixIndex != -1) { attrPrefix = name.substring(0, prefixIndex); name = name.substring(prefixIndex + 1); } if ("xmlns".equals(attrPrefix)) { writer.writeNamespace(name, attr.getNodeValue()); if (name.equals(prefix) && attr.getNodeValue().equals(nameSpace)) { declareNamespace = false; } } else { if ("xmlns".equals(name) && "".equals(attrPrefix)) { writer.writeNamespace("", attr.getNodeValue()); if (attr.getNodeValue().equals(nameSpace)) { declareNamespace = false; } } else { writer.writeAttribute(attrPrefix, attr.getNamespaceURI(), name, attr.getNodeValue()); } } } if (declareNamespace) { if (nameSpace == null) { writer.writeNamespace(prefix, ""); } else { writer.writeNamespace(prefix, nameSpace); } } NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node n = nodes.item(i); writeNode(n, writer); } writer.writeEndElement(); }
From source file:Main.java
private static void renderNode(StringBuffer sb, Node node) { if (node == null) { sb.append("null"); return;// www . j a v a2 s . c o m } switch (node.getNodeType()) { case Node.DOCUMENT_NODE: sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); Node root = ((Document) node).getDocumentElement(); renderNode(sb, root); break; case Node.ELEMENT_NODE: String name = getNodeNameWithNamespace(node); NamedNodeMap attributes = node.getAttributes(); if (attributes.getLength() == 0) { sb.append("<" + name + ">"); } else { sb.append("<" + name + " "); int attrlen = attributes.getLength(); for (int i = 0; i < attrlen; i++) { Node attr = attributes.item(i); String attrName = getNodeNameWithNamespace(attr); sb.append(attrName + "=\"" + escapeChars(attr.getNodeValue())); if (i < attrlen - 1) sb.append("\" "); else sb.append("\">"); } } NodeList children = node.getChildNodes(); if (children != null) { for (int i = 0; i < children.getLength(); i++) { renderNode(sb, children.item(i)); } } sb.append("</" + name + ">"); break; case Node.TEXT_NODE: sb.append(escapeChars(node.getNodeValue())); break; case Node.CDATA_SECTION_NODE: sb.append("<![CDATA[" + node.getNodeValue() + "]]>"); break; case Node.PROCESSING_INSTRUCTION_NODE: sb.append("<?" + node.getNodeName() + " " + escapeChars(node.getNodeValue()) + "?>"); break; case Node.ENTITY_REFERENCE_NODE: sb.append("&" + node.getNodeName() + ";"); break; case Node.DOCUMENT_TYPE_NODE: // Ignore document type nodes break; case Node.COMMENT_NODE: sb.append("<!--" + node.getNodeValue() + "-->"); break; } return; }
From source file:DOMUtils.java
public static void compareNodes(Node expected, Node actual) throws Exception { if (expected.getNodeType() != actual.getNodeType()) { throw new Exception("Different types of nodes: " + expected + " " + actual); }/* w ww. ja v a 2s . c om*/ if (expected instanceof Document) { Document expectedDoc = (Document) expected; Document actualDoc = (Document) actual; compareNodes(expectedDoc.getDocumentElement(), actualDoc.getDocumentElement()); } else if (expected instanceof Element) { Element expectedElement = (Element) expected; Element actualElement = (Element) actual; // compare element names if (!expectedElement.getLocalName().equals(actualElement.getLocalName())) { throw new Exception("Element names do not match: " + expectedElement.getLocalName() + " " + actualElement.getLocalName()); } // compare element ns String expectedNS = expectedElement.getNamespaceURI(); String actualNS = actualElement.getNamespaceURI(); if ((expectedNS == null && actualNS != null) || (expectedNS != null && !expectedNS.equals(actualNS))) { throw new Exception("Element namespaces names do not match: " + expectedNS + " " + actualNS); } String elementName = "{" + expectedElement.getNamespaceURI() + "}" + actualElement.getLocalName(); // compare attributes NamedNodeMap expectedAttrs = expectedElement.getAttributes(); NamedNodeMap actualAttrs = actualElement.getAttributes(); if (countNonNamespaceAttribures(expectedAttrs) != countNonNamespaceAttribures(actualAttrs)) { throw new Exception(elementName + ": Number of attributes do not match up: " + countNonNamespaceAttribures(expectedAttrs) + " " + countNonNamespaceAttribures(actualAttrs)); } for (int i = 0; i < expectedAttrs.getLength(); i++) { Attr expectedAttr = (Attr) expectedAttrs.item(i); if (expectedAttr.getName().startsWith("xmlns")) { continue; } Attr actualAttr = null; if (expectedAttr.getNamespaceURI() == null) { actualAttr = (Attr) actualAttrs.getNamedItem(expectedAttr.getName()); } else { actualAttr = (Attr) actualAttrs.getNamedItemNS(expectedAttr.getNamespaceURI(), expectedAttr.getLocalName()); } if (actualAttr == null) { throw new Exception(elementName + ": No attribute found:" + expectedAttr); } if (!expectedAttr.getValue().equals(actualAttr.getValue())) { throw new Exception(elementName + ": Attribute values do not match: " + expectedAttr.getValue() + " " + actualAttr.getValue()); } } // compare children NodeList expectedChildren = expectedElement.getChildNodes(); NodeList actualChildren = actualElement.getChildNodes(); if (expectedChildren.getLength() != actualChildren.getLength()) { throw new Exception(elementName + ": Number of children do not match up: " + expectedChildren.getLength() + " " + actualChildren.getLength()); } for (int i = 0; i < expectedChildren.getLength(); i++) { Node expectedChild = expectedChildren.item(i); Node actualChild = actualChildren.item(i); compareNodes(expectedChild, actualChild); } } else if (expected instanceof Text) { String expectedData = ((Text) expected).getData().trim(); String actualData = ((Text) actual).getData().trim(); if (!expectedData.equals(actualData)) { throw new Exception("Text does not match: " + expectedData + " " + actualData); } } }
From source file:Main.java
/** * Are elements equal./*from ww w . j a v a 2s. c o m*/ * * @param element1 * the element1 * @param element2 * the element2 * @return true, if successful */ public static boolean areElementsEqual(Element element1, Element element2) { if (!element1.getTagName().equals(element2.getTagName())) { return false; } NamedNodeMap nodeAttrMap = element1.getAttributes(); NamedNodeMap pathAttrMap = element2.getAttributes(); if ((nodeAttrMap == null && pathAttrMap == null) || (pathAttrMap.getLength() == 0 && nodeAttrMap.getLength() == 0)) { return true; } else { if (element1.hasAttribute("name") && element2.hasAttribute("name")) { if (element1.getAttribute("name").equals(element2.getAttribute("name"))) { return true; } } else if (nodeAttrMap != null && pathAttrMap != null && (nodeAttrMap.getLength() == pathAttrMap.getLength())) { for (int k = 0; k < nodeAttrMap.getLength(); k++) { Node nodeAttr = nodeAttrMap.item(k); String nodeAttrName = nodeAttr.getNodeName(); String nodeAttrValue = nodeAttr.getNodeValue(); if (element2.hasAttribute(nodeAttrName) && nodeAttrValue.equals(element2.getAttribute(nodeAttrName))) { return true; } } } } return false; }
From source file:com.centeractive.ws.SchemaUtils.java
/** * Returns a map mapping urls to corresponding XmlSchema XmlObjects for the * specified wsdlUrl//w ww . j ava2s.co m */ public static void getSchemas(String wsdlUrl, Map<String, XmlObject> existing, SchemaLoader loader, String tns, String xml) { 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(xml); 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, xml); } } 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, xml); } } 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, xml); } } } 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, xml); } } 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, xml); } } } } catch (Exception e) { throw new SoapBuilderException(e); } }