Example usage for org.jdom2 Element getAttributeValue

List of usage examples for org.jdom2 Element getAttributeValue

Introduction

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

Prototype

public String getAttributeValue(final String attname) 

Source Link

Document

This returns the attribute value for the attribute with the given name and within no namespace, null if there is no such attribute, and the empty string if the attribute value is empty.

Usage

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

License:Open Source License

private PlayerReference parsePlayer(final Element e) {
    final Element player = e.getChild("player", getNS());
    PlayerReference p = null;/*from  w  w w  . j a v a 2 s. co  m*/

    if (player != null) {
        final Integer width = player.getAttributeValue("width") == null ? null
                : new Integer(player.getAttributeValue("width"));
        final Integer height = player.getAttributeValue("height") == null ? null
                : new Integer(player.getAttributeValue("height"));

        try {
            p = new PlayerReference(new URI(player.getAttributeValue("url")), width, height);
        } catch (final Exception ex) {
            LOG.warn("Exception parsing player tag.", ex);
        }
    }

    return p;
}

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

License:Apache License

private static OSQuery parseQuery(final Element e) {

    final OSQuery query = new OSQuery();

    String att = e.getAttributeValue("role");
    query.setRole(att);/*from  w  w w .j a  v  a  2  s . c  o m*/

    att = e.getAttributeValue("osd");
    query.setOsd(att);

    att = e.getAttributeValue("searchTerms");
    query.setSearchTerms(att);

    att = e.getAttributeValue("title");
    query.setTitle(att);

    try {

        // someones mistake should not cause the parser to fail, since these
        // are only optional attributes

        att = e.getAttributeValue("totalResults");
        if (att != null) {
            query.setTotalResults(Integer.parseInt(att));
        }

        att = e.getAttributeValue("startPage");
        if (att != null) {
            query.setStartPage(Integer.parseInt(att));
        }

    } catch (final NumberFormatException ex) {
        System.err.println("Warning: Exception caught while trying to parse a non-numeric Query attribute "
                + ex.getMessage());
    }

    return query;
}

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

License:Apache License

private static Link parseLink(final Element e, final URL baseURI) {

    final Link link = new Link();

    String att = e.getAttributeValue("rel");// getAtomNamespace()); DONT
    // KNOW WHY DOESN'T WORK

    if (att != null) {
        link.setRel(att);//from w ww.ja  v a2  s.co  m
    }

    att = e.getAttributeValue("type");// getAtomNamespace()); DONT KNOW WHY
    // DOESN'T WORK

    if (att != null) {
        link.setType(att);
    }

    att = e.getAttributeValue("href");// getAtomNamespace()); DONT KNOW WHY
    // DOESN'T WORK

    if (att != null) {

        if (isRelativeURI(att)) { //
            link.setHref(resolveURI(baseURI, e, ""));
        } else {
            link.setHref(att);
        }
    }

    att = e.getAttributeValue("hreflang");// getAtomNamespace()); DONT KNOW
    // WHY DOESN'T WORK

    if (att != null) {
        link.setHreflang(att);
    }

    att = e.getAttributeValue("length");// getAtomNamespace()); DONT KNOW
    // WHY DOESN'T WORK

    return link;
}

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.  ja v  a2 s  .  c o  m*/
 *
 * @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>//from www  . java2  s  .c o  m
 *
 * @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.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;
    }/*  ww  w  .  j a  v  a  2s.c  o  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);//  w w  w .j  a v  a  2  s .  c  o 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.opml.io.impl.OPML10Parser.java

License:Apache License

/**
 * Inspects an XML Document (JDOM) to check if it can parse it.
 * <p>// w  ww  .  j  a  v  a2s .c  om
 * It checks if the given document if the type of feeds the parser understands.
 * <p>
 *
 * @param document XML Document (JDOM) to check if it can be parsed by this parser.
 * @return <b>true</b> if the parser know how to parser this feed, <b>false</b> otherwise.
 */
@Override
public boolean isMyType(final Document document) {
    final Element e = document.getRootElement();

    if (e.getName().equals("opml")
            && (e.getChild("head") == null || e.getChild("head").getChild("docs") == null)
            && (e.getAttributeValue("version") == null || e.getAttributeValue("version").equals("1.0"))) {
        return true;
    }

    return false;
}

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

License:Apache License

protected Outline parseOutline(final Element e, final boolean validate, final Locale locale)
        throws FeedException {
    if (!e.getName().equals("outline")) {
        throw new RuntimeException("Not an outline element.");
    }/*www .j  a v  a  2  s  .  c  o m*/

    final Outline outline = new Outline();
    outline.setText(e.getAttributeValue("text"));
    outline.setType(e.getAttributeValue("type"));
    outline.setTitle(e.getAttributeValue("title"));

    final List<org.jdom2.Attribute> jAttributes = e.getAttributes();
    final ArrayList<Attribute> attributes = new ArrayList<Attribute>();

    for (int i = 0; i < jAttributes.size(); i++) {
        final org.jdom2.Attribute a = jAttributes.get(i);

        if (!a.getName().equals("isBreakpoint") && !a.getName().equals("isComment")
                && !a.getName().equals("title") && !a.getName().equals("text") && !a.getName().equals("type")) {
            attributes.add(new Attribute(a.getName(), a.getValue()));
        }
    }

    outline.setAttributes(attributes);

    try {
        outline.setBreakpoint(readBoolean(e.getAttributeValue("isBreakpoint")));
    } catch (final Exception ex) {
        LOG.warn("Unable to parse isBreakpoint value", ex);

        if (validate) {
            throw new FeedException("Unable to parse isBreakpoint value", ex);
        }
    }

    try {
        outline.setComment(readBoolean(e.getAttributeValue("isComment")));
    } catch (final Exception ex) {
        LOG.warn("Unable to parse isComment value", ex);

        if (validate) {
            throw new FeedException("Unable to parse isComment value", ex);
        }
    }

    final List<Element> children = e.getChildren("outline");
    outline.setModules(parseItemModules(e, locale));
    outline.setChildren(parseOutlines(children, validate, locale));

    return outline;
}

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

License:Apache License

/**
 * Inspects an XML Document (JDOM) to check if it can parse it.
 * <p>//from   w  ww .  j a va 2s .c  o  m
 * It checks if the given document if the type of feeds the parser understands.
 * <p>
 *
 * @param document XML Document (JDOM) to check if it can be parsed by this parser.
 * @return <b>true</b> if the parser know how to parser this feed, <b>false</b> otherwise.
 */
@Override
public boolean isMyType(final Document document) {
    final Element e = document.getRootElement();

    if (e.getName().equals("opml") && (e.getChild("head") != null && e.getChild("head").getChild("docs") != null
            || e.getAttributeValue("version") != null && e.getAttributeValue("version").equals("2.0")
            || e.getChild("head") != null && e.getChild("head").getChild("ownerId") != null)) {
        return true;
    }

    return false;
}