Example usage for org.jdom2 Attribute Attribute

List of usage examples for org.jdom2 Attribute Attribute

Introduction

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

Prototype

public Attribute(final String name, final String value) 

Source Link

Document

This will create a new Attribute with the specified (local) name and value, and does not place the attribute in a Namespace .

Usage

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

License:Open Source License

protected void fillContentElement(final Element contentElement, final Content content) throws FeedException {

    final String type = content.getType();
    if (type != null) {
        final Attribute typeAttribute = new Attribute("type", type);
        contentElement.setAttribute(typeAttribute);
    }/*from ww w.  j a  v a2 s .  c o m*/

    final String mode = content.getMode();
    if (mode != null) {
        final Attribute modeAttribute = new Attribute("mode", mode.toString());
        contentElement.setAttribute(modeAttribute);
    }

    final String value = content.getValue();
    if (value != null) {

        if (mode == null || mode.equals(Content.ESCAPED)) {

            contentElement.addContent(value);

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

            contentElement.addContent(Base64.encode(value));

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

            final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
            tmpDocString.append(value);
            tmpDocString.append("</tmpdoc>");
            final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
            Document tmpDoc;

            try {
                final SAXBuilder saxBuilder = new SAXBuilder();
                tmpDoc = saxBuilder.build(tmpDocReader);
            } catch (final Exception ex) {
                throw new FeedException("Invalid XML", ex);
            }

            final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent();
            contentElement.addContent(children);
        }

    }
}

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

License:Open Source License

protected Element generateGeneratorElement(final Generator generator) {

    final Element generatorElement = new Element("generator", getFeedNamespace());

    final String url = generator.getUrl();
    if (url != null) {
        final Attribute urlAttribute = new Attribute("url", url);
        generatorElement.setAttribute(urlAttribute);
    }/*from  w ww.  j  a  va 2 s. c  o  m*/

    final String version = generator.getVersion();
    if (version != null) {
        final Attribute versionAttribute = new Attribute("version", version);
        generatorElement.setAttribute(versionAttribute);
    }

    final String value = generator.getValue();
    if (value != null) {
        generatorElement.addContent(value);
    }

    return generatorElement;

}

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

License:Open Source License

protected Element generateCategoryElement(final Category cat) {

    final Namespace namespace = getFeedNamespace();
    final Element catElement = new Element("category", namespace);

    final String term = cat.getTerm();
    if (term != null) {
        final Attribute termAttribute = new Attribute("term", term);
        catElement.setAttribute(termAttribute);
    }/*from   w w w . jav  a 2s.c  o m*/

    final String label = cat.getLabel();
    if (label != null) {
        final Attribute labelAttribute = new Attribute("label", label);
        catElement.setAttribute(labelAttribute);
    }

    final String scheme = cat.getScheme();
    if (scheme != null) {
        final Attribute schemeAttribute = new Attribute("scheme", scheme);
        catElement.setAttribute(schemeAttribute);
    }

    return catElement;

}

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

License:Open Source License

protected Element generateLinkElement(final Link link) {

    final Namespace namespace = getFeedNamespace();
    final Element linkElement = new Element("link", namespace);

    final String rel = link.getRel();
    if (rel != null) {
        final Attribute relAttribute = new Attribute("rel", rel);
        linkElement.setAttribute(relAttribute);
    }/*from w  w w  .  ja  va 2  s.  c  o m*/

    final String type = link.getType();
    if (type != null) {
        final Attribute typeAttribute = new Attribute("type", type);
        linkElement.setAttribute(typeAttribute);
    }

    final String href = link.getHref();
    if (href != null) {
        final Attribute hrefAttribute = new Attribute("href", href);
        linkElement.setAttribute(hrefAttribute);
    }

    final String hreflang = link.getHreflang();
    if (hreflang != null) {
        final Attribute hreflangAttribute = new Attribute("hreflang", hreflang);
        linkElement.setAttribute(hreflangAttribute);
    }

    final String linkTitle = link.getTitle();
    if (linkTitle != null) {
        final Attribute title = new Attribute("title", linkTitle);
        linkElement.setAttribute(title);
    }

    if (link.getLength() != 0) {
        final Attribute lenght = new Attribute("length", Long.toString(link.getLength()));
        linkElement.setAttribute(lenght);
    }

    return linkElement;

}

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

License:Open Source License

protected Element generateTagLineElement(final Content tagline) {

    final Element taglineElement = new Element("subtitle", getFeedNamespace());

    final String type = tagline.getType();
    if (type != null) {
        final Attribute typeAttribute = new Attribute("type", type);
        taglineElement.setAttribute(typeAttribute);
    }/*  w w  w .  j a v  a 2s  .  c o  m*/

    final String value = tagline.getValue();
    if (value != null) {
        taglineElement.addContent(value);
    }

    return taglineElement;

}

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

License:Open Source License

protected void fillContentElement(final Element contentElement, final Content content) throws FeedException {

    final String type = content.getType();

    String atomType = type;/*from  w  w w.ja v a 2 s  . com*/

    if (type != null) {
        // Fix for issue #39 "Atom 1.0 Text Types Not Set Correctly"
        // we're not sure who set this value, so ensure Atom types are used
        if ("text/plain".equals(type)) {
            atomType = Content.TEXT;
        } else if ("text/html".equals(type)) {
            atomType = Content.HTML;
        } else if ("application/xhtml+xml".equals(type)) {
            atomType = Content.XHTML;
        }

        final Attribute typeAttribute = new Attribute("type", atomType);
        contentElement.setAttribute(typeAttribute);
    }

    final String href = content.getSrc();
    if (href != null) {
        final Attribute srcAttribute = new Attribute("src", href);
        contentElement.setAttribute(srcAttribute);
    }

    final String value = content.getValue();
    if (value != null) {

        if (atomType != null && (atomType.equals(Content.XHTML) || atomType.indexOf("/xml") != -1
                || atomType.indexOf("+xml") != -1)) {

            final StringBuffer tmpDocString = new StringBuffer("<tmpdoc>");
            tmpDocString.append(value);
            tmpDocString.append("</tmpdoc>");
            final StringReader tmpDocReader = new StringReader(tmpDocString.toString());
            Document tmpDoc;
            try {
                final SAXBuilder saxBuilder = new SAXBuilder();
                tmpDoc = saxBuilder.build(tmpDocReader);
            } catch (final Exception ex) {
                throw new FeedException("Invalid XML", ex);
            }
            final List<org.jdom2.Content> children = tmpDoc.getRootElement().removeContent();
            contentElement.addContent(children);

        } else {

            // must be type html, text or some other non-XML format
            // JDOM will escape property for XML
            contentElement.addContent(value);

        }

    }
}

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

License:Open Source License

protected Element generateGeneratorElement(final Generator generator) {

    final Element generatorElement = new Element("generator", getFeedNamespace());

    final String url = generator.getUrl();
    if (url != null) {
        final Attribute urlAttribute = new Attribute("uri", url);
        generatorElement.setAttribute(urlAttribute);
    }//from w w  w  .j a v  a  2  s  . c  o m

    final String version2 = generator.getVersion();
    if (version2 != null) {
        final Attribute versionAttribute = new Attribute("version", version2);
        generatorElement.setAttribute(versionAttribute);
    }

    final String value = generator.getValue();
    if (value != null) {
        generatorElement.addContent(value);
    }

    return generatorElement;

}

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

License:Open Source License

@Override
protected Element createRootElement(final Channel channel) {
    final Element root = new Element("rss", getFeedNamespace());
    final Attribute version = new Attribute("version", getVersion());
    root.setAttribute(version);//from w ww. j a v a2 s .  c  om
    root.addNamespaceDeclaration(getContentNamespace());
    generateModuleNamespaceDefs(root);
    return root;
}

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

License:Open Source License

protected Element generateCloud(final Cloud cloud) {

    final Element eCloud = new Element("cloud", getFeedNamespace());

    final String domain = cloud.getDomain();
    if (domain != null) {
        eCloud.setAttribute(new Attribute("domain", domain));
    }//from ww w .  ja  v  a2s .c o m

    final int port = cloud.getPort();
    if (port != 0) {
        eCloud.setAttribute(new Attribute("port", String.valueOf(port)));
    }

    final String path = cloud.getPath();
    if (path != null) {
        eCloud.setAttribute(new Attribute("path", path));
    }

    final String registerProcedure = cloud.getRegisterProcedure();
    if (registerProcedure != null) {
        eCloud.setAttribute(new Attribute("registerProcedure", registerProcedure));
    }

    final String protocol = cloud.getProtocol();
    if (protocol != null) {
        eCloud.setAttribute(new Attribute("protocol", protocol));
    }

    return eCloud;

}

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

License:Open Source License

protected Element generateSourceElement(final Source source) {

    final Element sourceElement = new Element("source", getFeedNamespace());

    final String url = source.getUrl();
    if (url != null) {
        sourceElement.setAttribute(new Attribute("url", url));
    }/*from  w  w w. j  av a2 s . co  m*/

    sourceElement.addContent(source.getValue());

    return sourceElement;
}