Example usage for org.jdom2 Element getChild

List of usage examples for org.jdom2 Element getChild

Introduction

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

Prototype

public Element getChild(final String cname, final Namespace ns) 

Source Link

Document

This returns the first child element within this element with the given local name and belonging to the given namespace.

Usage

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

License:Apache License

/**
 * Parses the XML node (JDOM element) extracting module information.
 * <p>/*from   w  w  w.jav  a  2s . com*/
 *
 * @param element the XML node (JDOM element) to extract module information from.
 * @return a module instance, <b>null</b> if the element did not have module information.
 */
@Override
public Module parse(final Element element, final Locale locale) {
    if (element.getChild("treatAs", NS) == null) {
        return null;
    }

    final SimpleListExtension sle = new SimpleListExtensionImpl();
    sle.setTreatAs(element.getChildText("treatAs", NS));

    final Element listInfo = element.getChild("listinfo", NS);
    ArrayList<Object> values = new ArrayList<Object>();
    for (final Element ge : listInfo.getChildren("group", NS)) {
        final Namespace ns = ge.getAttribute("ns") == null ? element.getNamespace()
                : Namespace.getNamespace(ge.getAttributeValue("ns"));
        final String elementName = ge.getAttributeValue("element");
        final String label = ge.getAttributeValue("label");
        values.add(new Group(ns, elementName, label));
    }

    sle.setGroupFields(values.toArray(new Group[values.size()]));
    values = values.size() == 0 ? values : new ArrayList<Object>();

    for (final Element se : listInfo.getChildren("sort", NS)) {
        LOG.debug("Parse cf:sort {}{}", se.getAttributeValue("element"), se.getAttributeValue("data-type"));
        final Namespace ns = se.getAttributeValue("ns") == null ? element.getNamespace()
                : Namespace.getNamespace(se.getAttributeValue("ns"));
        final String elementName = se.getAttributeValue("element");
        final String label = se.getAttributeValue("label");
        final String dataType = se.getAttributeValue("data-type");
        final boolean defaultOrder = se.getAttributeValue("default") == null ? false
                : new Boolean(se.getAttributeValue("default")).booleanValue();
        values.add(new Sort(ns, elementName, dataType, label, defaultOrder));
    }

    sle.setSortFields(values.toArray(new Sort[values.size()]));
    insertValues(sle, element.getChildren());

    return sle;
}

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   ww w .  j  a v  a2s. 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.ThreadingModuleParser.java

License:Apache License

@Override
public Module parse(final Element element, final Locale locale) {
    final ThreadingModule tm = new ThreadingModuleImpl();
    Element inReplyTo = element.getChild("in-reply-to", ThreadingModuleParser.NS);

    if (inReplyTo != null) {
        tm.setHref(inReplyTo.getAttributeValue("href"));
        tm.setRef(inReplyTo.getAttributeValue("ref"));
        tm.setSource(inReplyTo.getAttributeValue("source"));
        tm.setType(inReplyTo.getAttributeValue("type"));
        return tm;
    }//from   w w  w. ja va 2s. co m

    return null;
}

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

License:Open Source License

@Override
public Module parse(final Element element, final Locale locale) {
    final YWeatherModuleImpl module = new YWeatherModuleImpl();
    final Element location = element.getChild("location", WeatherModuleParser.NS);

    if (location != null) {
        final Location l = new Location(location.getAttributeValue("city"),
                location.getAttributeValue("region"), location.getAttributeValue("country"));
        module.setLocation(l);/*from w w w  .j  a  v  a  2  s .co  m*/
    }

    final Element units = element.getChild("units", WeatherModuleParser.NS);

    if (units != null) {
        final Units u = new Units(units.getAttributeValue("temperature"), units.getAttributeValue("distance"),
                units.getAttributeValue("pressure"), units.getAttributeValue("speed"));
        module.setUnits(u);
    }

    final Element wind = element.getChild("wind", WeatherModuleParser.NS);

    if (wind != null) {
        try {
            final Wind w = new Wind(Integer.parseInt(wind.getAttributeValue("chill")),
                    Integer.parseInt(wind.getAttributeValue("direction")),
                    Integer.parseInt(wind.getAttributeValue("speed")));
            module.setWind(w);
        } catch (final NumberFormatException nfe) {
            LOG.warn("NumberFormatException processing <wind> tag.", nfe);
        }
    }

    final Element atmosphere = element.getChild("atmosphere", WeatherModuleParser.NS);

    if (atmosphere != null) {
        try {
            final Atmosphere a = new Atmosphere(Integer.parseInt(atmosphere.getAttributeValue("humidity")),
                    Double.parseDouble(atmosphere.getAttributeValue("visibility")) / 100,
                    Double.parseDouble(atmosphere.getAttributeValue("pressure")), Atmosphere.PressureChange
                            .fromCode(Integer.parseInt(atmosphere.getAttributeValue("rising"))));
            module.setAtmosphere(a);
        } catch (final NumberFormatException nfe) {
            LOG.warn("NumberFormatException processing <atmosphere> tag.", nfe);
        }
    }

    final Element astronomy = element.getChild("astronomy", WeatherModuleParser.NS);

    if (astronomy != null) {
        try {
            final SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a", locale);
            final Astronomy a = new Astronomy(
                    timeFormat.parse(astronomy.getAttributeValue("sunrise").replaceAll("am", "AM")
                            .replaceAll("pm", "PM")),
                    timeFormat.parse(astronomy.getAttributeValue("sunset").replaceAll("am", "AM")
                            .replaceAll("pm", "PM")));
            module.setAstronomy(a);
        } catch (final ParseException pe) {
            LOG.warn("ParseException processing <astronomy> tag.", pe);
        }
    }

    final Element condition = element.getChild("condition", WeatherModuleParser.NS);

    if (condition != null) {
        try {
            final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy h:mm a zzz", locale);
            final Condition c = new Condition(condition.getAttributeValue("text"),
                    ConditionCode.fromCode(Integer.parseInt(condition.getAttributeValue("code"))),
                    Integer.parseInt(condition.getAttributeValue("temp")), dateFormat.parse(
                            condition.getAttributeValue("date").replaceAll("pm", "PM").replaceAll("am", "AM")));
            module.setCondition(c);
        } catch (final NumberFormatException nfe) {
            LOG.warn("NumberFormatException processing <condition> tag.", nfe);
        } catch (final ParseException pe) {
            LOG.warn("ParseException processing <condition> tag.", pe);
        }
    }

    final List<Element> forecasts = element.getChildren("forecast", WeatherModuleParser.NS);

    if (forecasts != null) {
        final Forecast[] f = new Forecast[forecasts.size()];
        int i = 0;

        final SimpleDateFormat dateFormat = new SimpleDateFormat("d MMM yyyy", locale);
        for (final Iterator<Element> it = forecasts.iterator(); it.hasNext(); i++) {
            final Element forecast = it.next();

            try {
                f[i] = new Forecast(forecast.getAttributeValue("day"),
                        dateFormat.parse(forecast.getAttributeValue("date")),
                        Integer.parseInt(forecast.getAttributeValue("low")),
                        Integer.parseInt(forecast.getAttributeValue("high")),
                        forecast.getAttributeValue("text"),
                        ConditionCode.fromCode(Integer.parseInt(forecast.getAttributeValue("code"))));
            } catch (final NumberFormatException nfe) {
                LOG.warn("NumberFormatException processing <forecast> tag.", nfe);
            } catch (final ParseException pe) {
                LOG.warn("ParseException processing <forecast> tag.", pe);
            }
        }

        module.setForecasts(f);
    }

    return module;
}

From source file:com.rometools.propono.atom.client.ClientCollection.java

License:Open Source License

@Override
protected void parseCollectionElement(final Element element) throws ProponoException {
    if (workspace == null) {
        return;//  w  w  w  .j  a  va2  s.co  m
    }

    setHref(element.getAttribute("href").getValue());

    final Element titleElem = element.getChild("title", AtomService.ATOM_FORMAT);
    if (titleElem != null) {
        setTitle(titleElem.getText());
        if (titleElem.getAttribute("type", AtomService.ATOM_FORMAT) != null) {
            setTitleType(titleElem.getAttribute("type", AtomService.ATOM_FORMAT).getValue());
        }
    }

    final List<Element> acceptElems = element.getChildren("accept", AtomService.ATOM_PROTOCOL);
    if (acceptElems != null && !acceptElems.isEmpty()) {
        for (final Element acceptElem : acceptElems) {
            addAccept(acceptElem.getTextTrim());
        }
    }

    // Loop to parse <app:categories> element to Categories objects
    final List<Element> catsElems = element.getChildren("categories", AtomService.ATOM_PROTOCOL);
    for (final Element catsElem : catsElems) {
        final Categories cats = new ClientCategories(catsElem, this);
        addCategories(cats);
    }
}

From source file:com.rometools.propono.atom.client.ClientWorkspace.java

License:Open Source License

/** Deserialize a Atom workspace XML element into an object */
protected void parseWorkspaceElement(final Element element, final String baseURI) throws ProponoException {
    final Element titleElem = element.getChild("title", AtomService.ATOM_FORMAT);
    setTitle(titleElem.getText());//ww w.j  av a  2 s .  c om
    if (titleElem.getAttribute("type", AtomService.ATOM_FORMAT) != null) {
        setTitleType(titleElem.getAttribute("type", AtomService.ATOM_FORMAT).getValue());
    }
    final List<Element> collections = element.getChildren("collection", AtomService.ATOM_PROTOCOL);
    for (final Element e : collections) {
        addCollection(new ClientCollection(e, this, baseURI));
    }
}

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

License:Apache License

protected void parseCollectionElement(final Element element) throws ProponoException {
    setHref(element.getAttribute("href").getValue());

    final Element titleElem = element.getChild("title", AtomService.ATOM_FORMAT);
    if (titleElem != null) {
        setTitle(titleElem.getText());//www . j a va2s  .c  om
        if (titleElem.getAttribute("type", AtomService.ATOM_FORMAT) != null) {
            setTitleType(titleElem.getAttribute("type", AtomService.ATOM_FORMAT).getValue());
        }
    }

    final List<Element> acceptElems = element.getChildren("accept", AtomService.ATOM_PROTOCOL);
    if (acceptElems != null && !acceptElems.isEmpty()) {
        for (final Element acceptElem : acceptElems) {
            addAccept(acceptElem.getTextTrim());
        }
    }

    // Loop to parse <app:categories> element to Categories objects
    final List<Element> catsElems = element.getChildren("categories", AtomService.ATOM_PROTOCOL);
    for (final Element catsElem : catsElems) {
        final Categories cats = new Categories(catsElem, baseURI);
        addCategories(cats);
    }
}

From source file:com.rometools.propono.atom.common.rome.AppModuleParser.java

License:Apache License

/** Parse JDOM element into module */
@Override/*from www . ja v a 2s  . com*/
public Module parse(final Element elem, final Locale locale) {
    final AppModule m = new AppModuleImpl();
    final Element control = elem.getChild("control", getContentNamespace());
    if (control != null) {
        final Element draftElem = control.getChild("draft", getContentNamespace());
        if (draftElem != null) {
            if ("yes".equals(draftElem.getText())) {
                m.setDraft(Boolean.TRUE);
            }
            if ("no".equals(draftElem.getText())) {
                m.setDraft(Boolean.FALSE);
            }
        }
    }
    final Element edited = elem.getChild("edited", getContentNamespace());
    if (edited != null) {
        try {
            m.setEdited(DateParser.parseW3CDateTime(edited.getTextTrim(), locale));
        } catch (final Exception ignored) {
        }
    }
    return m;
}

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

License:Apache License

/** Deserialize a Atom workspace XML element into an object */
protected void parseWorkspaceElement(final Element element) throws ProponoException {
    final Element titleElem = element.getChild("title", AtomService.ATOM_FORMAT);
    setTitle(titleElem.getText());//  w  w w  . ja v  a  2 s . co m
    if (titleElem.getAttribute("type", AtomService.ATOM_FORMAT) != null) {
        setTitleType(titleElem.getAttribute("type", AtomService.ATOM_FORMAT).getValue());
    }
    final List<Element> collections = element.getChildren("collection", AtomService.ATOM_PROTOCOL);
    for (final Element e : collections) {
        addCollection(new Collection(e));
    }
}

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

License:Open Source License

protected WireFeed parseFeed(final Element eFeed, final Locale locale) {

    final String type = getType();
    final Document document = eFeed.getDocument();
    final String styleSheet = getStyleSheet(document);

    final Feed feed = new Feed(type);
    feed.setStyleSheet(styleSheet);//from  w  ww.  j  a  va  2 s  .  c o  m

    final Element title = eFeed.getChild("title", getAtomNamespace());
    if (title != null) {
        feed.setTitleEx(parseContent(title));
    }

    final List<Element> links = eFeed.getChildren("link", getAtomNamespace());
    feed.setAlternateLinks(parseAlternateLinks(links));
    feed.setOtherLinks(parseOtherLinks(links));

    final Element author = eFeed.getChild("author", getAtomNamespace());
    if (author != null) {
        final List<SyndPerson> authors = new ArrayList<SyndPerson>();
        authors.add(parsePerson(author));
        feed.setAuthors(authors);
    }

    final List<Element> contributors = eFeed.getChildren("contributor", getAtomNamespace());
    if (!contributors.isEmpty()) {
        feed.setContributors(parsePersons(contributors));
    }

    final Element tagline = eFeed.getChild("tagline", getAtomNamespace());
    if (tagline != null) {
        feed.setTagline(parseContent(tagline));
    }

    final Element id = eFeed.getChild("id", getAtomNamespace());
    if (id != null) {
        feed.setId(id.getText());
    }

    final Element generator = eFeed.getChild("generator", getAtomNamespace());
    if (generator != null) {
        final Generator gen = new Generator();
        gen.setValue(generator.getText());
        String att = getAttributeValue(generator, "url");
        if (att != null) {
            gen.setUrl(att);
        }
        att = getAttributeValue(generator, "version");
        if (att != null) {
            gen.setVersion(att);
        }
        feed.setGenerator(gen);
    }

    final Element copyright = eFeed.getChild("copyright", getAtomNamespace());
    if (copyright != null) {
        feed.setCopyright(copyright.getText());
    }

    final Element info = eFeed.getChild("info", getAtomNamespace());
    if (info != null) {
        feed.setInfo(parseContent(info));
    }

    final Element modified = eFeed.getChild("modified", getAtomNamespace());
    if (modified != null) {
        feed.setModified(DateParser.parseDate(modified.getText(), locale));
    }

    feed.setModules(parseFeedModules(eFeed, locale));

    final List<Element> entries = eFeed.getChildren("entry", getAtomNamespace());
    if (!entries.isEmpty()) {
        feed.setEntries(parseEntries(entries, locale));
    }

    final List<Element> foreignMarkup = extractForeignMarkup(eFeed, feed, getAtomNamespace());
    if (!foreignMarkup.isEmpty()) {
        feed.setForeignMarkup(foreignMarkup);
    }

    return feed;

}