Example usage for org.dom4j Document content

List of usage examples for org.dom4j Document content

Introduction

In this page you can find the example usage for org.dom4j Document content.

Prototype

List<Node> content();

Source Link

Document

Returns the content nodes of this branch as a backed List so that the content of this branch may be modified directly using the List interface.

Usage

From source file:org.firesoa.common.jxpath.model.dom4j.Dom4JNodePointer.java

License:Open Source License

@Override
public void setValue(Object value) {
    if (value == null)
        value = "";//null??
    if ((node instanceof org.dom4j.CharacterData || node instanceof Attribute || node instanceof DocumentType
            || node instanceof Entity || node instanceof ProcessingInstruction)) {
        String string = (String) TypeUtils.convert(value, String.class);
        if (string != null && !string.equals("")) {
            ((Node) node).setText(string);
        } else {/*  www . j a  va2 s.  com*/
            ((Node) node).getParent().remove((Node) node);
        }
    } else if (node instanceof Document) {
        Document theOriginalDoc = (Document) node;
        Element theOrigialRoot = theOriginalDoc.getRootElement();

        if (value instanceof Document) {//?document

            Document valueDoc = (Document) value;
            Element valueRoot = valueDoc.getRootElement();

            if (theOrigialRoot == null || valueRoot == null
                    || theOrigialRoot.getQName().equals(valueRoot.getQName())) {

                theOriginalDoc.clearContent();

                List content = valueDoc.content();
                if (content != null) {
                    for (int i = 0; i < content.size(); i++) {
                        Node dom4jNode = (Node) content.get(i);
                        Node newDom4jNode = (Node) dom4jNode.clone();
                        theOriginalDoc.add(newDom4jNode);
                    }
                }
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueRoot.getQName() + " to " + theOrigialRoot.getQName());

            }

        } else if (value instanceof Element) {
            Element valueElem = (Element) value;
            if (valueElem.getQName().equals(theOrigialRoot.getQName())) {
                theOriginalDoc.clearContent();
                Element newValueElem = (Element) valueElem.clone();
                theOriginalDoc.setRootElement(newValueElem);
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueElem.getQName() + " to " + theOrigialRoot.getQName());
            }
        } else {
            throw new RuntimeException("Can NOT assign " + value + " to " + theOrigialRoot.getQName());

        }
        //         else if (value instanceof Comment){
        //            Comment cmmt = (Comment)((Comment)value).clone();
        //            theOriginalDoc.add(cmmt);
        //            
        //         }else if (value instanceof ProcessingInstruction){
        //            ProcessingInstruction instru = (ProcessingInstruction)((ProcessingInstruction)value).clone();
        //            theOriginalDoc.add(instru);
        //         }

    } else if (node instanceof Element) {
        Element originalElem = ((Element) node);

        if (value != null && value instanceof Element) {
            Element valueElm = (Element) value;
            if (originalElem.getQName().equals(valueElm.getQName())) {
                originalElem.clearContent();
                List content = valueElm.content();
                if (content != null) {
                    for (int i = 0; i < content.size(); i++) {
                        Node dom4jNode = (Node) content.get(i);
                        Node newDom4jNode = (Node) dom4jNode.clone();
                        originalElem.add(newDom4jNode);
                    }
                }
            } else {
                throw new RuntimeException(
                        "Can NOT assign " + valueElm.getQName() + " to " + originalElem.getQName());
            }

        } else if (value != null && value instanceof Text) {
            originalElem.clearContent();
            Text txt = (Text) ((Text) value).clone();
            originalElem.add(txt);
        } else if (value != null && value instanceof CDATA) {
            originalElem.clearContent();
            CDATA cdata = (CDATA) ((CDATA) value).clone();
            originalElem.add(cdata);
        } else if (value != null && value instanceof java.util.Date) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateStr = format.format((java.util.Date) value);
            originalElem.clearContent();
            originalElem.addText(dateStr);
        } else {
            String string = (String) TypeUtils.convert(value, String.class);
            originalElem.clearContent();
            originalElem.addText(string);
        }

    }

}

From source file:org.orbeon.oxf.properties.Properties.java

License:Open Source License

/**
 * Make sure we have the latest properties, and if we don't (resource changed), reload them.
 *//*from www .  j a  v a2s  .c o  m*/
private void update() {
    if (!initializing) {
        done: try {
            initializing = true;
            final long current = System.currentTimeMillis();

            if (lastUpdate + RELOAD_DELAY >= current)
                break done;

            // Create mini-pipeline to read properties if needed
            if (urlGenerator == null) {
                urlGenerator = PipelineUtils.createURLGenerator(propertiesURI, true);// enable XInclude too
                domSerializer = new DOMSerializer();
                PipelineUtils.connect(urlGenerator, ProcessorImpl.OUTPUT_DATA, domSerializer,
                        ProcessorImpl.INPUT_DATA);
            }

            // Initialize pipeline
            // Candidate for Scala withPipelineContext
            final PipelineContext pipelineContext = new PipelineContext();
            boolean success = false;
            try {
                urlGenerator.reset(pipelineContext);
                domSerializer.reset(pipelineContext);

                // Find whether we can skip reloading
                if (propertyStore != null
                        && domSerializer.findInputLastModified(pipelineContext) <= lastUpdate) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Not reloading properties because they have not changed.");
                    }
                    lastUpdate = current;
                    success = true;
                    break done;
                }

                if (logger.isDebugEnabled()) {
                    logger.debug("Reloading properties because timestamp indicates they may have changed.");
                }

                // Read updated properties document
                final Document document = domSerializer.runGetDocument(pipelineContext);

                if (document == null || document.content() == null || document.content().size() == 0) {
                    throw new OXFException("Failure to initialize Orbeon Forms properties");
                }

                propertyStore = new PropertyStore(document);

                lastUpdate = current;

                success = true;
            } finally {
                pipelineContext.destroy(success);
            }
        } finally {
            initializing = false;
        }
    }
}

From source file:org.orbeon.saxon.dom4j.NodeWrapper.java

License:Open Source License

/**
 * Get the index position of this node among its siblings (starting from 0)
 *//* ww  w .ja v a 2  s  .  c om*/
public int getSiblingPosition() {
    if (index == -1) {
        int ix = 0;
        getParent();
        AxisIterator iter;
        switch (nodeKind) {
        case Type.ELEMENT:
        case Type.TEXT:
        case Type.COMMENT:
        case Type.PROCESSING_INSTRUCTION: {
            final NodeWrapper parent = (NodeWrapper) getParent();
            final List children;
            if (parent.getNodeKind() == Type.DOCUMENT) {
                // This is an attempt to work around a dom4j bug
                final Document document = (Document) parent.node;
                final List content = document.content();
                if (content.size() == 0 && document.getRootElement() != null)
                    children = Collections.singletonList(document.getRootElement());
                else
                    children = content;
            } else {
                // Beware: dom4j content() contains Namespace nodes (which is broken)!
                children = ((Element) parent.node).content();
            }
            for (ListIterator iterator = children.listIterator(); iterator.hasNext();) {
                final Object n = iterator.next();
                if (n == node) {
                    index = ix;
                    return index;
                }
                ix++;
            }
            throw new IllegalStateException("DOM4J node not linked to parent node");
        }
        case Type.ATTRIBUTE:
            iter = parent.iterateAxis(Axis.ATTRIBUTE);
            break;
        case Type.NAMESPACE:
            iter = parent.iterateAxis(Axis.NAMESPACE);
            break;
        default:
            index = 0;
            return index;
        }
        while (true) {
            NodeInfo n = (NodeInfo) iter.next();
            if (n == null) {
                break;
            }
            if (n.isSameNodeInfo(this)) {
                index = ix;
                return index;
            }
            ix++;
        }
        throw new IllegalStateException("DOM4J node not linked to parent node");
    }
    return index;
}