Java Code Examples for org.w3c.dom.NamedNodeMap#removeNamedItem()

The following examples show how to use org.w3c.dom.NamedNodeMap#removeNamedItem() . 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: ResXmlPatcher.java    From ratel with Apache License 2.0 6 votes vote down vote up
/**
 * Removes "debug" tag from file
 *
 * @param file AndroidManifest file
 * @throws AndrolibException
 */
public static void removeApplicationDebugTag(File file) throws AndrolibException {
    if (file.exists()) {
        try {
            Document doc = loadDocument(file);
            Node application = doc.getElementsByTagName("application").item(0);

            // load attr
            NamedNodeMap attr = application.getAttributes();
            Node debugAttr = attr.getNamedItem("android:debuggable");

            // remove application:debuggable
            if (debugAttr != null) {
                attr.removeNamedItem("android:debuggable");
            }

            saveDocument(file, doc);

        } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
        }
    }
}
 
Example 2
Source File: XmlConfiguration.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private String getType(final Element element) {
    if (strict) {
        final NamedNodeMap attrs = element.getAttributes();
        for (int i = 0; i < attrs.getLength(); ++i) {
            final org.w3c.dom.Node w3cNode = attrs.item(i);
            if (w3cNode instanceof Attr) {
                final Attr attr = (Attr) w3cNode;
                if (attr.getName().equalsIgnoreCase("type")) {
                    final String type = attr.getValue();
                    attrs.removeNamedItem(attr.getName());
                    return type;
                }
            }
        }
    }
    return element.getTagName();
}
 
Example 3
Source File: cfDOCUMENT.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
private void removeBackground(Node _node)
{
	// Remove any background items from the node.
	// For now we only remove the 'bgcolor' attribute.
	NamedNodeMap attributes = _node.getAttributes();
	if ( (attributes != null) && (attributes.getNamedItem("bgcolor") != null) )
		attributes.removeNamedItem("bgcolor");

	// If the node has children then make recursive calls to remove the
	// background items from the children too.
	if (_node.hasChildNodes())
	{
		NodeList children =_node.getChildNodes();
		for (int i = 0; i < children.getLength(); i++)
			removeBackground(children.item(i));
	}
}
 
Example 4
Source File: AbstractXmlNode.java    From JVoiceXML with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
public final void setAttribute(final String name, final String value) {
    final NamedNodeMap attributes = node.getAttributes();

    if (attributes == null) {
        return;
    }

    if (value == null) {
        // Remove the attribute if no value was specified.
        if (attributes.getNamedItem(name) != null) {
            attributes.removeNamedItem(name);
        }
    } else {
        // Remove a possibly existing attribute
        if (attributes.getNamedItem(name) != null) {
            attributes.removeNamedItem(name);
        }
        // Create a new attribute.
        final Document owner = node.getOwnerDocument();
        final Node item = owner.createAttribute(name);
        item.setNodeValue(value);
        attributes.setNamedItem(item);
    }
}
 
Example 5
Source File: ResXmlPatcher.java    From ratel with Apache License 2.0 6 votes vote down vote up
/**
 * Removes attributes like "versionCode" and "versionName" from file.
 *
 * @param file File representing AndroidManifest.xml
 * @throws AndrolibException
 */
public static void removeManifestVersions(File file) throws AndrolibException {
    if (file.exists()) {
        try {
            Document doc = loadDocument(file);
            Node manifest = doc.getFirstChild();
            NamedNodeMap attr = manifest.getAttributes();
            Node vCode = attr.getNamedItem("android:versionCode");
            Node vName = attr.getNamedItem("android:versionName");

            if (vCode != null) {
                attr.removeNamedItem("android:versionCode");
            }
            if (vName != null) {
                attr.removeNamedItem("android:versionName");
            }
            saveDocument(file, doc);

        } catch (SAXException | ParserConfigurationException | IOException | TransformerException ignored) {
        }
    }
}
 
Example 6
Source File: AbstractSoapEncoder.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
/**
 * Check SOS response for xsi:schemaLocation, remove attribute and add
 * attribute to SOAP message
 *
 * @param xmlObject
 *            the document
 * @param soapResponseMessage
 *            SOAP response message
 *
 * @throws SOAPException
 *             If an error occurs
 *
 * @deprecated javax.xml.soap.* is no longer supported from 8.0 because it
 *             was removed from Java
 */
@Deprecated
private void addAndRemoveSchemaLocationForSOAP(XmlObject xmlObject, SOAPMessage soapResponseMessage)
        throws SOAPException {
    String value = null;
    Node nodeToRemove = null;
    NamedNodeMap attributeMap = xmlObject.getDomNode()
            .getFirstChild()
            .getAttributes();
    for (int i = 0; i < attributeMap.getLength(); i++) {
        Node node = attributeMap.item(i);
        if (node.getLocalName()
                .equals(W3CConstants.AN_SCHEMA_LOCATION)) {
            value = node.getNodeValue();
            nodeToRemove = node;
        }
    }
    if (nodeToRemove != null) {
        attributeMap.removeNamedItem(nodeToRemove.getNodeName());
    }
    SOAPEnvelope envelope = soapResponseMessage.getSOAPPart()
            .getEnvelope();
    StringBuilder string = new StringBuilder();
    string.append(envelope.getNamespaceURI());
    string.append(' ');
    string.append(envelope.getNamespaceURI());
    if (value != null && !value.isEmpty()) {
        string.append(' ');
        string.append(value);
    }
    envelope.addAttribute(N52XmlHelper.getSchemaLocationQNameWithPrefix(), string.toString());
}
 
Example 7
Source File: NodeUtils.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Updates the namespace of a given node (and its children) to work in a given document
 * @param node the node to update
 * @param document the new document
 */
private static void updateNamespace(Node node, Document document) {

    // first process this node
    processSingleNodeNamespace(node, document);

    // then its attributes
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0, n = attributes.getLength(); i < n; i++) {
            Node attribute = attributes.item(i);
            if (!processSingleNodeNamespace(attribute, document)) {
                String nsUri = attribute.getNamespaceURI();
                if (nsUri != null) {
                    attributes.removeNamedItemNS(nsUri, attribute.getLocalName());
                } else {
                    attributes.removeNamedItem(attribute.getLocalName());
                }
            }
        }
    }

    // then do it for the children nodes.
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0, n = children.getLength(); i < n; i++) {
            Node child = children.item(i);
            if (child != null) {
                updateNamespace(child, document);
            }
        }
    }
}
 
Example 8
Source File: ClasspathEntry.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static Node removeNode(String nodeName, NamedNodeMap nodeMap) {
	try {
		return nodeMap.removeNamedItem(nodeName);
	} catch (DOMException e) {
		if (e.code != DOMException.NOT_FOUND_ERR)
			throw e;
		return null;
	}
}
 
Example 9
Source File: JavaClasspathParser.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static Node removeNode(String nodeName, NamedNodeMap nodeMap) {
    try {
        return nodeMap.removeNamedItem(nodeName);
    } catch (DOMException e) {
        if (e.code != DOMException.NOT_FOUND_ERR) {
            throw e;
        }
        return null;
    }
}
 
Example 10
Source File: ObjUtil.java    From XACML with MIT License 5 votes vote down vote up
/**
 * Recursively clean up this node and all its children,
 * where "clean" means
 * 	- remove comments
 * 	- remove empty nodes
 * 	- remove xmlns (Namespace) attributes
 * 
 * @param node
 */
private static void cleanXMLNode(Node node) {
	
	//
	// The loops in this method run from back to front because they are removing items from the lists
	//
	
	// remove xmlns (Namespace) attributes
	NamedNodeMap attributes = node.getAttributes();
	
	if (attributes != null) {
		for (int i = attributes.getLength() - 1; i >= 0; i--) {
			Node a = attributes.item(i);
			if (a.getNodeName().startsWith("xmlns")) {
				attributes.removeNamedItem(a.getNodeName());
			}
		}
	}
	
	NodeList childNodes = node.getChildNodes();
	
	for (int i = childNodes.getLength() - 1; i >= 0; i--) {
		Node child = childNodes.item(i);
		
		short type = child.getNodeType();
	
		// remove comments
		if (type == 8) {
			node.removeChild(child);
			continue;
		}

		// node is not text, so clean it too
		cleanXMLNode(child);
	}
	

}
 
Example 11
Source File: NodeUtils.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Updates the namespace of a given node (and its children) to work in a given document
 * @param node the node to update
 * @param document the new document
 */
private static void updateNamespace(Node node, Document document) {

    // first process this node
    processSingleNodeNamespace(node, document);

    // then its attributes
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        for (int i = 0, n = attributes.getLength(); i < n; i++) {
            Node attribute = attributes.item(i);
            if (!processSingleNodeNamespace(attribute, document)) {
                String nsUri = attribute.getNamespaceURI();
                if (nsUri != null) {
                    attributes.removeNamedItemNS(nsUri, attribute.getLocalName());
                } else {
                    attributes.removeNamedItem(attribute.getLocalName());
                }
            }
        }
    }

    // then do it for the children nodes.
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0, n = children.getLength(); i < n; i++) {
            Node child = children.item(i);
            if (child != null) {
                updateNamespace(child, document);
            }
        }
    }
}
 
Example 12
Source File: Soap12Encoder.java    From arctic-sea with Apache License 2.0 4 votes vote down vote up
private XmlObject createSOAP12Envelope(AbstractSoap<?> soap, EncodingContext additionalValues)
        throws EncodingException {
    String action = null;
    final EnvelopeDocument envelopeDoc = EnvelopeDocument.Factory.newInstance();
    final Envelope envelope = envelopeDoc.addNewEnvelope();
    final Body body = envelope.addNewBody();
    if (soap.getSoapFault() != null) {
        body.set(createSOAP12Fault(soap.getSoapFault()));
    } else {
        if (soap instanceof SoapResponse && ((SoapResponse) soap).hasException()) {
            SoapResponse response = (SoapResponse) soap;
            if (!response.getException().getExceptions().isEmpty()) {
                final CodedException firstException = response.getException().getExceptions().get(0);
                action = getExceptionActionURI(firstException.getCode());
            }
            body.set(createSOAP12FaultFromExceptionResponse(response.getException()));
            N52XmlHelper.setSchemaLocationsToDocument(
                    envelopeDoc,
                    Sets.newHashSet(N52XmlHelper.getSchemaLocationForSOAP12(),
                                    N52XmlHelper.getSchemaLocationForOWS110Exception()));
        } else {
            action = soap.getSoapAction();

            final XmlObject bodyContent = getBodyContent(soap);
            String value = null;
            Node nodeToRemove = null;
            final NamedNodeMap attributeMap = bodyContent.getDomNode().getFirstChild().getAttributes();
            for (int i = 0; i < attributeMap.getLength(); i++) {
                final Node node = attributeMap.item(i);
                if (node.getLocalName().equals(W3CConstants.AN_SCHEMA_LOCATION)) {
                    value = node.getNodeValue();
                    nodeToRemove = node;
                }
            }
            if (nodeToRemove != null) {
                attributeMap.removeNamedItem(nodeToRemove.getNodeName());
            }
            final Set<SchemaLocation> schemaLocations = Sets.newHashSet();
            schemaLocations.add(N52XmlHelper.getSchemaLocationForSOAP12());
            if (value != null && !value.isEmpty()) {
                String[] split = value.split(" ");
                for (int i = 0; i <= split.length - 2; i += 2) {
                    schemaLocations.add(new SchemaLocation(split[i], split[i + 1]));
                }
            }
            N52XmlHelper.setSchemaLocationsToDocument(envelopeDoc, schemaLocations);
            body.set(bodyContent);
        }
    }

    if (soap.getHeader() != null) {
        createSOAP12Header(envelope, soap.getHeader(), action);
    } else {
        envelope.addNewHeader();
    }

    // TODO for testing an validating
    // checkAndValidateSoapMessage(envelopeDoc);
    return envelopeDoc;
}
 
Example 13
Source File: ElementImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
protected static Iterator<Name> getAllAttributesFrom(final Element element) {
    final NamedNodeMap attributes = element.getAttributes();

    return new Iterator<Name>() {
        int attributesLength = attributes.getLength();
        int attributeIndex = 0;
        String currentName;

        @Override
        public boolean hasNext() {
            return attributeIndex < attributesLength;
        }

        @Override
        public Name next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }
            Node current = attributes.item(attributeIndex++);
            currentName = current.getNodeName();

            String prefix = NameImpl.getPrefixFromTagName(currentName);
            if (prefix.length() == 0) {
                return NameImpl.createFromUnqualifiedName(currentName);
            } else {
                Name attributeName =
                    NameImpl.createFromQualifiedName(
                        currentName,
                        current.getNamespaceURI());
                return attributeName;
            }
        }

        @Override
        public void remove() {
            if (currentName == null) {
                throw new IllegalStateException();
            }
            attributes.removeNamedItem(currentName);
        }
    };
}
 
Example 14
Source File: DomUtil.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
public static void removeAttribute( Node node, String attName ) {
    NamedNodeMap attributes=node.getAttributes();
    attributes.removeNamedItem(attName);
}
 
Example 15
Source File: DomUtil.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
public static void removeAttribute( Node node, String attName ) {
    NamedNodeMap attributes=node.getAttributes();
    attributes.removeNamedItem(attName);
}
 
Example 16
Source File: DOMUtils.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static void removeAttribute(Node node, String attName) {
    NamedNodeMap attributes = node.getAttributes();
    attributes.removeNamedItem(attName);
}
 
Example 17
Source File: DOMUtils.java    From cxf-fediz with Apache License 2.0 4 votes vote down vote up
public static void removeAttribute(Node node, String attName) {
    NamedNodeMap attributes = node.getAttributes();
    attributes.removeNamedItem(attName);
}