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.modules.base.io.CustomTagGenerator.java

License:Open Source License

@Override
public void generate(final Module module, final Element element) {
    if (!(module instanceof CustomTags)) {
        return;/*from ww w. j a  va  2 s . c  o m*/
    }

    final List<CustomTag> tags = ((CustomTags) module).getValues();
    final Iterator<CustomTag> it = tags.iterator();

    while (it.hasNext()) {
        final CustomTag tag = it.next();

        if (tag.getValue() instanceof DateTimeRange) {
            final DateTimeRange dtr = (DateTimeRange) tag.getValue();
            final Element newTag = new Element(tag.getName(), CustomTagParser.NS);
            newTag.setAttribute("type", "dateTimeRange");
            newTag.addContent(
                    generateSimpleElement("start", GoogleBaseParser.LONG_DT_FMT.format(dtr.getStart())));
            newTag.addContent(generateSimpleElement("end", GoogleBaseParser.LONG_DT_FMT.format(dtr.getEnd())));
            element.addContent(newTag);
        } else if (tag.getValue() instanceof ShortDate) {
            final ShortDate sd = (ShortDate) tag.getValue();
            final Element newTag = generateSimpleElement(tag.getName(),
                    GoogleBaseParser.SHORT_DT_FMT.format(sd));
            newTag.setAttribute("type", "date");
            element.addContent(newTag);
        } else if (tag.getValue() instanceof Date) {
            final Date d = (Date) tag.getValue();
            final Element newTag = generateSimpleElement(tag.getName(),
                    GoogleBaseParser.SHORT_DT_FMT.format(d));
            newTag.setAttribute("type", "dateTime");
            element.addContent(newTag);
        } else if (tag.getValue() instanceof Integer) {
            final Element newTag = generateSimpleElement(tag.getName(), tag.getValue().toString());
            newTag.setAttribute("type", "int");
            element.addContent(newTag);
        } else if (tag.getValue() instanceof IntUnit) {
            final Element newTag = generateSimpleElement(tag.getName(), tag.getValue().toString());
            newTag.setAttribute("type", "intUnit");
            element.addContent(newTag);
        } else if (tag.getValue() instanceof Float) {
            final Element newTag = generateSimpleElement(tag.getName(), tag.getValue().toString());
            newTag.setAttribute("type", "float");
            element.addContent(newTag);
        } else if (tag.getValue() instanceof FloatUnit) {
            final Element newTag = generateSimpleElement(tag.getName(), tag.getValue().toString());
            newTag.setAttribute("type", "floatUnit");
            element.addContent(newTag);
        } else if (tag.getValue() instanceof String) {
            final Element newTag = generateSimpleElement(tag.getName(), tag.getValue().toString());
            newTag.setAttribute("type", "string");
            element.addContent(newTag);
        } else if (tag.getValue() instanceof URL) {
            final Element newTag = generateSimpleElement(tag.getName(), tag.getValue().toString());
            newTag.setAttribute("type", "url");
            element.addContent(newTag);
        } else if (tag.getValue() instanceof Boolean) {
            final Element newTag = generateSimpleElement(tag.getName(), tag.getValue().toString());
            newTag.setAttribute("type", "boolean");
            element.addContent(newTag);
        } else if (tag.getValue() instanceof CustomTagImpl.Location) {
            final Element newTag = generateSimpleElement(tag.getName(), tag.getValue().toString());
            newTag.setAttribute("type", "location");
            element.addContent(newTag);
        }
    }
}

From source file:com.rometools.modules.itunes.io.ITunesGenerator.java

License:Open Source License

@Override
public void generate(final Module module, final Element element) {
    Element root = element;//from w  ww  . j  a v a2 s. c  o m

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

    root.addNamespaceDeclaration(NAMESPACE);

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

    final AbstractITunesObject itunes = (AbstractITunesObject) module;

    if (itunes instanceof FeedInformationImpl) {
        // Do Channel Specific Stuff.
        final FeedInformationImpl info = (FeedInformationImpl) itunes;
        final Element owner = generateSimpleElement("owner", "");
        final Element email = generateSimpleElement("email", info.getOwnerEmailAddress());
        owner.addContent(email);

        final Element name = generateSimpleElement("name", info.getOwnerName());
        owner.addContent(name);
        element.addContent(owner);

        if (info.getImage() != null) {
            final Element image = generateSimpleElement("image", "");
            image.setAttribute("href", info.getImage().toExternalForm());
            element.addContent(image);
        }

        final List<Category> categories = info.getCategories();
        for (final Category cat : categories) {

            final Element category = generateSimpleElement("category", "");
            category.setAttribute("text", cat.getName());

            if (cat.getSubcategory() != null) {
                final Element subcat = generateSimpleElement("category", "");
                subcat.setAttribute("text", cat.getSubcategory().getName());
                category.addContent(subcat);
            }

            element.addContent(category);
        }
    } else if (itunes instanceof EntryInformationImpl) {
        final EntryInformationImpl info = (EntryInformationImpl) itunes;

        if (info.getDuration() != null) {
            element.addContent(generateSimpleElement("duration", info.getDuration().toString()));
        }
    }

    if (itunes.getAuthor() != null) {
        element.addContent(generateSimpleElement("author", itunes.getAuthor()));
    }

    if (itunes.getBlock()) {
        element.addContent(generateSimpleElement("block", ""));
    }

    if (itunes.getExplicit()) {
        element.addContent(generateSimpleElement("explicit", "yes"));
    } else {
        element.addContent(generateSimpleElement("explicit", "no"));
    }

    if (itunes.getKeywords() != null) {
        final StringBuffer sb = new StringBuffer();

        for (int i = 0; i < itunes.getKeywords().length; i++) {
            if (i != 0) {
                sb.append(", ");
            }

            sb.append(itunes.getKeywords()[i]);
        }

        element.addContent(generateSimpleElement("keywords", sb.toString()));
    }

    if (itunes.getSubtitle() != null) {
        element.addContent(generateSimpleElement("subtitle", itunes.getSubtitle()));
    }

    if (itunes.getSummary() != null) {
        element.addContent(generateSimpleElement("summary", itunes.getSummary()));
    }
}

From source file:com.rometools.modules.mediarss.io.MediaModuleGenerator.java

License:Open Source License

protected void addNotNullAttribute(final Element target, final String name, final Object value) {
    if (target == null || value == null) {
        return;//from  w ww. j a va2 s . c  o m
    } else {
        target.setAttribute(name, value.toString());
    }
}

From source file:com.rometools.modules.sle.io.ModuleParser.java

License:Apache License

public void insertValues(final SimpleListExtension sle, final List<Element> elements) {
    for (int i = 0; elements != null && i < elements.size(); i++) {
        final Element e = elements.get(i);
        final Group[] groups = sle.getGroupFields();

        for (final Group group2 : groups) {
            final Element value = e.getChild(group2.getElement(), group2.getNamespace());

            if (value == null) {
                continue;
            }/*from  w  ww.  j a va 2  s . co m*/

            final Element group = new Element("group", TEMP);
            addNotNullAttribute(group, "element", group2.getElement());
            addNotNullAttribute(group, "label", group2.getLabel());
            addNotNullAttribute(group, "value", value.getText());
            addNotNullAttribute(group, "ns", group2.getNamespace().getURI());

            e.addContent(group);
        }

        final Sort[] sorts = sle.getSortFields();

        for (final Sort sort2 : sorts) {
            LOG.debug("Inserting for {} {}", sort2.getElement(), sort2.getDataType());
            final Element sort = new Element("sort", TEMP);
            // this is the default sort order, so I am just going to ignore
            // the actual values and add a number type. It really shouldn't
            // work this way. I should be checking to see if any of the elements
            // defined have a value then use that value. This will preserve the
            // sort order, however, if anyone is using the SleEntry to display
            // the value of the field, it will not give the correct value.
            // This, however, would require knowledge in the item parser that I don't
            // have right now.
            if (sort2.getDefaultOrder()) {
                sort.setAttribute("label", sort2.getLabel());
                sort.setAttribute("value", Integer.toString(i));
                sort.setAttribute("data-type", Sort.NUMBER_TYPE);
                e.addContent(sort);

                continue;
            }
            // LOG.debug(e.getName());
            final Element value = e.getChild(sort2.getElement(), sort2.getNamespace());
            if (value == null) {
                LOG.debug("No value for {} : {}", sort2.getElement(), sort2.getNamespace());
            } else {
                LOG.debug("{} value: {}", sort2.getElement(), value.getText());
            }
            if (value == null) {
                continue;
            }

            addNotNullAttribute(sort, "label", sort2.getLabel());
            addNotNullAttribute(sort, "element", sort2.getElement());
            addNotNullAttribute(sort, "value", value.getText());
            addNotNullAttribute(sort, "data-type", sort2.getDataType());
            addNotNullAttribute(sort, "ns", sort2.getNamespace().getURI());
            e.addContent(sort);
            LOG.debug("Added {} {} = {}", sort, sort2.getLabel(), value.getText());
        }
    }
}

From source file:com.rometools.modules.thr.io.ThreadingModuleGenerator.java

License:Apache License

@Override
public void generate(Module module, Element element) {
    if (module != null && module instanceof ThreadingModule) {
        ThreadingModule threadedModule = (ThreadingModule) module;
        Element inReplyTo = new Element("in-reply-to", NAMESPACE);

        if (threadedModule.getHref() != null) {
            inReplyTo.setAttribute("href", threadedModule.getHref());
        }/* ww  w  .  j ava 2s. c o  m*/
        if (threadedModule.getRef() != null) {
            inReplyTo.setAttribute("ref", threadedModule.getRef());
        }
        if (threadedModule.getType() != null) {
            inReplyTo.setAttribute("type", threadedModule.getType());
        }
        if (threadedModule.getSource() != null) {
            inReplyTo.setAttribute("source", threadedModule.getSource());
        }

        element.addContent(inReplyTo);
    }
}

From source file:com.rometools.modules.yahooweather.io.WeatherModuleGenerator.java

License:Open Source License

@Override
public void generate(final Module module, final Element element) {
    if (!(module instanceof YWeatherModuleImpl)) {
        return;/*from   w w w .ja va 2  s  . c  o  m*/
    }

    final YWeatherModuleImpl weather = (YWeatherModuleImpl) module;

    if (weather.getAstronomy() != null) {
        final Element astro = new Element("astronomy", WeatherModuleGenerator.NS);

        if (weather.getAstronomy().getSunrise() != null) {
            astro.setAttribute("sunrise", TIME_ONLY.format(weather.getAstronomy().getSunrise()).toLowerCase());
        }

        if (weather.getAstronomy().getSunrise() != null) {
            astro.setAttribute("sunset", TIME_ONLY.format(weather.getAstronomy().getSunset()).toLowerCase());
        }

        element.addContent(astro);
    }

    if (weather.getAtmosphere() != null) {
        final Element atmos = new Element("atmosphere", WeatherModuleGenerator.NS);
        atmos.setAttribute("humidity", Integer.toString(weather.getAtmosphere().getHumidity()));
        atmos.setAttribute("visibility",
                Integer.toString((int) (weather.getAtmosphere().getVisibility() * 100d)));
        atmos.setAttribute("pressure", Double.toString(weather.getAtmosphere().getPressure()));

        if (weather.getAtmosphere().getChange() != null) {
            atmos.setAttribute("rising", Integer.toString(weather.getAtmosphere().getChange().getCode()));
        }

        element.addContent(atmos);
    }

    if (weather.getCondition() != null) {
        final Element condition = new Element("condition", WeatherModuleGenerator.NS);

        if (weather.getCondition().getText() != null) {
            condition.setAttribute("text", weather.getCondition().getText());
        }

        if (weather.getCondition().getCode() != null) {
            condition.setAttribute("code", Integer.toString(weather.getCondition().getCode().getCode()));
        }

        if (weather.getCondition().getDate() != null) {
            condition.setAttribute("date", LONG_DATE.format(weather.getCondition().getDate())
                    .replaceAll("AM", "am").replaceAll("PM", "pm"));
        }

        condition.setAttribute("temp", Integer.toString(weather.getCondition().getTemperature()));
        element.addContent(condition);
    }

    if (weather.getLocation() != null) {
        final Element location = new Element("location", WeatherModuleGenerator.NS);

        if (weather.getLocation().getCity() != null) {
            location.setAttribute("city", weather.getLocation().getCity());
        }

        if (weather.getLocation().getRegion() != null) {
            location.setAttribute("region", weather.getLocation().getRegion());
        }

        if (weather.getLocation().getCountry() != null) {
            location.setAttribute("country", weather.getLocation().getCountry());
        }

        element.addContent(location);
    }

    if (weather.getUnits() != null) {
        final Element units = new Element("units", WeatherModuleGenerator.NS);

        if (weather.getUnits().getDistance() != null) {
            units.setAttribute("distance", weather.getUnits().getDistance());
        }

        if (weather.getUnits().getPressure() != null) {
            units.setAttribute("pressure", weather.getUnits().getPressure());
        }

        if (weather.getUnits().getSpeed() != null) {
            units.setAttribute("speed", weather.getUnits().getSpeed());
        }

        if (weather.getUnits().getTemperature() != null) {
            units.setAttribute("temperature", weather.getUnits().getTemperature());
        }

        element.addContent(units);
    }

    if (weather.getWind() != null) {
        final Element wind = new Element("wind", WeatherModuleGenerator.NS);
        wind.setAttribute("chill", Integer.toString(weather.getWind().getChill()));
        wind.setAttribute("direction", Integer.toString(weather.getWind().getDirection()));
        wind.setAttribute("speed", Integer.toString(weather.getWind().getSpeed()));
        element.addContent(wind);
    }

    if (weather.getForecasts() != null) {
        for (int i = 0; i < weather.getForecasts().length; i++) {
            final Element forecast = new Element("forecast", WeatherModuleGenerator.NS);
            final Forecast f = weather.getForecasts()[i];

            if (f.getCode() != null) {
                forecast.setAttribute("code", Integer.toString(f.getCode().getCode()));
            }

            if (f.getDate() != null) {
                forecast.setAttribute("date", SHORT_DATE.format(f.getDate()));
            }

            if (f.getDay() != null) {
                forecast.setAttribute("day", f.getDay());
            }

            if (f.getText() != null) {
                forecast.setAttribute("text", f.getText());
            }

            forecast.setAttribute("high", Integer.toString(f.getHigh()));
            forecast.setAttribute("low", Integer.toString(f.getLow()));
            element.addContent(forecast);
        }
    }
}

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

License:Apache License

/**
 * Creates an XML document (JDOM) for the given feed bean.
 *
 * @param feed the feed bean to generate the XML document from.
 * @return the generated XML document (JDOM).
 * @throws IllegalArgumentException thrown if the type of the given feed bean does not match with the type of the
 *             WireFeedGenerator.//  w ww .j  a  v  a 2  s.  co  m
 * @throws FeedException thrown if the XML Document could not be created.
 */
@Override
public Document generate(final WireFeed feed) throws IllegalArgumentException, FeedException {

    if (!(feed instanceof Opml)) {
        throw new IllegalArgumentException("Not an OPML file");
    }

    final Opml opml = (Opml) feed;
    final Document doc = new Document();
    final Element root = new Element("opml");
    root.setAttribute("version", "1.0");
    doc.addContent(root);

    final Element head = generateHead(opml);

    if (head != null) {
        root.addContent(head);
    }

    final Element body = new Element("body");
    root.addContent(body);
    super.generateFeedModules(opml.getModules(), root);
    body.addContent(generateOutlines(opml.getOutlines()));

    return doc;
}

From source file:com.rometools.opml.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;
    }/*from w w  w  .  j av a 2s. c o  m*/
    target.setAttribute(name, value.toString());
    return true;
}

From source file:com.rometools.opml.io.impl.OPML20Generator.java

License:Apache License

@Override
protected Element generateOutline(final Outline outline) {

    final Element outlineElement = super.generateOutline(outline);

    if (outline.getCreated() != null) {
        outlineElement.setAttribute("created", DateParser.formatRFC822(outline.getCreated(), Locale.US));
    }/*from w w  w . ja v  a2  s  .c  om*/

    final List<String> categories = outline.getCategories();
    final String categoryValue = generateCategoryValue(categories);
    addNotNullAttribute(outlineElement, "category", categoryValue);

    return outlineElement;

}

From source file:com.rometools.propono.atom.common.Collection.java

License:Apache License

/**
 * Serialize an AtomService.Collection into an XML element
 *//*from www  .j  a  v  a  2s. c o m*/
public Element collectionToElement() {
    final Collection collection = this;
    final Element element = new Element("collection", AtomService.ATOM_PROTOCOL);
    element.setAttribute("href", collection.getHref());

    final Element titleElem = new Element("title", AtomService.ATOM_FORMAT);
    titleElem.setText(collection.getTitle());
    if (collection.getTitleType() != null && !collection.getTitleType().equals("TEXT")) {
        titleElem.setAttribute("type", collection.getTitleType(), AtomService.ATOM_FORMAT);
    }
    element.addContent(titleElem);

    // Loop to create <app:categories> elements
    for (final Object element2 : collection.getCategories()) {
        final Categories cats = (Categories) element2;
        element.addContent(cats.categoriesToElement());
    }

    for (final Object element2 : collection.getAccepts()) {
        final String range = (String) element2;
        final Element acceptElem = new Element("accept", AtomService.ATOM_PROTOCOL);
        acceptElem.setText(range);
        element.addContent(acceptElem);
    }

    return element;
}