Android App für SRCP gesteuerte Modelleisenbahnen
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

110 wiersze
3.2KB

  1. package de.erich.railcontrol;
  2. import android.util.Log;
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. //import java.io.FileReader;
  6. import java.io.FileNotFoundException;
  7. import java.io.FileWriter;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. public class ParseLoksConfiguration {
  12. static String ip;
  13. static int port;
  14. static int numLoks = 0;
  15. public enum Section {
  16. NONE, CONFIGURATION, GENERAL, LOKS
  17. }
  18. public void parseConfiguration(String fileName) throws IOException {
  19. try{
  20. InputStream fis=new FileInputStream(fileName);
  21. BufferedReader br=new BufferedReader(new InputStreamReader(fis));
  22. String Line = br.readLine();
  23. Section sec = Section.NONE;
  24. while(Line != null){
  25. String[] words=Line.split("\t");
  26. if ( words[0].equalsIgnoreCase("/>")) {
  27. sec = Section.NONE;
  28. } else if ( words[0].equalsIgnoreCase("<General")) {
  29. sec = Section.GENERAL;
  30. } else if ( words[0].equalsIgnoreCase("<Lok")) {
  31. sec = Section.LOKS;
  32. MyActivity.zuege[numLoks] = new zug();
  33. numLoks++;
  34. } else if ( words[0].equalsIgnoreCase("") ) {//&& words.length > 3) {
  35. if ( words.length > 1 ) {
  36. if (sec == Section.GENERAL) {
  37. if ( words[1].equalsIgnoreCase("ip:")) ip = words[2];
  38. else if (words[1].equalsIgnoreCase("port:"))
  39. port = Integer.parseInt(words[2]);
  40. } else if (sec == Section.LOKS) {
  41. MyActivity.zuege[numLoks-1].setProperty(words);
  42. }
  43. }
  44. }
  45. try {
  46. Line = br.readLine();
  47. } catch (Exception e){
  48. System.err.println("Error in Parseloop: " + e);
  49. }
  50. }
  51. br.close();
  52. }
  53. catch(FileNotFoundException e){
  54. this.printConfiguration(fileName);
  55. }
  56. Log.d("printConfig", "Parsed Configuration");
  57. }
  58. public void printConfiguration(String fileName ) throws IOException {
  59. FileWriter outStream = null;
  60. /*
  61. try {
  62. Runtime.getRuntime().exec("cp " + fileName + " " + fileName + ".bck");
  63. } catch (Exception e) {
  64. System.out.println("Error: " + e);
  65. System.exit(0);
  66. }
  67. */
  68. try {
  69. outStream = new FileWriter(fileName);
  70. } catch (IOException e) {
  71. System.out.println("Error " + e);
  72. }
  73. outStream.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  74. outStream.write("<Configuration>\n");
  75. outStream.write("<General\n");
  76. outStream.write("\tip:\t" + ip + "\n\tport:\t" + port + "\n");
  77. outStream.write("/>\n");
  78. if ( MyActivity.zuege != null ) {
  79. for (int i = 0; i < numLoks; i++) {
  80. if(MyActivity.zuege[i] != null) {
  81. MyActivity.zuege[i].printConfiguration(outStream);
  82. }
  83. }
  84. }
  85. outStream.write("</Configuration>\n");
  86. // if (outStream != null) {
  87. outStream.close();
  88. Log.d("printConfig", "Printed Configuration");
  89. // }
  90. }
  91. }