package com.hello.helloxml; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.net.URI; import java.util.LinkedHashMap; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import android.app.Activity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.widget.TextView; public class HelloXmlActivity extends Activity { private static final String TAG = "HelloXmlActivity"; private static LinkedHashMap<String, String> widgetInfoMap = new LinkedHashMap<String, String>(); private static final String WIDGET_NAME = "widgetname"; private static final String WIDTH = "width"; private static final String HEIGHT = "height"; private XmlDataSet xmlDataSet; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); xmlDataSet = new XmlDataSet(); String zipfile = "//sdcard//dev-analog_clock-vf_owm.widget"; try { unzip(zipfile,"//sdcard//"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // File f = new File("//sdcard//dev-analog_clock-vf_owm//config.xml"); // if(f.exists()){ // parseXml("//sdcard//dev-analog_clock-vf_owm//config.xml"); // saveXmlToDb(xmlDataSet); // } else { // Log.e(TAG, "file not exist."); // } } //-------------------verify ok while uncompressing a widget file---------------- private void unzip(String zipFileName, String outputDirectory) throws Exception { ZipInputStream in = new ZipInputStream(new FileInputStream(zipFileName)); ZipEntry z; String name = ""; String extractedFile = ""; int counter = 0; while ((z = in.getNextEntry()) != null) { name = z.getName(); Log.d(TAG, "unzipping file: " + name); if (z.isDirectory()) { Log.d(TAG, name + "is a folder"); // get the folder name of the widget name = name.substring(0, name.length() - 1); File folder = new File(outputDirectory + File.separator + name); folder.mkdir(); if(counter == 0){ extractedFile = folder.toString(); } counter++; Log.d(TAG, "mkdir " + outputDirectory + File.separator + name); } else { Log.d(TAG, name + "is a normal file"); File file = new File(outputDirectory + File.separator + name); file.createNewFile(); // get the output stream of the file FileOutputStream out = new FileOutputStream(file); int ch; byte[] buffer = new byte[1024]; //read (ch) bytes into buffer while ((ch = in.read(buffer)) != -1){ // write (ch) byte from buffer at the position 0 out.write(buffer, 0, ch); out.flush(); } out.close(); } } in.close(); parseXml(extractedFile + File.separator +"config.xml"); saveXmlToDb(xmlDataSet); // parseXml("//sdcard//dev-analog_clock-vf_owm//config.xml"); } private void saveXmlToDb(XmlDataSet dataSet){ TextView tv = (TextView)findViewById(R.id.xmlinfo); String info = xmlDataSet.toString();; tv.setText(info); // DownloadDBHelper dbHelper = new DownloadDBHelper(getBaseContext(), "download.db", null, 1); // SQLiteDatabase mDb = dbHelper.getWritableDatabase(); // // String widgetName = dataSet.getWidgetName(); // int width = dataSet.getWidth(); // int height = dataSet.getHeight(); // // ContentValues xmlValues = new ContentValues(); // xmlValues.put(WIDGET_NAME, widgetName); // xmlValues.put(WIDTH, width); // xmlValues.put(HEIGHT, height); // // long rowID = mDb.insert("download", null, xmlValues); // if (rowID < 0){ // Log.e(TAG, "insert xml info to DB error."); // } } private void parseXml(String fileName) { try { // Get SAX Parser Factory SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader xr = parser.getXMLReader(); if(xr == null){ //parse the xml file XMLReader xr.setContentHandler(new ParserHandler()); xr.parse(fileName); }else{ //parse the xml file with parser parser.parse(new File(fileName), new ParserHandler()); } } catch (ParserConfigurationException e) { // TODO Auto-generated catch block Log.e(TAG, "ParserConfigurationException: " + e.getMessage()); } catch (SAXException e) { // TODO Auto-generated catch block Log.e(TAG, "SAXException: " + e.getMessage()); } catch (IOException e) { // TODO Auto-generated catch block Log.e(TAG, "IOException: " + e.getMessage()); } } private class ParserHandler extends DefaultHandler { // SAX callback implementations from DocumentHandler, ErrorHandler, etc. private Writer out; private String locName = "default"; /** * constructor method * @throws SAXException */ public ParserHandler() throws SAXException { try { out = new OutputStreamWriter(System.out, "UTF8"); } catch (IOException e) { throw new SAXException("Error getting output handle.", e); } } /** * call back method when enter in the start point */ public void startDocument() throws SAXException { Log.v(TAG, "-->startDocument()"); } /** * call back method when enter in the end point */ public void endDocument() throws SAXException { Log.v(TAG, "-->endDocument()"); } /** * This is the call back method which will be called when finding a * new start tag(<title>title content</title>) name for each line. */ public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { Log.v(TAG, "-->startElement(): localName = " + localName); locName = localName; } /** * This is the call back method will be called when finding a * new end tag(<title>title content</title>) name for each line. */ public void endElement(String uri, String localName, String qName) throws SAXException { //clear the localName with default value locName = "default"; } /** * get the value of certain tags */ public void characters(char[] ch, int start, int len) throws SAXException { String s = new String(ch, start, len); if (locName.equals(WIDGET_NAME)) { xmlDataSet.setWidgetName(s); } else if (locName.equals(WIDTH)) { xmlDataSet.setWidth(Integer.parseInt(s)); } else if (locName.equals(HEIGHT)) { xmlDataSet.setHeight(Integer.parseInt(s)); } } } }