2 * PropertyParser class
\r
3 * Parses property files
\r
7 * @author Daniel Jackson
\r
8 * @version 0, 07/02/01
\r
13 //import java.util.*;
\r
15 public class PropertyParser {
\r
16 private FileInputStream reader;
\r
17 private String token;
\r
18 private int next_char;
\r
20 public PropertyParser (FileInputStream r) {
\r
22 next_char = reader.read ();
\r
23 consume_comments ();
\r
26 private void consume_comments () {
\r
27 // consume lines that don't start with <
\r
28 while (next_char != '<' && !is_eos (next_char)) {
\r
29 if (!is_eol (next_char))
\r
35 private void consume_char () {
\r
36 token += (char) next_char;
\r
37 next_char = reader.read ();
\r
38 //while(next_char == 13 || next_char==10) {
\r
39 // next_char = reader.read ();
\r
41 //System.out.println( "next_char: "+(char)next_char );
\r
44 private void error (String msg) {
\r
45 // correct to number from 1, not zero
\r
46 //t line_number = reader.getLineNumber() + 1;
\r
47 System.out.println (msg);
\r
51 public boolean has_more_properties () {
\r
52 return (!is_eos (next_char));
\r
56 * requires: next_char contains next character in reader <p>
\r
57 * ensures: returns list of properties until end of line or stream <p>
\r
58 * according to the following syntax:
\r
59 * property list is sequence of properties followed by eol of eos
\r
60 * property is left-angle, property-name, colon, value, right-angle
\r
61 * property-name is alphanumeric string, but value is any char sequence
\r
62 * skips lines that do not start with <
\r
63 * reports syntax errors on this.error_reporter
\r
65 * @return list of properties until end of line or stream.
\r
66 * Notes: chose LinkedList because it provides removeFirst, to support common
\r
67 * case in which first property is removed (eg, because it's a style name)
\r
69 public LinkedList get_property_list () {
\r
70 LinkedList result = /*disjoint llPropList*/ new LinkedList ();
\r
71 while (!is_eol (next_char) && !is_eos(next_char))
\r
72 result.add (get_property ());
\r
74 consume_comments ();
\r
78 private Property get_property () {
\r
79 if (next_char != '<')
\r
80 error ("Found " + next_char + " when expecting <");
\r
83 while (is_alphanumeric (next_char)) consume_char ();
\r
84 String property = token;
\r
85 if (next_char != ':')
\r
86 error ("Found " + next_char + " following " + token + " when expecting :");
\r
89 while (next_char != '>' && !is_eol(next_char) && !is_eos (next_char))
\r
91 String value = token;
\r
92 if (next_char != '>')
\r
93 error ("Found " + next_char + " following " + token + " when expecting >");
\r
95 return new Property (property, value);
\r
98 static boolean is_eol (int c) {return c == '\n';}
\r
99 static boolean is_eos (int c) {return c == -1;}
\r
100 static boolean is_alphabetic (int c) {
\r
101 return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
\r
103 static boolean is_numeric (int c) {return c >= '0' && c <= '9';}
\r
104 static boolean is_alphanumeric (int c) {
\r
105 return is_numeric (c) || is_alphabetic (c);
\r