Example usage for org.jdom2 Element getContent

List of usage examples for org.jdom2 Element getContent

Introduction

In this page you can find the example usage for org.jdom2 Element getContent.

Prototype

@Override
public List<Content> getContent() 

Source Link

Document

This returns the full content of the element as a List which may contain objects of type Text, Element, Comment, ProcessingInstruction, CDATA, and EntityRef.

Usage

From source file:fi.ni.bcfextractor.BCFExtractorController.java

License:Open Source License

private void listChildren(Element current) {

    System.out.println(current.getName());
    if (current.getName().equals("Comment")) {
        List<Content> contents = current.getContent();
        for (Content c : contents)
            if (c instanceof Text) {
                String txt = ((Text) c).getTextNormalize();
                if (txt.length() > 0)
                    data.add(new BCF("", txt));
            }/*from   w w  w .  j  a v  a2 s  .c  om*/
    }
    List children = current.getChildren();
    Iterator iterator = children.iterator();
    while (iterator.hasNext()) {
        Element child = (Element) iterator.next();
        listChildren(child);
    }

}

From source file:filter.Filter.java

private void converterDataCycle(Cycle cycle, List<Element> elements) {
    for (Element el : elements) {
        new CycleDAO().registerCycleData(cycle.getId(), el.getContent().get(0).getValue());
        incrementValue();//  ww w. ja v a  2 s  .c  om
    }
}

From source file:io.wcm.handler.richtext.impl.RichTextRewriteContentHandlerImpl.java

License:Apache License

/**
 * Checks if the given element has to be rewritten.
 * Is called for every child single element of the parent given to rewriteContent method.
 * @param element Element to check//  w  ww  .ja va  2 s .c  om
 * @return null if nothing is to do with this element.
 *         Return empty list to remove this element.
 *         Return list with other content to replace element with new content.
 */
@Override
public List<Content> rewriteElement(Element element) {

    // rewrite anchor elements
    if (StringUtils.equalsIgnoreCase(element.getName(), "a")) {
        return rewriteAnchor(element);
    }

    // rewrite image elements
    else if (StringUtils.equalsIgnoreCase(element.getName(), "img")) {
        return rewriteImage(element);
    }

    // detect BR elements and turn those into "self-closing" elements
    // since the otherwise generated <br> </br> structures are illegal and
    // are not handled correctly by Internet Explorers
    else if (StringUtils.equalsIgnoreCase(element.getName(), "br")) {
        if (element.getContent().size() > 0) {
            element.removeContent();
        }
        return null;
    }

    // detect empty elements and insert at least an empty string to avoid "self-closing" elements
    // that are not handled correctly by most browsers
    else if (NONSELFCLOSING_TAGS.contains(StringUtils.lowerCase(element.getName()))) {
        if (element.getContent().isEmpty()) {
            element.setText("");
        }
        return null;
    }

    return null;
}

From source file:io.wcm.handler.richtext.impl.RichTextRewriteContentHandlerImpl.java

License:Apache License

/**
 * Checks if the given anchor element has to be rewritten.
 * @param element Element to check/*from www .  j a  v a 2s .co  m*/
 * @return null if nothing is to do with this element.
 *         Return empty list to remove this element.
 *         Return list with other content to replace element with new content.
 */
private List<Content> rewriteAnchor(Element element) {

    // detect empty anchor elements and insert at least an empty string to avoid "self-closing" elements
    // that are not handled correctly by most browsers
    if (element.getContent().isEmpty()) {
        element.setText("");
    }

    // resolve link metadata from DOM element
    Link link = getAnchorLink(element);

    // build anchor for link metadata
    Element anchorElement = buildAnchorElement(link, element);

    // Replace anchor tag or remove anchor tag if invalid - add any sub-content in every case
    List<Content> content = new ArrayList<Content>();
    if (anchorElement != null) {
        anchorElement.addContent(element.cloneContent());
        content.add(anchorElement);
    } else {
        content.addAll(element.getContent());
    }
    return content;
}

From source file:io.wcm.handler.richtext.util.RichTextUtil.java

License:Apache License

/**
 * Rewrites all children/sub-tree of the given parent element.
 * For rewrite operations the given rewrite content handler is called.
 * @param parent Parent element//from w w w. j  av  a2 s.c  om
 * @param rewriteContentHandler Rewrite content handler
 */
public static void rewriteContent(Element parent, RewriteContentHandler rewriteContentHandler) {

    // iterate through content list and build new content list
    List<Content> originalContent = parent.getContent();
    List<Content> newContent = new ArrayList<Content>();
    for (Content contentElement : originalContent) {

        // handle element
        if (contentElement instanceof Element) {
            Element element = (Element) contentElement;

            // check if rewrite is needed for element
            List<Content> rewriteContent = rewriteContentHandler.rewriteElement(element);
            if (rewriteContent != null) {
                // element was removed
                if (rewriteContent.isEmpty()) {
                    // do not add to newContent
                }

                // element is the same - rewrite child elements
                else if (rewriteContent.size() == 1 && rewriteContent.get(0) == element) { //NOPMD
                    rewriteContent(element, rewriteContentHandler);
                    newContent.add(element);
                }

                // element was replaced with other content - rewrite and add instead
                else {
                    for (Content newContentItem : rewriteContent) {
                        if (newContentItem instanceof Element) {
                            Element newElement = (Element) newContentItem;
                            rewriteContent(newElement, rewriteContentHandler);
                        }
                        newContent.add(newContentItem.clone());
                    }
                }
            }

            // nothing to rewrite - do nothing, but rewrite child element
            else {
                rewriteContent(element, rewriteContentHandler);
                newContent.add(element);
            }

        }

        // handle text node
        else if (contentElement instanceof Text) {
            Text text = (Text) contentElement;

            // check if rewrite is needed for text node
            List<Content> rewriteContent = rewriteContentHandler.rewriteText(text);
            if (rewriteContent != null) {
                // element was removed
                if (rewriteContent.isEmpty()) {
                    // do not add to newContent
                }

                // element is the same - ignore
                else if (rewriteContent.size() == 1 && rewriteContent.get(0) == text) { //NOPMD
                    // add original element
                    newContent.add(text);
                }

                // element was replaced with other content - add instead
                else {
                    for (Content newContentItem : rewriteContent) {
                        newContent.add(newContentItem.clone());
                    }
                }
            }

            // nothing to rewrite - do nothing, but add original text element
            else {
                newContent.add(text);
            }

        }

        // unknown element - just add to new content
        else {
            newContent.add(contentElement);
        }

    }

    // replace original content with new content
    parent.removeContent();
    parent.addContent(newContent);

}

From source file:org.apache.maven.io.util.WriterUtils.java

License:Apache License

/**
 * Method replaceXpp3DOM.//from   w w w.  j  av a  2s. c  om
 * 
 * @param parent
 * @param counter
 * @param parentDom
 */
public static void replaceXpp3DOM(final Element parent, final Xpp3Dom parentDom,
        final IndentationCounter counter) {
    if (parentDom.getChildCount() > 0) {
        final Xpp3Dom[] childs = parentDom.getChildren();
        final Collection domChilds = new ArrayList();
        for (final Xpp3Dom child : childs) {
            domChilds.add(child);
        }
        final ListIterator it = parent.getChildren().listIterator();
        while (it.hasNext()) {
            final Element elem = (Element) it.next();
            final Iterator it2 = domChilds.iterator();
            Xpp3Dom corrDom = null;
            while (it2.hasNext()) {
                final Xpp3Dom dm = (Xpp3Dom) it2.next();
                if (dm.getName().equals(elem.getName())) {
                    corrDom = dm;
                    break;
                }
            }
            if (corrDom != null) {
                domChilds.remove(corrDom);
                replaceXpp3DOM(elem, corrDom, new IndentationCounter(counter.getDepth() + 1));
                counter.increaseCount();
            } else {
                it.remove();
            }
        }
        final Iterator it2 = domChilds.iterator();
        while (it2.hasNext()) {
            final Xpp3Dom dm = (Xpp3Dom) it2.next();
            final String rawName = dm.getName();
            final String[] parts = rawName.split(":");

            Element elem;
            if (parts.length > 1) {
                final String nsId = parts[0];
                String nsUrl = dm.getAttribute("xmlns:" + nsId);
                final String name = parts[1];
                if (nsUrl == null) {
                    nsUrl = parentDom.getAttribute("xmlns:" + nsId);
                }
                elem = factory.element(name, Namespace.getNamespace(nsId, nsUrl));
            } else {
                Namespace root = parent.getNamespace();
                for (Namespace n : parent.getNamespacesInherited()) {
                    if (n.getPrefix() == null || n.getPrefix().length() == 0) {
                        root = n;
                        break;
                    }
                }
                elem = factory.element(dm.getName(), root);
            }

            final String[] attributeNames = dm.getAttributeNames();
            for (final String attrName : attributeNames) {
                if (attrName.startsWith("xmlns:")) {
                    continue;
                }
                elem.setAttribute(attrName, dm.getAttribute(attrName));
            }

            insertAtPreferredLocation(parent, elem, counter);
            counter.increaseCount();
            replaceXpp3DOM(elem, dm, new IndentationCounter(counter.getDepth() + 1));
        }
    } else if (parentDom.getValue() != null) {
        List<Content> cl = parent.getContent();
        boolean foundCdata = false;
        for (Content c : cl) {
            if (c instanceof CDATA) {
                foundCdata = true;
            }
        }

        if (!foundCdata) {
            parent.setText(parentDom.getValue());
        }
    }
}

From source file:org.apache.maven.io.util.WriterUtils.java

License:Apache License

/**
 * Method insertAtPreferredLocation.//w w  w  . ja  va  2s  .  com
 * 
 * @param parent
 * @param counter
 * @param child
 */
public static void insertAtPreferredLocation(final Element parent, final Element child,
        final IndentationCounter counter) {
    int contentIndex = 0;
    int elementCounter = 0;
    final Iterator it = parent.getContent().iterator();
    Text lastText = null;
    int offset = 0;
    while (it.hasNext() && elementCounter <= counter.getCurrentIndex()) {
        final Object next = it.next();
        offset = offset + 1;
        if (next instanceof Element) {
            elementCounter = elementCounter + 1;
            contentIndex = contentIndex + offset;
            offset = 0;
        }
        if (next instanceof Text && it.hasNext()) {
            lastText = (Text) next;
        }
    }
    if (lastText != null && lastText.getTextTrim().length() == 0) {
        lastText = lastText.clone();
    } else {
        String starter = lineSeparator;
        for (int i = 0; i < counter.getDepth(); i++) {
            starter = starter + INDENT; // TODO make settable?
        }
        lastText = factory.text(starter);
    }
    if (parent.getContentSize() == 0) {
        final Text finalText = lastText.clone();
        finalText.setText(finalText.getText().substring(0, finalText.getText().length() - INDENT.length()));
        parent.addContent(contentIndex, finalText);
    }
    parent.addContent(contentIndex, child);
    parent.addContent(contentIndex, lastText);
}

From source file:org.apache.maven.toolchain.model.io.jdom.MavenToolchainsJDOMWriter.java

License:Apache License

/**
 * Method insertAtPreferredLocation.//from   w  ww  .  j  a v  a2 s  . c o m
 * 
 * @param parent
 * @param counter
 * @param child
 */
protected void insertAtPreferredLocation(final Element parent, final Element child,
        final IndentationCounter counter) {
    int contentIndex = 0;
    int elementCounter = 0;
    final Iterator it = parent.getContent().iterator();
    Text lastText = null;
    int offset = 0;
    while (it.hasNext() && elementCounter <= counter.getCurrentIndex()) {
        final Object next = it.next();
        offset = offset + 1;
        if (next instanceof Element) {
            elementCounter = elementCounter + 1;
            contentIndex = contentIndex + offset;
            offset = 0;
        }
        if (next instanceof Text && it.hasNext()) {
            lastText = (Text) next;
        }
    }
    if (lastText != null && lastText.getTextTrim().length() == 0) {
        lastText = lastText.clone();
    } else {
        String starter = lineSeparator;
        for (int i = 0; i < counter.getDepth(); i++) {
            starter = starter + INDENT; // TODO make settable?
        }
        lastText = factory.text(starter);
    }
    if (parent.getContentSize() == 0) {
        final Text finalText = lastText.clone();
        finalText.setText(finalText.getText().substring(0, finalText.getText().length() - INDENT.length()));
        parent.addContent(contentIndex, finalText);
    }
    parent.addContent(contentIndex, child);
    parent.addContent(contentIndex, lastText);
}

From source file:org.esa.nest.dat.layersrc.ObjectDetectionLayer.java

License:Open Source License

private void LoadTargets(final File file) {
    if (file == null)
        return;/*w w w .  ja v  a  2 s. c  o  m*/

    Document doc;
    try {
        doc = XMLSupport.LoadXML(file.getAbsolutePath());
    } catch (IOException e) {
        return;
    }

    targetList.clear();

    final Element root = doc.getRootElement();

    final List children = root.getContent();
    for (Object aChild : children) {
        if (aChild instanceof Element) {
            final Element targetsDetectedElem = (Element) aChild;
            if (targetsDetectedElem.getName().equals("targetsDetected")) {
                final Attribute attrib = targetsDetectedElem.getAttribute("bandName");
                if (attrib != null && band.getName().equalsIgnoreCase(attrib.getValue())) {
                    final List content = targetsDetectedElem.getContent();
                    for (Object det : content) {
                        if (det instanceof Element) {
                            final Element targetElem = (Element) det;
                            if (targetElem.getName().equals("target")) {
                                final Attribute lat = targetElem.getAttribute("lat");
                                if (lat == null)
                                    continue;
                                final Attribute lon = targetElem.getAttribute("lon");
                                if (lon == null)
                                    continue;
                                final Attribute width = targetElem.getAttribute("width");
                                if (width == null)
                                    continue;
                                final Attribute length = targetElem.getAttribute("length");
                                if (length == null)
                                    continue;
                                final Attribute intensity = targetElem.getAttribute("intensity");
                                if (intensity == null)
                                    continue;

                                targetList.add(new ObjectDiscriminationOp.ShipRecord(
                                        Double.parseDouble(lat.getValue()), Double.parseDouble(lon.getValue()),
                                        (Double.parseDouble(width.getValue()) / rangeSpacing) + border,
                                        (Double.parseDouble(length.getValue()) / azimuthSpacing) + border,
                                        Double.parseDouble(intensity.getValue())));
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:org.esa.nest.dat.layersrc.WindFieldEstimationLayer.java

License:Open Source License

private void LoadTargets(final File file) {
    if (file == null)
        return;//from ww w .j  a v  a 2s . c  o  m

    Document doc;
    try {
        doc = XMLSupport.LoadXML(file.getAbsolutePath());
    } catch (IOException e) {
        return;
    }

    targetList.clear();

    final Element root = doc.getRootElement();

    final List children = root.getContent();
    for (Object aChild : children) {
        if (aChild instanceof Element) {
            final Element targetsDetectedElem = (Element) aChild;
            if (targetsDetectedElem.getName().equals("windFieldEstimated")) {
                final Attribute attrib = targetsDetectedElem.getAttribute("bandName");
                if (attrib != null && band.getName().equalsIgnoreCase(attrib.getValue())) {
                    final List content = targetsDetectedElem.getContent();
                    for (Object det : content) {
                        if (det instanceof Element) {
                            final Element targetElem = (Element) det;
                            if (targetElem.getName().equals("windFieldInfo")) {
                                final Attribute lat = targetElem.getAttribute("lat");
                                if (lat == null)
                                    continue;
                                final Attribute lon = targetElem.getAttribute("lon");
                                if (lon == null)
                                    continue;
                                final Attribute speed = targetElem.getAttribute("speed");
                                if (speed == null)
                                    continue;
                                final Attribute dx = targetElem.getAttribute("dx");
                                if (dx == null)
                                    continue;
                                final Attribute dy = targetElem.getAttribute("dy");
                                if (dy == null)
                                    continue;
                                final Attribute ratio = targetElem.getAttribute("ratio");
                                if (ratio == null)
                                    continue;

                                targetList.add(new WindFieldEstimationOp.WindFieldRecord(
                                        Double.parseDouble(lat.getValue()), Double.parseDouble(lon.getValue()),
                                        Double.parseDouble(speed.getValue()), Double.parseDouble(dx.getValue()),
                                        Double.parseDouble(dy.getValue()),
                                        Double.parseDouble(ratio.getValue())));
                            }
                        }
                    }
                }
            }
        }
    }
}