1 4 package nl.justobjects.pushlet.core; 5 6 import java.io.*; 7 import java.util.HashMap ; 8 9 15 public class EventParser { 16 17 18 private EventParser() { 19 } 20 21 22 public static Event parse(File aFile) throws IOException { 23 BufferedReader br = new BufferedReader(new FileReader(aFile)); 24 return parse(br); 25 } 26 27 28 public static Event parse(Reader aReader) throws IOException { 29 StringBuffer preparsedString = new StringBuffer (24); 30 31 char nextChar; 33 while ((nextChar = (char) aReader.read()) != '<') ; 34 35 preparsedString.append(nextChar); 37 38 do { 40 nextChar = (char) aReader.read(); 41 preparsedString.append(nextChar); 42 } while (nextChar != '>'); 43 44 return parse(preparsedString.toString()); 45 } 46 47 48 public static Event parse(String aString) throws IOException { 49 aString = aString.trim(); 50 51 if (!aString.startsWith("<") || !aString.endsWith("/>")) { 52 throw new IOException("No start or end tag found while parsing event [" + aString + "]"); 53 } 54 55 HashMap properties = new HashMap (3); 57 58 aString = aString.substring(1, aString.length() - 2).trim(); 60 61 int index = 0; 62 63 while (!Character.isWhitespace(aString.charAt(index)) 65 && (index < aString.length())) { 66 index++; 67 } 68 69 aString = aString.substring(index).trim(); 71 index = 0; 72 73 String attrName; 74 String attrValue; 75 76 while (index < aString.length()) { 77 78 while ((aString.charAt(index) != '=') 80 && (index < aString.length())) { 81 index++; 82 } 83 84 attrName = aString.substring(0, index).trim(); 86 87 aString = aString.substring(index + 1).trim(); 89 index = 1; 91 while ((aString.charAt(index) != '\"') 93 && (index < aString.length())) { 94 95 if (aString.charAt(index) == '\\') { 99 aString = aString.substring(0, index) 100 + aString.substring(index + 1); } 102 103 index++; 104 } 105 106 attrValue = aString.substring(1, index); 108 109 properties.put(attrName, attrValue); 111 112 aString = aString.substring(index + 1).trim(); 113 index = 0; 114 } 115 116 return new Event(properties); 117 } 118 119 120 public static void main(String [] args) { 121 try { 122 Event event = parse(new File(args[0])); 123 System.out.println("OK parsed Event file " + args[0]); 124 System.out.println(event.toXML()); 125 126 event = parse(event.toXML()); 127 System.out.println("OK parsed Event string"); 128 System.out.println(event.toXML()); 129 } catch (Throwable t) { 130 System.out.println("Error parsing event file: " + args[0]); 131 t.printStackTrace(); 132 } 133 } 134 } 135 136 151 | Popular Tags |