Back to project page AGOGCyberStat.
The source code is released under:
MIT License
If you think the Android project AGOGCyberStat listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.agog.cyberstat; //from w w w . j av a 2 s. com import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import org.w3c.dom.Node; /** * @author greg@agog.com * * Parse and change the xml returning from the Motison webapp interface */ public class MotisonXML { private Document mDoc; private String mNotifyString; /** * Only written for single thermostat case. See device tag. * Modifies mNotifyString for status string to show in later notification * * @param xmlstring quickdata xml returned from Motison website * @param newtemp new temperature string to set as target temperature. * @return true if no change from current setting */ public Boolean setXML(String xmlstring,String newtemp) { DocumentBuilderFactory dbf = DocumentBuilderFactory .newInstance(); dbf.setValidating(false); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException e1) { e1.printStackTrace(); } // the XML file mDoc = null; try { mDoc = db.parse(new ByteArrayInputStream(xmlstring.getBytes("UTF-8"))); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mDoc.getDocumentElement().normalize(); String currTemp = getFirstValue("currTemp"); String setTemp = getFirstValue("setTemp"); String localWeather = getFirstValue("localWeather"); NodeList nodelist; String devId = null; nodelist = mDoc.getElementsByTagName("device"); for (int temp = 0; temp < nodelist.getLength() && temp < 1; temp++) { Node nNode = nodelist.item(temp); NamedNodeMap nodemap = nNode.getAttributes(); nNode = nodemap.getNamedItem("devId"); devId = nNode.getTextContent(); } setFirstValue("modified",devId); setFirstValue("setTemp",newtemp); Boolean nochange = newtemp.equals(setTemp); if(nochange) { mNotifyString = currTemp + "/" + setTemp + " -> " + newtemp + " ("+localWeather+") "; } else { mNotifyString = currTemp + "/" + setTemp + " -> " + newtemp + " ("+localWeather+") "; } return(newtemp.equals(setTemp)); } public String getNotifyString() { return(mNotifyString); } protected String getFirstValue(String tag) { NodeList nodelist; String ret = ""; nodelist = mDoc.getElementsByTagName(tag); for (int temp = 0; temp < nodelist.getLength() && temp < 1; temp++) { Node nNode = nodelist.item(temp); ret = nNode.getTextContent(); } return(ret); } protected void setFirstValue(String tag,String newvalue) { NodeList nodelist; nodelist = mDoc.getElementsByTagName(tag); for (int temp = 0; temp < nodelist.getLength() && temp < 1; temp++) { Node nNode = nodelist.item(temp); nNode.setTextContent(newvalue); } } public String getXML() { String output = null; // write the content into xml file TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer; try { transformer = transformerFactory.newTransformer(); // Output to console for testing // StreamResult result = new StreamResult(System.out); StringWriter writer = new StringWriter(); try { DOMSource source = new DOMSource(mDoc); transformer.transform(source, new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } output = writer.toString(); } catch (TransformerConfigurationException e1) { e1.printStackTrace(); } return(output); } }