List of usage examples for org.w3c.dom NamedNodeMap item
public Node item(int index);
index
th item in the map. From source file:eu.elf.license.LicenseParser.java
/** * Creates LicenseConcludeResponseObject from the concludeLicense operation's response string * // w w w.j av a 2 s. c o m * @param response * @return * @throws Exception */ public static LicenseConcludeResponseObject parseConcludeLicenseResponse(String response) throws Exception { LicenseConcludeResponseObject lcro = new LicenseConcludeResponseObject(); try { Document xmlDoc = LicenseParser.createXMLDocumentFromString(response); NodeList LicenseReferenceNL = xmlDoc.getElementsByTagNameNS("http://www.52north.org/license/0.3.2", "LicenseReference"); for (int i = 0; i < LicenseReferenceNL.getLength(); i++) { Element attributeStatementElement = (Element) LicenseReferenceNL.item(i); NodeList attributeElementList = attributeStatementElement .getElementsByTagNameNS("urn:oasis:names:tc:SAML:2.0:assertion", "Attribute"); for (int j = 0; j < attributeElementList.getLength(); j++) { Element attributeElement = (Element) attributeElementList.item(j); Element AttributeValueElement = (Element) attributeElement .getElementsByTagName("AttributeValue").item(0); NamedNodeMap licenseReferenceElementAttributeMap = attributeElement.getAttributes(); for (int k = 0; k < licenseReferenceElementAttributeMap.getLength(); k++) { Attr attrs = (Attr) licenseReferenceElementAttributeMap.item(k); if (attrs.getNodeName().equals("Name")) { if (attrs.getNodeValue().equals("urn:opengeospatial:ows4:geodrm:NotOnOrAfter")) { lcro.setValidTo(AttributeValueElement.getTextContent()); } if (attrs.getNodeValue().equals("urn:opengeospatial:ows4:geodrm:ProductID")) { lcro.setProductId(AttributeValueElement.getTextContent()); } if (attrs.getNodeValue().equals("urn:opengeospatial:ows4:geodrm:LicenseID")) { lcro.setLicenseId(AttributeValueElement.getTextContent()); } } } } } } catch (Exception e) { throw e; } return lcro; }
From source file:eu.elf.license.LicenseParser.java
/** * Creates list of LicenseModel elements * * @param productElementList - NodeList of <ns:product> elements: * @return List of LicenseModel objects/*from www . j a v a 2 s .c om*/ */ private static List<LicenseModel> createLicenseModelList(NodeList productElementList) { List<LicenseModel> lmList = new ArrayList<LicenseModel>(); for (int i = 0; i < productElementList.getLength(); i++) { LicenseModel tempLM = new LicenseModel(); Boolean isRestricted = true; Element productElement = (Element) productElementList.item(i); NamedNodeMap productElementAttributeMap = productElement.getAttributes(); for (int j = 0; j < productElementAttributeMap.getLength(); j++) { Attr attrs = (Attr) productElementAttributeMap.item(j); if (attrs.getNodeName().equals("id")) { if (attrs.getNodeName() != null) { tempLM.setId(attrs.getNodeValue()); } } } Element titleElement = (Element) productElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "title").item(0); if (titleElement != null) { tempLM.setName(titleElement.getTextContent()); } Element abstractElement = (Element) productElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "abstract").item(0); if (abstractElement != null) { tempLM.setDescription(abstractElement.getTextContent()); } Element calculationElement = (Element) productElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "calculation").item(0); Element declarationListElement = (Element) calculationElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "declarationList").item(0); Element predefinedParametersElement = (Element) declarationListElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "predefinedParameters").item(0); NodeList predefinedParametersParameterElementList = predefinedParametersElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "parameter"); for (int k = 0; k < predefinedParametersParameterElementList.getLength(); k++) { Element parameterElement = (Element) predefinedParametersParameterElementList.item(k); NamedNodeMap parameterElementAttributeMap = parameterElement.getAttributes(); for (int l = 0; l < parameterElementAttributeMap.getLength(); l++) { Attr attrs = (Attr) parameterElementAttributeMap.item(l); if (attrs.getNodeName().equals("name")) { if (attrs.getNodeName() != null) { if (attrs.getNodeValue().equals("ALL_ROLES")) { Element valueElement = (Element) parameterElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "value").item(0); if (valueElement.getTextContent().equals("true")) { isRestricted = false; } } } } if (attrs.getNodeName().equals("name")) { if (attrs.getNodeName() != null) { if (attrs.getNodeValue().equals("ALLOWED_ROLES")) { NodeList valueElementList = parameterElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "value"); for (int m = 0; m < valueElementList.getLength(); m++) { tempLM.addRole(valueElementList.item(m).getTextContent()); } } } } } } tempLM.setRestricted(isRestricted); tempLM.setParams(createLicenseModelParamList(declarationListElement)); lmList.add(tempLM); } return lmList; }
From source file:eu.europa.esig.dss.DSSXMLUtils.java
/** * If this method finds an attribute with names ID (case-insensitive) then it is returned. If there is more than one ID attributes then the first one is returned. * * @param element to be checked/*w w w .j a v a 2s. c o m*/ * @return the ID attribute value or null */ public static String getIDIdentifier(final Element element) { final NamedNodeMap attributes = element.getAttributes(); for (int jj = 0; jj < attributes.getLength(); jj++) { final Node item = attributes.item(jj); final String localName = item.getNodeName(); if (localName != null) { final String id = localName.toLowerCase(); if (ID_ATTRIBUTE_NAME.equals(id)) { return item.getTextContent(); } } } return null; }
From source file:com.centeractive.ws.builder.soap.SchemaUtils.java
/** * Returns a map mapping urls to corresponding XmlSchema XmlObjects for the * specified wsdlUrl/*from w w w. j ava2s. c om*/ */ public static void getSchemas(String wsdlUrl, Map<String, XmlObject> existing, SchemaLoader loader, String tns) { if (existing.containsKey(wsdlUrl)) { return; } // if( add ) // existing.put( wsdlUrl, null ); log.info("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.europa.esig.dss.DSSXMLUtils.java
/** * If this method finds an attribute with names ID (case-insensitive) then declares it to be a user-determined ID attribute. * * @param childElement//from w ww . j a v a 2 s . c om */ public static void setIDIdentifier(final DOMValidateContext context, final Element childElement) { final NamedNodeMap attributes = childElement.getAttributes(); for (int jj = 0; jj < attributes.getLength(); jj++) { final Node item = attributes.item(jj); final String localName = item.getNodeName(); if (localName != null) { final String id = localName.toLowerCase(); if (ID_ATTRIBUTE_NAME.equals(id)) { context.setIdAttributeNS(childElement, null, localName); break; } } } }
From source file:eu.europa.esig.dss.DSSXMLUtils.java
/** * If this method finds an attribute with names ID (case-insensitive) then declares it to be a user-determined ID attribute. * * @param childElement//from w w w . j a va 2s.co m */ public static void setIDIdentifier(final Element childElement) { final NamedNodeMap attributes = childElement.getAttributes(); for (int jj = 0; jj < attributes.getLength(); jj++) { final Node item = attributes.item(jj); final String localName = item.getNodeName(); if (localName != null) { final String id = localName.toLowerCase(); if (ID_ATTRIBUTE_NAME.equals(id)) { childElement.setIdAttribute(localName, true); break; } } } }
From source file:bridge.toolkit.commands.S1000DConverter.java
/** * Iterate through the DOM tree/*from ww w . j a v a 2 s .c o m*/ * * @param node * @param output * @throws IOException */ public static void writeNode(Node node, Writer output) throws IOException { int type = node.getNodeType(); switch (type) { case Node.ATTRIBUTE_NODE: output.write(' '); output.write(node.getNodeName()); output.write("=\""); writeXMLData(node.getNodeValue(), true, output); output.write('"'); break; case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: writeXMLData(node.getNodeValue(), false, output); break; case Node.COMMENT_NODE: output.write("<!--"); output.write(((Comment) node).getNodeValue()); output.write("-->"); break; case Node.DOCUMENT_FRAGMENT_NODE: writeNodes(node.getChildNodes(), output); break; case Node.DOCUMENT_NODE: writeNodes(node.getChildNodes(), output); break; case Node.DOCUMENT_TYPE_NODE: break; case Node.ELEMENT_NODE: { NamedNodeMap atts = node.getAttributes(); output.write('<'); output.write(node.getNodeName()); if (atts != null) { int length = atts.getLength(); for (int i = 0; i < length; i++) writeNode(atts.item(i), output); } if (node.hasChildNodes()) { output.write('>'); writeNodes(node.getChildNodes(), output); output.write("</"); output.write(node.getNodeName()); output.write('>'); } else { output.write("/>"); } break; } case Node.ENTITY_NODE: break; case Node.ENTITY_REFERENCE_NODE: writeNodes(node.getChildNodes(), output); break; case Node.NOTATION_NODE: break; case Node.PROCESSING_INSTRUCTION_NODE: break; default: throw new Error("Unexpected DOM node type: " + type); } }
From source file:DomUtils.java
/** * Rename element./*from w ww. j a v a 2 s. c om*/ * @param element The element to be renamed. * @param replacementElement The tag name of the replacement element. * @param keepChildContent <code>true</code> if the target element's child content * is to be copied to the replacement element, false if not. Default <code>true</code>. * @param keepAttributes <code>true</code> if the target element's attributes * are to be copied to the replacement element, false if not. Default <code>true</code>. * @return The renamed element. */ public static Element renameElement(Element element, String replacementElement, boolean keepChildContent, boolean keepAttributes) { Element replacement = element.getOwnerDocument().createElement(replacementElement); if (keepChildContent) { DomUtils.copyChildNodes(element, replacement); } if (keepAttributes) { NamedNodeMap attributes = element.getAttributes(); int attributeCount = attributes.getLength(); for (int i = 0; i < attributeCount; i++) { Attr attribute = (Attr) attributes.item(i); replacement.setAttribute(attribute.getName(), attribute.getValue()); } } DomUtils.replaceNode(replacement, element); return replacement; }
From source file:com.esri.geoevent.solutions.adapter.cot.CoTAdapterInbound.java
public static String elementToString(Node n) throws Exception { try {//from w w w .jav a 2 s .c o m String name = n.getNodeName(); short type = n.getNodeType(); if (Node.CDATA_SECTION_NODE == type) { return "<![CDATA[" + n.getNodeValue() + "]]>"; } if (name.startsWith("#")) { return ""; } StringBuffer sb = new StringBuffer(); sb.append('<').append(name); NamedNodeMap attrs = n.getAttributes(); if (attrs != null) { for (int i = 0; i < attrs.getLength(); i++) { Node attr = attrs.item(i); sb.append(' ').append(attr.getNodeName()).append("=\"").append(attr.getNodeValue()) .append("\""); } } String textContent = null; NodeList children = n.getChildNodes(); if (children.getLength() == 0) { if ((textContent = n.getTextContent()) != null && !"".equals(textContent)) { sb.append(textContent).append("</").append(name).append('>'); } else { sb.append("/>").append('\n'); } } else { sb.append('>').append('\n'); boolean hasValidChildren = false; for (int i = 0; i < children.getLength(); i++) { String childToString = elementToString(children.item(i)); if (!"".equals(childToString)) { sb.append(childToString); hasValidChildren = true; } } if (!hasValidChildren && ((textContent = n.getTextContent()) != null)) { sb.append(textContent); } sb.append("</").append(name).append('>'); } return sb.toString(); } catch (Exception e) { log.error(e); log.error(e.getStackTrace()); throw (e); } }
From source file:Main.java
private static void renderNode(StringBuffer sb, Node node, String margin, String indent, String lab, String rab, String nl) {// w w w .j ava 2 s.co m if (node == null) { sb.append("null"); return; } switch (node.getNodeType()) { case Node.DOCUMENT_NODE: //sb.append(margin + lab +"?xml version=\"1.0\" encoding=\"UTF-8\"?" + rab + nl); Node root = ((Document) node).getDocumentElement(); renderNode(sb, root, margin, indent, lab, rab, nl); break; case Node.ELEMENT_NODE: String name = getNodeNameWithNamespace(node); NodeList children = node.getChildNodes(); int nChildren = children.getLength(); NamedNodeMap attributes = node.getAttributes(); int nAttrs = attributes.getLength(); boolean singleShortTextChild = (nAttrs == 0) && (nChildren == 1) && (children.item(0).getNodeType() == Node.TEXT_NODE) && (children.item(0).getTextContent().length() < 70) && (!children.item(0).getTextContent().contains("\n")); if (singleShortTextChild) { sb.append(margin + lab + name + ((nChildren == 0) ? "/" : "") + rab); } else if (nAttrs == 0 && !singleShortTextChild) { sb.append(margin + lab + name + ((nChildren == 0) ? "/" : "") + rab + nl); } else if (nAttrs == 1) { Node attr = attributes.item(0); String attrName = getNodeNameWithNamespace(attr); sb.append(margin + lab + name + " " + attrName + "=\"" + escapeChars(attr.getNodeValue()) + "\"" + ((nChildren == 0) ? "/" : "") + rab + nl); } else { sb.append(margin + lab + name + nl); for (int i = 0; i < nAttrs; i++) { Node attr = attributes.item(i); String attrName = getNodeNameWithNamespace(attr); sb.append(margin + indent + attrName + "=\"" + escapeChars(attr.getNodeValue())); if (i < nAttrs - 1) sb.append("\"" + nl); else sb.append("\"" + ((nChildren == 0) ? "/" : "") + rab + nl); } } if (singleShortTextChild) { String text = escapeChars(node.getTextContent()); sb.append(text.trim()); sb.append(lab + "/" + name + rab + nl); } else { for (int i = 0; i < nChildren; i++) { renderNode(sb, children.item(i), margin + indent, indent, lab, rab, nl); } } if (nChildren != 0 && !singleShortTextChild) sb.append(margin + lab + "/" + name + rab + nl); break; case Node.TEXT_NODE: String text = escapeChars(node.getNodeValue()); String[] lines = text.split("\n"); for (String line : lines) { line = line.trim(); if (!line.equals("")) sb.append(margin + line + nl); } break; case Node.CDATA_SECTION_NODE: String cdataText = node.getNodeValue(); String[] cdataLines = cdataText.split("\n"); sb.append(margin + lab + "![CDATA[" + nl); for (String line : cdataLines) { line = line.trim(); if (!line.equals("")) sb.append(margin + indent + line + nl); } sb.append(margin + "]]" + rab + nl); break; case Node.PROCESSING_INSTRUCTION_NODE: sb.append(margin + lab + "?" + node.getNodeName() + " " + escapeChars(node.getNodeValue()) + "?" + rab + nl); 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(margin + lab + "!--" + node.getNodeValue() + "--" + rab + nl); break; } return; }