📄 yweatherparser.java
字号:
/*
* YWeatherParser.java
*
* Created on May 25, 2007, 2:37 PM
*
*/
package yweatherbeta;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
*
* @author BEN
*/
public class YWeatherParser {
static char FAHRENHEIT = 'f';
static char CELSIUS = 'c';
static int TODAY = 0;
static int TOMORROW = 1 ;
private final String YWEATHER_PATH = "http://weather.yahooapis.com/forecastrss";
private int unit;
private String location;
private DocumentBuilder builder;
private XPath xpath;
private Document doc;
/**
* Creates a new instance of YWeatherParser
*
* @param location The location of this forecast
*
*/
public YWeatherParser(String location) throws ParserConfigurationException{
this(location, FAHRENHEIT);
}
/**
* Creates a new instance of YWeatherParser
*
* @param location The location of this forecast
* @param unit Units for various aspects of the forecast.
*
*/
public YWeatherParser(String location, char unit) throws ParserConfigurationException{
this.setLocation(location);
if(unit != FAHRENHEIT && unit !=CELSIUS)
throw(new IllegalArgumentException());
this.setUnit(unit);
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
this.builder = domFactory.newDocumentBuilder();
XPathFactory factory = XPathFactory.newInstance();
this.xpath = factory.newXPath();
this.doc = null;
xpath.setNamespaceContext(new YWeatherNamespaceContext());
}
public int getUnit() {
return unit;
}
public void setUnit(int unit) {
if(unit != FAHRENHEIT && unit !=CELSIUS)
throw(new IllegalArgumentException());
this.unit = unit;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public void parse() throws IOException, YWeatherParserException{
try {
this.doc = builder.parse("http://weather.yahooapis.com/forecastrss?p="+location+"&u="+unit);
XPathExpression expr = xpath.compile("//title/text()");
String result = (String) expr.evaluate(doc,XPathConstants.STRING);
if(result.equals("Yahoo! Weather - Error"))
throw new YWeatherParserException("Location or unit is not in database");
} catch (XPathExpressionException ex) {
//swallow this exception
} catch (SAXException ex){
throw new YWeatherParserException(ex.getMessage());
}
}
public String getCity(){
String returnValue = null;
try {
XPathExpression expr = xpath.compile("//@city");
returnValue = (String) expr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public String getRegion(){
String returnValue = null;
try {
XPathExpression expr = xpath.compile("//@region");
returnValue = (String) expr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public String getLanguage(){
String returnValue = null;
try {
XPathExpression expr = xpath.compile("//language/text()");
returnValue = (String) expr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public int getTTL(){
int returnValue = 0;
try {
XPathExpression expr = xpath.compile("//ttl/text()");
returnValue = new Integer((String) expr.evaluate(doc, XPathConstants.STRING));
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public String getCountry(){
String returnValue = null;
try {
XPathExpression expr = xpath.compile("//@country");
returnValue = (String) expr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public String getLastBuildDate(){
String returnValue = null;
try {
XPathExpression expr = xpath.compile("//lastBuildDate/text()");
returnValue = (String) expr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public int getWindChill(){
int returnValue = 0;
try {
XPathExpression expr = xpath.compile("//@chill");
returnValue = new Integer((String) expr.evaluate(doc, XPathConstants.STRING));
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public int getWindDirection(){
int returnValue = 0;
try {
XPathExpression expr = xpath.compile("//@direction");
returnValue = new Integer((String) expr.evaluate(doc, XPathConstants.STRING));
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public int getWindSpeed(){
int returnValue = 0;
try {
XPathExpression expr = xpath.compile("//@speed");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
returnValue = new Integer((String) nodes.item(1).getNodeValue());
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public String getDegreeUnit(){
String returnValue = null;
try {
XPathExpression expr = xpath.compile("//@distance");
returnValue = (String)expr.evaluate(doc, XPathConstants.STRING);
//returnValue = (char) Integer.parseInt(result);
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public String getDistanceUnit(){
String returnValue = null;
try {
XPathExpression expr = xpath.compile("//@distance");
returnValue = (String)expr.evaluate(doc, XPathConstants.STRING);
//returnValue = (char) Integer.parseInt(result);
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public String getPressureUnit(){
String returnValue = null;
try {
XPathExpression expr = xpath.compile("//@pressure");
returnValue = (String)expr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public String getSpeedUnit(){
String returnValue = null;
try {
XPathExpression expr = xpath.compile("//@speed");
returnValue = (String)expr.evaluate(doc, XPathConstants.STRING);
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public int getHumidity(){
int returnValue = 0;
try {
XPathExpression expr = xpath.compile("//@humidity");
returnValue = new Integer((String)expr.evaluate(doc, XPathConstants.STRING));
} catch (XPathExpressionException ex) {
//swallow exception
}
return returnValue;
}
public Forecast getForecast(int day) throws YWeatherParserException{
Forecast forecast= new Forecast();
if(day == TODAY){
try {
XPathExpression expr = xpath.compile("//yweather:forecast");
NodeList result = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
Node node = result.item(0);
forecast.setDay(node.getAttributes().getNamedItem("day").getNodeValue());
forecast.setDate(node.getAttributes().getNamedItem("date").getNodeValue());
forecast.setTempHigh(new Integer(node.getAttributes().getNamedItem("high").getNodeValue()));
forecast.setTempLow(new Integer(node.getAttributes().getNamedItem("low").getNodeValue()));
forecast.setCondition(node.getAttributes().getNamedItem("text").getNodeValue());
forecast.setCode(new Integer(node.getAttributes().getNamedItem("code").getNodeValue()));
} catch (XPathExpressionException ex) {
//swallow exception
}
}else if (day == TOMORROW){
try{
XPathExpression expr = xpath.compile("//yweather:forecast");
NodeList result = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
Node node = result.item(1);
forecast.setDay(node.getAttributes().getNamedItem("day").getNodeValue());
forecast.setDate(node.getAttributes().getNamedItem("date").getNodeValue());
forecast.setTempHigh(new Integer(node.getAttributes().getNamedItem("high").getNodeValue()));
forecast.setTempLow(new Integer(node.getAttributes().getNamedItem("low").getNodeValue()));
forecast.setCondition(node.getAttributes().getNamedItem("text").getNodeValue());
forecast.setCode(new Integer(node.getAttributes().getNamedItem("code").getNodeValue()));
} catch (XPathExpressionException ex) {
//swallow exception
}
}else
throw new YWeatherParserException("Day format is wrong");
return forecast;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -