Example usage for org.w3c.dom NamedNodeMap removeNamedItem

List of usage examples for org.w3c.dom NamedNodeMap removeNamedItem

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap removeNamedItem.

Prototype

public Node removeNamedItem(String name) throws DOMException;

Source Link

Document

Removes a node specified by name.

Usage

From source file:org.adl.parsers.dom.DOMTreeUtility.java

/**
 * This method removes the specified attribute from the specified node
 *
 * @param iNode The node whose attribute is to be removed
 * @param iAttributeName The name of the attribute to be removed
 *///  www.  ja  va  2s. c  o m
public static void removeAttribute(Node iNode, String iAttributeName) {
    NamedNodeMap attrList = iNode.getAttributes();
    attrList.removeNamedItem(iAttributeName);
}

From source file:org.apache.any23.extractor.html.HCardExtractor.java

private void fixIncludes(HTMLDocument document, Node node, IssueReport report) {
    NamedNodeMap attributes = node.getAttributes();
    // header case test 32
    if ("TD".equals(node.getNodeName()) && (null != attributes.getNamedItem("headers"))) {
        String id = attributes.getNamedItem("headers").getNodeValue();
        Node header = document.findNodeById(id);
        if (null != header) {
            node.appendChild(header.cloneNode(true));
            attributes.removeNamedItem("headers");
        }//  ww  w .  j a v a 2s  .c  o m
    }

    // include pattern, test 31
    for (Node current : DomUtils.findAllByAttributeName(document.getDocument(), "class")) {
        if (!DomUtils.hasClassName(current, "include"))
            continue;
        // we have to remove the field soon to avoid infinite loops
        // no null check, we know it's there or we won't be in the loop
        current.getAttributes().removeNamedItem("class");
        ArrayList<TextField> res = new ArrayList<TextField>();
        HTMLDocument.readUrlField(res, current);
        TextField id = res.get(0);
        if (null == id)
            continue;
        TextField refId = new TextField(StringUtils.substringAfter(id.value(), "#"), id.source());
        Node included = document.findNodeById(refId.value());
        if (null == included)
            continue;
        if (DomUtils.isAncestorOf(included, current)) {
            final int[] nodeLocation = DomUtils.getNodeLocation(current);
            report.notifyIssue(IssueReport.IssueLevel.Warning,
                    "Current node tries to include an ancestor node.", nodeLocation[0], nodeLocation[1]);
            continue;
        }
        current.appendChild(included.cloneNode(true));
    }
}

From source file:org.apache.geode.session.tests.ContainerInstall.java

/**
 * Replaces the node's attributes with the attributes in the given hashmap
 * //from ww w. j a  va2  s . c o m
 * @param node XML node that should be edited
 * @param attributes HashMap of strings representing the attributes of a node (key = value)
 * @return The given node with ONLY the given attributes
 */
private static Node rewriteNodeAttributes(Node node, HashMap<String, String> attributes) {
    NamedNodeMap nodeAttrs = node.getAttributes();

    // Remove all previous attributes
    while (nodeAttrs.getLength() > 0) {
        nodeAttrs.removeNamedItem(nodeAttrs.item(0).getNodeName());
    }

    // Set to new attributes
    for (String key : attributes.keySet()) {
        ((Element) node).setAttribute(key, attributes.get(key));
    }

    return node;
}

From source file:org.apache.openaz.xacml.util.ObjUtil.java

/**
 * Recursively clean up this node and all its children, where "clean" means - remove comments - remove
 * empty nodes - remove xmlns (Namespace) attributes
 *
 * @param node//ww w .ja v a  2s.  c o  m
 */
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);
    }

}

From source file:org.deri.any23.extractor.html.HCardExtractor.java

private void fixIncludes(HTMLDocument document, Node node) {
    NamedNodeMap attributes = node.getAttributes();
    // header case test 32
    if ("TD".equals(node.getNodeName()) && (null != attributes.getNamedItem("headers"))) {
        String id = attributes.getNamedItem("headers").getNodeValue();
        Node header = document.findNodeById(id);
        if (null != header) {
            node.appendChild(header.cloneNode(true));
            attributes.removeNamedItem("headers");
        }/*  w  ww  .j  a v  a  2  s.  co m*/
    }
    // include pattern, test 31

    for (Node current : document.findAll("//*[@class]")) {
        if (!DomUtils.hasClassName(current, "include"))
            continue;
        // we have to remove the field soon to avoid infinite loops
        // no null check, we know it's there or we won't be in the loop
        current.getAttributes().removeNamedItem("class");
        ArrayList<TextField> res = new ArrayList<TextField>();
        HTMLDocument.readUrlField(res, current);
        TextField id = res.get(0);
        if (null == id)
            continue;
        id = new TextField(StringUtils.substringAfter(id.value(), "#"), id.source());
        Node included = document.findNodeById(id.value());
        if (null == included)
            continue;
        current.appendChild(included.cloneNode(true));
    }
}

From source file:org.openehealth.coms.cc.web_frontend.consentcreator.service.DocumentFactory.java

/**
 * Constructs a CDA Consent for the given user.
 * /* ww w .j  a va2 s.c om*/
 * @param user
 * @param policySet
 *            - null if either the participation should end or if a new
 *            PolicySet should be created.
 * @param author
 * @param participation
 *            - true if the user wants to participate, else false
 * @param out
 *            - The OutputStream on which the generated PDF presentation
 *            will be written
 * @return - The generated CDA file.
 * 
 */
public Document constructCDA(User user, Document policySet, User author, boolean participation,
        OutputStream out) {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document domCDA = null;
    Document domAssignedAuthor = null;
    Document domScanningDevice = null;
    Document domDataEnterer = null;
    Document domCustodian = null;
    Document domLegalAuthenticator = null;
    String cdaAsString = "";

    DocumentBuilder db = null;
    try {
        db = dbf.newDocumentBuilder();

        domCDA = db.parse(urlCdaSkeleton);
        domAssignedAuthor = db.parse(urlAssignedAuthor);
        domScanningDevice = db.parse(urlScanningDevice);
        domDataEnterer = db.parse(urlDataEnterer);
        domCustodian = db.parse(urlCustodian);
        domLegalAuthenticator = db.parse(urlLegalAuthenticator);

    } catch (Exception e) {
        Logger.getLogger(this.getClass()).error(e);
    }

    Date d = new Date();

    String uniqueID = RandomStringUtils.randomNumeric(64);

    domCDA.getElementsByTagName("id").item(0).getAttributes().item(0).setNodeValue(uniqueID);
    domCDA.getElementsByTagName("id").item(0).getAttributes().item(1)
            .setNodeValue("1.2.276.0.76.3.1.78.1.0.10.40");

    SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmmssZ");

    domCDA.getElementsByTagName("effectiveTime").item(0).getAttributes().item(0).setTextContent(sd.format(d));

    Node title = domCDA.getElementsByTagName("title").item(0);

    if (participation) {
        title.setTextContent("Dies ist die Einwilligungserklrung fr die Teilnahme an ISIS von "
                + user.getForename() + " " + user.getName());
    } else {
        title.setTextContent("Dieses Dokument erklrt den Verzicht von " + user.getForename() + " "
                + user.getName() + " auf die Teilnahme an ISIS.");
    }

    Node pR = domCDA.getElementsByTagName("patientRole").item(0);

    NodeList prChildren = pR.getChildNodes();

    Node id = prChildren.item(1);

    // extension
    id.getAttributes().item(0).setNodeValue(user.getID()); // PID
    // root
    id.getAttributes().item(1).setNodeValue("1.2.276.0.76.3.1.78.1.0.10.20"); // Assigning
    // Authority

    NodeList addr = prChildren.item(3).getChildNodes();

    addr.item(1).setTextContent(user.getStreet());
    addr.item(3).setTextContent(user.getCity());
    addr.item(5).setTextContent("");//Hier knnte das Bundesland
    // stehen!

    addr.item(7).setTextContent(String.valueOf(user.getZipcode()));
    addr.item(9).setTextContent("Deutschland");// Es sollte nicht
    // standardmig Deutschland
    // gesetzt werden!

    NodeList pat = prChildren.item(5).getChildNodes();

    NodeList name = pat.item(1).getChildNodes();

    if (user.getGender().equalsIgnoreCase("male")) {
        name.item(1).setTextContent("Herr");
        pat.item(3).getAttributes().item(0).setTextContent("M");
    } else {
        name.item(1).setTextContent("Frau");
        pat.item(3).getAttributes().item(0).setTextContent("F");
    }
    name.item(3).setTextContent(user.getForename());
    name.item(5).setTextContent(user.getName());

    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

    pat.item(5).getAttributes().item(0).setTextContent(formatter.format(user.getBirthdate()));

    if (policySet == null) {
        policySet = getSkeletonPolicySet(participation, user);

    }
    Document doc = db.newDocument();

    Element root = doc.createElement("component");
    doc.appendChild(root);

    Node copy = doc.importNode(policySet.getDocumentElement(), true);
    root.appendChild(copy);

    NodeList list = domCDA.getElementsByTagName("structuredBody");

    Node f = domCDA.importNode(doc.getDocumentElement(), true);

    list.item(0).appendChild(f);

    Node docuOfNode = domCDA.getElementsByTagName("documentationOf").item(0);

    if (author == user) {

        domAssignedAuthor.getElementsByTagName("time").item(0).getAttributes().item(0)
                .setNodeValue(sd.format(d));

        domAssignedAuthor.getElementsByTagName("id").item(0).getAttributes().item(0)
                .setNodeValue(author.getID()); // ID
        domAssignedAuthor.getElementsByTagName("id").item(0).getAttributes().item(1)
                .setNodeValue("1.2.276.0.76.3.1.78.1.0.10.20");// MPI-assigning
        // authority

        if (user.getGender().equalsIgnoreCase("male")) {
            domAssignedAuthor.getElementsByTagName("prefix").item(0).setTextContent("Herr");
        } else {
            name.item(1).setTextContent("Frau");
            domAssignedAuthor.getElementsByTagName("prefix").item(0).setTextContent("Frau");
        }

        // suffix Wert setzen
        // domAssignedAuthor.getElementsByTagName("suffix").item(0).getAttributes().item(0).setNodeValue("");

        domAssignedAuthor.getElementsByTagName("given").item(0).setTextContent(author.getForename());
        domAssignedAuthor.getElementsByTagName("family").item(0).setTextContent(author.getName());

        // TODO Werte setzen assignedPerson oder Werte loeschen
    } else if (author != user && author != null) {
        // Zukunft: Erzeugung durch Dritte
    } else {
        // Std values from AssignedAuthor.xml
    }

    Node copyAssignedAuthorNode = domCDA.importNode(domAssignedAuthor.getDocumentElement(), true);
    Node copyScanningDeviceNode = domCDA.importNode(domScanningDevice.getDocumentElement(), true);
    Node copyDataEntererNode = domCDA.importNode(domDataEnterer.getDocumentElement(), true);
    Node copyCustodianNode = domCDA.importNode(domCustodian.getDocumentElement(), true);
    Node copyLegalAuthenticator = domCDA.importNode(domLegalAuthenticator.getDocumentElement(), true);

    sd.applyPattern("yyyyMMdd");

    domCDA.getElementsByTagName("low").item(0).getAttributes().item(0).setNodeValue(sd.format(d));

    Date d2 = new Date((long) (d.getTime() + 30 * 3.1556926 * Math.pow(10, 10)));

    domCDA.getElementsByTagName("high").item(0).getAttributes().item(0).setNodeValue(sd.format(d2));

    copyAssignedAuthorNode.getChildNodes().item(3).getAttributes().item(0).setNodeValue(sd.format(d));

    Node clinicalDocumentNode = domCDA.getElementsByTagName("ClinicalDocument").item(0);

    clinicalDocumentNode.insertBefore(copyAssignedAuthorNode, docuOfNode);
    clinicalDocumentNode.insertBefore(copyScanningDeviceNode, docuOfNode);
    clinicalDocumentNode.insertBefore(copyDataEntererNode, docuOfNode);
    clinicalDocumentNode.insertBefore(copyCustodianNode, docuOfNode);
    clinicalDocumentNode.insertBefore(copyLegalAuthenticator, docuOfNode);

    Document noNSCDA = (Document) domCDA.cloneNode(true);

    NodeList l = noNSCDA.getElementsByTagName("ClinicalDocument");

    NamedNodeMap attributes = l.item(0).getAttributes();
    attributes.removeNamedItem("xmlns");
    attributes.removeNamedItem("xmlns:voc");
    attributes.removeNamedItem("xmlns:xsi");
    attributes.removeNamedItem("xsi:schemaLocation");

    l = noNSCDA.getElementsByTagName("PolicySet");
    attributes = l.item(0).getAttributes();
    attributes.removeNamedItem("xmlns");
    attributes.removeNamedItem("PolicyCombiningAlgId");
    attributes.removeNamedItem("xmlns:xsi");
    attributes.removeNamedItem("xsi:schemaLocation");
    attributes.removeNamedItem("PolicySetId");

    formatter = new SimpleDateFormat("dd.MM.yyyy");

    noNSCDA.getElementsByTagName("birthTime").item(0).getAttributes().item(0)
            .setTextContent(formatter.format(user.getBirthdate()));

    cdaAsString = getStringFromDocument(noNSCDA);
    Document nonXMLBody = constructPDF(cdaAsString, out);

    Node copyNode = domCDA.importNode(nonXMLBody.getDocumentElement(), true);

    Node component = domCDA.getElementsByTagName("component").item(0);

    Node structBody = component.getChildNodes().item(1);

    component.insertBefore(copyNode, structBody);

    NamedNodeMap nlm = domCDA.getDocumentElement().getElementsByTagName("PolicySet").item(0).getAttributes();
    // nlm.removeNamedItem("xmlns:xsi");
    // nlm.removeNamedItem("xsi:schemaLocation");

    return domCDA;
}

From source file:org.wso2.carbon.tomcat.internal.ServerManager.java

/**
 * Resolves the secured attributes in the configuration
 * Here we have assumed that secured attributes in each connector
 * has the same secret value. As an example, if two connectors has
 * keystore password value, it must be same value.
 *
 * @param root <code>Element</code>
 * @param tempToken  secured attribute alias as <code>String</code> 
 *//* w w  w.j  a  v  a2  s  .  com*/
private void resolveSecuredConfig(Node root, String tempToken) {

    String token = null;
    NamedNodeMap nodeMap = root.getAttributes();
    if (tempToken == null) {
        tempToken = root.getNodeName();
    } else {
        tempToken = tempToken + "." + root.getNodeName();
    }
    if (nodeMap != null) {
        for (int j = 0; j < nodeMap.getLength(); j++) {
            Node node = nodeMap.item(j);
            if (node != null) {
                String attributeName = node.getNodeName();
                token = tempToken + "." + attributeName;
                if (resolver.isTokenProtected(token)) {
                    node.setNodeValue(resolver.resolve(token));
                    nodeMap.removeNamedItem(
                            SVNS + SecurityConstants.NS_SEPARATOR + SecurityConstants.SECURE_VAULT_ALIAS);
                }
            }
        }
    }

    NodeList nodeList = root.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        resolveSecuredConfig(nodeList.item(i), tempToken);
    }
}