Example usage for org.w3c.dom Element removeChild

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

Introduction

In this page you can find the example usage for org.w3c.dom Element 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:de.knowwe.defi.usermanager.XMLUserDatabase.java

/**
 * Saves a {@link org.apache.wiki.auth.user.UserProfile}to the user database, overwriting the
 * existing profile if it exists. The user name under which the profile
 * should be saved is returned by the supplied profile's
 * {@link org.apache.wiki.auth.user.UserProfile#getLoginName()}method.
 * @param profile the user profile to save
 * @throws org.apache.wiki.auth.WikiSecurityException if the profile cannot be saved
 *///  w w w.  j av a 2 s.  c om
@Override
public synchronized void save(UserProfile profile) throws WikiSecurityException {
    if (c_dom == null) {
        Log.severe("Could not save profile " + profile + " database does not exist");
        throw new IllegalStateException("FATAL: database does not exist");
    }

    checkForRefresh();

    DateFormat c_format = new SimpleDateFormat(DATE_FORMAT);
    String index = profile.getLoginName();
    NodeList users = c_dom.getElementsByTagName(USER_TAG);
    Element user = null;
    for (int i = 0; i < users.getLength(); i++) {
        Element currentUser = (Element) users.item(i);
        if (currentUser.getAttribute(LOGIN_NAME).equals(index)) {
            user = currentUser;
            break;
        }
    }

    boolean isNew = false;

    Date modDate = new Date(System.currentTimeMillis());
    if (user == null) {
        // Create new user node
        profile.setCreated(modDate);
        Log.info("Creating new user " + index);
        user = c_dom.createElement(USER_TAG);
        c_dom.getDocumentElement().appendChild(user);
        setAttribute(user, CREATED, c_format.format(profile.getCreated()));
        isNew = true;
    } else {
        // To update existing user node, delete old attributes first...
        NodeList attributes = user.getElementsByTagName(ATTRIBUTES_TAG);
        for (int i = 0; i < attributes.getLength(); i++) {
            user.removeChild(attributes.item(i));
        }
    }

    setAttribute(user, UID, profile.getUid());
    setAttribute(user, LAST_MODIFIED, c_format.format(modDate));
    setAttribute(user, LOGIN_NAME, profile.getLoginName());
    setAttribute(user, FULL_NAME, profile.getFullname());
    setAttribute(user, WIKI_NAME, profile.getWikiName());
    setAttribute(user, EMAIL, profile.getEmail());
    Date lockExpiry = profile.getLockExpiry();
    setAttribute(user, LOCK_EXPIRY, lockExpiry == null ? "" : c_format.format(lockExpiry));

    // Hash and save the new password if it's different from old one
    String newPassword = profile.getPassword();
    if (newPassword != null && !newPassword.equals("")) {
        String oldPassword = user.getAttribute(PASSWORD);
        if (!oldPassword.equals(newPassword)) {
            setAttribute(user, PASSWORD, getHash(newPassword));
        }
    }

    // Save the attributes as as Base64 string
    if (!profile.getAttributes().isEmpty()) {
        try {
            String encodedAttributes = Serializer.serializeToBase64(profile.getAttributes());
            Element attributes = c_dom.createElement(ATTRIBUTES_TAG);
            user.appendChild(attributes);
            Text value = c_dom.createTextNode(encodedAttributes);
            attributes.appendChild(value);
        } catch (IOException e) {
            throw new WikiSecurityException("Could not save user profile attribute. Reason: " + e.getMessage(),
                    e);
        }
    }

    // Set the profile timestamps
    if (isNew) {
        profile.setCreated(modDate);
    }
    profile.setLastModified(modDate);

    // Commit to disk
    saveDOM();
}

From source file:jef.tools.XMLUtils.java

/**
 * ?????/*from   w ww. j a  v  a2  s . c o m*/
 * 
 * @param node
 *            ????
 * @param newName
 *            ??
 * @return ????DOMElement
 */
public static Element changeNodeName(Element node, String newName) {
    Document doc = node.getOwnerDocument();
    Element newEle = doc.createElement(newName);
    Node parent = node.getParentNode();
    parent.removeChild(node);
    parent.appendChild(newEle);

    for (Node child : toArray(node.getChildNodes())) {
        node.removeChild(child);
        newEle.appendChild(child);
    }
    return newEle;
}

From source file:de.qucosa.webapi.v1.DocumentResource.java

private void removeFileElementsWithoutCorrespondingDatastream(String pid, Document doc)
        throws FedoraClientException {
    Element root = (Element) doc.getElementsByTagName("Opus_Document").item(0);
    NodeList fileNodes = root.getElementsByTagName("File");

    // removing nodes from the node list changes the node list
    ArrayList<Node> removees = new ArrayList<>(fileNodes.getLength());

    for (int i = 0; i < fileNodes.getLength(); i++) {
        Node fileNode = fileNodes.item(i);
        Node idAttr = fileNode.getAttributes().getNamedItem("id");
        if (idAttr == null) {
            removees.add(fileNode);/*from www  . j a v  a  2 s  .  c  o m*/
        } else {
            String fid = idAttr.getTextContent();
            String dsid = "QUCOSA-ATT-".concat(fid);
            if (!fedoraRepository.hasDatastream(pid, dsid)) {
                removees.add(fileNode);
            }
        }
    }
    for (Node n : removees)
        root.removeChild(n);
}

From source file:com.krawler.portal.tools.ServiceBuilder.java

public void deleteSourceEntryCfgXml(String fileName) {
    try {//from  w  ww  .ja v  a 2s .  com
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(PropsValues.CFG_SOURCE_FILE_PATH);
        Node hibernate_conf = doc.getChildNodes().item(1);
        Node SessionFac = hibernate_conf.getChildNodes().item(1);
        Element sesFac = (Element) SessionFac;
        NodeList mapping_lists = sesFac.getElementsByTagName("mapping");
        Node toDelete = null;
        for (int num = 0; num < mapping_lists.getLength(); num++) {
            Element mapEle = (Element) mapping_lists.item(num);
            if (mapEle.getAttribute("resource").equals(PropsValues.PACKAGE_FILE_PATH + fileName + ".hbm.xml")) {
                toDelete = mapEle;
                break;
            }
        }
        sesFac.removeChild(toDelete);
        DOMSource ds = new DOMSource(doc);
        StreamResult sr = new StreamResult(PropsValues.CFG_SOURCE_FILE_PATH);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.setOutputProperty(OutputKeys.VERSION, "1.0");
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
        trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Configuration DTD 3.0//EN");
        trans.transform(ds, sr);
    } catch (TransformerException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (SAXException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (IOException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (ParserConfigurationException ex) {
        logger.warn(ex.getMessage(), ex);
    }
}

From source file:com.krawler.portal.tools.ServiceBuilder.java

public void deleteClassesEntryCfgXml(String fileName) {
    try {//w  w w  . j av  a  2s  . c  o m
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(PropsValues.CFG_CLASSES_FILE_PATH);
        Node hibernate_conf = doc.getChildNodes().item(1);
        Node SessionFac = hibernate_conf.getChildNodes().item(1);
        Element sesFac = (Element) SessionFac;
        NodeList mapping_lists = sesFac.getElementsByTagName("mapping");
        Node toDelete = null;
        for (int num = 0; num < mapping_lists.getLength(); num++) {
            Element mapEle = (Element) mapping_lists.item(num);
            if (mapEle.getAttribute("resource").equals(PropsValues.PACKAGE_FILE_PATH + fileName + ".hbm.xml")) {
                toDelete = mapEle;
                break;
            }
        }
        sesFac.removeChild(toDelete);
        DOMSource ds = new DOMSource(doc);
        StreamResult sr = new StreamResult(PropsValues.CFG_CLASSES_FILE_PATH);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer trans = tf.newTransformer();
        trans.setOutputProperty(OutputKeys.VERSION, "1.0");
        trans.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
                "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd");
        trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Configuration DTD 3.0//EN");
        trans.transform(ds, sr);
    } catch (TransformerException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (SAXException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (IOException ex) {
        logger.warn(ex.getMessage(), ex);
    } catch (ParserConfigurationException ex) {
        logger.warn(ex.getMessage(), ex);
    }
}

From source file:com.bluexml.xforms.controller.navigation.NavigationManager.java

/**
 * Removes a specific submit button from the group of submit buttons.
 * //from www .ja v  a  2s.c o  m
 * @param docElt
 * @param buttonId
 */
private void removeSubmitButton(Element docElt, MsgId buttonId) {
    String realCaption = MsgPool.getMsg(buttonId);
    Element button = null;
    // find the group of submits
    String groupId = MsgId.INT_SUBMIT_BUTTONS_GROUP_ID.getText();
    Element submitGroup = DOMUtil.getEltInDescentByAttrValue(docElt, "id", groupId);
    // find the "xf:submit" node that has the right caption in its
    // "xf:label"
    List<Element> listOfSubmits = DOMUtil.getAllChildren(submitGroup);
    for (Element aSubmit : listOfSubmits) {
        if (StringUtils.equals(aSubmit.getTagName(), "xf:submit")) {
            Element label = DOMUtil.getChild(aSubmit, "xf:label");
            if (StringUtils.equals(label.getTextContent(), realCaption)) {
                button = aSubmit;
                break;
            }
        }
    }
    if (button != null) {
        submitGroup.removeChild(button);
    }
}

From source file:es.bsc.servicess.ide.ProjectMetadata.java

/** Remove a elements package
 * @param name Package name/*from  ww  w . j  ava 2s.  c  o m*/
 */
public void removePackage(String name) {
    NodeList nl = projectElement.getElementsByTagName(PACKAGE_TAG);
    if (nl != null && nl.getLength() > 0) {
        for (int i = 0; i < nl.getLength(); i++) {
            Element dependency = (Element) nl.item(i);
            if (dependency.getAttribute(NAME_ATTR).equals(name)) {
                NodeList childs = dependency.getChildNodes();
                if (childs != null && childs.getLength() > 0) {
                    for (int j = 0; j < childs.getLength(); j++) {
                        dependency.removeChild(childs.item(j));
                    }
                }
                projectElement.removeChild(dependency);
            }
        }
    }
}

From source file:es.bsc.servicess.ide.ProjectMetadata.java

/** Remove a dependency from the project
 * @param loc Dependency location (file or folder location)
 *//*from  w  ww.  j  ava 2 s.  co m*/
public void removeDependency(String loc) {
    NodeList nl = projectElement.getElementsByTagName(DEPENDENCY_TAG);
    if (nl != null && nl.getLength() > 0) {
        for (int i = 0; i < nl.getLength(); i++) {
            Element dependency = (Element) nl.item(i);
            if (dependency.getAttribute(LOCATION_ATTR).equals(loc)) {
                NodeList childs = dependency.getElementsByTagName(ELEMENT_TAG);
                if (childs != null && childs.getLength() > 0) {
                    for (int j = 0; j < childs.getLength(); j++) {
                        dependency.removeChild(childs.item(j));
                    }
                }
                projectElement.removeChild(dependency);
            }
        }
    }
}

From source file:com.mirth.connect.model.util.ImportConverter.java

private static void updateTransformerFor1_7_1(Document document) {
    Element inboundPropertiesElement, outboundPropertiesElement;

    NodeList transformers = document.getElementsByTagName("transformer");

    for (int i = 0; i < transformers.getLength(); i++) {
        Element transformerRoot = (Element) transformers.item(i);

        // Update the inbound protocol properties.
        if (transformerRoot.getElementsByTagName("inboundProtocol").item(0).getTextContent().equals("HL7V2")
                || transformerRoot.getElementsByTagName("inboundProtocol").item(0).getTextContent()
                        .equals("EDI")) {
            if (transformerRoot.getElementsByTagName("inboundProperties").getLength() != 0) {
                inboundPropertiesElement = (Element) transformerRoot.getElementsByTagName("inboundProperties")
                        .item(0);//from www  .java 2  s  .  c  o  m

                // Remove encode entities if it exists.
                NodeList propertyNames = inboundPropertiesElement.getElementsByTagName("property");
                for (int j = 0; j < propertyNames.getLength(); j++) {
                    Node nameAttribute = propertyNames.item(j).getAttributes().getNamedItem("name");
                    if (propertyNames.item(j).getAttributes().getLength() > 0 && nameAttribute != null) {
                        if (nameAttribute.getNodeValue().equals("encodeEntities")) {
                            inboundPropertiesElement.removeChild(propertyNames.item(j));
                        }
                    }
                }

                // Override convertLFtoCR and set it to "true".
                propertyNames = inboundPropertiesElement.getElementsByTagName("property");
                for (int j = 0; j < propertyNames.getLength(); j++) {
                    Node nameAttribute = propertyNames.item(j).getAttributes().getNamedItem("name");
                    if (propertyNames.item(j).getAttributes().getLength() > 0 && nameAttribute != null) {
                        if (nameAttribute.getNodeValue().equals("convertLFtoCR")) {
                            propertyNames.item(j).setTextContent("true");
                        }
                    }
                }
            }
        }

        // Update the outbound protocol properties.
        if (transformerRoot.getElementsByTagName("outboundProtocol").item(0).getTextContent().equals("HL7V2")
                || transformerRoot.getElementsByTagName("outboundProtocol").item(0).getTextContent()
                        .equals("EDI")) {
            if (transformerRoot.getElementsByTagName("outboundProperties").getLength() != 0) {
                outboundPropertiesElement = (Element) transformerRoot.getElementsByTagName("outboundProperties")
                        .item(0);

                // Remove encode entities if it exists.
                NodeList propertyNames = outboundPropertiesElement.getElementsByTagName("property");
                for (int j = 0; j < propertyNames.getLength(); j++) {
                    Node nameAttribute = propertyNames.item(j).getAttributes().getNamedItem("name");
                    if (propertyNames.item(j).getAttributes().getLength() > 0 && nameAttribute != null) {
                        if (nameAttribute.getNodeValue().equals("encodeEntities")) {
                            outboundPropertiesElement.removeChild(propertyNames.item(j));
                        }
                    }
                }

                // Override convertLFtoCR and set it to "true".
                propertyNames = outboundPropertiesElement.getElementsByTagName("property");
                for (int j = 0; j < propertyNames.getLength(); j++) {
                    Node nameAttribute = propertyNames.item(j).getAttributes().getNamedItem("name");
                    if (propertyNames.item(j).getAttributes().getLength() > 0 && nameAttribute != null) {
                        if (nameAttribute.getNodeValue().equals("convertLFtoCR")) {
                            propertyNames.item(j).setTextContent("true");
                        }
                    }
                }
            }
        }
    }
}

From source file:com.bluexml.xforms.controller.mapping.MappingToolClassFormsToAlfresco.java

/**
 * Removes the reference./*ww  w  . j  av a 2  s.co  m*/
 * 
 * @param node
 *            the node
 * @param elementId
 *            the element id
 */
public void removeReference(Node node, String elementId) {
    String relementId = controller.patchDataId(elementId);

    Element element = null;
    if (node instanceof Document) {
        element = ((Document) node).getDocumentElement();
    } else {
        if (node instanceof Element) {
            element = (Element) node;
        } else {
            throw new RuntimeException("Unknow type of DOM node element");
        }

    }

    List<Element> children = DOMUtil.getAllChildren(element);
    ClassType classType = null;
    Element dataType = DOMUtil.getOneElementByTagName(children, MsgId.INT_INSTANCE_SIDE_DATATYPE.getText());
    if (dataType != null) {
        classType = getClassType(dataType.getTextContent());
    } else {
        classType = getClassType(element.getTagName());
    }
    List<ClassType> classTypes = getParentClassTypes(classType);

    List<Element> elementsToRemove = new ArrayList<Element>();

    for (ClassType subClassType : classTypes) {
        List<AssociationType> xformsAssociations = subClassType.getAssociation();
        for (AssociationType associationType : xformsAssociations) {
            Element associationElement = DOMUtil.getOneElementByTagName(children, associationType.getName());
            if (associationElement != null) {
                List<Element> associationElements = DOMUtil.getAllChildren(associationElement);
                for (Element association : associationElements) {
                    processRemoveReference(relementId, elementsToRemove, associationType, association,
                            isMultiple(associationType));
                }
            }
        }
    }
    for (Element elementToRemove : elementsToRemove) {
        element.removeChild(elementToRemove);
    }
}