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.facebook.buck.ide.intellij.projectview.ProjectView.java

License:Apache License

private static Element newElement(String name, Attribute... attributes) {
    Element element = new Element(name);
    if (attributes != null) {
        for (Attribute attribute : attributes) {
            element.setAttribute(attribute);
        }/*from   ww  w.j ava 2 s  .c om*/
    }
    return element;
}

From source file:com.rometools.modules.atom.io.AtomModuleGenerator.java

License:Apache License

private Element generateLink(Link link) {
    Element linkElement = new Element("link", NS);

    if (link.getHref() != null) {
        Attribute href = new Attribute(AtomLinkAttribute.HREF, link.getHref());
        linkElement.setAttribute(href);
    }/*from w ww  . j  ava2s  .c  o m*/
    if (link.getType() != null) {
        Attribute type = new Attribute(AtomLinkAttribute.TYPE, link.getType());
        linkElement.setAttribute(type);
    }
    if (link.getRel() != null) {
        Attribute rel = new Attribute(AtomLinkAttribute.REL, link.getRel());
        linkElement.setAttribute(rel);
    }

    if (link.getHreflang() != null) {
        final Attribute hreflangAttribute = new Attribute(AtomLinkAttribute.HREF_LANG, link.getHreflang());
        linkElement.setAttribute(hreflangAttribute);
    }

    if (link.getTitle() != null) {
        final Attribute title = new Attribute(AtomLinkAttribute.TITLE, link.getTitle());
        linkElement.setAttribute(title);
    }

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

    return linkElement;
}

From source file:com.rometools.modules.content.io.ContentModuleGenerator.java

License:Open Source License

@Override
public void generate(final com.rometools.rome.feed.module.Module module, final org.jdom2.Element element) {
    // this is not necessary, it is done to avoid the namespace definition in every item.
    Element root = element;//from  w  ww .  j  a v  a2s.c  o m

    while (root.getParent() != null && root.getParent() instanceof Element) {
        root = (Element) root.getParent();
    }

    root.addNamespaceDeclaration(CONTENT_NS);

    if (!(module instanceof ContentModule)) {
        return;
    }

    final ContentModule cm = (ContentModule) module;

    final List<String> encodeds = cm.getEncodeds();

    if (encodeds != null) {
        LOG.debug("{}", cm.getEncodeds().size());
        for (int i = 0; i < encodeds.size(); i++) {
            element.addContent(generateCDATAElement("encoded", encodeds.get(i).toString()));
        }
    }

    final List<ContentItem> contentItems = cm.getContentItems();

    if (contentItems != null && !contentItems.isEmpty()) {
        final Element items = new Element("items", CONTENT_NS);
        final Element bag = new Element("Bag", RDF_NS);
        items.addContent(bag);

        for (int i = 0; i < contentItems.size(); i++) {
            final ContentItem contentItem = contentItems.get(i);
            final Element li = new Element("li", RDF_NS);
            final Element item = new Element("item", CONTENT_NS);

            if (contentItem.getContentAbout() != null) {
                final Attribute about = new Attribute("about", contentItem.getContentAbout(), RDF_NS);
                item.setAttribute(about);
            }

            if (contentItem.getContentFormat() != null) {
                // LOG.debug( "Format");
                final Element format = new Element("format", CONTENT_NS);
                final Attribute formatResource = new Attribute("resource", contentItem.getContentFormat(),
                        RDF_NS);
                format.setAttribute(formatResource);

                item.addContent(format);
            }

            if (contentItem.getContentEncoding() != null) {
                // LOG.debug( "Encoding");
                final Element encoding = new Element("encoding", CONTENT_NS);
                final Attribute encodingResource = new Attribute("resource", contentItem.getContentEncoding(),
                        RDF_NS);
                encoding.setAttribute(encodingResource);
                item.addContent(encoding);
            }

            if (contentItem.getContentValue() != null) {
                final Element value = new Element("value", RDF_NS);

                if (contentItem.getContentValueParseType() != null) {
                    final Attribute parseType = new Attribute("parseType",
                            contentItem.getContentValueParseType(), RDF_NS);
                    value.setAttribute(parseType);
                }

                if (contentItem.getContentValueNamespaces() != null) {
                    final List<Namespace> namespaces = contentItem.getContentValueNamespaces();

                    for (int ni = 0; ni < namespaces.size(); ni++) {
                        value.addNamespaceDeclaration(namespaces.get(ni));
                    }
                }

                final List<Content> detached = new ArrayList<Content>();

                for (int c = 0; c < contentItem.getContentValueDOM().size(); c++) {
                    detached.add(contentItem.getContentValueDOM().get(c).clone().detach());
                }

                value.setContent(detached);
                item.addContent(value);
            } // end value

            li.addContent(item);
            bag.addContent(li);
        } // end contentItems loop

        element.addContent(items);
    }
}

From source file:com.rometools.modules.opensearch.impl.OpenSearchModuleGenerator.java

License:Apache License

protected Element generateQueryElement(final OSQuery query) {

    final Element qElement = new Element("Query", OS_NS);

    if (query.getRole() != null) {
        final Attribute roleAttribute = new Attribute("role", query.getRole());
        qElement.setAttribute(roleAttribute);
    } else {/*from   ww w .  j av  a2 s.  com*/
        throw new RequiredAttributeMissingException(
                "If declaring a Query element, the field 'role' must be be specified");
    }

    if (query.getOsd() != null) {
        final Attribute osd = new Attribute("osd", query.getOsd());
        qElement.setAttribute(osd);
    }

    if (query.getSearchTerms() != null) {
        final Attribute searchTerms = new Attribute("searchTerms", query.getSearchTerms());
        qElement.setAttribute(searchTerms);
    }

    if (query.getStartPage() > -1) {
        final int startPage = query.getStartPage() != 0 ? query.getStartPage() : 1;
        final Attribute sp = new Attribute("startPage", Integer.toString(startPage));
        qElement.setAttribute(sp);
    }

    if (query.getTitle() != null) {
        qElement.setAttribute(new Attribute("title", query.getTitle()));
    }

    if (query.getTotalResults() > -1) {
        qElement.setAttribute(new Attribute("totalResults", Integer.toString(query.getTotalResults())));
    }

    return qElement;
}

From source file:com.rometools.modules.opensearch.impl.OpenSearchModuleGenerator.java

License:Apache License

protected Element generateLinkElement(final Link link) {
    final Element linkElement = new Element("link", OS_NS);

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

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

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

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

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

License:Open Source License

protected Element createRootElement(final Feed feed) {
    final Element root = new Element("feed", getFeedNamespace());
    root.addNamespaceDeclaration(getFeedNamespace());
    final Attribute version = new Attribute("version", getVersion());
    root.setAttribute(version);
    generateModuleNamespaceDefs(root);/*from  www. j a v  a2 s. c  o m*/
    return root;
}

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

License:Open Source License

protected Element generateLinkElement(final Link link) {

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

    final String rel = link.getRel();
    if (rel != null) {
        final Attribute relAttribute = new Attribute("rel", rel.toString());
        linkElement.setAttribute(relAttribute);
    }//from   ww w . j  a va2s  . c om

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

    return linkElement;

}

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

License:Open Source License

protected Element generateTagLineElement(final Content tagline) {

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

    final String type = tagline.getType();
    if (type != null) {
        final Attribute typeAttribute = new Attribute("type", type);
        taglineElement.setAttribute(typeAttribute);
    }//from   w w  w  .j  av a2 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.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  w ww. j av  a  2 s.  co 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);
    }/* w  ww  .j  a v a  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;

}