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:com.rometools.rome.io.impl.Atom03Parser.java

License:Open Source License

private Content parseContent(final Element e) {

    String value = null;//from  w w  w .  jav a  2s  . c  om

    String type = getAttributeValue(e, "type");
    if (type == null) {
        type = "text/plain";
    }

    String mode = getAttributeValue(e, "mode");
    if (mode == null) {
        mode = Content.XML; // default to xml content
    }

    if (mode.equals(Content.ESCAPED)) {

        // do nothing XML Parser took care of this
        value = e.getText();

    } else if (mode.equals(Content.BASE64)) {

        value = Base64.decode(e.getText());

    } else if (mode.equals(Content.XML)) {

        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);

    }

    final Content content = new Content();
    content.setType(type);
    content.setMode(mode);
    content.setValue(value);
    return content;
}

From source file:com.rometools.rome.io.impl.Atom10Parser.java

License:Open Source License

private String parseTextConstructToString(final Element e) {

    String type = getAttributeValue(e, "type");
    if (type == null) {
        type = Content.TEXT;/*w w  w.  j  a va2 s  .  co  m*/
    }

    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;

}

From source file:com.rometools.rome.io.impl.RSS092Parser.java

License:Open Source License

@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());/*from   w  ww . ja  va2s .c  o  m*/
            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;
}

From source file:com.sun.syndication.io.impl.Atom03Parser.java

License:Open Source License

private Content parseContent(Element e) {
    String value = null;/*from ww  w.  j a  va2s. c o m*/
    String type = getAttributeValue(e, "type");
    type = (type != null) ? type : "text/plain";
    String mode = getAttributeValue(e, "mode");
    if (mode == null) {
        mode = Content.XML; // default to xml content
    }
    if (mode.equals(Content.ESCAPED)) {
        // do nothing XML Parser took care of this
        value = e.getText();
    } else if (mode.equals(Content.BASE64)) {
        value = Base64.decode(e.getText());
    } else if (mode.equals(Content.XML)) {
        XMLOutputter outputter = new XMLOutputter();
        List eContent = e.getContent();
        Iterator i = eContent.iterator();
        while (i.hasNext()) {
            org.jdom2.Content c = (org.jdom2.Content) i.next();
            if (c instanceof Element) {
                Element eC = (Element) c;
                if (eC.getNamespace().equals(getAtomNamespace())) {
                    ((Element) c).setNamespace(Namespace.NO_NAMESPACE);
                }
            }
        }
        value = outputter.outputString(eContent);
    }

    Content content = new Content();
    content.setType(type);
    content.setMode(mode);
    content.setValue(value);
    return content;
}

From source file:com.sun.syndication.io.impl.Atom10Parser.java

License:Open Source License

private String parseTextConstructToString(Element e) {
    String value = null;/*from w w  w.  j a  v  a2s .c om*/
    String type = getAttributeValue(e, "type");
    type = (type != null) ? type : Content.TEXT;
    if (type.equals(Content.XHTML) || (type.indexOf("/xml")) != -1 || (type.indexOf("+xml")) != -1) {
        // XHTML content needs special handling
        XMLOutputter outputter = new XMLOutputter();
        List eContent = e.getContent();
        Iterator i = eContent.iterator();
        while (i.hasNext()) {
            org.jdom2.Content c = (org.jdom2.Content) i.next();
            if (c instanceof Element) {
                Element eC = (Element) c;
                if (eC.getNamespace().equals(getAtomNamespace())) {
                    ((Element) c).setNamespace(Namespace.NO_NAMESPACE);
                }
            }
        }
        value = outputter.outputString(eContent);
    } else {
        // Everything else comes in verbatim
        value = e.getText();
    }
    return value;
}

From source file:com.webfront.app.utils.PDFImporter.java

private void getConfig() {
    jdomBuilder = new SAXBuilder();
    String xmlSource = cfg.getInstallDir() + cfg.getFileSep() + "pnc.xml";
    try {//from  w  ww  . j a v  a 2s  . com
        xmlDoc = jdomBuilder.build(xmlSource);
        Element root = xmlDoc.getRootElement();
        List<Content> configContent = root.getContent();
        List<Element> childElements = root.getChildren();
        Element header = root.getChild("header");
        for (Element e : header.getChildren()) {
            String content = e.getAttributeValue("content");
            String format = e.getAttributeValue("format");
            String text = e.getText();
        }
    } catch (JDOMException ex) {
        Logger.getLogger(PDFImporter.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(PDFImporter.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Contabilidad.CodeBase64.java

public void limpia(String ruta) {
    try {/*from ww w  .ja v  a2 s  .com*/
        org.jdom2.Document doc = new SAXBuilder().build(ruta);
        Element rootNode = doc.getRootElement();
        List list = rootNode.getContent();
        for (int i = 0; i < list.size(); i++) {
            Content elementos = (Content) list.get(i);
            if (elementos.getCType() == Content.CType.Element) {
                Element aux = (Element) elementos;
                if (aux.getName().compareToIgnoreCase("Addenda") == 0) {
                    List list2 = aux.getContent();
                    for (int j = 0; j < list2.size(); j++) {
                        Content elementos2 = (Content) list2.get(j);
                        if (elementos2.getCType() == Content.CType.Element) {
                            Element aux2 = (Element) elementos2;
                            if (aux2.getName().compareToIgnoreCase("FactDocMX") == 0) {
                                list2.remove(aux2);
                            }
                            if (aux2.getName().compareToIgnoreCase("ECFD") == 0) {
                                Namespace NP = Namespace.getNamespace("", "");
                                aux2.setNamespace(NP);
                                List list3 = aux2.getContent();
                                for (int k = 0; k < list3.size(); k++) {
                                    Content elementos3 = (Content) list3.get(k);
                                    if (elementos3.getCType() == Content.CType.Element) {
                                        Element aux3 = (Element) elementos3;
                                        aux3.setNamespace(NP);
                                        List list4 = aux3.getContent();
                                        for (int l = 0; l < list4.size(); l++) {
                                            Content elementos4 = (Content) list4.get(l);
                                            if (elementos4.getCType() == Content.CType.Element) {
                                                Element aux4 = (Element) elementos4;
                                                aux4.setNamespace(NP);
                                                List list5 = aux4.getContent();
                                                for (int m = 0; m < list5.size(); m++) {
                                                    Content elementos5 = (Content) list5.get(m);
                                                    if (elementos5.getCType() == Content.CType.Element) {
                                                        Element aux5 = (Element) elementos5;
                                                        aux5.setNamespace(NP);
                                                        List list6 = aux5.getContent();
                                                        for (int n = 0; n < list6.size(); n++) {
                                                            Content elementos6 = (Content) list6.get(n);
                                                            if (elementos6
                                                                    .getCType() == Content.CType.Element) {
                                                                Element aux6 = (Element) elementos6;
                                                                aux6.setNamespace(NP);
                                                                List list7 = aux6.getContent();
                                                                for (int p = 0; p < list7.size(); p++) {
                                                                    Content elementos7 = (Content) list7.get(p);
                                                                    if (elementos7
                                                                            .getCType() == Content.CType.Element) {
                                                                        Element aux7 = (Element) elementos7;
                                                                        aux7.setNamespace(NP);
                                                                        List list8 = aux7.getContent();
                                                                        for (int q = 0; q < list8.size(); q++) {
                                                                            Content elementos8 = (Content) list8
                                                                                    .get(q);
                                                                            if (elementos8
                                                                                    .getCType() == Content.CType.Element) {
                                                                                Element aux8 = (Element) elementos8;
                                                                                aux8.setNamespace(NP);
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                                List atributos = aux2.getAttributes();
                                for (int a = 0; a < atributos.size(); a++) {
                                    Attribute at = (Attribute) atributos.get(a);
                                    if (at.getName().compareToIgnoreCase("schemaLocation") == 0)
                                        aux2.removeAttribute(at);
                                }
                            }
                        }
                    }
                }
            }
        }
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        try {
            outputter.output(doc, new FileOutputStream(ruta));
        } catch (IOException e) {
            System.out.println(e);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.danielluedecke.zettelkasten.database.Bookmarks.java

License:Open Source License

/**
 * 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/*w  ww  .  ja  va  2 s. co m*/
 * 
 * @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;
    }
}

From source file:de.nava.informa.utils.AtomParserUtils.java

License:Open Source License

/**
 * Clears namespace signature from the element and all of the children.
 *///from   w  w w  .  ja v a2 s.  c  om
private static void clearNamespace(Element elt) {
    if (elt == null) {
        return;
    }

    elt.setNamespace(null);

    for (Object item : elt.getContent()) {
        if (item instanceof Element) {
            clearNamespace((Element) item);
        }
    }
}

From source file:de.nava.informa.utils.AtomParserUtils.java

License:Open Source License

/**
 * Returns value of the element./*  ww w.j  a v  a2  s  .c  o  m*/
 *
 * @param elt  the element to retrieve the value from.
 * @param mode can be one of "escaped", "base64" or "xml". If null, "xml" is assumed.
 */
public static String getValue(Element elt, String mode) {
    if (elt == null) {
        return "";
    }

    mode = (mode == null) ? "xml" : mode;

    clearNamespace(elt);

    String value;

    // Here we convert the contents of element into some readable text.
    // If the contents (after removing leading and trailing spaces) is CDATA only,
    // we need to treat it specially by unpacking the contents it has. Otherwise,
    // we simply output what we have.
    List<Content> content = elt.getContent();
    content = trimContents(content);

    if ((content.size() == 1) && content.get(0) instanceof CDATA) {
        value = content.get(0).getValue();
    } else {
        value = OUTPUTER.outputString(content);
    }

    // Unescape or decode stuff if necessary
    if ("escaped".equals(mode)) {
        value = ParserUtils.unEscape(value);
    } else if ("base64".equals(mode)) {
        value = ParserUtils.decodeBase64(value);
    }

    return value;
}