Example usage for org.jdom2 Element getNamespace

List of usage examples for org.jdom2 Element getNamespace

Introduction

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

Prototype

public Namespace getNamespace() 

Source Link

Document

Returns the element's Namespace .

Usage

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

License:Open Source License

private Content parseContent(final Element e) {

    String value = null;// w w w .ja v  a  2s .c o m

    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;//from ww w  . j a v a 2 s.c o 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.ModuleParsers.java

License:Open Source License

private boolean hasElementsFrom(final Element root, final Namespace namespace) {
    boolean hasElements = false;
    for (final Element child : root.getChildren()) {
        final Namespace childNamespace = child.getNamespace();
        if (namespace.equals(childNamespace)) {
            hasElements = true;//from   ww w.  j a v a  2  s  .  c o m
            break;
        }
    }
    return hasElements;
}

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

License:Open Source License

@Override
public boolean isMyType(final Document document) {

    final Element rssRoot = document.getRootElement();
    final Namespace defaultNS = rssRoot.getNamespace();
    final List<Namespace> additionalNSs = rssRoot.getAdditionalNamespaces();

    boolean myType = false;
    if (defaultNS != null && defaultNS.equals(getRDFNamespace()) && additionalNSs != null) {
        for (final Namespace namespace : additionalNSs) {
            if (getRSSNamespace().equals(namespace)) {
                myType = true;//from   ww w  .j a  v a 2s  .co m
                break;
            }
        }
    }
    return myType;

}

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

License:Open Source License

/**
 * Parses an item element of an RSS document looking for item information.
 * <p/>//from  ww  w .j  a  va2  s  .c o  m
 * It reads title and link out of the 'item' element.
 * <p/>
 *
 * @param rssRoot the root element of the RSS document in case it's needed for context.
 * @param eItem the item element to parse.
 * @return the parsed RSSItem bean.
 */
protected Item parseItem(final Element rssRoot, final Element eItem, final Locale locale) {

    final Item item = new Item();

    final Element title = eItem.getChild("title", getRSSNamespace());
    if (title != null) {
        item.setTitle(title.getText());
    }

    final Element link = eItem.getChild("link", getRSSNamespace());
    if (link != null) {
        item.setLink(link.getText());
        item.setUri(link.getText());
    }

    item.setModules(parseItemModules(eItem, locale));

    final List<Element> foreignMarkup = extractForeignMarkup(eItem, item, getRSSNamespace());
    // content:encoded elements are treated special, without a module, they have to be removed
    // from the foreign markup to avoid duplication in case of read/write. Note that this fix
    // will break if a content module is used
    final Iterator<Element> iterator = foreignMarkup.iterator();
    while (iterator.hasNext()) {
        final Element element = iterator.next();
        final Namespace eNamespace = element.getNamespace();
        final String eName = element.getName();
        if (getContentNamespace().equals(eNamespace) && eName.equals("encoded")) {
            iterator.remove();
        }
    }

    if (!foreignMarkup.isEmpty()) {
        item.setForeignMarkup(foreignMarkup);
    }

    return item;
}

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

License:Open Source License

/**
 * Indicates if a JDom document is an RSS instance that can be parsed with the parser.
 * <p/>//from   w ww .j ava  2s.  c o m
 * It checks for RDF ("http://www.w3.org/1999/02/22-rdf-syntax-ns#") namespace being defined in
 * the root element and for the RSS 1.0 ("http://purl.org/rss/1.0/") namespace in the channel
 * element.
 *
 * @param document document to check if it can be parsed with this parser implementation.
 * @return <b>true</b> if the document is RSS1., <b>false</b> otherwise.
 */
@Override
public boolean isMyType(final Document document) {
    final Element rssRoot = document.getRootElement();
    final Namespace defaultNS = rssRoot.getNamespace();
    return defaultNS != null && defaultNS.equals(getRDFNamespace())
            && rssRoot.getChild("channel", getRSSNamespace()) != null;
}

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

License:Open Source License

@Override
public boolean isMyType(final Document document) {
    final Element rssRoot = document.getRootElement();
    final Namespace defaultNS = rssRoot.getNamespace();
    return defaultNS != null && defaultNS.equals(getRSSNamespace()) && super.isMyType(document);
}

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

License:Open Source License

public boolean isMyType(Document document) {
    Element rssRoot = document.getRootElement();
    Namespace defaultNS = rssRoot.getNamespace();
    return (defaultNS != null) && defaultNS.equals(getAtomNamespace());
}

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

License:Open Source License

private Content parseContent(Element e) {
    String value = null;//from  w w  w .  java2s. 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;// www.j ava 2  s  .  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;
}