Example usage for org.jdom2 Element Element

List of usage examples for org.jdom2 Element Element

Introduction

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

Prototype

public Element(final String name, final String uri) 

Source Link

Document

Creates a new element with the supplied (local) name and a namespace given by a URI.

Usage

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  a 2 s .c o  m
        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  w w w  .  j  av a2s .  co 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.modules.opensearch.impl.OpenSearchModuleGenerator.java

License:Apache License

protected Element generateSimpleElement(final String name, final String value) {

    final Element element = new Element(name, OS_NS);
    element.addContent(value);/*  www .j av a  2s  .com*/

    return element;
}

From source file:com.rometools.modules.photocast.io.Generator.java

License:Open Source License

@Override
public void generate(final Module module, final Element element) {
    if (!(module instanceof PhotocastModule)) {
        return;//from w ww  . j  a va 2  s . c o  m
    }
    final PhotocastModule pm = (PhotocastModule) module;
    if (element.getName().equals("channel") || element.getName().equals("feed")) {
        element.addContent(generateSimpleElement("feedVersion", FEED_VERSION));
        return;
    }
    element.addContent(generateSimpleElement("photoDate", Parser.PHOTO_DATE_FORMAT.format(pm.getPhotoDate())));
    element.addContent(generateSimpleElement("cropDate", Parser.CROP_DATE_FORMAT.format(pm.getCropDate())));
    element.addContent(generateSimpleElement("thumbnail", pm.getThumbnailUrl().toString()));
    element.addContent(generateSimpleElement("image", pm.getImageUrl().toString()));
    final Element e = new Element("metadata", NS);
    final Element pd = new Element("PhotoDate", "");
    pd.addContent(pm.getMetadata().getPhotoDate().toString());
    e.addContent(pd);
    final Element com = new Element("Comments", "");
    com.addContent(pm.getMetadata().getComments());
    e.addContent(com);
    element.addContent(e);
}

From source file:com.rometools.modules.slash.io.SlashModuleGenerator.java

License:Open Source License

protected Element generateSimpleElement(final String name, final String value) {
    final Element element = new Element(name, SlashModuleGenerator.NAMESPACE);
    element.addContent(value);/* w  w  w  .ja v a2 s. c  o  m*/
    return element;
}

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  w w .  j  a  v  a 2  s .c o 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());
        }//  w w  w .  java 2  s . co  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;//w  w w .  java 2 s  .co  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.modules.yahooweather.io.WeatherModuleGenerator.java

License:Open Source License

protected Element generateSimpleElement(final String name, final String value) {
    final Element element = new Element(name, WeatherModuleGenerator.NS);
    element.addContent(value);/* w w  w .j a  v a 2 s .c  om*/

    return element;
}

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

License:Apache License

/**
 * Serialize an AtomService object into an XML document
 */// w  w w  . ja  va2  s .c  o m
public Document serviceToDocument() {
    final AtomService service = this;

    final Document doc = new Document();
    final Element root = new Element("service", ATOM_PROTOCOL);
    doc.setRootElement(root);
    final List<Workspace> spaces = service.getWorkspaces();
    for (final Workspace space : spaces) {
        root.addContent(space.workspaceToElement());
    }
    return doc;
}