KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nl > justobjects > pushlet > core > EventParser


1 // Copyright (c) 2000 Just Objects B.V. <just@justobjects.nl>
2
// Distributable under LGPL license. See terms of license at gnu.org.
3

4 package nl.justobjects.pushlet.core;
5
6 import java.io.*;
7 import java.util.HashMap JavaDoc;
8
9 /**
10  * Parses XML into Event objects.
11  *
12  * @version $Id: EventParser.java,v 1.1 2005/02/18 10:07:23 justb Exp $
13  * @author Just van den Broecke - Just Objects &copy;
14  **/

15 public class EventParser {
16
17
18     private EventParser() {
19     }
20
21     /** Parse Event from a File. */
22     public static Event parse(File aFile) throws IOException {
23         BufferedReader br = new BufferedReader(new FileReader(aFile));
24         return parse(br);
25     }
26
27     /** Parse Event from input Reader. */
28     public static Event parse(Reader aReader) throws IOException {
29         StringBuffer JavaDoc preparsedString = new StringBuffer JavaDoc(24);
30
31         // First find the opening tag ('<')
32
char nextChar;
33         while ((nextChar = (char) aReader.read()) != '<') ;
34
35         // Append '<'
36
preparsedString.append(nextChar);
37
38         // Then find end-tag ('>'), appending all chars to preparsed string.
39
do {
40             nextChar = (char) aReader.read();
41             preparsedString.append(nextChar);
42         } while (nextChar != '>');
43
44         return parse(preparsedString.toString());
45     }
46
47     /** Parse Event from a String. */
48     public static Event parse(String JavaDoc 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         // Create the attributes object.
56
HashMap JavaDoc properties = new HashMap JavaDoc(3);
57
58         // Remove the start and end (< ... />) from the string
59
aString = aString.substring(1, aString.length() - 2).trim();
60
61         int index = 0;
62
63         // Parse the tag
64
while (!Character.isWhitespace(aString.charAt(index))
65                 && (index < aString.length())) {
66             index++;
67         }
68
69         // We don't use the tag: remove from string
70
aString = aString.substring(index).trim();
71         index = 0;
72
73         String JavaDoc attrName;
74         String JavaDoc attrValue;
75
76         while (index < aString.length()) {
77
78             // Parse attribute name
79
while ((aString.charAt(index) != '=')
80                     && (index < aString.length())) {
81                 index++;
82             }
83
84             // Create attr name string
85
attrName = aString.substring(0, index).trim();
86
87             // remove the attributeName and the '=' from the string
88
aString = aString.substring(index + 1).trim();
89             index = 1; // read past the first wrapping "\""
90

91             // Parse attribute value
92
while ((aString.charAt(index) != '\"')
93                     && (index < aString.length())) {
94
95                 // bypass the special characters '\' and '"' inside the
96
// attributevalue itself which are deliniated with a preceding
97
// '\'
98
if (aString.charAt(index) == '\\') {
99                     aString = aString.substring(0, index)
100                             + aString.substring(index + 1); // remove the '\'
101
}
102
103                 index++;
104             }
105
106             // create the attribute value; exclude the wrapping quote-characters
107
attrValue = aString.substring(1, index);
108
109             // Set the attribute N/V
110
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     /** Test method: use files to test. */
120     public static void main(String JavaDoc[] 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 JavaDoc t) {
130             System.out.println("Error parsing event file: " + args[0]);
131             t.printStackTrace();
132         }
133     }
134 }
135
136 /*
137   * $Log: EventParser.java,v $
138   * Revision 1.1 2005/02/18 10:07:23 justb
139   * many renamings of classes (make names compact)
140   *
141   * Revision 1.3 2004/09/03 22:35:37 justb
142   * Almost complete rewrite, just checking in now
143   *
144   * Revision 1.2 2003/08/15 08:37:40 justb
145   * fix/add Copyright+LGPL file headers and footers
146   *
147   * Revision 1.1 2003/05/18 16:12:27 justb
148   * adding support for XML encoded Events
149   *
150   */

151
Popular Tags