Example usage for org.jdom2 Element setAttribute

List of usage examples for org.jdom2 Element setAttribute

Introduction

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

Prototype

public Element setAttribute(final Attribute attribute) 

Source Link

Document

This sets an attribute value for this element.

Usage

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);
    }/* w  w w . j  a  va 2  s.  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 www  .j av a2  s .  co 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);
    }//from   w ww.  j  ava2 s  . 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  www. j  a  va2  s. c  om

    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. java 2  s.co  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.DCModuleGenerator.java

License:Open Source License

/**
 * Utility method to generate an element for a subject.
 * <p>/*from  w  ww  .  j  a va 2s.co m*/
 *
 * @param subject the subject to generate an element for.
 * @return the element for the subject.
 */
protected final Element generateSubjectElement(final DCSubject subject) {

    final Element subjectElement = new Element("subject", getDCNamespace());

    final String taxonomyUri = subject.getTaxonomyUri();
    final String value = subject.getValue();

    if (taxonomyUri != null) {

        final Attribute resourceAttribute = new Attribute("resource", taxonomyUri, getRDFNamespace());

        final Element topicElement = new Element("topic", getTaxonomyNamespace());
        topicElement.setAttribute(resourceAttribute);

        final Element descriptionElement = new Element("Description", getRDFNamespace());
        descriptionElement.addContent(topicElement);

        if (value != null) {
            final Element valueElement = new Element("value", getRDFNamespace());
            valueElement.addContent(value);
            descriptionElement.addContent(valueElement);
        }

        subjectElement.addContent(descriptionElement);

    } else {
        subjectElement.addContent(value);
    }

    return subjectElement;
}

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);
    root.addNamespaceDeclaration(getContentNamespace());
    generateModuleNamespaceDefs(root);// w w  w  .  j a va 2s.  c om
    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));
    }// ww w .j av  a  2  s  .c om

    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  a  va  2  s. c  om

    sourceElement.addContent(source.getValue());

    return sourceElement;
}

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

License:Open Source License

@Override
protected void populateItem(final Item item, final Element eItem, final int index) {
    super.populateItem(item, eItem, index);

    final Description description = item.getDescription();
    if (description != null && description.getType() != null) {
        final Element eDescription = eItem.getChild("description", getFeedNamespace());
        eDescription.setAttribute(new Attribute("type", description.getType()));
    }//from   w  w  w  .  j  av  a  2s. c  o  m
    eItem.removeChild("expirationDate", getFeedNamespace());
}