List of usage examples for org.w3c.dom Element getAttributes
public NamedNodeMap getAttributes();
NamedNodeMap
containing the attributes of this node (if it is an Element
) or null
otherwise. From source file:com.msopentech.odatajclient.engine.data.ODataBinder.java
/** * Gets an <tt>ODataProperty</tt> from the given DOM element. * * @param property content./* w ww. j av a 2 s.co m*/ * @return <tt>ODataProperty</tt> object. */ public static ODataProperty getProperty(final Element property) { final ODataProperty res; final Node nullNode = property.getAttributes().getNamedItem(ODataConstants.ATTR_NULL); if (nullNode == null) { final EdmType edmType = StringUtils.isBlank(property.getAttribute(ODataConstants.ATTR_M_TYPE)) ? null : new EdmType(property.getAttribute(ODataConstants.ATTR_M_TYPE)); final PropertyType propType = edmType == null ? guessPropertyType(property) : edmType.isCollection() ? PropertyType.COLLECTION : edmType.isSimpleType() ? PropertyType.PRIMITIVE : PropertyType.COMPLEX; switch (propType) { case COLLECTION: res = fromCollectionPropertyElement(property, edmType); break; case COMPLEX: res = fromComplexPropertyElement(property, edmType); break; case PRIMITIVE: res = fromPrimitivePropertyElement(property, edmType); break; case EMPTY: default: res = ODataFactory.newPrimitiveProperty(XMLUtils.getSimpleName(property), null); } } else { res = ODataFactory.newPrimitiveProperty(XMLUtils.getSimpleName(property), null); } return res; }
From source file:com.vinexs.tool.XML.java
private static Object getChildJSONObject(org.w3c.dom.Element tag) { int i, k;/*w ww . ja v a2s.c o m*/ //get attributes && child nodes NamedNodeMap attributes = tag.getAttributes(); NodeList childNodes = tag.getChildNodes(); int numAttr = attributes.getLength(); int numChild = childNodes.getLength(); //get element nodes Boolean hasTagChild = false; Map<String, ArrayList<Object>> childMap = new HashMap<>(); for (i = 0; i < numChild; i++) { Node node = childNodes.item(i); //not process non-element node if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) { continue; } hasTagChild = true; org.w3c.dom.Element childTag = (org.w3c.dom.Element) node; String tagName = childTag.getTagName(); if (!childMap.containsKey(tagName)) { childMap.put(tagName, new ArrayList<>()); } childMap.get(tagName).add(getChildJSONObject(childTag)); } if (numAttr == 0 && !hasTagChild) { // Return String return stringToValue(tag.getTextContent()); } else { // Return JSONObject JSONObject data = new JSONObject(); if (numAttr > 0) { for (i = 0; i < numAttr; i++) { Node attr = attributes.item(i); try { data.put(attr.getNodeName(), stringToValue(attr.getNodeValue())); } catch (JSONException e) { e.printStackTrace(); } } } if (hasTagChild) { for (Map.Entry<String, ArrayList<Object>> tagMap : childMap.entrySet()) { ArrayList<Object> tagList = tagMap.getValue(); if (tagList.size() == 1) { try { data.put(tagMap.getKey(), tagList.get(0)); } catch (JSONException e) { e.printStackTrace(); } } else { JSONArray array = new JSONArray(); for (k = 0; k < tagList.size(); k++) { array.put(tagList.get(k)); } try { data.put(tagMap.getKey(), array); } catch (JSONException e) { e.printStackTrace(); } } } } else { try { data.put("content", stringToValue(tag.getTextContent())); } catch (JSONException e) { e.printStackTrace(); } } return data; } }
From source file:mondrian.test.DiffRepository.java
private static void writeNode(Node node, XMLOutput out) { final NodeList childNodes; switch (node.getNodeType()) { case Node.DOCUMENT_NODE: out.print("<?xml version=\"1.0\" ?>" + Util.nl); childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); writeNode(child, out);//from w ww .ja v a 2 s.c o m } // writeNode(((Document) node).getDocumentElement(), out); break; case Node.ELEMENT_NODE: Element element = (Element) node; final String tagName = element.getTagName(); out.beginBeginTag(tagName); // Attributes. final NamedNodeMap attributeMap = element.getAttributes(); for (int i = 0; i < attributeMap.getLength(); i++) { final Node att = attributeMap.item(i); out.attribute(att.getNodeName(), att.getNodeValue()); } out.endBeginTag(tagName); // Write child nodes, ignoring attributes but including text. childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node child = childNodes.item(i); if (child.getNodeType() == Node.ATTRIBUTE_NODE) { continue; } writeNode(child, out); } out.endTag(tagName); break; case Node.ATTRIBUTE_NODE: out.attribute(node.getNodeName(), node.getNodeValue()); break; case Node.CDATA_SECTION_NODE: CDATASection cdata = (CDATASection) node; out.cdata(cdata.getNodeValue(), true); break; case Node.TEXT_NODE: Text text = (Text) node; final String wholeText = text.getNodeValue(); if (!isWhitespace(wholeText)) { out.cdata(wholeText, false); } break; case Node.COMMENT_NODE: Comment comment = (Comment) node; out.print("<!--" + comment.getNodeValue() + "-->" + Util.nl); break; default: throw new RuntimeException("unexpected node type: " + node.getNodeType() + " (" + node + ")"); } }
From source file:eu.elf.license.LicenseParser.java
/** * Creates List of LicenseModelGroup objects * * @param productGroupElementList - List of <ns:productGroup> elements * @return list of LicenseModelGroup objects *//* w ww.j a v a 2s . c om*/ private static List<LicenseModelGroup> createLicenseModelGroupList(NodeList productGroupElementList) { List<LicenseModelGroup> lmgList = new ArrayList<LicenseModelGroup>(); for (int i = 0; i < productGroupElementList.getLength(); i++) { LicenseModelGroup tempLMG = new LicenseModelGroup(); Boolean isAccountGroup = false; Element productGroupElement = (Element) productGroupElementList.item(i); NamedNodeMap productGroupElementAttributeMap = productGroupElement.getAttributes(); for (int j = 0; j < productGroupElementAttributeMap.getLength(); j++) { Attr attrs = (Attr) productGroupElementAttributeMap.item(j); if (attrs.getNodeName().equals("id")) { if (attrs.getNodeValue().equals("ACCOUNTING_SUMMARY_GROUP")) { // Skip element with id="ACCOUNTING_SUMMARY_GROUP" - do not add to the list isAccountGroup = true; } if (attrs.getNodeName() != null) { tempLMG.setId(attrs.getNodeValue()); } } if (attrs.getNodeName().equals("name")) { if (attrs.getNodeName() != null) { tempLMG.setUrl(attrs.getNodeValue()); } } } if (isAccountGroup == true) { continue; // Skip element with id="ACCOUNTING_SUMMARY_GROUP" - do not add to the list } Element abstractElement = (Element) productGroupElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "abstract").item(0); if (abstractElement != null) { tempLMG.setDescription(abstractElement.getTextContent()); } Element titleElement = (Element) productGroupElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "title").item(0); if (titleElement != null) { tempLMG.setName(titleElement.getTextContent()); } NodeList productElementList = productGroupElement .getElementsByTagNameNS("http://www.conterra.de/xcpf/1.1", "product"); tempLMG.setLicenseModels(createLicenseModelList(productElementList)); lmgList.add(tempLMG); } return lmgList; }
From source file:com.centeractive.ws.SchemaUtils.java
/** * Returns a map mapping urls to corresponding XmlSchema XmlObjects for the * specified wsdlUrl//from ww w . j a v a2 s.c o 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); } }
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//ww w . j a va 2 s .c om * @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.example.soaplegacy.SchemaUtils.java
/** * Returns a map mapping urls to corresponding XmlSchema XmlObjects for the * specified wsdlUrl/* w w w .j a v a 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.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 va2 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 declares it to be a user-determined ID attribute. * * @param childElement//from w ww . jav a2 s.c o m */ 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/* ww w.ja v a 2s .c om*/ */ 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; } } } }