Java Code Examples for org.jdom2.Element#getContent()

The following examples show how to use org.jdom2.Element#getContent() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Returns the content of an element as string. The element itself
 * is ignored.
 * 
 * @param e the element to get the content from
 * @return the content as string
 */
protected String getContent(Element e) throws IOException {
    XMLOutputter out = new XMLOutputter();
    StringWriter writer = new StringWriter();
    for (Content child : e.getContent()) {
        if (child instanceof Element) {
            out.output((Element) child, writer);
        } else if (child instanceof Text) {
            Text t = (Text) child;
            String trimmedText = t.getTextTrim();
            if (!"".equals(trimmedText)) {
                Text newText = new Text(trimmedText);
                out.output(newText, writer);
            }
        }
    }
    return writer.toString();
}
 
Example 2
private void processChildren(Path file, Element parent) throws InvalidXMLException {
    for(int i = 0; i < parent.getContentSize(); i++) {
        Content content = parent.getContent(i);
        if(!(content instanceof Element)) continue;

        Element child = (Element) content;
        List<Content> replacement = null;

        switch(child.getName()) {
            case "include":
                replacement = processIncludeElement(file, child);
                break;

            case "if":
                replacement = processConditional(child, false);
                break;

            case "unless":
                replacement = processConditional(child, true);
                break;
        }

        if(replacement != null) {
            parent.removeContent(i);
            parent.addContent(i, replacement);
            i--; // Process replacement content
        } else {
            processChildren(file, child);
        }
    }
}
 
Example 3
/**
 * This method returns the position of a bookmarked entry-number in the
 * bookmarks XML file. if the entry-number was not bookmarked (i.e. the
 * entry-number does not exist as bookmark-number), the return value is -1
 *
 * @param nr the number of the bookmarked entry
 * @return the position of the bookmark within the xml-file, or -1 if no
 * bookmark was found
 */
public int getBookmarkPosition(int nr) {
    // create a list of all bookmarks elements from the bookmark xml file
    try {
        // get root element
        Element bme = bookmarks.getRootElement();
        // get child element
        Element bookchild = bme.getChild("bookmark");
        // check for valid value
        if (bookchild != null) {
            // we are interested in the children of the child-element "bookmark"
            List<?> bookmarkList = bookchild.getContent();
            // an iterator for the loop below
            Iterator<?> iterator = bookmarkList.iterator();
            // counter for the return value if a found bookmark-number matches the parameter
            int cnt = 0;
            // start loop
            while (iterator.hasNext()) {
                // retrieve each element
                Element bm = (Element) iterator.next();
                // if bookmark-number matches the parameter integer value, return the position
                if (Integer.parseInt(bm.getAttributeValue("id")) == nr) {
                    return cnt;
                }
                // else increase counter
                cnt++;
            }
        }
        // if no bookmark was found, return -1
        return -1;
    } catch (IllegalStateException e) {
        Constants.zknlogger.log(Level.WARNING, e.getLocalizedMessage());
        return -1;
    }
}
 
Example 4
Source Project: rome   File: ITunesParser.java    License: Apache License 2.0 5 votes vote down vote up
protected String getXmlInnerText(final Element e) {
    final StringBuffer sb = new StringBuffer();
    final XMLOutputter xo = new XMLOutputter();
    final List<Content> children = e.getContent();
    sb.append(xo.outputString(children));
    return sb.toString();
}
 
Example 5
Source Project: rome   File: ContentModuleParser.java    License: Apache License 2.0 5 votes vote down vote up
protected String getXmlInnerText(final Element e) {
    final StringBuffer sb = new StringBuffer();
    final XMLOutputter xo = new XMLOutputter();
    final List<Content> children = e.getContent();
    sb.append(xo.outputString(children));

    return sb.toString();
}
 
Example 6
Source Project: rome   File: SSE091Parser.java    License: Apache License 2.0 5 votes vote down vote up
private List<Conflict> parseConflicts(final Element syncElement, final Locale locale) {
    List<Conflict> conflicts = null;

    final List<Element> conflictsContent = syncElement.getContent(new ContentFilter(Conflicts.NAME));
    for (final Element conflictsElement : conflictsContent) {
        final List<Element> conflictContent = conflictsElement.getContent(new ContentFilter(Conflict.NAME));
        for (final Element element : conflictContent) {
            final Element conflictElement = element;

            final Conflict conflict = new Conflict();
            conflict.setBy(parseStringAttribute(conflictElement, Conflict.BY_ATTRIBUTE));
            conflict.setWhen(parseDateAttribute(conflictElement, Conflict.WHEN_ATTRIBUTE, locale));
            conflict.setVersion(parseIntegerAttribute(conflictElement, Conflict.VERSION_ATTRIBUTE));

            final List<Element> conflictItemContent = conflictElement.getContent(new ContentFilter("item"));
            for (final Element element2 : conflictItemContent) {
                final Element conflictItemElement = element2;
                final Element root = getRoot(conflictItemElement);
                final Item conflictItem = rssParser.parseItem(root, conflictItemElement, locale);
                conflict.setItem(conflictItem);

                if (conflicts == null) {
                    conflicts = new ArrayList<Conflict>();
                }
                conflicts.add(conflict);
            }
        }
    }

    return conflicts;
}
 
Example 7
Source Project: rome   File: SSE091Parser.java    License: Apache License 2.0 5 votes vote down vote up
private Element getFirstContent(final Element element, final String name) {
    final List<Element> filterList = element.getContent(new ContentFilter(name));
    Element firstContent = null;
    if (filterList != null && !filterList.isEmpty()) {
        firstContent = filterList.get(0);
    }
    return firstContent;
}
 
Example 8
Source Project: rome   File: SSE091Parser.java    License: Apache License 2.0 5 votes vote down vote up
private void parseUpdates(final Element historyChild, final History history, final Locale locale) {
    final List<Element> updatedChildren = historyChild.getContent(new ContentFilter(Update.NAME));
    for (final Element updateChild : updatedChildren) {
        final Update update = new Update();
        update.setBy(parseStringAttribute(updateChild, Update.BY_ATTRIBUTE));
        update.setWhen(parseDateAttribute(updateChild, Update.WHEN_ATTRIBUTE, locale));
        history.addUpdate(update);
    }
}
 
Example 9
Source Project: rome   File: SSEParserTest.java    License: Apache License 2.0 5 votes vote down vote up
private void asserEqualContent(final Element one, final Element two) {
    final List<Content> oneContent = one.getContent();
    final List<Content> twoContent = two.getContent();
    if (bothNull(oneContent, twoContent)) {
        return;
    }

    assertNullEqual("missing compare content", oneContent, twoContent);
    assertEqualAttributes(one, two);

    // scan through the content to make sure each element is equal
    for (final Object content1 : oneContent) {
        if (content1 instanceof Element) {
            final Element e1 = (Element) content1;

            boolean foundEqual = false;
            final ArrayList<String> messages = new ArrayList<String>();
            for (final Object o : twoContent) {
                if (o instanceof Element) {
                    final Element e2 = (Element) o;

                    try {
                        // have to check all elements to be order insensitive
                        if (e1.getName().equals(e2.getName()) && equalAttributes(e1, e2, false)) {
                            assertEqualElements(e1, e2);
                            foundEqual = true;
                            messages.clear();
                            break;
                        }
                    } catch (final Error e) {
                        messages.add(e.getMessage());
                    }
                }
            }

            // look for the content in the other tree
            assertTrue("could not find matching element for: " + one.getName(), foundEqual);
        }
    }
}
 
Example 10
Source Project: rome   File: Atom10Parser.java    License: Apache License 2.0 5 votes vote down vote up
private String parseTextConstructToString(final Element e) {

        String type = getAttributeValue(e, "type");
        if (type == null) {
            type = Content.TEXT;
        }

        String value = null;
        if (type.equals(Content.XHTML) || type.indexOf("/xml") != -1 || type.indexOf("+xml") != -1) {
            // XHTML content needs special handling
            final XMLOutputter outputter = new XMLOutputter();
            final List<org.jdom2.Content> contents = e.getContent();
            for (final org.jdom2.Content content : contents) {
                if (content instanceof Element) {
                    final Element element = (Element) content;
                    if (element.getNamespace().equals(getAtomNamespace())) {
                        element.setNamespace(Namespace.NO_NAMESPACE);
                    }
                }
            }
            value = outputter.outputString(contents);
        } else {
            // Everything else comes in verbatim
            value = e.getText();
        }

        return value;

    }
 
Example 11
Source Project: rome   File: RSS092Parser.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Description parseItemDescription(final Element rssRoot, final Element eDesc) {
    final Description desc = new Description();
    final StringBuilder sb = new StringBuilder();
    final XMLOutputter xmlOut = new XMLOutputter();
    for (final Content c : eDesc.getContent()) {
        switch (c.getCType()) {
            case Text:
            case CDATA:
                sb.append(c.getValue());
                break;
            case EntityRef:
                LOG.debug("Entity: {}", c.getValue());
                sb.append(c.getValue());
                break;
            case Element:
                sb.append(xmlOut.outputString((Element) c));
                break;
            default:
                // ignore
                break;
        }
    }
    desc.setValue(sb.toString());
    String att = eDesc.getAttributeValue("type");
    if (att == null) {
        att = "text/html";
    }
    desc.setType(att);
    return desc;
}
 
Example 12
Source Project: snap-desktop   File: AOI.java    License: GNU General Public License v3.0 4 votes vote down vote up
private boolean load(final File file) {
    Document doc;
    try {
        doc = XMLSupport.LoadXML(file.getAbsolutePath());

        final Element root = doc.getRootElement();
        final Attribute nameAttrib = root.getAttribute("name");
        if (nameAttrib != null)
            this.name = nameAttrib.getValue();

        final List<GeoPos> geoPosList = new ArrayList<GeoPos>();

        final List<Content> children = root.getContent();
        for (Object aChild : children) {
            if (aChild instanceof Element) {
                final Element child = (Element) aChild;
                if (child.getName().equals("param")) {
                    inputFolder = XMLSupport.getAttrib(child, "inputFolder");
                    outputFolder = XMLSupport.getAttrib(child, "outputFolder");
                    processingGraph = XMLSupport.getAttrib(child, "graph");
                    lastProcessed = XMLSupport.getAttrib(child, "lastProcessed");
                    final Attribute findSlavesAttrib = child.getAttribute("findSlaves");
                    if (findSlavesAttrib != null)
                        findSlaves = Boolean.parseBoolean(findSlavesAttrib.getValue());
                    final Attribute maxSlavesAttrib = child.getAttribute("maxSlaves");
                    if (maxSlavesAttrib != null)
                        maxSlaves = Integer.parseInt(maxSlavesAttrib.getValue());
                } else if (child.getName().equals("points")) {
                    final List<Content> pntsList = child.getContent();
                    for (Object o : pntsList) {
                        if (o instanceof Element) {
                            final Element pntElem = (Element) o;
                            final String latStr = XMLSupport.getAttrib(pntElem, "lat");
                            final String lonStr = XMLSupport.getAttrib(pntElem, "lon");
                            if (!latStr.isEmpty() && !lonStr.isEmpty()) {
                                final float lat = Float.parseFloat(latStr);
                                final float lon = Float.parseFloat(lonStr);
                                geoPosList.add(new GeoPos(lat, lon));
                            }
                        }
                    }
                } else if (child.getName().equals(DBQuery.DB_QUERY)) {
                    slaveDBQuery = new DBQuery();
                    //todo slaveDBQuery.fromXML(child);
                }
            }
        }

        aoiPoints = geoPosList.toArray(new GeoPos[geoPosList.size()]);
    } catch (IOException e) {
        SnapApp.getDefault().handleError("Unable to load AOI", e);
        return false;
    }
    return true;
}