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

The following examples show how to use org.w3c.dom.Element#removeAttributeNS() . 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: SAX2DOMEx.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
protected void namespace(Element element, String prefix, String uri) {
    String qname;
    if ("".equals(prefix) || prefix == null) {
        qname = "xmlns";
    } else {
        qname = "xmlns:" + prefix;
    }

    // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
    // have a problem of re-setting the same namespace attribute twice.
    // work around this bug removing it first.
    if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) {
        // further workaround for an old Crimson bug where the removeAttribtueNS
        // method throws NPE when the element doesn't have any attribute.
        // to be on the safe side, check the existence of attributes before
        // attempting to remove it.
        // for details about this bug, see org.apache.crimson.tree.ElementNode2
        // line 540 or the following message:
        // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
        element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname);
    }
    // workaround until here

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri);
}
 
Example 2
Source File: OverrideProcessor.java    From vespa with Apache License 2.0 6 votes vote down vote up
private void applyOverrides(Element parent, Context context) {
    context = getParentContext(parent, context);

    Map<String, List<Element>> elementsByTagName = elementsByTagNameAndId(XML.getChildren(parent));

    retainOverriddenElements(elementsByTagName);

    // For each tag name, prune overrides
    for (Map.Entry<String, List<Element>> entry : elementsByTagName.entrySet()) {
        pruneOverrides(parent, entry.getValue(), context);
    }

    // Repeat for remaining children;
    for (Element child : XML.getChildren(parent)) {
        applyOverrides(child, context);
        // Remove attributes
        child.removeAttributeNS(XmlPreProcessor.deployNamespaceUri, INSTANCE_ATTRIBUTE);
        child.removeAttributeNS(XmlPreProcessor.deployNamespaceUri, ENVIRONMENT_ATTRIBUTE);
        child.removeAttributeNS(XmlPreProcessor.deployNamespaceUri, REGION_ATTRIBUTE);
    }
}
 
Example 3
Source File: SAX2DOMEx.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
protected void namespace(Element element, String prefix, String uri) {
    String qname;
    if ("".equals(prefix) || prefix == null) {
        qname = "xmlns";
    } else {
        qname = "xmlns:" + prefix;
    }

    // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
    // have a problem of re-setting the same namespace attribute twice.
    // work around this bug removing it first.
    if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) {
        // further workaround for an old Crimson bug where the removeAttribtueNS
        // method throws NPE when the element doesn't have any attribute.
        // to be on the safe side, check the existence of attributes before
        // attempting to remove it.
        // for details about this bug, see org.apache.crimson.tree.ElementNode2
        // line 540 or the following message:
        // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
        element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname);
    }
    // workaround until here

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri);
}
 
Example 4
Source File: SAX2DOMEx.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
protected void namespace(Element element, String prefix, String uri) {
    String qname;
    if ("".equals(prefix) || prefix == null) {
        qname = "xmlns";
    } else {
        qname = "xmlns:" + prefix;
    }

    // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
    // have a problem of re-setting the same namespace attribute twice.
    // work around this bug removing it first.
    if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) {
        // further workaround for an old Crimson bug where the removeAttribtueNS
        // method throws NPE when the element doesn't have any attribute.
        // to be on the safe side, check the existence of attributes before
        // attempting to remove it.
        // for details about this bug, see org.apache.crimson.tree.ElementNode2
        // line 540 or the following message:
        // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
        element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname);
    }
    // workaround until here

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri);
}
 
Example 5
Source File: SAX2DOMEx.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
protected void namespace(Element element, String prefix, String uri) {
    String qname;
    if ("".equals(prefix) || prefix == null) {
        qname = "xmlns";
    } else {
        qname = "xmlns:" + prefix;
    }

    // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
    // have a problem of re-setting the same namespace attribute twice.
    // work around this bug removing it first.
    if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) {
        // further workaround for an old Crimson bug where the removeAttribtueNS
        // method throws NPE when the element doesn't have any attribute.
        // to be on the safe side, check the existence of attributes before
        // attempting to remove it.
        // for details about this bug, see org.apache.crimson.tree.ElementNode2
        // line 540 or the following message:
        // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
        element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname);
    }
    // workaround until here

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri);
}
 
Example 6
Source File: SAX2DOMEx.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
protected void namespace(Element element, String prefix, String uri) {
    String qname;
    if ("".equals(prefix) || prefix == null) {
        qname = "xmlns";
    } else {
        qname = "xmlns:" + prefix;
    }

    // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
    // have a problem of re-setting the same namespace attribute twice.
    // work around this bug removing it first.
    if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) {
        // further workaround for an old Crimson bug where the removeAttribtueNS
        // method throws NPE when the element doesn't have any attribute.
        // to be on the safe side, check the existence of attributes before
        // attempting to remove it.
        // for details about this bug, see org.apache.crimson.tree.ElementNode2
        // line 540 or the following message:
        // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
        element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname);
    }
    // workaround until here

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri);
}
 
Example 7
Source File: SAX2DOMEx.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
protected void namespace(Element element, String prefix, String uri) {
    String qname;
    if ("".equals(prefix) || prefix == null) {
        qname = "xmlns";
    } else {
        qname = "xmlns:" + prefix;
    }

    // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
    // have a problem of re-setting the same namespace attribute twice.
    // work around this bug removing it first.
    if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) {
        // further workaround for an old Crimson bug where the removeAttribtueNS
        // method throws NPE when the element doesn't have any attribute.
        // to be on the safe side, check the existence of attributes before
        // attempting to remove it.
        // for details about this bug, see org.apache.crimson.tree.ElementNode2
        // line 540 or the following message:
        // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
        element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname);
    }
    // workaround until here

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri);
}
 
Example 8
Source File: SAX2DOMEx.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
protected void namespace(Element element, String prefix, String uri) {
    String qname;
    if ("".equals(prefix) || prefix == null) {
        qname = "xmlns";
    } else {
        qname = "xmlns:" + prefix;
    }

    // older version of Xerces (I confirmed that the bug is gone with Xerces 2.4.0)
    // have a problem of re-setting the same namespace attribute twice.
    // work around this bug removing it first.
    if (element.hasAttributeNS("http://www.w3.org/2000/xmlns/", qname)) {
        // further workaround for an old Crimson bug where the removeAttribtueNS
        // method throws NPE when the element doesn't have any attribute.
        // to be on the safe side, check the existence of attributes before
        // attempting to remove it.
        // for details about this bug, see org.apache.crimson.tree.ElementNode2
        // line 540 or the following message:
        // https://jaxb.dev.java.net/servlets/ReadMsg?list=users&msgNo=2767
        element.removeAttributeNS("http://www.w3.org/2000/xmlns/", qname);
    }
    // workaround until here

    element.setAttributeNS("http://www.w3.org/2000/xmlns/", qname, uri);
}
 
Example 9
Source File: LocationAttributes.java    From walkmod-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Remove the location attributes from a DOM element.
 * 
 * @param elem
 *            the element to remove the location attributes from.
 * @param recurse
 *            if <code>true</code>, also remove location attributes on
 *            descendant elements.
 */
public static void remove(Element elem, boolean recurse) {
	elem.removeAttributeNS(URI, SRC_ATTR);
	elem.removeAttributeNS(URI, LINE_ATTR);
	elem.removeAttributeNS(URI, COL_ATTR);
	if (recurse) {
		NodeList children = elem.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				remove((Element) child, recurse);
			}
		}
	}
}
 
Example 10
Source File: ElementTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testRemoveAttributeNS() throws Exception {
    final String nsURI = "urn:BooksAreUs.org:BookInfo";
    final String localName = "category";
    Document document = createDOMWithNS("ElementSample01.xml");
    Element elemNode = (Element) document.getElementsByTagName("book").item(0);
    elemNode.removeAttributeNS(nsURI, localName);

    assertNull(elemNode.getAttributeNodeNS(nsURI, localName));
}
 
Example 11
Source File: FoxmlUtils.java    From proarc with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Fixes DublinCore exported by Fedora Commons.
 * <p>The {@code schemaLocation} is removed.
 * <br>The namespace declaration is added to the root element.
 * @param dcRoot the root element of DC
 * @return the root element of DC
 */
public static Element fixFoxmlDc(Element dcRoot) {
    // remove xsi:schemaLocation attribute to make FOXML valid for Fedora ingest
    dcRoot.removeAttributeNS(XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "schemaLocation");
    // optimize XML namespace declaration
    dcRoot.setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,
            XMLConstants.XMLNS_ATTRIBUTE + ":" + DcConstants.PREFIX_NS_PURL,
            DcConstants.NS_PURL);
    return dcRoot;
}
 
Example 12
Source File: Documents.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void removeAttribute(Element e, QName qn) {
    String ns       = qn.getNamespaceURI();
    String ln       = qn.getLocalPart();
    if ((ns == null) || (ns.length() == 0))
        e.removeAttribute(ln);
    else
        e.removeAttributeNS(ns, ln);
}
 
Example 13
Source File: LocationAttributes.java    From walkmod-core with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Remove the location attributes from a DOM element.
 * 
 * @param elem
 *            the element to remove the location attributes from.
 * @param recurse
 *            if <code>true</code>, also remove location attributes on
 *            descendant elements.
 */
public static void remove(Element elem, boolean recurse) {
	elem.removeAttributeNS(URI, SRC_ATTR);
	elem.removeAttributeNS(URI, LINE_ATTR);
	elem.removeAttributeNS(URI, COL_ATTR);
	if (recurse) {
		NodeList children = elem.getChildNodes();
		for (int i = 0; i < children.getLength(); i++) {
			Node child = children.item(i);
			if (child.getNodeType() == Node.ELEMENT_NODE) {
				remove((Element) child, recurse);
			}
		}
	}
}
 
Example 14
Source File: XMLUtil.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void removeXmlBase(Element e) {
    e.removeAttributeNS("http://www.w3.org/XML/1998/namespace", "base"); // NOI18N
    e.removeAttribute("xml:base"); // NOI18N
}
 
Example 15
Source File: ToolsInstructionsCleaner.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private static MergingReport.Result cleanToolsReferences(
        Element element,
        ILogger logger) {

    NamedNodeMap namedNodeMap = element.getAttributes();
    if (namedNodeMap != null) {
        // make a copy of the original list of attributes as we will remove some during this
        // process.
        List<Node> attributes = new ArrayList<Node>();
        for (int i = 0; i < namedNodeMap.getLength(); i++) {
            attributes.add(namedNodeMap.item(i));
        }
        for (Node attribute : attributes) {
            if (SdkConstants.TOOLS_URI.equals(attribute.getNamespaceURI())) {
                // we need to special case when the element contained tools:node="remove"
                // since it also needs to be deleted unless it had a selector.
                // if this is ools:node="removeAll", we always delete the element whether or
                // not there is a tools:selector.
                boolean hasSelector = namedNodeMap.getNamedItemNS(
                        SdkConstants.TOOLS_URI, "selector") != null;
                if (attribute.getLocalName().equals(NodeOperationType.NODE_LOCAL_NAME)
                        && (attribute.getNodeValue().equals(REMOVE_ALL_OPERATION_XML_MAME)
                            || (attribute.getNodeValue().equals(REMOVE_OPERATION_XML_MAME))
                                && !hasSelector)) {

                    if (element.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
                        logger.error(null /* Throwable */,
                                String.format(
                                    "tools:node=\"%1$s\" not allowed on top level %2$s element",
                                    attribute.getNodeValue(),
                                    XmlNode.unwrapName(element)));
                        return ERROR;
                    } else {
                        element.getParentNode().removeChild(element);
                    }
                } else {
                    // anything else, we just clean the attribute.
                    element.removeAttributeNS(
                            attribute.getNamespaceURI(), attribute.getLocalName());
                }
            }
            // this could also be the xmlns:tools declaration.
            if (attribute.getNodeName().startsWith(SdkConstants.XMLNS_PREFIX)
                && SdkConstants.TOOLS_URI.equals(attribute.getNodeValue())) {
                element.removeAttribute(attribute.getNodeName());
            }
        }
    }
    // make a copy of the element children since we will be removing some during
    // this process, we don't want side effects.
    NodeList childNodes = element.getChildNodes();
    ImmutableList.Builder<Element> childElements = ImmutableList.builder();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) node);
        }
    }
    for (Element childElement : childElements.build()) {
        if (cleanToolsReferences(childElement, logger) == ERROR) {
            return ERROR;
        }
    }
    return MergingReport.Result.SUCCESS;
}
 
Example 16
Source File: ToolsInstructionsCleaner.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
private static MergingReport.Result cleanToolsReferences(
        Element element,
        ILogger logger) {

    NamedNodeMap namedNodeMap = element.getAttributes();
    if (namedNodeMap != null) {
        // make a copy of the original list of attributes as we will remove some during this
        // process.
        List<Node> attributes = new ArrayList<Node>();
        for (int i = 0; i < namedNodeMap.getLength(); i++) {
            attributes.add(namedNodeMap.item(i));
        }
        for (Node attribute : attributes) {
            if (SdkConstants.TOOLS_URI.equals(attribute.getNamespaceURI())) {
                // we need to special case when the element contained tools:node="remove"
                // since it also needs to be deleted unless it had a selector.
                // if this is ools:node="removeAll", we always delete the element whether or
                // not there is a tools:selector.
                boolean hasSelector = namedNodeMap.getNamedItemNS(
                        SdkConstants.TOOLS_URI, "selector") != null;
                if (attribute.getLocalName().equals(NodeOperationType.NODE_LOCAL_NAME)
                        && (attribute.getNodeValue().equals(REMOVE_ALL_OPERATION_XML_MAME)
                            || (attribute.getNodeValue().equals(REMOVE_OPERATION_XML_MAME))
                                && !hasSelector)) {

                    if (element.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
                        logger.error(null /* Throwable */,
                                String.format(
                                    "tools:node=\"%1$s\" not allowed on top level %2$s element",
                                    attribute.getNodeValue(),
                                    XmlNode.unwrapName(element)));
                        return ERROR;
                    } else {
                        element.getParentNode().removeChild(element);
                    }
                } else {
                    // anything else, we just clean the attribute.
                    element.removeAttributeNS(
                            attribute.getNamespaceURI(), attribute.getLocalName());
                }
            }
            // this could also be the xmlns:tools declaration.
            if (attribute.getNodeName().startsWith(SdkConstants.XMLNS_PREFIX)
                && SdkConstants.TOOLS_URI.equals(attribute.getNodeValue())) {
                element.removeAttribute(attribute.getNodeName());
            }
        }
    }
    // make a copy of the element children since we will be removing some during
    // this process, we don't want side effects.
    NodeList childNodes = element.getChildNodes();
    ImmutableList.Builder<Element> childElements = ImmutableList.builder();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) node);
        }
    }
    for (Element childElement : childElements.build()) {
        if (cleanToolsReferences(childElement, logger) == ERROR) {
            return ERROR;
        }
    }
    return MergingReport.Result.SUCCESS;
}
 
Example 17
Source File: XMLUtil.java    From org.hl7.fhir.core with Apache License 2.0 4 votes vote down vote up
public static void deleteAttr(Element e, String namespaceURI, String localName) {
  if (e.hasAttributeNS(namespaceURI, localName))
    e.removeAttributeNS(namespaceURI, localName);
  
}
 
Example 18
Source File: ISD.java    From ttt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static void pruneTimingAttributes(Element elt) {
    elt.removeAttributeNS(null, "begin");
    elt.removeAttributeNS(null, "end");
    elt.removeAttributeNS(null, "dur");
    elt.removeAttributeNS(null, "timeContainer");
}
 
Example 19
Source File: ISD.java    From ttt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static void pruneRegionAttributes(Element elt) {
    elt.removeAttributeNS(null, "region");
}
 
Example 20
Source File: ToolsInstructionsCleaner.java    From buck with Apache License 2.0 4 votes vote down vote up
@NonNull
private static MergingReport.Result cleanToolsReferences(
        @NonNull ManifestMerger2.MergeType mergeType,
        @NonNull Element element,
        @NonNull ILogger logger) {

    NamedNodeMap namedNodeMap = element.getAttributes();
    if (namedNodeMap != null) {
        // make a copy of the original list of attributes as we will remove some during this
        // process.
        List<Node> attributes = new ArrayList<Node>();
        for (int i = 0; i < namedNodeMap.getLength(); i++) {
            attributes.add(namedNodeMap.item(i));
        }
        for (Node attribute : attributes) {
            if (SdkConstants.TOOLS_URI.equals(attribute.getNamespaceURI())) {
                // we need to special case when the element contained tools:node="remove"
                // since it also needs to be deleted unless it had a selector.
                // if this is tools:node="removeAll", we always delete the element whether or
                // not there is a tools:selector.
                boolean hasSelector = namedNodeMap.getNamedItemNS(
                        SdkConstants.TOOLS_URI, "selector") != null;
                if (attribute.getLocalName().equals(NodeOperationType.NODE_LOCAL_NAME)
                        && (attribute.getNodeValue().equals(REMOVE_ALL_OPERATION_XML_MAME)
                            || (attribute.getNodeValue().equals(REMOVE_OPERATION_XML_MAME))
                                && !hasSelector)) {

                    if (element.getParentNode().getNodeType() == Node.DOCUMENT_NODE) {
                        logger.error(null /* Throwable */,
                                String.format(
                                    "tools:node=\"%1$s\" not allowed on top level %2$s element",
                                    attribute.getNodeValue(),
                                    XmlNode.unwrapName(element)));
                        return ERROR;
                    } else {
                        element.getParentNode().removeChild(element);
                    }
                } else {
                    // anything else, we just clean the attribute unless we are merging for
                    // libraries.
                    if (mergeType != ManifestMerger2.MergeType.LIBRARY) {
                        element.removeAttributeNS(
                                attribute.getNamespaceURI(), attribute.getLocalName());
                    }
                }
            }
            // this could also be the xmlns:tools declaration.
            if (attribute.getNodeName().startsWith(SdkConstants.XMLNS_PREFIX)
                && SdkConstants.TOOLS_URI.equals(attribute.getNodeValue())
                    && mergeType != ManifestMerger2.MergeType.LIBRARY) {
                element.removeAttribute(attribute.getNodeName());
            }
        }
    }
    // make a copy of the element children since we will be removing some during
    // this process, we don't want side effects.
    NodeList childNodes = element.getChildNodes();
    ImmutableList.Builder<Element> childElements = ImmutableList.builder();
    for (int i = 0; i < childNodes.getLength(); i++) {
        Node node = childNodes.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            childElements.add((Element) node);
        }
    }
    for (Element childElement : childElements.build()) {
        if (cleanToolsReferences(mergeType, childElement, logger) == ERROR) {
            return ERROR;
        }
    }
    return MergingReport.Result.SUCCESS;
}