Example usage for org.dom4j Node detach

List of usage examples for org.dom4j Node detach

Introduction

In this page you can find the example usage for org.dom4j Node detach.

Prototype

Node detach();

Source Link

Document

Removes this node from its parent if there is one.

Usage

From source file:org.withinsea.izayoi.commons.dom.DOMUtils.java

License:Mozilla Public License

public static Node replaceBy(List<Node> nodes, Node newNode)
        throws InvocationTargetException, IllegalAccessException {
    insertBefore(newNode, nodes.get(0));
    for (Node node : nodes) {
        node.detach();
    }// www .  j a v  a2s.c o m
    return newNode;
}

From source file:org.withinsea.izayoi.commons.dom.DOMUtils.java

License:Mozilla Public License

@SuppressWarnings("unchecked")
public static void mergeTexts(Branch root) {
    List<Node> nodes = (List<Node>) root.content();
    for (int i = nodes.size() - 2; i >= 0; i--) {
        Node node = nodes.get(i);
        Node next = nodes.get(i + 1);
        if (node instanceof Text && next instanceof Text) {
            node.setText(node.getText() + next.getText());
            next.detach();
        }//from ww  w.j a  v a 2s.com
    }
    for (Node node : (List<Node>) root.content()) {
        if (node instanceof Branch) {
            mergeTexts((Branch) node);
        }
    }
}

From source file:org.withinsea.izayoi.cortile.template.html.grammar.attr.core.Mock.java

License:Mozilla Public License

@Override
@Priority(Priority.HIGHER)/*w ww  . j  a v a  2s. c om*/
public void processAttr(Attribute attr) throws CortileException {

    Element elem = attr.getParent();

    String mockType = attr.getValue();

    java.util.Set<Node> mocks = new HashSet<Node>();
    @SuppressWarnings("unchecked")
    List<Node> siblings = (List<Node>) DOMUtils.parent(elem).content();
    if (mockType.equals("siblings")) {
        mocks.addAll(siblings);
    } else if (mockType.equals("neighbors")) {
        mocks.addAll(siblings);
        mocks.remove(elem);
    } else if (mockType.equals("below")) {
        mocks.addAll(siblings.subList(siblings.indexOf(elem) + 1, siblings.size()));
    } else if (mockType.equals("toend")) {
        mocks.addAll(siblings.subList(siblings.indexOf(elem), siblings.size()));
    } else {
        mocks.add(elem);
    }

    for (Node mock : mocks) {
        mock.detach();
    }

    attr.detach();
}

From source file:server.data.ObjectSystemData.java

License:Open Source License

/**
 * Set the Metadata on this object. Tries to parse the submitted string
 * and throws an exception if it is not valid XML.<br/>
 * Note: this parses the meta-xml into metasets and stores them as such.
 * This method will unlink existing metasets if they are missing from the metadata,
 * that is, you cannot submit partial metadata to setMetadata. You must set <em>all</em>
 * metadata if you use this method. (see addMetaset to add an individual metaset)
 *
 * @param metadata    the custom metadata
 * @param writePolicy the write policy - what to do if other items already reference a metaset.
 *//*from   ww  w. j  a  v a 2 s .  c om*/
public void setMetadata(String metadata, WritePolicy writePolicy) {
    try {
        MetasetService metasetService = new MetasetService();

        EntityManager em = HibernateSession.getLocalEntityManager();
        if (metadata == null || metadata.trim().length() == 0) {
            this.metadata = "<meta/>";
            for (OsdMetaset om : getOsdMetasets()) {
                em.remove(om);
            }
        } else {
            Document doc = ParamParser.parseXmlToDocument(metadata, "error.param.metadata");
            List<Node> sets = doc.selectNodes("//metaset");
            //            log.debug("found "+sets.size()+" metaset nodes in:\n"+metadata);
            if (sets.size() == 0) {
                this.metadata = metadata;
                if (osdMetasets.size() > 0) {
                    // delete obsolete metasets:
                    for (Metaset m : fetchMetasets()) {
                        new MetasetService().unlinkMetaset(this, m);
                    }
                }
                return;
            }

            Set<MetasetType> currentMetasetMap = new HashSet<MetasetType>();
            for (Metaset metaset : fetchMetasets()) {
                // create a set of the currently existing metasets.
                currentMetasetMap.add(metaset.getType());
            }

            for (Node metasetNode : sets) {
                String content = metasetNode.detach().asXML();
                String metasetTypeName = metasetNode.selectSingleNode("@type").getText();
                log.debug("metasetType: " + metasetTypeName);
                MetasetTypeDAO mtDao = daoFactory.getMetasetTypeDAO(em);
                MetasetType metasetType = mtDao.findByName(metasetTypeName);
                if (metasetType == null) {
                    throw new CinnamonException("error.unknown.metasetType", metasetTypeName);
                }
                metasetService.createOrUpdateMetaset(this, metasetType, content, writePolicy);
                currentMetasetMap.remove(metasetType);
            }
            for (MetasetType metasetType : currentMetasetMap) {
                // any metaset that was not found in the metadata parameter will be deleted.                    
                metasetService.unlinkMetaset(this, this.fetchMetaset(metasetType.getName())); // somewhat convoluted.
            }
        }
    } catch (Exception e) {
        log.debug("failed to add metadata:", e);
        throw new RuntimeException(e);
    }
    // remove legacy metadata:
    this.metadata = "<meta />";
}

From source file:server.Folder.java

License:Open Source License

/**
 * Set the Metadata on this item. Tries to parse the submitted string
 * and throws an exception if it is not valid XML.<br/>
 * Note: this parses the meta-xml into metasets and stores them as such.
 * This method will unlink existing metasets if they are missing from the metadata,
 * that is, you cannot submit partial metadata to setMetadata. You must set <em>all</em>
 * metadata if you use this method. (see addMetaset to add an individual metaset) 
 * @param metadata the custom metadata//  w  ww  .  j ava2s.  c  o  m
 * @param writePolicy the WritePolicy - how to treat existing metasets.
 */
public void setMetadata(String metadata, WritePolicy writePolicy) {
    EntityManager em = HibernateSession.getLocalEntityManager();
    if (metadata == null || metadata.trim().length() == 0) {
        this.metadata = "<meta/>";
        for (FolderMetaset fm : getFolderMetasets()) {
            em.remove(fm);
        }
    } else {
        MetasetService metasetService = new MetasetService();
        Document doc = ParamParser.parseXmlToDocument(metadata, "error.param.metadata");
        List<Node> sets = doc.selectNodes("//metaset");
        if (sets.size() == 0) {
            this.metadata = metadata;
            if (folderMetasets.size() > 0) {
                // delete obsolete metasets:
                for (Metaset m : fetchMetasets()) {
                    new MetasetService().unlinkMetaset(this, m);
                }
            }
            return;
        }

        Set<MetasetType> currentMetasetMap = new HashSet<MetasetType>();
        for (Metaset metaset : fetchMetasets()) {
            // create a set of the currently existing metasets.
            currentMetasetMap.add(metaset.getType());
        }

        for (Node metasetNode : sets) {
            String content = metasetNode.detach().asXML();
            String metasetTypeName = metasetNode.selectSingleNode("@type").getText();
            log.debug("metasetType: " + metasetTypeName);
            MetasetTypeDAO mtDao = daoFactory.getMetasetTypeDAO(em);
            MetasetType metasetType = mtDao.findByName(metasetTypeName);
            if (metasetType == null) {
                throw new CinnamonException("error.unknown.metasetType", metasetTypeName);
            }
            metasetService.createOrUpdateMetaset(this, metasetType, content, writePolicy);
            currentMetasetMap.remove(metasetType);
        }
        for (MetasetType metasetType : currentMetasetMap) {
            // any metaset that was not found in the metadata parameter will be deleted.                    
            metasetService.unlinkMetaset(this, this.fetchMetaset(metasetType.getName())); // somewhat convoluted.
        }
    }
    // remove legacy metadata
    this.metadata = "<meta />";
}