Example usage for org.w3c.dom Node removeChild

List of usage examples for org.w3c.dom Node removeChild

Introduction

In this page you can find the example usage for org.w3c.dom Node removeChild.

Prototype

public Node removeChild(Node oldChild) throws DOMException;

Source Link

Document

Removes the child node indicated by oldChild from the list of children, and returns it.

Usage

From source file:org.apache.ode.bpel.runtime.AssignHelper.java

/**
 * isInsert flag desginates this as an 'element' type insertion, which
 * requires insert the actual element value, rather than it's children
 *
 * @return/*from w ww. j ava  2s.  co m*/
 * @throws FaultException
 */
private Node replaceContent(Node lvalue, Node lvaluePtr, String rvalue) throws FaultException {
    Document d = lvaluePtr.getOwnerDocument();

    if (__log.isDebugEnabled()) {
        __log.debug("lvaluePtr type " + lvaluePtr.getNodeType());
        __log.debug("lvaluePtr " + DOMUtils.domToString(lvaluePtr));
        __log.debug("lvalue " + lvalue);
        __log.debug("rvalue " + rvalue);
    }

    switch (lvaluePtr.getNodeType()) {
    case Node.ELEMENT_NODE:

        // Remove all the children.
        while (lvaluePtr.hasChildNodes())
            lvaluePtr.removeChild(lvaluePtr.getFirstChild());

        // Append a new text node.
        lvaluePtr.appendChild(d.createTextNode(rvalue));

        // If lvalue is a text, removing all lvaluePtr children had just removed it
        // so we need to rebuild it as a child of lvaluePtr
        if (lvalue instanceof Text)
            lvalue = lvaluePtr.getFirstChild();
        break;

    case Node.TEXT_NODE:

        Node newval = d.createTextNode(rvalue);
        // Replace ourselves .
        lvaluePtr.getParentNode().replaceChild(newval, lvaluePtr);

        // A little kludge, let our caller know that the root element has changed.
        // (used for assignment to a simple typed variable)
        if (lvalue.getNodeType() == Node.ELEMENT_NODE) {
            // No children, adding an empty text children to point to
            if (lvalue.getFirstChild() == null) {
                Text txt = lvalue.getOwnerDocument().createTextNode("");
                lvalue.appendChild(txt);
            }
            if (lvalue.getFirstChild().getNodeType() == Node.TEXT_NODE)
                lvalue = lvalue.getFirstChild();
        }
        if (lvalue.getNodeType() == Node.TEXT_NODE
                && ((Text) lvalue).getWholeText().equals(((Text) lvaluePtr).getWholeText()))
            lvalue = lvaluePtr = newval;
        break;

    case Node.ATTRIBUTE_NODE:

        ((Attr) lvaluePtr).setValue(rvalue);
        break;

    default:
        // This could occur if the expression language selects something
        // like
        // a PI or a CDATA.
        String msg = __msgs.msgInvalidLValue();
        if (__log.isDebugEnabled())
            __log.debug(lvaluePtr + ": " + msg);
        throw new FaultException(getOActivity().getOwner().constants.qnSelectionFailure, msg);
    }

    return lvalue;
}

From source file:org.apache.ode.utils.DOMUtils.java

/**
 * Remove the child nodes under another node.
 * @param target the <code>Node</code> to remove the children from.
 *//*from  ww  w.j  a v  a2 s .c o  m*/
public static void removeChildren(Node target) {
    while (target.hasChildNodes()) {
        target.removeChild(target.getFirstChild());
    }
}

From source file:org.apache.openaz.xacml.pdp.policy.dom.DOMAdviceExpression.java

public static boolean repair(Node nodeAdviceExpression) throws DOMStructureException {
    Element elementAdviceExpression = DOMUtil.getElement(nodeAdviceExpression);
    boolean result = false;

    NodeList children = elementAdviceExpression.getChildNodes();
    int numChildren;
    if (children != null && (numChildren = children.getLength()) > 0) {
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (DOMUtil.isElement(child)) {
                if (DOMUtil.isInNamespace(child, XACML3.XMLNS)
                        && XACML3.ELEMENT_ATTRIBUTEASSIGNMENTEXPRESSION.equals(child.getLocalName())) {
                    result = DOMAttributeAssignmentExpression.repair(child) || result;
                } else {
                    logger.warn("Unexpected element " + child.getNodeName());
                    nodeAdviceExpression.removeChild(child);
                    result = true;//  w w w .ja  v  a  2 s.  co  m
                }
            }
        }
    }

    result = DOMUtil.repairIdentifierAttribute(elementAdviceExpression, XACML3.ATTRIBUTE_ADVICEID, logger)
            || result;
    result = DOMUtil.repairStringAttribute(elementAdviceExpression, XACML3.ATTRIBUTE_APPLIESTO,
            RuleEffect.DENY.getName(), logger) || result;
    String stringRuleEffect = DOMUtil.getStringAttribute(elementAdviceExpression, XACML3.ATTRIBUTE_APPLIESTO);
    RuleEffect ruleEffect = RuleEffect.getRuleEffect(stringRuleEffect);
    if (ruleEffect == null) {
        logger.warn("Setting invalid RuleEffect " + stringRuleEffect + " to " + RuleEffect.DENY.getName());
        elementAdviceExpression.setAttribute(XACML3.ATTRIBUTE_APPLIESTO, RuleEffect.DENY.getName());
        result = true;
    }
    return result;
}

From source file:org.apache.openaz.xacml.pdp.policy.dom.DOMAdviceExpression.java

public static boolean repairList(Node nodeAdviceExpressions) throws DOMStructureException {
    Element elementAdviceExpressions = DOMUtil.getElement(nodeAdviceExpressions);
    boolean result = false;

    boolean sawAdviceExpression = false;
    NodeList children = elementAdviceExpressions.getChildNodes();
    int numChildren;
    if (children != null && (numChildren = children.getLength()) > 0) {
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (DOMUtil.isElement(child)) {
                if (DOMUtil.isInNamespace(child, XACML3.XMLNS)
                        && XACML3.ELEMENT_ADVICEEXPRESSION.equals(child.getLocalName())) {
                    sawAdviceExpression = true;
                    result = result || DOMAdviceExpression.repair(child);
                } else {
                    logger.warn("Unexpected element " + child.getNodeName());
                    nodeAdviceExpressions.removeChild(child);
                    result = true;//from  www  .  j  a  v a2  s  .  co  m
                }
            }
        }
    }

    if (!sawAdviceExpression) {
        throw DOMUtil.newMissingElementException(nodeAdviceExpressions, XACML3.XMLNS,
                XACML3.ELEMENT_ADVICEEXPRESSION);
    }

    return result;
}

From source file:org.apache.openaz.xacml.std.dom.DOMRequest.java

/**
 * Convert XACML2 into XACML3./*w w  w  .j  a va 2s  .c  om*/
 *
 * @param nodeRequest
 * @return
 * @throws DOMStructureException
 */
public static boolean repair(Node nodeRequest) throws DOMStructureException {
    Element elementRequest = DOMUtil.getElement(nodeRequest);
    boolean result = false;

    result = DOMUtil.repairBooleanAttribute(elementRequest, XACML3.ATTRIBUTE_RETURNPOLICYIDLIST, false, logger)
            || result;
    result = DOMUtil.repairBooleanAttribute(elementRequest, XACML3.ATTRIBUTE_COMBINEDDECISION, false, logger)
            || result;

    NodeList children = elementRequest.getChildNodes();
    int numChildren;
    boolean sawAttributes = false;
    if (children != null && (numChildren = children.getLength()) > 0) {
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (DOMUtil.isElement(child)) {
                if (DOMUtil.isInNamespace(child, XACML3.XMLNS)) {
                    String childName = child.getLocalName();
                    if (XACML3.ELEMENT_ATTRIBUTES.equals(childName)) {
                        result = DOMRequestAttributes.repair(child) || result;
                        sawAttributes = true;
                    } else if (XACML3.ELEMENT_REQUESTDEFAULTS.equals(childName)) {
                        result = result || DOMRequestDefaults.repair(child);
                    } else if (XACML3.ELEMENT_MULTIREQUESTS.equals(childName)) {
                        NodeList grandchildren = child.getChildNodes();
                        int numGrandchildren;
                        if (grandchildren != null && (numGrandchildren = grandchildren.getLength()) > 0) {
                            for (int j = 0; j < numGrandchildren; j++) {
                                Node grandchild = grandchildren.item(j);
                                if (DOMUtil.isElement(grandchild)) {
                                    if (DOMUtil.isInNamespace(grandchild, XACML3.XMLNS)) {
                                        if (XACML3.ELEMENT_REQUESTREFERENCE.equals(grandchild.getLocalName())) {
                                            result = DOMRequestReference.repair(grandchild) || result;
                                        } else {
                                            logger.warn("Unexpected element " + grandchild.getNodeName());
                                            child.removeChild(grandchild);
                                            result = true;
                                        }
                                    } else {
                                        logger.warn("Unexpected element " + grandchild.getNodeName());
                                        child.removeChild(grandchild);
                                        result = true;
                                    }
                                }
                            }
                        }
                    } else {
                        logger.warn("Unexpected element " + child.getNodeName());
                        elementRequest.removeChild(child);
                        result = true;
                    }
                } else {
                    logger.warn("Unexpected element " + child.getNodeName());
                    elementRequest.removeChild(child);
                    result = true;
                }
            }
        }
    }
    if (!sawAttributes) {
        throw DOMUtil.newMissingElementException(nodeRequest, XACML3.XMLNS, XACML3.ELEMENT_ATTRIBUTES);
    }

    return result;
}

From source file:org.apache.openaz.xacml.std.dom.DOMResult.java

public static boolean repair(Node nodeResult) throws DOMStructureException {
    Element elementResult = DOMUtil.getElement(nodeResult);
    boolean result = false;

    NodeList children = elementResult.getChildNodes();
    int numChildren;
    boolean sawDecision = false;

    if (children != null && (numChildren = children.getLength()) > 0) {
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (DOMUtil.isElement(child)) {
                if (DOMUtil.isInNamespace(child, XACML3.XMLNS)) {
                    String childName = child.getLocalName();
                    if (XACML3.ELEMENT_DECISION.equals(childName)) {
                        Decision decision = Decision.get(child.getTextContent());
                        if (decision == null) {
                            throw new DOMStructureException(child, "Unknown Decision \""
                                    + child.getTextContent() + "\" in \"" + DOMUtil.getNodeLabel(child) + "\"");
                        }/*from   w ww .  j  a v a2s  .c om*/
                        sawDecision = true;
                    } else if (XACML3.ELEMENT_STATUS.equals(childName)) {
                        result = DOMStatus.repair(child) || result;
                    } else if (XACML3.ELEMENT_OBLIGATIONS.equals(childName)) {
                        result = DOMObligation.repairList(child) || result;
                    } else if (XACML3.ELEMENT_ASSOCIATEDADVICE.equals(childName)) {
                        result = DOMAdvice.repairList(child) || result;
                    } else if (XACML3.ELEMENT_ATTRIBUTES.equals(childName)) {
                        result = DOMAttributeCategory.repair(child) || result;
                    } else if (XACML3.ELEMENT_POLICYIDENTIFIERLIST.equals(childName)) {
                        NodeList grandchildren = child.getChildNodes();
                        int numGrandchildren;
                        if (grandchildren != null && (numGrandchildren = grandchildren.getLength()) > 0) {
                            for (int j = 0; j < numGrandchildren; j++) {
                                Node grandchild = grandchildren.item(j);
                                if (DOMUtil.isElement(grandchild)) {
                                    String grandchildName = grandchild.getLocalName();
                                    if (DOMUtil.isInNamespace(grandchild, XACML3.XMLNS)) {
                                        if (XACML3.ELEMENT_POLICYIDREFERENCE.equals(grandchildName)) {
                                            result = DOMIdReference.repair(grandchild) || result;
                                        } else if (XACML3.ELEMENT_POLICYSETIDREFERENCE.equals(grandchildName)) {
                                            result = DOMIdReference.repair(grandchild) || result;
                                        } else {
                                            logger.warn("Unexpected element " + grandchild.getNodeName());
                                            child.removeChild(grandchild);
                                            result = true;
                                        }
                                    } else {
                                        logger.warn("Unexpected element " + grandchild.getNodeName());
                                        child.removeChild(grandchild);
                                        result = true;
                                    }
                                }
                            }
                        }
                    } else {
                        logger.warn("Unexpected element " + child.getNodeName());
                        elementResult.removeChild(child);
                        result = true;
                    }
                } else {
                    logger.warn("Unexpected element " + child.getNodeName());
                    elementResult.removeChild(child);
                    result = true;
                }
            }
        }
    }

    if (!sawDecision) {
        throw DOMUtil.newMissingElementException(nodeResult, XACML3.XMLNS, XACML3.ELEMENT_DECISION);
    }
    return result;
}

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//from   w w w .  j  a  v  a2 s. c om
 */
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.apache.openmeetings.backup.BackupImport.java

private List<User> readUserList(InputSource xml, String listNodeName) throws Exception {
    Registry registry = new Registry();
    Strategy strategy = new RegistryStrategy(registry);
    Serializer ser = new Persister(strategy);

    registry.bind(Group.class, new GroupConverter(groupDao, groupMap));
    registry.bind(Salutation.class, SalutationConverter.class);
    registry.bind(Date.class, DateConverter.class);

    DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = dBuilder.parse(xml);
    userEmailMap.clear();//  ww w.  j  a  va 2 s .  co m
    //add existence email from database
    List<User> users = userDao.getAllUsers();
    for (User u : users) {
        if (u.getAddress() == null || u.getAddress().getEmail() == null || User.Type.user != u.getType()) {
            continue;
        }
        userEmailMap.put(u.getAddress().getEmail(), Integer.valueOf(-1));
    }
    Node nList = getNode(getNode(doc, "root"), listNodeName);
    if (nList != null) {
        NodeList nl = nList.getChildNodes();
        // one of the old OM version created 2 nodes "deleted" this code block handles this
        for (int i = 0; i < nl.getLength(); ++i) {
            Node user = nl.item(i);
            NodeList nl1 = user.getChildNodes();
            boolean deletedFound = false;
            for (int j = 0; j < nl1.getLength(); ++j) {
                Node node = nl1.item(j);
                if (node.getNodeType() == Node.ELEMENT_NODE && "deleted".equals(node.getNodeName())) {
                    if (deletedFound) {
                        user.removeChild(node);
                        break;
                    }
                    deletedFound = true;
                }
            }
        }
    }

    StringWriter sw = new StringWriter();
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(new DOMSource(doc), new StreamResult(sw));

    List<User> list = new ArrayList<>();
    InputNode root = NodeBuilder.read(new StringReader(sw.toString()));
    InputNode root1 = NodeBuilder.read(new StringReader(sw.toString())); //HACK to handle Address inside user
    InputNode root2 = NodeBuilder.read(new StringReader(sw.toString())); //HACK to handle old om_time_zone, level_id, status
    InputNode listNode = root.getNext();
    InputNode listNode1 = root1.getNext(); //HACK to handle Address inside user
    InputNode listNode2 = root2.getNext(); //HACK to handle old om_time_zone
    if (listNodeName.equals(listNode.getName())) {
        InputNode item = listNode.getNext();
        InputNode item1 = listNode1.getNext(); //HACK to handle Address inside user
        InputNode item2 = listNode2.getNext(); //HACK to handle old om_time_zone, level_id, status
        while (item != null) {
            User u = ser.read(User.class, item, false);

            boolean needToSkip1 = true;
            //HACK to handle Address inside user
            if (u.getAddress() == null) {
                Address a = ser.read(Address.class, item1, false);
                u.setAddress(a);
                needToSkip1 = false;
            }
            if (needToSkip1) {
                do {
                    item1 = listNode1.getNext(); //HACK to handle Address inside user
                } while (item1 != null && !"user".equals(item1.getName()));
            }
            String levelId = null, status = null, stateId = null;
            do {
                if (u.getTimeZoneId() == null && "omTimeZone".equals(item2.getName())) {
                    String jName = item2.getValue();
                    u.setTimeZoneId(jName == null ? null : tzUtil.getTimeZone(jName).getID());
                }
                if ("level_id".equals(item2.getName())) {
                    levelId = item2.getValue();
                }
                if ("status".equals(item2.getName())) {
                    status = item2.getValue();
                }
                if ("state_id".equals(item2.getName())) {
                    stateId = item2.getValue();
                }
                item2 = listNode2.getNext(); //HACK to handle old om_time_zone, level_id, status
            } while (item2 != null && !"user".equals(item2.getName()));
            if (u.getRights().isEmpty()) {
                u.getRights().add(Right.Room);
                if ("1".equals(status)) {
                    u.getRights().add(Right.Dashboard);
                    u.getRights().add(Right.Login);
                }
                if ("3".equals(levelId)) {
                    u.getRights().add(Right.Admin);
                    u.getRights().add(Right.Soap);
                }
                if ("4".equals(levelId)) {
                    u.getRights().add(Right.Soap);
                }
            }
            // check that email is unique
            if (u.getAddress() != null && u.getAddress().getEmail() != null && User.Type.user == u.getType()) {
                if (userEmailMap.containsKey(u.getAddress().getEmail())) {
                    log.warn("Email is duplicated for user " + u.toString());
                    String updateEmail = "modified_by_import_<" + list.size() + ">" + u.getAddress().getEmail();
                    u.getAddress().setEmail(updateEmail);
                }
                userEmailMap.put(u.getAddress().getEmail(), Integer.valueOf(userEmailMap.size()));
            }
            // check old stateId
            if (!Strings.isEmpty(stateId)) {
                String country = getCountry(stateId);
                if (!Strings.isEmpty(country)) {
                    if (u.getAddress() == null) {
                        u.setAddress(new Address());
                    }
                    u.getAddress().setCountry(country);
                }
            }
            if (u.getGroupUsers() != null) {
                for (GroupUser gu : u.getGroupUsers()) {
                    gu.setUser(u);
                }
            }
            list.add(u);
            item = listNode.getNext();
        }
    }
    return list;
}

From source file:org.apache.openmeetings.servlet.outputhandler.BackupImportController.java

private List<User> readUserList(InputSource xml, String listNodeName) throws Exception {
    Registry registry = new Registry();
    Strategy strategy = new RegistryStrategy(registry);
    Serializer ser = new Persister(strategy);

    registry.bind(Organisation.class, new OrganisationConverter(orgDao, organisationsMap));
    registry.bind(OmTimeZone.class, new OmTimeZoneConverter(omTimeZoneDaoImpl));
    registry.bind(State.class, new StateConverter(statemanagement));
    registry.bind(Date.class, DateConverter.class);

    DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = dBuilder.parse(xml);
    NodeList nl = getNode(getNode(doc, "root"), listNodeName).getChildNodes();
    for (int i = 0; i < nl.getLength(); ++i) {
        Node user = nl.item(i);
        NodeList nl1 = user.getChildNodes();
        boolean deletedFound = false;
        for (int j = 0; j < nl1.getLength(); ++j) {
            Node node = nl1.item(j);
            if (node.getNodeType() == Node.ELEMENT_NODE && "deleted".equals(node.getNodeName())) {
                if (deletedFound) {
                    user.removeChild(node);
                    break;
                }/*  ww w.j av  a  2  s.c  o  m*/
                deletedFound = true;
            }
        }
    }

    StringWriter sw = new StringWriter();
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(new DOMSource(doc), new StreamResult(sw));

    List<User> list = new ArrayList<User>();
    InputNode root = NodeBuilder.read(new StringReader(sw.toString()));
    InputNode root1 = NodeBuilder.read(new StringReader(sw.toString())); //HACK to handle Address inside user
    InputNode listNode = root.getNext();
    InputNode listNode1 = root1.getNext(); //HACK to handle Address inside user
    if (listNodeName.equals(listNode.getName())) {
        InputNode item = listNode.getNext();
        InputNode item1 = listNode1.getNext(); //HACK to handle Address inside user
        while (item != null) {
            User u = ser.read(User.class, item, false);

            //HACK to handle Address inside user
            if (u.getAdresses() == null) {
                Address a = ser.read(Address.class, item1, false);
                u.setAdresses(a);
            }
            list.add(u);
            item = listNode.getNext();
            do {
                item1 = listNode1.getNext(); //HACK to handle Address inside user
            } while (item != null && !"user".equals(item1.getName()));
        }
    }
    return list;
}

From source file:org.apache.shindig.gadgets.parse.nekohtml.NekoSimplifiedHtmlParser.java

private void fixNekoWeirdness(Document document) {
    // Neko as of versions > 1.9.13 stuffs all leading <script> nodes into <head>.
    // This breaks all sorts of assumptions in gadgets, notably the existence of document.body.
    // We can't tell Neko to avoid putting <script> into <head> however, since gadgets
    // like <Content><script>...</script><style>...</style> will break due to both
    // <script> and <style> ending up in <body> -- at which point Neko unceremoniously
    // drops the <style> (and <link>) elements.
    // Therefore we just search for <script> elements in <head> and stuff them all into
    // the top of <body>.
    // This method assumes a normalized document as input.
    Node html = DomUtil.getFirstNamedChildNode(document, "html");
    if (html.getNextSibling() != null && html.getNextSibling().getNodeName().equalsIgnoreCase("html")) {
        // if a doctype is specified, then the desired root <html> node is wrapped by an <HTML> node
        // Pull out the <html> root.
        html = html.getNextSibling();//w  w  w .j  a va  2s. c  o  m
    }
    Node head = DomUtil.getFirstNamedChildNode(html, "head");
    if (head == null) {
        head = document.createElement("head");
        html.insertBefore(head, html.getFirstChild());
    }
    NodeList headNodes = head.getChildNodes();
    Stack<Node> headScripts = new Stack<Node>();
    for (int i = 0; i < headNodes.getLength(); ++i) {
        Node headChild = headNodes.item(i);
        if (headChild.getNodeName().equalsIgnoreCase("script")) {
            headScripts.add(headChild);
        }
    }

    // Remove from head, add to top of <body> in <head> order.
    Node body = DomUtil.getFirstNamedChildNode(html, "body");
    if (body == null) {
        body = document.createElement("body");
        html.insertBefore(body, head.getNextSibling());
    }
    Node bodyFirst = body.getFirstChild();
    while (!headScripts.isEmpty()) {
        Node headScript = headScripts.pop();
        head.removeChild(headScript);
        body.insertBefore(headScript, bodyFirst);
        bodyFirst = headScript;
    }
}