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;
8 
9 /**
10 * Parses XML into Event objects.
11 *
12 * @author Just van den Broecke - Just Objects &copy;
13 * @version $Id: EventParser.java,v 1.3 2007/11/23 14:33:07 justb Exp $
14 */
15public class EventParser {
16
17
18    private EventParser() {
19    }
20
21    /**
22     * Parse Event from a File.
23     */
24    public static Event parse(File aFile) throws IOException {
25        BufferedReader br = new BufferedReader(new FileReader(aFile));
26        return parse(br);
27    }
28
29    /**
30     * Parse Event from input Reader.
31     */
32    public static Event parse(Reader aReader) throws IOException {
33        StringBuffer preparsedString = new StringBuffer(24);
34
35        // First find the opening tag ('<')
36        char nextChar;
37        while ((nextChar = (char) aReader.read()) != '<') ;
38
39        // Append '<'
40        preparsedString.append(nextChar);
41
42        // Then find end-tag ('>'), appending all chars to preparsed string.
43        do {
44            nextChar = (char) aReader.read();
45            preparsedString.append(nextChar);
46        } while (nextChar != '>');
47
48        return parse(preparsedString.toString());
49    }
50
51    /**
52     * Parse Event from a String.
53     */
54    public static Event parse(String aString) throws IOException {
55        aString = aString.trim();
56
57        if (!aString.startsWith("<") || !aString.endsWith("/>")) {
58            throw new IOException("No start or end tag found while parsing event [" + aString + "]");
59        }
60
61        // Create the attributes object.
62        HashMap properties = new HashMap(3);
63
64        // Remove the start and end (< ... />) from the string
65        aString = aString.substring(1, aString.length() - 2).trim();
66
67        int index = 0;
68
69        // Parse the tag
70        while (!Character.isWhitespace(aString.charAt(index))
71                && (index < aString.length())) {
72            index++;
73        }
74
75        // We don't use the tag: remove from string
76        aString = aString.substring(index).trim();
77        index = 0;
78
79        String attrName;
80        String attrValue;
81
82        while (index < aString.length()) {
83
84            // Parse attribute name
85            while ((aString.charAt(index) != '=')
86                    && (index < aString.length())) {
87                index++;
88            }
89
90            // Create attr name string
91            attrName = aString.substring(0, index).trim();
92
93            // remove the attributeName and the '=' from the string
94            aString = aString.substring(index + 1).trim();
95            index = 1;  // read past the first wrapping "\""
96
97            // Parse attribute value
98            while ((aString.charAt(index) != '\"')
99                    && (index < aString.length())) {
00
01                // bypass the special characters '\' and '"' inside the
02                // attributevalue itself which are deliniated with a preceding
03                // '\'
04                if (aString.charAt(index) == '\\') {
05                    aString = aString.substring(0, index)
06                            + aString.substring(index + 1); // remove the '\'
07                }
08
09                index++;
10            }
11
12            // create the attribute value; exclude the wrapping quote-characters
13            attrValue = aString.substring(1, index);
14
15            // Set the attribute N/V
16            properties.put(attrName, attrValue);
17
18            aString = aString.substring(index + 1).trim();
19            index = 0;
20        }
21
22        return new Event(properties);
23    }
24
25    /**
26     * Test method: use files to test.
27     */
28    public static void main(String[] args) {
29        try {
30            Event event = parse(new File(args[0]));
31            System.out.println("OK parsed Event file " + args[0]);
32            System.out.println(event.toXML());
33
34            event = parse(event.toXML());
35            System.out.println("OK parsed Event string");
36            System.out.println(event.toXML());
37        } catch (Throwable t) {
38            System.out.println("Error parsing event file: " + args[0]);
39            t.printStackTrace();
40        }
41    }
42}
43
44/*
45  * $Log: EventParser.java,v $
46  * Revision 1.3  2007/11/23 14:33:07  justb
47  * core classes now configurable through factory
48  *
49  * Revision 1.2  2006/05/06 00:10:11  justb
50  * various chgs but not too serious...
51  *
52  * Revision 1.1  2005/02/18 10:07:23  justb
53  * many renamings of classes (make names compact)
54  *
55  * Revision 1.3  2004/09/03 22:35:37  justb
56  * Almost complete rewrite, just checking in now
57  *
58  * Revision 1.2  2003/08/15 08:37:40  justb
59  * fix/add Copyright+LGPL file headers and footers
60  *
61  * Revision 1.1  2003/05/18 16:12:27  justb
62  * adding support for XML encoded Events
63  *
64  */
65