KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > template > SettingStringParser


1 package freemarker.template;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.HashMap JavaDoc;
5
6 import freemarker.core.ParseException;
7 import freemarker.template.utility.StringUtil;
8
9 /**
10  * Helper class for parsing setting values given with string.
11  */

12 class SettingStringParser {
13     private String JavaDoc text;
14     private int p;
15     private int ln;
16
17     SettingStringParser(String JavaDoc text) {
18         this.text = text;
19         this.p = 0;
20         this.ln = text.length();
21     }
22
23     ArrayList JavaDoc parseAsList() throws ParseException {
24         char c;
25         ArrayList JavaDoc seq = new ArrayList JavaDoc();
26         while (true) {
27             c = skipWS();
28             if (c == ' ') break;
29             seq.add(fetchStringValue());
30             c = skipWS();
31             if (c == ' ') break;
32             if (c != ',') throw new ParseException(
33                     "Expected \",\" or the end of text but "
34                     + "found \"" + c + "\"", 0, 0);
35             p++;
36         }
37         return seq;
38     }
39
40     HashMap JavaDoc parseAsImportList() throws ParseException {
41         char c;
42         HashMap JavaDoc map = new HashMap JavaDoc();
43         while (true) {
44             c = skipWS();
45             if (c == ' ') break;
46             String JavaDoc lib = fetchStringValue();
47
48             c = skipWS();
49             if (c == ' ') throw new ParseException(
50                     "Unexpected end of text: expected \"as\"", 0, 0);
51             String JavaDoc s = fetchKeyword();
52             if (!s.equalsIgnoreCase("as")) throw new ParseException(
53                     "Expected \"as\", but found " + StringUtil.jQuote(s), 0, 0);
54
55             c = skipWS();
56             if (c == ' ') throw new ParseException(
57                     "Unexpected end of text: expected gate hash name", 0, 0);
58             String JavaDoc ns = fetchStringValue();
59             
60             map.put(ns, lib);
61
62             c = skipWS();
63             if (c == ' ') break;
64             if (c != ',') throw new ParseException(
65                     "Expected \",\" or the end of text but "
66                     + "found \"" + c + "\"", 0, 0);
67             p++;
68         }
69         return map;
70     }
71
72     String JavaDoc fetchStringValue() throws ParseException {
73         String JavaDoc w = fetchWord();
74         if (w.startsWith("'") || w.startsWith("\"")) {
75             w = w.substring(1, w.length() - 1);
76         }
77         return StringUtil.FTLStringLiteralDec(w);
78     }
79
80     String JavaDoc fetchKeyword() throws ParseException {
81         String JavaDoc w = fetchWord();
82         if (w.startsWith("'") || w.startsWith("\"")) {
83             throw new ParseException(
84                 "Keyword expected, but a string value found: " + w, 0, 0);
85         }
86         return w;
87     }
88
89     char skipWS() {
90         char c;
91         while (p < ln) {
92             c = text.charAt(p);
93             if (!Character.isWhitespace(c)) return c;
94             p++;
95         }
96         return ' ';
97     }
98
99     private String JavaDoc fetchWord() throws ParseException {
100         if (p == ln) throw new ParseException(
101                 "Unexpeced end of text", 0, 0);
102
103         char c = text.charAt(p);
104         int b = p;
105         if (c == '\'' || c == '"') {
106             boolean escaped = false;
107             char q = c;
108             p++;
109             while (p < ln) {
110                 c = text.charAt(p);
111                 if (!escaped) {
112                     if (c == '\\') {
113                         escaped = true;
114                     } else if (c == q) {
115                         break;
116                     }
117                 } else {
118                     escaped = false;
119                 }
120                 p++;
121             }
122             if (p == ln) {
123                 throw new ParseException("Missing " + q, 0, 0);
124             }
125             p++;
126             return text.substring(b, p);
127         } else {
128             do {
129                 c = text.charAt(p);
130                 if (!(Character.isLetterOrDigit(c)
131                         || c == '/' || c == '\\' || c == '_'
132                         || c == '.' || c == '-' || c == '!'
133                         || c == '*' || c == '?')) break;
134                 p++;
135             } while (p < ln);
136             if (b == p) {
137                 throw new ParseException("Unexpected character: " + c, 0, 0);
138             } else {
139                 return text.substring(b, p);
140             }
141         }
142     }
143 }
Popular Tags