|
- package de.erich.railcontrol;
-
- import android.util.Log;
-
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- //import java.io.FileReader;
- import java.io.FileNotFoundException;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
-
- public class ParseLoksConfiguration {
-
- static String ip;
- static int port;
- static int numLoks = 0;
-
- public enum Section {
- NONE, CONFIGURATION, GENERAL, LOKS
- }
-
-
- public void parseConfiguration(String fileName) throws IOException {
-
- try{
- InputStream fis=new FileInputStream(fileName);
- BufferedReader br=new BufferedReader(new InputStreamReader(fis));
-
- String Line = br.readLine();
- Section sec = Section.NONE;
- while(Line != null){
- String[] words=Line.split("\t");
- if ( words[0].equalsIgnoreCase("/>")) {
- sec = Section.NONE;
- } else if ( words[0].equalsIgnoreCase("<General")) {
- sec = Section.GENERAL;
- } else if ( words[0].equalsIgnoreCase("<Lok")) {
- sec = Section.LOKS;
- MyActivity.zuege[numLoks] = new zug();
- numLoks++;
- } else if ( words[0].equalsIgnoreCase("") ) {//&& words.length > 3) {
- if ( words.length > 1 ) {
- if (sec == Section.GENERAL) {
- if ( words[1].equalsIgnoreCase("ip:")) ip = words[2];
- else if (words[1].equalsIgnoreCase("port:"))
- port = Integer.parseInt(words[2]);
- } else if (sec == Section.LOKS) {
- MyActivity.zuege[numLoks-1].setProperty(words);
- }
- }
- }
- try {
- Line = br.readLine();
- } catch (Exception e){
- System.err.println("Error in Parseloop: " + e);
- }
- }
- br.close();
- }
- catch(FileNotFoundException e){
- this.printConfiguration(fileName);
- }
- Log.d("printConfig", "Parsed Configuration");
-
- }
-
- public void printConfiguration(String fileName ) throws IOException {
- FileWriter outStream = null;
-
- /*
- try {
-
- Runtime.getRuntime().exec("cp " + fileName + " " + fileName + ".bck");
- } catch (Exception e) {
- System.out.println("Error: " + e);
- System.exit(0);
- }
- */
-
- try {
- outStream = new FileWriter(fileName);
- } catch (IOException e) {
- System.out.println("Error " + e);
- }
-
- outStream.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
- outStream.write("<Configuration>\n");
- outStream.write("<General\n");
- outStream.write("\tip:\t" + ip + "\n\tport:\t" + port + "\n");
- outStream.write("/>\n");
-
- if ( MyActivity.zuege != null ) {
- for (int i = 0; i < numLoks; i++) {
- if(MyActivity.zuege[i] != null) {
- MyActivity.zuege[i].printConfiguration(outStream);
- }
- }
- }
- outStream.write("</Configuration>\n");
- // if (outStream != null) {
- outStream.close();
- Log.d("printConfig", "Printed Configuration");
-
- // }
- }
-
- }
|