Java Code Examples for org.w3c.dom.Element#removeAttributeNode()

The following examples show how to use org.w3c.dom.Element#removeAttributeNode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskUtils.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
private static void secureLoadElement(Element element) throws CryptoException {
    Attr secureAttr = element.getAttributeNodeNS(SECURE_VAULT_NS, SECRET_ALIAS_ATTR_NAME);
    if (secureAttr != null) {
        element.setTextContent(loadFromSecureVault(secureAttr.getValue()));
        element.removeAttributeNode(secureAttr);
    }
    NodeList childNodes = element.getChildNodes();
    int count = childNodes.getLength();
    Node tmpNode;
    for (int i = 0; i < count; i++) {
        tmpNode = childNodes.item(i);
        if (tmpNode instanceof Element) {
            secureLoadElement((Element) tmpNode);
        }
    }
}
 
Example 2
Source File: NodesTest.java    From caja with Apache License 2.0 6 votes vote down vote up
public final void testRenderWithUnknownNamespace() throws Exception {
  DocumentFragment fragment = xmlFragment(fromString(
      ""
      + "<foo xmlns='http://www.w3.org/XML/1998/namespace'"
      + " xmlns:bar='http://bobs.house.of/XML&BBQ'>"
      + "<bar:baz boo='howdy' xml:lang='es'/>"
      + "</foo>"));
  // Remove any XMLNS attributes and prefixes.
  Element el = (Element) fragment.getFirstChild();
  while (el.getAttributes().getLength() != 0) {
    el.removeAttributeNode((Attr) el.getAttributes().item(0));
  }
  el.setPrefix("");
  el.getFirstChild().setPrefix("");
  assertEquals(
      ""
      + "<xml:foo>"
      + "<_ns1:baz xmlns:_ns1=\"http://bobs.house.of/XML&amp;BBQ\""
      + " boo=\"howdy\" xml:lang=\"es\"></_ns1:baz>"
      + "</xml:foo>",
      Nodes.render(fragment, MarkupRenderMode.XML));
}
 
Example 3
Source File: TestSummary.java    From caja with Apache License 2.0 6 votes vote down vote up
private static int rewriteChildElements(
    Element parent, String oldName, String newName) {
  int nRewritten = 0;
  for (Node c = parent.getFirstChild(); c != null; c = c.getNextSibling()) {
    if (c instanceof Element && oldName.equals(c.getNodeName())) {
      Element el = (Element) c;
      Element replacement = parent.getOwnerDocument().createElement(newName);
      while (el.getFirstChild() != null) {
        replacement.appendChild(el.getFirstChild());
      }
      NamedNodeMap attrMap = el.getAttributes();
      Attr[] attrs = new Attr[attrMap.getLength()];
      for (int i = 0, n = attrMap.getLength(); i < n; ++i) {
        attrs[i] = (Attr) attrMap.item(i);
      }
      for (Attr attr : attrs) {
        el.removeAttributeNode(attr);
        replacement.setAttributeNode(attr);
      }
      parent.replaceChild(replacement, el);
      ++nRewritten;
    }
  }
  return nRewritten;
}
 
Example 4
Source File: DDTreeWalker.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void removeAttribute(Element element, String attrName){
    org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
    for (int in = 0; in < attrs.getLength(); in++) {
        org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(in);
        if (attr.getName().equals(attrName)) {
            element.removeAttributeNode(attr);
        }
    }
}
 
Example 5
Source File: ElementTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testRemoveAttributeNode() throws Exception {
    Document document = createDOMWithNS("ElementSample01.xml");
    Element elemNode = (Element) document.getElementsByTagName("book").item(1);
    Attr attr = elemNode.getAttributeNode("category1");
    assertEquals(attr.getValue(), "research");

    assertEquals(elemNode.getTagName(), "book");
    elemNode.removeAttributeNode(attr);
    assertEquals(elemNode.getAttribute("category1"), "");
}
 
Example 6
Source File: DomParser.java    From caja with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for creating a new attribute with the fixed up namespace
 * name. Ignores DOMException's raised by {@code createAttributeNS()} in case
 * the document being processed is html (removing the attribute that caused
 * the exception), rethrows it as a ParseException otherwise.
 *
 * @param doc The document being parsed.
 * @param attrNsUri The namespace uri for the attribute.
 * @param qname The fully qualified name of the attribute to insert.
 * @param attr The attribute to copy value from.
 * @param el The element to insert the attribute to.
 * @throws ParseException In case of errors.
 */
void createAttributeAndAddToElement(Document doc, String attrNsUri,
                                    String qname, Attr attr, Element el)
    throws ParseException {
  Attr newAttr;
  try {
    newAttr = doc.createAttributeNS(attrNsUri, qname);
  } catch (DOMException e) {
    // Ignore DOMException's like NAMESPACE_ERR if the document being
    // processed is html. Also, remove the associated attribute node.
    if (!asXml) {
      mq.addMessage(DomParserMessageType.IGNORING_TOKEN,
                    Nodes.getFilePositionFor(attr),
                    MessagePart.Factory.valueOf("'" + qname + "'"));
      el.removeAttributeNode(attr);
      return;
    }
    throw new ParseException(new Message(DomParserMessageType.IGNORING_TOKEN,
        Nodes.getFilePositionFor(attr),
        MessagePart.Factory.valueOf("'" + qname + "'")), e);
  }

  newAttr.setValue(attr.getValue());
  if (needsDebugData) {
    Nodes.setFilePositionFor(newAttr, Nodes.getFilePositionFor(attr));
    Nodes.setFilePositionForValue(
        newAttr, Nodes.getFilePositionForValue(attr));
    Nodes.setRawValue(newAttr, Nodes.getRawValue(attr));
  }

  el.removeAttributeNode(attr);
  el.setAttributeNodeNS(newAttr);
}
 
Example 7
Source File: Namespaces.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static void normalizeDeclaration(Attr attr, Element elt, Map<String,String> normalizedPrefixes) {
    String nsUri = attr.getValue();
    String normalizedPrefix = normalizedPrefixes.get(nsUri);
    if (normalizedPrefix != null) {
        elt.removeAttributeNode(attr);
    }
}
 
Example 8
Source File: XMLHelpers.java    From cougar with Apache License 2.0 5 votes vote down vote up
public void renameRootElement(Document document, String newRootElementName) {
	
	Node rootNode = document.getDocumentElement();
	NodeList childNodes = rootNode.getChildNodes();

	NamedNodeMap attributes = rootNode.getAttributes();
	
	document.removeChild(rootNode);
	
	Node newRootNode = document.createElement(newRootElementName); 
	document.appendChild(newRootNode);
		
	int numberOfAttributes = new Integer(attributes.getLength());
	for (int i = 0; i < numberOfAttributes; i++) {
		Attr attributeNode = (Attr)attributes.item(0);
		Element newRootNodeElement = (Element)newRootNode;
		Element rootNodeElement = (Element)rootNode;
		rootNodeElement.removeAttributeNode(attributeNode);
		newRootNodeElement.setAttributeNode(attributeNode);
	}
	      
	int numberOfChildNodes = new Integer(childNodes.getLength());
	for (int i = 0; i < numberOfChildNodes; i++) {
		Node childNode = childNodes.item(0);
		newRootNode.appendChild(childNode);
	}
}
 
Example 9
Source File: XmlFilterReader.java    From knox with Apache License 2.0 4 votes vote down vote up
private void streamAttribute( Element element, Attribute attribute ) throws XPathExpressionException {
  Attr node;
  QName name = attribute.getName();
  String prefix = name.getPrefix();
  String uri = name.getNamespaceURI();
  if( uri == null || uri.isEmpty() ) {
    node = document.createAttribute( name.getLocalPart() );
    element.setAttributeNode( node );
  } else {
    node = document.createAttributeNS( uri, name.getLocalPart() );
    if( prefix != null && !prefix.isEmpty() ) {
      node.setPrefix( prefix );
    }
    element.setAttributeNodeNS( node );
  }

  String value = attribute.getValue();
  Level level = stack.peek();
  if( ( level.scopeConfig ) == null || ( level.scopeConfig.getSelectors().isEmpty() ) ) {
    value = filterAttribute( null, attribute.getName(), value, null );
    node.setValue( value );
  } else {
    UrlRewriteFilterPathDescriptor path = pickFirstMatchingPath( level );
    if( path instanceof UrlRewriteFilterApplyDescriptor ) {
      String rule = ((UrlRewriteFilterApplyDescriptor)path).rule();
      value = filterAttribute( null, attribute.getName(), value, rule );
      node.setValue( value );
    }
  }

  if( prefix == null || prefix.isEmpty() ) {
    writer.write( " " );
    writer.write( name.getLocalPart() );
  } else {
    writer.write( " " );
    writer.write( prefix );
    writer.write( ":" );
    writer.write( name.getLocalPart() );
  }
  writer.write( "=\"" );
  writer.write( value );
  writer.write( "\"" );
  element.removeAttributeNode( node );
}
 
Example 10
Source File: DomXmlAttribute.java    From camunda-spin with Apache License 2.0 4 votes vote down vote up
public SpinXmlElement remove() {
  Element ownerElement = attributeNode.getOwnerElement();
  ownerElement.removeAttributeNode(attributeNode);
  return dataFormat.createElementWrapper(ownerElement);
}
 
Example 11
Source File: CustomizationParser.java    From cxf with Apache License 2.0 4 votes vote down vote up
protected void copyAllJaxbDeclarations(final Node schemaNode, final Element jaxwsBindingNode) {
    if (isSchemaElement(schemaNode)) {
        appendJaxbVersion((Element)schemaNode);
    }

    Node[] embededNodes = getAnnotationNodes(schemaNode);
    Node annotationNode = embededNodes[0];
    Node appinfoNode = embededNodes[1];

    for (Node childNode = jaxwsBindingNode.getFirstChild();
        childNode != null;
        childNode = childNode.getNextSibling()) {

        if (isSchemaElement(schemaNode)) {
            copyJaxbAttributes(childNode, (Element)schemaNode);
        }

        //TODO: check for valid extension namespaces
        if (!(childNode instanceof Element)) { //!isJaxbBindings(childNode)) {
            continue;
        }

        Element childEl = (Element)childNode;
        if (isJaxbBindings(childNode) && isJaxbBindingsElement(childEl)) {

            NodeList nlist = nodeSelector.queryNodes(schemaNode, childEl.getAttribute("node"));
            for (int i = 0; i < nlist.getLength(); i++) {
                Node node = nlist.item(i);
                copyAllJaxbDeclarations(node, childEl);
            }

        } else {
            Element cloneNode = (Element)ProcessorUtil.cloneNode(schemaNode.getOwnerDocument(),
                                                                 childEl, true);

            NamedNodeMap atts = cloneNode.getAttributes();
            for (int x = 0; x < atts.getLength(); x++) {
                Attr attr = (Attr)atts.item(x);
                if (ToolConstants.NS_JAXB_BINDINGS.equals(attr.getNamespaceURI())) {
                    cloneNode.removeAttributeNode(attr);
                    atts = cloneNode.getAttributes();
                    x = -1;
                }
            }
            appinfoNode.appendChild(cloneNode);
        }
    }

    if (schemaNode.getFirstChild() != null) {
        schemaNode.insertBefore(annotationNode, schemaNode.getFirstChild());
    } else {
        schemaNode.appendChild(annotationNode);
    }
}