Package org.rometools.feed.module.yahooweather

Examples of org.rometools.feed.module.yahooweather.YWeatherModuleImpl


    public String getNamespaceUri() {
        return YWeatherModule.URI;
    }

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

        if(location != null) {
            Location l = new Location(location.getAttributeValue("city"),
                    location.getAttributeValue("region"),
                    location.getAttributeValue("country"));
            module.setLocation(l);
        }

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

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

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

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

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

        if(atmosphere != null) {
            try {
                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(NumberFormatException nfe) {
                Logger.getAnonymousLogger()
                      .warning("NumberFormatException processing <atmosphere> tag.");
            }
        }

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

        if(astronomy != null) {
            try {
                Astronomy a = new Astronomy(TIME_ONLY.parse(
                            astronomy.getAttributeValue("sunrise")
                                     .replaceAll("am", "AM")
                                     .replaceAll("pm", "PM")),
                        TIME_ONLY.parse(astronomy.getAttributeValue("sunset")
                                                 .replaceAll("am", "AM")
                                                 .replaceAll("pm", "PM")));
                module.setAstronomy(a);
            } catch(ParseException pe) {
                Logger.getAnonymousLogger()
                      .warning("ParseException processing <astronomy> tag.");
            }
        }

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

        if(condition != null) {
            try {
                Condition c = new Condition(condition.getAttributeValue("text"),
                        ConditionCode.fromCode(Integer.parseInt(
                                condition.getAttributeValue("code"))),
                        Integer.parseInt(condition.getAttributeValue("temp")),
                        LONG_DATE.parse(condition.getAttributeValue("date")
                                                 .replaceAll("pm", "PM")
                                                 .replaceAll("am", "AM")));
                module.setCondition(c);
            } catch(NumberFormatException nfe) {
                Logger.getAnonymousLogger()
                      .warning("NumberFormatException processing <condition> tag.");
            } catch(ParseException pe) {
                Logger.getAnonymousLogger()
                      .warning("ParseException processing <condition> tag.");
            }
        }

        List forecasts = element.getChildren("forecast", WeatherModuleParser.NS);

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

            for(Iterator it = forecasts.iterator(); it.hasNext(); i++) {
                Element forecast = (Element) it.next();

                try {
                    f[i] = new Forecast(forecast.getAttributeValue("day"),
                            SHORT_DATE.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(NumberFormatException nfe) {
                    Logger.getAnonymousLogger()
                          .warning("NumberFormatException processing <forecast> tag.");
                } catch(ParseException pe) {
                    Logger.getAnonymousLogger()
                          .warning("ParseException processing <forecast> tag.");
                }
            }

            module.setForecasts(f);
        }

        return module;
    }
View Full Code Here


    public void generate(Module module, Element element) {
        if(!(module instanceof YWeatherModuleImpl)) {
            return;
        }

        YWeatherModuleImpl weather = (YWeatherModuleImpl) module;

        if(weather.getAstronomy() != null) {
            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) {
            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) {
            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) {
            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) {
            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) {
            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++) {
                Element forecast = new Element("forecast",
                        WeatherModuleGenerator.NS);
                Forecast f = weather.getForecasts()[i];

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

TOP

Related Classes of org.rometools.feed.module.yahooweather.YWeatherModuleImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.