Example usage for org.jdom2 Element getChildren

List of usage examples for org.jdom2 Element getChildren

Introduction

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

Prototype

public List<Element> getChildren(final String cname, final Namespace ns) 

Source Link

Document

This returns a List of all the child elements nested directly (one level deep) within this element with the given local name and belonging to the given Namespace, returned as Element objects.

Usage

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

License:Apache License

/** Use feed links and/or xml:base attribute to determine baseURI of feed */
private static URL findBaseURI(final Element root) {
    URL baseURI = null;/*from ww w.j a v  a  2  s .  c  o  m*/
    final List<Element> linksList = root.getChildren("link", OS_NS);
    if (linksList != null) {
        for (final Element element : linksList) {
            final Element link = element;
            if (!root.equals(link.getParent())) {
                break;
            }
            String href = link.getAttribute("href").getValue();
            if (link.getAttribute("rel", OS_NS) == null
                    || link.getAttribute("rel", OS_NS).getValue().equals("alternate")) {
                href = resolveURI(null, link, href);
                try {
                    baseURI = new URL(href);
                    break;
                } catch (final MalformedURLException e) {
                    System.err.println("Base URI is malformed: " + href);
                }
            }
        }
    }
    return baseURI;
}

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

License:Apache License

/**
 * Parses the XML node (JDOM element) extracting module information.
 * <p>/*ww  w .j a  v  a2s  .  c om*/
 *
 * @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) {
    final SleEntryImpl sle = new SleEntryImpl();
    ArrayList<EntryValue> values = new ArrayList<EntryValue>();
    final List<Element> groups = element.getChildren("group", ModuleParser.TEMP);

    for (final Element group : groups) {
        final StringValue value = new StringValue();
        value.setElement(group.getAttributeValue("element"));
        value.setLabel(group.getAttributeValue("label"));
        value.setValue(group.getAttributeValue("value"));
        if (group.getAttributeValue("ns") != null) {
            value.setNamespace(Namespace.getNamespace(group.getAttributeValue("ns")));
        } else {
            value.setNamespace(element.getDocument().getRootElement().getNamespace());
        }
        values.add(value);
        element.removeContent(group);
    }

    sle.setGroupValues(values.toArray(new EntryValue[values.size()]));
    values = values.size() == 0 ? values : new ArrayList<EntryValue>();

    final List<Element> sorts = new ArrayList<Element>(element.getChildren("sort", ModuleParser.TEMP));
    // LOG.debug("]]] sorts on element"+sorts.size());
    for (final Element sort : sorts) {
        final String dataType = sort.getAttributeValue("data-type");
        // LOG.debug("Doing datatype "+dataType +" :: "+sorts.size());
        if (dataType == null || dataType.equals(Sort.TEXT_TYPE)) {
            final StringValue value = new StringValue();
            value.setElement(sort.getAttributeValue("element"));
            value.setLabel(sort.getAttributeValue("label"));
            value.setValue(sort.getAttributeValue("value"));
            if (sort.getAttributeValue("ns") != null) {
                value.setNamespace(Namespace.getNamespace(sort.getAttributeValue("ns")));
            } else {
                value.setNamespace(element.getDocument().getRootElement().getNamespace());
            }
            values.add(value);

            element.removeContent(sort);

        } else if (dataType.equals(Sort.DATE_TYPE)) {
            final DateValue value = new DateValue();
            value.setElement(sort.getAttributeValue("element"));
            value.setLabel(sort.getAttributeValue("label"));
            if (sort.getAttributeValue("ns") != null) {
                value.setNamespace(Namespace.getNamespace(sort.getAttributeValue("ns")));
            } else {
                value.setNamespace(element.getDocument().getRootElement().getNamespace());
            }
            Date dateValue = null;

            try {
                dateValue = DateParser.parseRFC822(sort.getAttributeValue("value"), locale);
                if (dateValue == null) {
                    dateValue = DateParser.parseW3CDateTime(sort.getAttributeValue("value"), locale);
                }
            } catch (final Exception e) {
                ; // ignore parse exceptions
            }

            value.setValue(dateValue);
            values.add(value);
            element.removeContent(sort);
        } else if (dataType.equals(Sort.NUMBER_TYPE)) {
            final NumberValue value = new NumberValue();
            value.setElement(sort.getAttributeValue("element"));
            value.setLabel(sort.getAttributeValue("label"));
            if (sort.getAttributeValue("ns") != null) {
                value.setNamespace(Namespace.getNamespace(sort.getAttributeValue("ns")));
            } else {
                value.setNamespace(element.getDocument().getRootElement().getNamespace());
            }

            try {
                value.setValue(new BigDecimal(sort.getAttributeValue("value")));
            } catch (final NumberFormatException nfe) {
                ; // ignore
                values.add(value);
                element.removeContent(sort);
            }
        } else {
            throw new RuntimeException("Unknown datatype");
        }
    }
    // LOG.debug("Values created "+values.size()+" from sorts" +sorts.size());
    sle.setSortValues(values.toArray(new EntryValue[values.size()]));

    return sle;
}

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

License:Apache License

/**
 * Parses the XML node (JDOM element) extracting module information.
 * <p>//w w  w .ja  v  a  2  s  . 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.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 ww.  j a  va2s  .  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.ClientAtomService.java

License:Open Source License

/** Deserialize an Atom service XML document into an object */
private void parseAtomServiceDocument(final Document document) throws ProponoException {
    final Element root = document.getRootElement();
    final List<Element> spaces = root.getChildren("workspace", AtomService.ATOM_PROTOCOL);
    for (final Element e : spaces) {
        addWorkspace(new ClientWorkspace(e, this, uri));
    }/*from   w w w .ja  va2  s.co  m*/
}

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;/*from w ww.  j a  v  a 2  s . com*/
    }

    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());/*from   w w w  . ja  v  a  2 s. c  o  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 ClientCollection(e, this, baseURI));
    }
}

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

License:Apache License

/**
 * Deserialize an Atom service XML document into an object
 *//*from  ww  w . j av  a2  s.  c  o m*/
public static AtomService documentToService(final Document document) throws ProponoException {
    final AtomService service = new AtomService();
    final Element root = document.getRootElement();
    final List<Element> spaces = root.getChildren("workspace", ATOM_PROTOCOL);
    for (final Element e : spaces) {
        service.addWorkspace(Workspace.elementToWorkspace(e));
    }
    return service;
}

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

License:Apache License

protected void parseCategoriesElement(final Element catsElem) {

    if (catsElem.getAttribute("href", AtomService.ATOM_PROTOCOL) != null) {
        setHref(catsElem.getAttribute("href", AtomService.ATOM_PROTOCOL).getValue());
    }/*from www  .java 2 s  . c o  m*/

    if (catsElem.getAttribute("fixed", AtomService.ATOM_PROTOCOL) != null) {
        if ("yes".equals(catsElem.getAttribute("fixed", AtomService.ATOM_PROTOCOL).getValue())) {
            setFixed(true);
        }
    }

    if (catsElem.getAttribute("scheme", AtomService.ATOM_PROTOCOL) != null) {
        setScheme(catsElem.getAttribute("scheme", AtomService.ATOM_PROTOCOL).getValue());
    }

    // Loop to parse <atom:category> elemenents to Category objects
    final List<Element> catElems = catsElem.getChildren("category", AtomService.ATOM_FORMAT);
    for (final Element catElem : catElems) {
        final Category cat = new Category();
        cat.setTerm(catElem.getAttributeValue("term", AtomService.ATOM_FORMAT));
        cat.setLabel(catElem.getAttributeValue("label", AtomService.ATOM_FORMAT));
        cat.setScheme(catElem.getAttributeValue("scheme", AtomService.ATOM_FORMAT));
        addCategory(cat);
    }
}

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());/* ww  w  .j a va2 s .c  o m*/
        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);
    }
}