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 String name, final String value) 

Source Link

Document

This sets an attribute value for this element.

Usage

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

License:Apache License

protected boolean addNotNullAttribute(final Element target, final String name, final Object value) {
    if (target == null || name == null || value == null) {
        return false;
    }/*ww w.j av a 2s . c om*/

    target.setAttribute(name, value.toString());

    return true;
}

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

License:Open Source License

protected Element generateEnclosure(final Enclosure enclosure) {

    final Element enclosureElement = new Element("enclosure", getFeedNamespace());

    final String url = enclosure.getUrl();
    if (url != null) {
        enclosureElement.setAttribute("url", url);
    }//from w  w  w .  ja v  a  2  s  .  c  o m

    final long length = enclosure.getLength();
    if (length != 0) {
        enclosureElement.setAttribute("length", String.valueOf(length));
    }

    final String type = enclosure.getType();
    if (type != null) {
        enclosureElement.setAttribute("type", type);
    }

    return enclosureElement;
}

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

License:Open Source License

protected Element generateCategoryElement(final Category category) {

    final Element categoryElement = new Element("category", getFeedNamespace());

    final String domain = category.getDomain();
    if (domain != null) {
        categoryElement.setAttribute("domain", domain);
    }/*from  w ww .j  a  v  a2  s.com*/

    categoryElement.addContent(category.getValue());

    return categoryElement;
}

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

License:Open Source License

@Override
public void populateItem(final Item item, final Element eItem, final int index) {

    super.populateItem(item, eItem, index);

    final Element description = eItem.getChild("description", getFeedNamespace());
    if (description != null) {
        description.removeAttribute("type");
    }/*w  w  w .j a v a2 s.com*/

    final String author = item.getAuthor();
    if (author != null) {
        eItem.addContent(generateSimpleElement("author", author));
    }

    final String comments = item.getComments();
    if (comments != null) {
        eItem.addContent(generateSimpleElement("comments", comments));
    }

    final Guid guid = item.getGuid();
    if (guid != null) {
        final Element eGuid = generateSimpleElement("guid", guid.getValue());
        if (!guid.isPermaLink()) {
            eGuid.setAttribute("isPermaLink", "false");
        }
        eItem.addContent(eGuid);
    }
}

From source file:com.soulgalore.web.pagesavings.reporters.XMLReporter.java

License:Apache License

public void report(Set<SiteResult> results, Map<String, DescriptiveStatistics> statistics) {

    Date reportDate = new Date();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    Element root = new Element("savings");
    root.setAttribute("date", "" + reportDate);

    Element resultsXML = new Element("results");
    root.addContent(resultsXML);// ww w  . ja va 2s .  co m

    Double totalPw = 0D;
    Double totalUnique = 0D;
    for (SiteResult siteResult : results) {
        totalPw += siteResult.getTotalSavings() * siteResult.getSite().getPageViews();
        totalUnique += siteResult.getTotalSavings() * siteResult.getSite().getUniqueBrowsers();
        Element site = new Element("site");
        Element url = new Element("url");
        Element weight = new Element("total-page-weight");
        Element savingsPerPage = new Element("savings-per-page");
        Element savingsForPW = new Element("savings-for-page-view");
        Element savingsForUnique = new Element("savings-for-unique-browsers");
        Element savingsPercentage = new Element("savings-percentage");

        url.addContent(new CDATA(siteResult.getSite().getUrl()));

        savingsPerPage.setAttribute(READABLE,
                humanReadableByteCount(Math.round(siteResult.getTotalSavings()), true));
        savingsPerPage.setAttribute(KB, "" + Math.round(siteResult.getTotalSavings()));

        savingsForPW.setAttribute(READABLE, humanReadableByteCount(
                Math.round(siteResult.getTotalSavings() * siteResult.getSite().getPageViews()), true));
        savingsForPW.setAttribute(KB,
                "" + Math.round(siteResult.getTotalSavings() * siteResult.getSite().getPageViews()));

        savingsForUnique.setAttribute(READABLE, humanReadableByteCount(
                Math.round(siteResult.getTotalSavings() * siteResult.getSite().getUniqueBrowsers()), true));
        savingsForUnique.setAttribute(KB,
                "" + Math.round(siteResult.getTotalSavings() * siteResult.getSite().getUniqueBrowsers()));

        savingsPercentage.addContent("" + Math.round(siteResult.getSavingsPercentage() * 100) / 100.0d);

        weight.setAttribute(READABLE, humanReadableByteCount(siteResult.getTotalSizeBytes() / 1000, true));
        weight.setAttribute(KB, "" + siteResult.getTotalSizeBytes() / 1000);

        Element rulesSavings = new Element("rule-savings");
        for (RuleResult ruleResult : siteResult.getResults()) {
            Element rule = new Element(ruleResult.getRule());
            rule.addContent("" + ruleResult.getSavings());
            rulesSavings.addContent(rule);
        }

        site.addContent(url);
        site.addContent(weight);
        site.addContent(savingsPerPage);
        site.addContent(savingsForPW);
        site.addContent(savingsForUnique);
        site.addContent(savingsPercentage);
        site.addContent(rulesSavings);
        resultsXML.addContent(site);
    }

    Element summary = new Element("summary");
    summary.setAttribute("nrofsites", "" + results.size());
    Element totalPW = new Element("total-pw");
    Element totalUniqueSummary = new Element("total-unique");
    totalPW.setAttribute(READABLE, humanReadableByteCount(Math.round(totalPw), true));
    totalPW.setAttribute(KB, "" + Math.round(totalPw));
    totalUniqueSummary.setAttribute(READABLE, humanReadableByteCount(Math.round(totalUnique), true));
    totalUniqueSummary.setAttribute(KB, "" + Math.round(totalUnique));
    summary.addContent(totalPW);
    summary.addContent(totalUniqueSummary);

    for (String rule : statistics.keySet()) {
        DescriptiveStatistics stats = statistics.get(rule);
        Element ruleElement = new Element(rule);
        Element max = new Element("max");
        Element median = new Element("median");
        max.addContent("" + stats.getMax());
        median.addContent("" + stats.getPercentile(50));
        ruleElement.addContent(max);
        ruleElement.addContent(median);
        summary.addContent(ruleElement);
    }

    resultsXML.addContent(summary);

    Document doc = new Document(root);
    XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());

    try {
        FileWriter writer = new FileWriter("report-" + df.format(reportDate) + ".xml");
        outputter.output(doc, writer);
    } catch (Exception e) {
    }
}

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

License:Open Source License

protected Element generateEnclosure(Enclosure enclosure) {
    Element enclosureElement = new Element("enclosure", getFeedNamespace());
    if (enclosure.getUrl() != null) {
        enclosureElement.setAttribute("url", enclosure.getUrl());
    }//from   ww w  .ja v  a2 s.  c o  m
    if (enclosure.getLength() != 0) {
        enclosureElement.setAttribute("length", String.valueOf(enclosure.getLength()));
    }
    if (enclosure.getType() != null) {
        enclosureElement.setAttribute("type", enclosure.getType());
    }
    return enclosureElement;
}

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

License:Open Source License

protected Element generateCategoryElement(Category category) {
    Element categoryElement = new Element("category", getFeedNamespace());
    if (category.getDomain() != null) {
        categoryElement.setAttribute("domain", category.getDomain());
    }/*  w  w  w  .  jav  a  2  s  .  c  o  m*/
    categoryElement.addContent(category.getValue());
    return categoryElement;
}

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

License:Open Source License

public void populateItem(Item item, Element eItem, int index) {
    super.populateItem(item, eItem, index);

    Element eDescription = eItem.getChild("description", getFeedNamespace());
    if (eDescription != null)
        eDescription.removeAttribute("type");

    String author = item.getAuthor();
    if (author != null) {
        eItem.addContent(generateSimpleElement("author", author));
    }/*  w  ww.j ava 2 s  .c  om*/

    String comments = item.getComments();
    if (comments != null) {
        eItem.addContent(generateSimpleElement("comments", comments));
    }

    Guid guid = item.getGuid();
    if (guid != null) {
        Element eGuid = generateSimpleElement("guid", guid.getValue());
        if (!guid.isPermaLink()) {
            eGuid.setAttribute("isPermaLink", "false");
        }
        eItem.addContent(eGuid);
    }
}

From source file:com.tactfactory.harmony.generator.androidxml.ManifestUpdater.java

License:Open Source License

@Override
protected Element getDefaultRoot() {
    Namespace androidNs = Namespace.getNamespace("android", "http://schemas.android.com/apk/res/android");
    Element rootElement = new Element("manifest");
    rootElement.addNamespaceDeclaration(androidNs);
    rootElement.setAttribute("package", ApplicationMetadata.INSTANCE.getProjectNameSpace());

    rootElement.setAttribute("versionCode", "1", androidNs);

    rootElement.setAttribute("versionName", "@string/app_version", androidNs);
    return rootElement;
}

From source file:com.tactfactory.harmony.platform.winphone.updater.XmlProjectWinphone.java

License:Open Source License

public void addCompileFile(String filename, String depends) {
    final Element newNode = new Element(XML_ELEMENT_COMPILE);

    newNode.setAttribute(XML_ATTRIBUT_INCLUDE, filename);

    if (depends != null) {
        Element childDepends = new Element(XML_ELEMENT_DEPENDENT);
        childDepends.addContent(depends);
        newNode.addContent(childDepends);
    }//ww w .  ja  v a  2s.c om

    Element itemGroup = this.findFirstItemGroup(XML_ELEMENT_COMPILE);

    this.removeIfExists(itemGroup, XML_ELEMENT_COMPILE, filename);
    itemGroup.addContent(newNode);
}