safedispatch-mobile/libSafeMobile/src/main/java/com/safemobile/lib/XMLParser.java
2022-03-14 11:53:00 +02:00

334 lines
12 KiB
Java

package com.safemobile.lib;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.os.Environment;
import android.util.Xml;
import com.safemobile.lib.radio.Channel;
import com.safemobile.lib.radio.Zone;
public class XMLParser {
private ConfigFile config;
private ArrayList<String> xmlPath;
public XMLParser()
{
}
public ArrayList<String> getXMLPath()
{
// reset xmlPath
xmlPath = new ArrayList<String>();
File file[] = Environment.getExternalStorageDirectory().listFiles();
recursiveFileFind(file);
return xmlPath;
}
public ConfigFile parseXML(InputStream input)
{
XmlPullParser parser = Xml.newPullParser();
ArrayList<Contact> contacts = new ArrayList<Contact>();
ArrayList<Zone> zones = new ArrayList<Zone>();
Radio radio = new Radio();
try
{
parser.setInput(input, null);
Contact contact = null;
Zone zone = null;
Channel channel = null;
int eventType = parser.getEventType();
boolean done = false;
while (eventType != XmlPullParser.END_DOCUMENT && !done){
String name = null;
switch (eventType){
// build config object on xml start
case XmlPullParser.START_DOCUMENT:
config = new ConfigFile();
break;
// if start tag (ex. <configuration>, <contact>, <channel>)
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("contact"))
{
try {
String section = parser.getAttributeValue(3);
if(section.equalsIgnoreCase("Digital"))
{
/* <contact name='Call1' call-type='Group Call' call-id='1' section='Digital' /> */
contact = new Contact(0,"", 0,1,1);
// get id and name for Contact
contact.name = parser.getAttributeValue(0);
String type = parser.getAttributeValue(1);
if(type.equals("Private Call"))
contact.contactType = Contact.PRIVATE;
else if (type.equals("Group Call"))
contact.contactType = Contact.GROUP;
contact.id = Integer.parseInt(parser.getAttributeValue(2));
}
else
contact = null;
}
catch (Exception e) {
SM.Exception(e.toString());
contact.id = 0;
}
}
else if (name.equalsIgnoreCase("radio-id"))
{
/* <radio-id>101</radio-id> */
try {
// get radio id
radio.id = Integer.parseInt(parser.nextText());
}
catch(Exception ex) {
SM.Debug(ex.toString());
}
}
else if (name.equalsIgnoreCase("radio-ip"))
{
/* <radio-ip>192.168.10.1</radio-ip> */
// get radio ip
radio.ip = parser.nextText();
}
else if (name.equalsIgnoreCase("zone"))
{
/* <zone position='1' name='zone1'> */
zone = new Zone();
try {
// set zone Id, Number and Name
zone.dbID = Integer.valueOf(parser.getAttributeValue(0));
zone.id = Integer.valueOf(parser.getAttributeValue(0));
zone.ZoneName = parser.getAttributeValue(1);
}
catch(Exception ex) {
SM.Debug(ex.toString());
}
}
else if(name.equalsIgnoreCase("channel"))
{
/* <channel position='1' name='Channel1' /> */
channel = new Channel();
try {
// set channel Id, Number and Name
channel.chName = parser.getAttributeValue(1);
channel.dbID = Integer.valueOf(parser.getAttributeValue(0));
channel.id = Integer.valueOf(parser.getAttributeValue(0));
}
catch(Exception ex) {
SM.Debug(ex.toString());
}
}
break;
// if end tag (ex. </contacts>, </contact>, </channel>)
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("contact") && contact != null)
{
if(contact.contactType == Contact.PRIVATE || contact.contactType == Contact.GROUP)
contacts.add(contact); // add contact to Contacts list
}
else if (name.equalsIgnoreCase("channel"))
zone.channelList.add(channel); // add channel to Channels List
else if (name.equalsIgnoreCase("zone"))
zones.add(zone); // add zone to Zones List
else if (name.equalsIgnoreCase("configuration"))
done = true; // flag finish parsing
break;
}
eventType = parser.next();
}
// add Lists to config
config.contacts = contacts;
config.radio = radio;
// check if every zone has at least a channel and if not, create one
for(Zone zn :zones)
{
if(zn.channelList==null || (zn.channelList!=null && zn.channelList.size()== 0))
{
zn.channelList = new ArrayList<Channel>();
Channel ch = new Channel();
ch.dbID = 0;
ch.chName = "N/A";
ch.id = 0;
zn.channelList.add(ch);
}
}
config.zones = zones;
//SM.Debug("CONFIG", config.toLongString());
}
catch (XmlPullParserException e) {
//e.printStackTrace();
SM.Exception(e.toString());
config = null;
}
catch (IOException e) {
//e.printStackTrace();
SM.Exception(e.toString());
config = null;
}
return config;
}
// get files and directory for crt Directory (file1)
private void recursiveFileFind(File[] crtFile){
int i = 0;
String filePath="";
if(crtFile!=null)
{
while(i != crtFile.length)
{
// get absolute path for crtFile
filePath = crtFile[i].getPath();
if(crtFile[i].isDirectory()){
// if crt file is a Directory get files for new file
File file[] = crtFile[i].listFiles();
recursiveFileFind(file);
}
i++;
// if crtFile path extension is xml, add to ArrayList
if(filePath.substring(filePath.length()-4, filePath.length()).equalsIgnoreCase(".xml"))
{
xmlPath.add(filePath);
}
}
}
}
public boolean downloadXMLFile(String Path)
{
try {
//set the download URL, a url that points to a file on the internet
//this is the file to be downloaded
//Path = "http://www.safemobile.com/upload/codeplugs/037TMA2140.xml";
URL url = new URL(Path);
//create the new connection
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//set up some things on the connection
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
//and connect!
urlConnection.connect();
//set the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File SDCardRoot = Environment.getExternalStorageDirectory();
//create a new file, specifying the path, and the filename
//which we want to save the file as.
File file = new File(SDCardRoot,"radioConfigFile.xml");
//this will be used to write the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
//int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//now, read through the input buffer and write the contents to the file
while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//add up the size so we know how much is downloaded
downloadedSize += bufferLength;
}
//close the output stream when done
fileOutput.close();
SM.Debug(" ############## FINISH " + downloadedSize);
return true;
//catch some possible errors...
} catch (MalformedURLException e) {
e.printStackTrace();
SM.Exception(e.toString());
File SDCardRoot = Environment.getExternalStorageDirectory();
//delete file is failed to download
File file = new File(SDCardRoot,"radioConfigFile.xml");
Boolean result = file.delete();
SM.Debug("DELETE FILE: " + result);
return false;
} catch (IOException e) {
e.printStackTrace();
SM.Exception(e.toString());
File SDCardRoot = Environment.getExternalStorageDirectory();
//delete file is failed to download
File file = new File(SDCardRoot,"radioConfigFile.xml");
Boolean result = file.delete();
SM.Debug("DELETE FILE: " + result);
return false;
}
}
/*
private InputStream getTextFile(FileInputStream inputStream)
{
InputStream input = inputStream;
int read;
FileOutputStream fos = new FileOutputStream(new File(path + ".tmp"));
//DataOutputStream os = new DataOutputStream(new FileOutputStream("\\data\\data\\com.safemobile.radiopadd\\files\\"));
try {
input = new DataInputStream(new FileInputStream("c:\\binary.smc"));
while (true) { // exception deals catches EOF
read = input.r
fos.write(read);
//os.writeInt(is.readByte());
}
} catch (EOFException eof) {
System.out.println("Normal program termination.");
} catch (FileNotFoundException noFile) {
System.err.println("File not found! " + noFile);
} catch (IOException io) {
System.err.println("I/O error occurred: " + io);
} catch (Throwable anything) {
System.err.println("Abnormal exception caught !: " + anything);
} finally {
if (is != null) {
try {
is.close();
os.close();
} catch (IOException ignored) {
}
}
}
}
*/
}