List of usage examples for org.w3c.dom Element hasAttributes
public boolean hasAttributes();
From source file:loci.formats.in.LIFReader.java
private void populateOriginalMetadata(Element root, Deque<String> nameStack) { String name = root.getNodeName(); if (root.hasAttributes() && !name.equals("Element") && !name.equals("Attachment") && !name.equals("LMSDataContainerHeader")) { nameStack.push(name);/*from w w w . j a va2 s .c o m*/ String suffix = root.getAttribute("Identifier"); String value = root.getAttribute("Variant"); if (suffix == null || suffix.trim().length() == 0) { suffix = root.getAttribute("Description"); } StringBuffer key = new StringBuffer(); final Iterator<String> nameStackIterator = nameStack.descendingIterator(); while (nameStackIterator.hasNext()) { final String k = nameStackIterator.next(); key.append(k); key.append("|"); } if (suffix != null && value != null && suffix.length() > 0 && value.length() > 0 && !suffix.equals("HighInteger") && !suffix.equals("LowInteger")) { addSeriesMetaList(key.toString() + suffix, value); } else { NamedNodeMap attributes = root.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { Attr attr = (Attr) attributes.item(i); if (!attr.getName().equals("HighInteger") && !attr.getName().equals("LowInteger")) { addSeriesMeta(key.toString() + attr.getName(), attr.getValue()); } } } } NodeList children = root.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { Object child = children.item(i); if (child instanceof Element) { populateOriginalMetadata((Element) child, nameStack); } } if (root.hasAttributes() && !name.equals("Element") && !name.equals("Attachment") && !name.equals("LMSDataContainerHeader")) { nameStack.pop(); } }
From source file:com.twinsoft.convertigo.engine.translators.WebServiceTranslator.java
private SOAPElement addSoapElement(Context context, SOAPEnvelope se, SOAPElement soapParent, Node node) throws Exception { String prefix = node.getPrefix(); String namespace = node.getNamespaceURI(); String nodeName = node.getNodeName(); String localName = node.getLocalName(); String value = node.getNodeValue(); boolean includeResponseElement = true; if (context.sequenceName != null) { includeResponseElement = ((Sequence) context.requestedObject).isIncludeResponseElement(); }/*from w w w. j av a 2 s . c om*/ SOAPElement soapElement = null; if (node.getNodeType() == Node.ELEMENT_NODE) { Element element = (Element) node; boolean toAdd = true; if (!includeResponseElement && "response".equalsIgnoreCase(localName)) { toAdd = false; } if ("http://schemas.xmlsoap.org/soap/envelope/".equals(element.getParentNode().getNamespaceURI()) || "http://schemas.xmlsoap.org/soap/envelope/".equals(namespace) || nodeName.toLowerCase().endsWith(":envelope") || nodeName.toLowerCase().endsWith(":body") //element.getParentNode().getNodeName().toUpperCase().indexOf("NS0:") != -1 || //nodeName.toUpperCase().indexOf("NS0:") != -1 ) { toAdd = false; } if (toAdd) { if (prefix == null || prefix.equals("")) { soapElement = soapParent.addChildElement(nodeName); } else { soapElement = soapParent.addChildElement(se.createName(localName, prefix, namespace)); } } else { soapElement = soapParent; } if (soapElement != null) { if (soapParent.equals(se.getBody()) && !soapParent.equals(soapElement)) { if (XsdForm.qualified == context.project.getSchemaElementForm()) { if (soapElement.getAttribute("xmlns") == null) { soapElement.addAttribute(se.createName("xmlns"), context.project.getTargetNamespace()); } } } if (element.hasAttributes()) { String attrType = element.getAttribute("type"); if (("attachment").equals(attrType)) { if (context.requestedObject instanceof AbstractHttpTransaction) { AttachmentDetails attachment = AttachmentManager.getAttachment(element); if (attachment != null) { byte[] raw = attachment.getData(); if (raw != null) soapElement.addTextNode(Base64.encodeBase64String(raw)); } /* DON'T WORK YET *\ AttachmentPart ap = responseMessage.createAttachmentPart(new ByteArrayInputStream(raw), element.getAttribute("content-type")); ap.setContentId(key); ap.setContentLocation(element.getAttribute("url")); responseMessage.addAttachmentPart(ap); \* DON'T WORK YET */ } } if (!includeResponseElement && "response".equalsIgnoreCase(localName)) { // do not add attributes } else { NamedNodeMap attributes = element.getAttributes(); int len = attributes.getLength(); for (int i = 0; i < len; i++) { Node item = attributes.item(i); addSoapElement(context, se, soapElement, item); } } } if (element.hasChildNodes()) { NodeList childNodes = element.getChildNodes(); int len = childNodes.getLength(); for (int i = 0; i < len; i++) { Node item = childNodes.item(i); switch (item.getNodeType()) { case Node.ELEMENT_NODE: addSoapElement(context, se, soapElement, item); break; case Node.CDATA_SECTION_NODE: case Node.TEXT_NODE: String text = item.getNodeValue(); text = (text == null) ? "" : text; soapElement.addTextNode(text); break; default: break; } } } } } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) { if (prefix == null || prefix.equals("")) { soapElement = soapParent.addAttribute(se.createName(nodeName), value); } else { soapElement = soapParent.addAttribute(se.createName(localName, prefix, namespace), value); } } return soapElement; }
From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java
private synchronized String duplicate(final Document originalDom, final Element originalRootElement, final Element patchElement) throws Exception { boolean isdone = false; Element parentElement = null; DuplicateChildElementObject childElementObject = isChildElement(originalRootElement, patchElement); if (!childElementObject.isNeedDuplicate()) { isdone = true;/*from w ww . j a v a2 s . c o m*/ parentElement = childElementObject.getElement(); } else if (childElementObject.getElement() != null) { parentElement = childElementObject.getElement(); } else { parentElement = originalRootElement; } String son_name = patchElement.getNodeName(); Element subITEM = null; if (!isdone) { subITEM = originalDom.createElement(son_name); if (patchElement.hasChildNodes()) { if (patchElement.getFirstChild().getNodeType() == Node.TEXT_NODE) { subITEM.setTextContent(patchElement.getTextContent()); } } if (patchElement.hasAttributes()) { NamedNodeMap attributes = patchElement.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { String attribute_name = attributes.item(i).getNodeName(); String attribute_value = attributes.item(i).getNodeValue(); subITEM.setAttribute(attribute_name, attribute_value); } } parentElement.appendChild(subITEM); } else { subITEM = parentElement; } NodeList sub_messageItems = patchElement.getChildNodes(); int sub_item_number = sub_messageItems.getLength(); logger.debug("patchEl: " + DomUtils.elementToString(patchElement) + "length: " + sub_item_number); if (sub_item_number == 0) { isdone = true; } else { for (int j = 0; j < sub_item_number; j++) { if (sub_messageItems.item(j).getNodeType() == Node.ELEMENT_NODE) { Element sub_messageItem = (Element) sub_messageItems.item(j); logger.debug("node name: " + DomUtils.elementToString(subITEM) + " node type: " + subITEM.getNodeType()); duplicate(originalDom, subITEM, sub_messageItem); } } } return (parentElement != null) ? DomUtils.elementToString(parentElement) : ""; }
From source file:com.twinsoft.convertigo.engine.translators.WebServiceTranslator.java
private void addElement(SOAPMessage responseMessage, SOAPEnvelope soapEnvelope, Context context, Element elementToAdd, SOAPElement soapElement) throws SOAPException { SOAPElement soapMethodResponseElement = (SOAPElement) soapEnvelope.getBody().getFirstChild(); String targetNamespace = soapMethodResponseElement.getNamespaceURI(); String prefix = soapMethodResponseElement.getPrefix(); String nodeType = elementToAdd.getAttribute("type"); SOAPElement childSoapElement = soapElement; boolean elementAdded = true; boolean bTable = false; if (nodeType.equals("table")) { bTable = true;//from w w w . ja v a2s . c o m /*childSoapElement = soapElement.addChildElement("ArrayOf" + context.transactionName + "_" + tableName + "_Row", ""); if (!context.httpServletRequest.getServletPath().endsWith(".wsl")) { childSoapElement.addAttribute(soapEnvelope.createName("xsi:type"), "soapenc:Array"); }*/ childSoapElement = soapElement.addChildElement(elementToAdd.getNodeName()); } else if (nodeType.equals("row")) { /*String elementType = context.transactionName + "_" + tableName + "_Row"; childSoapElement = soapElement.addChildElement(elementType, "");*/ childSoapElement = soapElement.addChildElement(elementToAdd.getNodeName()); } else if (nodeType.equals("attachment")) { childSoapElement = soapElement.addChildElement(elementToAdd.getNodeName()); if (context.requestedObject instanceof AbstractHttpTransaction) { AttachmentDetails attachment = AttachmentManager.getAttachment(elementToAdd); if (attachment != null) { byte[] raw = attachment.getData(); if (raw != null) childSoapElement.addTextNode(Base64.encodeBase64String(raw)); } /* DON'T WORK YET *\ AttachmentPart ap = responseMessage.createAttachmentPart(new ByteArrayInputStream(raw), elementToAdd.getAttribute("content-type")); ap.setContentId(key); ap.setContentLocation(elementToAdd.getAttribute("url")); responseMessage.addAttachmentPart(ap); \* DON'T WORK YET */ } } else { String elementNodeName = elementToAdd.getNodeName(); String elementNodeNsUri = elementToAdd.getNamespaceURI(); String elementNodePrefix = getPrefix(context.projectName, elementNodeNsUri); XmlSchemaElement xmlSchemaElement = getXmlSchemaElementByName(context.projectName, elementNodeName); boolean isGlobal = xmlSchemaElement != null; if (isGlobal) { elementNodeNsUri = xmlSchemaElement.getQName().getNamespaceURI(); elementNodePrefix = getPrefix(context.projectName, elementNodeNsUri); } // ignore original SOAP message response elements // if ((elementNodeName.toUpperCase().indexOf("SOAP-ENV:") != -1) || ((elementToAdd.getParentNode().getNodeName().toUpperCase().indexOf("SOAP-ENV:") != -1)) || // (elementNodeName.toUpperCase().indexOf("SOAPENV:") != -1) || ((elementToAdd.getParentNode().getNodeName().toUpperCase().indexOf("SOAPENV:") != -1)) || // (elementNodeName.toUpperCase().indexOf("NS0:") != -1) || ((elementToAdd.getParentNode().getNodeName().toUpperCase().indexOf("NS0:") != -1))) { // elementAdded = false; // } if ("http://schemas.xmlsoap.org/soap/envelope/".equals(elementToAdd.getNamespaceURI()) || "http://schemas.xmlsoap.org/soap/envelope/" .equals(elementToAdd.getParentNode().getNamespaceURI()) || elementToAdd.getParentNode().getNodeName().toUpperCase().indexOf("NS0:") != -1 || elementNodeName.toUpperCase().indexOf("NS0:") != -1) { elementAdded = false; } else { if (XsdForm.qualified == context.project.getSchemaElementForm() || isGlobal) { if (elementNodePrefix == null) { childSoapElement = soapElement .addChildElement(soapEnvelope.createName(elementNodeName, prefix, targetNamespace)); } else { childSoapElement = soapElement.addChildElement( soapEnvelope.createName(elementNodeName, elementNodePrefix, elementNodeNsUri)); } } else { childSoapElement = soapElement.addChildElement(elementNodeName); } } } if (elementAdded && elementToAdd.hasAttributes()) { addAttributes(responseMessage, soapEnvelope, context, elementToAdd.getAttributes(), childSoapElement); } if (elementToAdd.hasChildNodes()) { NodeList childNodes = elementToAdd.getChildNodes(); int len = childNodes.getLength(); if (bTable) { /*if (!context.httpServletRequest.getServletPath().endsWith(".wsl")) { childSoapElement.addAttribute(soapEnvelope.createName("soapenc:arrayType"), context.projectName+"_ns:" + context.transactionName + "_" + tableName + "_Row[" + (len - 1) + "]"); }*/ } org.w3c.dom.Node node; Element childElement; for (int i = 0; i < len; i++) { node = childNodes.item(i); switch (node.getNodeType()) { case org.w3c.dom.Node.ELEMENT_NODE: childElement = (Element) node; addElement(responseMessage, soapEnvelope, context, childElement, childSoapElement); break; case org.w3c.dom.Node.CDATA_SECTION_NODE: case org.w3c.dom.Node.TEXT_NODE: String text = node.getNodeValue(); text = (text == null) ? "" : text; childSoapElement.addTextNode(text); break; default: break; } } /*org.w3c.dom.Node node; Element childElement; for (int i = 0 ; i < len ; i++) { node = childNodes.item(i); if (node instanceof Element) { childElement = (Element) node; addElement(responseMessage, soapEnvelope, context, childElement, childSoapElement); } else if (node instanceof CDATASection) { Node textNode = XMLUtils.findChildNode(elementToAdd, org.w3c.dom.Node.CDATA_SECTION_NODE); String text = textNode.getNodeValue(); if (text == null) { text = ""; } childSoapElement.addTextNode(text); } else { Node textNode = XMLUtils.findChildNode(elementToAdd, org.w3c.dom.Node.TEXT_NODE); if (textNode != null) { String text = textNode.getNodeValue(); if (text == null) { text = ""; } childSoapElement.addTextNode(text); } } }*/ } }
From source file:openblocks.yacodeblocks.BlockSaveFileTest.java
private void assertElementContainsOnlyAsciiCharacters(Element element) throws Exception { assertStringContainsOnlyAsciiCharacters(element.getNodeName(), "name", element); if (element.hasAttributes()) { NamedNodeMap attributes = element.getAttributes(); for (int i = 0; i < attributes.getLength(); i++) { assertNodeContainsOnlyAsciiCharacters(attributes.item(i)); }/*from w w w . j a v a 2 s .com*/ } if (element.hasChildNodes()) { NodeList childNodes = element.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { assertNodeContainsOnlyAsciiCharacters(childNodes.item(i)); } } }
From source file:org.apache.ode.utils.DOMUtils.java
/** * Drop the attributes from an element, except possibly an <code>xmlns</code> * attribute that declares its namespace. * @param target the element whose attributes will be removed. * @param flag preserve namespace declaration *//*w ww . ja v a 2s. c om*/ public static void removeAttributes(Element target, boolean flag) { if (!target.hasAttributes()) { return; } String prefix = target.getPrefix(); NamedNodeMap nnm = target.getAttributes(); Attr toPutBack = null; if (flag) { if (prefix == null) { toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns"); } else { toPutBack = target.getAttributeNodeNS(NS_URI_XMLNS, "xmlns:" + prefix); } } while (nnm.getLength() != 0) { target.removeAttributeNode((Attr) nnm.item(0)); } if (toPutBack != null) { target.setAttributeNodeNS(toPutBack); } }
From source file:org.apache.sling.its.servlets.ItsImportServlet.java
/** * Get the attributes and call output to set the attributes into * jackrabbit.//from w w w .ja v a2s . c om * * @param element * an Element from the Document object. * @param path * jackrabbit path to store the attributes */ private void setAttributes(final Element element, final String path) { if (element.hasAttributes()) { final NamedNodeMap map = element.getAttributes(); final ArrayList<String> list = new ArrayList<String>(); for (int i = 0; i < map.getLength(); i++) { list.add(((Attr) map.item(i)).getNodeName()); } Collections.sort(list); for (final String attrName : list) { final Attr attr = (Attr) map.getNamedItem(attrName); output(path, attr, null); } } }
From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java
/** * Returns the Attr[]s to be output for the given element. * <br>// w ww . j a v a2s . com * The code of this method is a copy of {@link #handleAttributes(Element, * NameSpaceSymbTable)}, * whereas it takes into account that subtree-c14n is -- well -- * subtree-based. * So if the element in question isRoot of c14n, it's parent is not in the * node set, as well as all other ancestors. * * @param element * @param ns * @return the Attr[]s to be output * @throws CanonicalizationException */ @Override protected Iterator<Attr> handleAttributesSubtree(Element element, NameSpaceSymbTable ns) throws CanonicalizationException { if (!element.hasAttributes() && !firstCall) { return null; } // result will contain the attrs which have to be output final SortedSet<Attr> result = this.result; result.clear(); if (element.hasAttributes()) { NamedNodeMap attrs = element.getAttributes(); int attrsLength = attrs.getLength(); for (int i = 0; i < attrsLength; i++) { Attr attribute = (Attr) attrs.item(i); String NUri = attribute.getNamespaceURI(); String NName = attribute.getLocalName(); String NValue = attribute.getValue(); if (!XMLNS_URI.equals(NUri)) { // It's not a namespace attr node. Add to the result and continue. result.add(attribute); } else if (!(XML.equals(NName) && XML_LANG_URI.equals(NValue))) { // The default mapping for xml must not be output. Node n = ns.addMappingAndRender(NName, NValue, attribute); if (n != null) { // Render the ns definition result.add((Attr) n); if (C14nHelper.namespaceIsRelative(attribute)) { Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() }; throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs); } } } } } if (firstCall) { // It is the first node of the subtree // Obtain all the namespaces defined in the parents, and added to the output. ns.getUnrenderedNodes(result); // output the attributes in the xml namespace. xmlattrStack.getXmlnsAttr(result); firstCall = false; } return result.iterator(); }
From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java
/** * Returns the Attr[]s to be output for the given element. * <br>//from w w w . j a v a 2 s.c om * IMPORTANT: This method expects to work on a modified DOM tree, i.e. a * DOM which has been prepared using * {@link org.apache.xml.security.utils.XMLUtils#circumventBug2650( * org.w3c.dom.Document)}. * * @param element * @param ns * @return the Attr[]s to be output * @throws CanonicalizationException */ @Override protected Iterator<Attr> handleAttributes(Element element, NameSpaceSymbTable ns) throws CanonicalizationException { // result will contain the attrs which have to be output xmlattrStack.push(ns.getLevel()); boolean isRealVisible = isVisibleDO(element, ns.getLevel()) == 1; final SortedSet<Attr> result = this.result; result.clear(); if (element.hasAttributes()) { NamedNodeMap attrs = element.getAttributes(); int attrsLength = attrs.getLength(); for (int i = 0; i < attrsLength; i++) { Attr attribute = (Attr) attrs.item(i); String NUri = attribute.getNamespaceURI(); String NName = attribute.getLocalName(); String NValue = attribute.getValue(); if (!XMLNS_URI.equals(NUri)) { //A non namespace definition node. if (XML_LANG_URI.equals(NUri)) { if (NName.equals("id")) { if (isRealVisible) { // treat xml:id like any other attribute // (emit it, but don't inherit it) result.add(attribute); } } else { xmlattrStack.addXmlnsAttr(attribute); } } else if (isRealVisible) { //The node is visible add the attribute to the list of output attributes. result.add(attribute); } } else if (!XML.equals(NName) || !XML_LANG_URI.equals(NValue)) { /* except omit namespace node with local name xml, which defines * the xml prefix, if its string value is * http://www.w3.org/XML/1998/namespace. */ // add the prefix binding to the ns symb table. if (isVisible(attribute)) { if (isRealVisible || !ns.removeMappingIfRender(NName)) { // The xpath select this node output it if needed. Node n = ns.addMappingAndRender(NName, NValue, attribute); if (n != null) { result.add((Attr) n); if (C14nHelper.namespaceIsRelative(attribute)) { Object exArgs[] = { element.getTagName(), NName, attribute.getNodeValue() }; throw new CanonicalizationException("c14n.Canonicalizer.RelativeNamespace", exArgs); } } } } else { if (isRealVisible && !XMLNS.equals(NName)) { ns.removeMapping(NName); } else { ns.addMapping(NName, NValue, attribute); } } } } } if (isRealVisible) { //The element is visible, handle the xmlns definition Attr xmlns = element.getAttributeNodeNS(XMLNS_URI, XMLNS); Node n = null; if (xmlns == null) { //No xmlns def just get the already defined. n = ns.getMapping(XMLNS); } else if (!isVisible(xmlns)) { //There is a definition but the xmlns is not selected by the xpath. //then xmlns="" n = ns.addMappingAndRender(XMLNS, "", nullNode); } //output the xmlns def if needed. if (n != null) { result.add((Attr) n); } //Float all xml:* attributes of the unselected parent elements to this one. xmlattrStack.getXmlnsAttr(result); ns.getUnrenderedNodes(result); } return result.iterator(); }
From source file:org.apache.xml.security.c14n.implementations.Canonicalizer11.java
protected void handleParent(Element e, NameSpaceSymbTable ns) { if (!e.hasAttributes() && e.getNamespaceURI() == null) { return;//from w w w. java 2 s . c o m } xmlattrStack.push(-1); NamedNodeMap attrs = e.getAttributes(); int attrsLength = attrs.getLength(); for (int i = 0; i < attrsLength; i++) { Attr attribute = (Attr) attrs.item(i); String NName = attribute.getLocalName(); String NValue = attribute.getNodeValue(); if (Constants.NamespaceSpecNS.equals(attribute.getNamespaceURI())) { if (!XML.equals(NName) || !Constants.XML_LANG_SPACE_SpecNS.equals(NValue)) { ns.addMapping(NName, NValue, attribute); } } else if (!"id".equals(NName) && XML_LANG_URI.equals(attribute.getNamespaceURI())) { xmlattrStack.addXmlnsAttr(attribute); } } if (e.getNamespaceURI() != null) { String NName = e.getPrefix(); String NValue = e.getNamespaceURI(); String Name; if (NName == null || NName.equals("")) { NName = "xmlns"; Name = "xmlns"; } else { Name = "xmlns:" + NName; } Attr n = e.getOwnerDocument().createAttributeNS("http://www.w3.org/2000/xmlns/", Name); n.setValue(NValue); ns.addMapping(NName, NValue, n); } }