import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
public class weathertest extends DefaultHandler
{
// bei weather.com kostenlos anmelden, da bekommt man die daten
// und ein sdk, wo naehere infos im pdf zu finden sind
public static String partnerID = "EureID";
public static String licenseKey = "EuerKey";
public static int depth=-1;
public static ArrayList<String> path = new ArrayList<String>();
public static HashMap<String, String> weatherData = new HashMap<String, String>();
public static HashMap<String, String> translation = new HashMap<String, String>();
public static String element = "";
public static void main(String[] args) {
weatherData = getWeatherData();
// abfragen:
System.out.println("Mond: " + weatherData.get("weather-cc-moon-t"));
System.out.println("Wetter: " + weatherData.get("weather-cc-t"));
System.out.println("Temperatur: " + weatherData.get("weather-cc-tmp"));
System.out.println("Icon: " + weatherData.get("weather-cc-icon"));
System.out.println("Trend: " + weatherData.get("weather-cc-bar-d"));
}
private static HashMap<String, String> getWeatherData() {
XMLReader xr=null;
try{
xr = XMLReaderFactory.createXMLReader();
DefaultHandler handler = new weathertest();
xr.setContentHandler(handler);
xr.setErrorHandler(handler);
} catch( Throwable t ) {
t.printStackTrace();
}
// Parse each file provided on the
// command line.
try {
xr.parse(new InputSource(getXMLfromFile()));
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
}
return weatherData;
}
private static BufferedReader getXML() {
BufferedReader input=null;
try
{
URL url = new URL("http://xoap.weather.com/weather/local/GMXX0086?par=partnerID&
key=licenseKey&prod=xoap&cc=*&link=xoap&unit=m");
URLConnection connection = url.openConnection();
connection.setDoInput(true);
InputStream inStream = connection.getInputStream();
input = new BufferedReader(new InputStreamReader(inStream));
}
catch (Exception e){
System.out.println(e.toString());
}
return input;
}
private static DataInputStream getXMLfromFile() {
File file = new File("C:/workspace/weathertest/GMXX0086.xml");
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try {
fis = new FileInputStream(file);
// Here BufferedInputStream is added for fast reading.
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return dis;
}
public weathertest ()
{
super();
initializeTranslationTable();
}
////////////////////////////////////////////////////////////////////
// Event handlers. for xml parsing
////////////////////////////////////////////////////////////////////
public void startElement (String uri, String name,
String qName, Attributes atts)
{
depth++;
//path.add("name:" + name + " qName:" + qName );
path.add(qName );
}
public void endElement (String uri, String name, String qName)
{
//System.out.println(toString(path) + " --> " + element);
weatherData.put(toString(path), translateItem(element));
path.remove(depth);
depth--;
}
private String toString(ArrayList<String> path2) {
String output="";
if(!path2.isEmpty()){
// format allShutterMovements:{ shutterRef, dir, duration } 1:up, 0:down
for (String tmp : path2) {
output+=tmp+"-";
}
}
output=output.substring(0, output.length()-1);
return output;
}
public void characters (char ch[], int start, int length)
{
String orig = path.get(depth);
String changed="", tmp="";
//System.out.println(start);
if(ch[start]==0)
return;
for (int i = start; i < start + length; i++) {
switch (ch[i]) {
case '\\':
//System.out.print("\\\\");
break;
case '"':
//System.out.print("\\\"");
break;
case '\n':
//System.out.print("\\n");
break;
case '\r':
//System.out.print("\\r");
break;
case '\t':
//System.out.print("\\t");
break;
default:
tmp+=ch[i];
break;
}
}
//System.out.print("\"\n");
element = tmp;
changed=orig + "(" + tmp + ")";
}
private String translateItem(String item) {
item = item.toLowerCase();
//System.out.println(item);
if(translation.containsKey(item))
return translation.get(item);
else
return item;
}
private void initializeTranslationTable() {
// some general things...
translation.put("n/a","nicht verfgbar");
translation.put("N/A","nicht verfgbar");
translation.put("Not Available", "nicht verfgbar");
translation.put("unknown", "unbekannt");
translation.put("NONE", "keine");
translation.put("day","Tag");
translation.put("night", "Nacht");
// first all about moon phases
translation.put("new","Neumond");
translation.put("first quarter", "Viertelmond");
translation.put("full","Vollmond");
translation.put("last quarter", "Dreiviertelmond");
translation.put("waning crescent", "abnehmend");
translation.put("waning gibbous", "abnehmend");
translation.put("waxing crescent", "zunehmend");
translation.put("waxing gibbous", "zunehmend");
// about UV Index...
translation.put("extreme", "extrem");
translation.put("very high", "sehr hoch");
translation.put("high", "hoch");
translation.put("moderate", "moderat");
translation.put("low", "niedrig");
// tendencies used for barometric pressure
translation.put("rising", "steigend");
translation.put("falling", "fallend");
translation.put("steady", "stabil");
// all about weather conditions
translation.put("blowing dust","Sandsturm");
translation.put("blowing dust and windy", "Sandsturm und windig");
translation.put("blowing snow","Schneesturm");
translation.put("blowing snow and windy", "Schneestreiben und windig");
translation.put("clear","klar");
translation.put("clear and windy","klar und windig");
translation.put("cloudy","bewlkt");
translation.put("cloudy and windy","bewlkt und windig");
translation.put("drifting snow","Schneetreiben");
translation.put("drizzle","Nieselregen");
translation.put("fair","heiter");
translation.put("fair and windy","heiter und windig");
translation.put("fog","Nebel");
translation.put("fog and windy","Nebel und windig");
translation.put("haze","trb");
translation.put("haze and windy","trb und windig");
translation.put("heavy drizzle","schwerer Sprhregen");
translation.put("heavy rain","schwerer Regen");
translation.put("heavy rain and windy", "schwerer Regen und windig");
translation.put("heavy rain shower","schwere Regenschauer");
translation.put("heavy rain shower and windy", "schwere Regenschauer und windig");
translation.put("heavy snow","schwerer Schneefall");
translation.put("heavy snow and windy", "schwerer Schneefall und windig");
translation.put("heavy snow shower","schwere Schneeschauer");
translation.put("heavy t-storm","schweres Gewitter");
translation.put("heavy t-storm and windy", "schweres Gewitter und windig");
translation.put("light drizzle","leichter Nieselregen");
translation.put("light drizzle and windy", "leichter Nieselregen und windig");
translation.put("light freezing drizzle", "leichter Eisregen");
translation.put("light freezing drizzle and fog", "leichter Eisregen und Nebel");
translation.put("light freezing rain", "leichter Eisregen");
translation.put("light rain","leichter Regen");
translation.put("light rain shower","leichte Regenschauer");
translation.put("light rain shower and windy", "leichte Regenschauer und windig");
translation.put("light rain and fog","leichter Regen und Nebel");
translation.put("light rain and freezing rain", "leichter Regen und Eisregen");
translation.put("light rain with thunder", "leichter Regen und Donner");
translation.put("light rain and windy", "leichter Regen und windig");
translation.put("light snow","leichter Schneefall");
translation.put("light snow grains","leichte Schneegraupel");
translation.put("light snow shower","leichte Schneeschauer");
translation.put("light snow shower and windy", "leichte Schneeschauer und windig");
translation.put("light snow and sleet", "leichter Schneefall und Schneeregen");
translation.put("light snow and windy", "leichter Schneefall und windig");
translation.put("mist","Sprhregen");
translation.put("mostly cloudy","berwiegend bewlkt");
translation.put("mostly cloudy and windy", "berwiegend bewlkt und windig");
translation.put("partial fog","rtlich Nebel");
translation.put("partly cloudy","teilweise bewlkt");
translation.put("partly cloudy and windy", "teilweise bewlkt und windig");
translation.put("patches of fog","Nebelfelder");
translation.put("rain","Regen");
translation.put("rain and sleet","Regen und Schneeregen");
translation.put("rain and snow","Schneeregen");
translation.put("rain shower","Regenschauern");
translation.put("rain shower and windy", "Regenschauern und windig");
translation.put("rain and fog","Regen und Nebel");
translation.put("rain and freezing rain", "Regen und Eisregen");
translation.put("rain and windy","Regen und windig");
translation.put("sand","Sand");
translation.put("sand and windy","Sand und windig");
translation.put("shallow fog","Bodennebel");
translation.put("showers in the vicinity", "rtliche Schauern");
translation.put("sleet","Graupel");
translation.put("smoke","Dunst");
translation.put("snow","Schnee");
translation.put("snow and fog","Schneefall und nebelig");
translation.put("snow and freezing rain", "Schnee und Eisregen");
translation.put("snow grains","Schneegraupel");
translation.put("snow showers","Schneeschauern");
translation.put("snow and windy and fog", "Schneefall, windig und nebelig");
translation.put("squalls","Sturmben");
translation.put("squalls and windy","Wind und Sturmben");
translation.put("sunny","sonnig");
translation.put("sunny and windy","sonnig und windig");
translation.put("t-storm","Gewitter");
translation.put("t-storm and windy","Gewitter und windig");
translation.put("thunder","Gewitter");
translation.put("thunder and wintry mix", "Gewitter und wechselnde Winde");
translation.put("thunder in the vicinity", "rtliche Gewitter");
translation.put("unknown precip","unbekannt");
translation.put("widespread dust","verbreitet staubig");
translation.put("widespread dust and windy", "verbreitet staubig und windig");
translation.put("wintry mix","wechselnde Winde");
// wind directions long
translation.put("East","Osten");
translation.put("East Northeast", "Ost Nordost");
translation.put("East Southeast", "Ost Sdost");
translation.put("North","Norden");
translation.put("Northeast", "Nordosten");
translation.put("North Northeast", "Nord Nordost");
translation.put("North Northwest", "Nord Nordwest");
translation.put("Northwest", "Nordwesten");
translation.put("South","Sden");
translation.put("Souteast", "Sdosten");
translation.put("South Southeast", "Sd Sdost");
translation.put("South Southwest", "Sd Sdwest");
translation.put("Southwest", "Sdwesten");
translation.put("variable", "wechselnd");
translation.put("West","Westen");
translation.put("West Northwest", "West Nordwest");
translation.put("West Southwest", "West Sdwest");
// wind directions short
translation.put("E", "O");
translation.put("ENE", "ONO");
translation.put("ESE", "OSO");
translation.put("N", "N");
translation.put("NE", "NO");
translation.put("NNE", "NNO");
translation.put("NNW", "NNW");
translation.put("NW", "NW");
translation.put("S", "S");
translation.put("SE", "SO");
translation.put("SSE", "SSO");
translation.put("SSW", "SSW");
translation.put("SW", "SW");
translation.put("VAR", "wechselnd");
translation.put("W", "W");
translation.put("WNW", "WNW");
translation.put("WSW", "WSW");
}
}//end class TestWeatherGateway