KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > attributes > compiler > AttributeExpressionParser


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.attributes.compiler;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import org.apache.tools.ant.BuildException;
22
23 /**
24  * Parser for attribute expressions.
25  */

26 public class AttributeExpressionParser {
27     
28     public static class Argument {
29         public final String JavaDoc field;
30         public final String JavaDoc text;
31         public final int length;
32         
33         public Argument (String JavaDoc field, String JavaDoc text, int length) {
34             this.field = field;
35             this.text = text;
36             this.length = length;
37         }
38         
39         public boolean equalsOrNull (String JavaDoc a, String JavaDoc b) {
40             if (a == null) {
41                 return b == null;
42             } else {
43                 return b != null && a.equals (b);
44             }
45         }
46         
47         public boolean equals (Object JavaDoc o) {
48             return o instanceof Argument &&
49                 equalsOrNull (field, ((Argument) o).field) &&
50                 ((Argument) o).text.equals (text);
51         }
52         
53         public String JavaDoc toString () {
54             return "[Argument: " + field + ", " + text + ", " + length + "]";
55         }
56     }
57     
58     public static class ParseResult {
59         public final List JavaDoc arguments = new ArrayList JavaDoc ();
60         public final String JavaDoc className;
61         
62         public ParseResult (String JavaDoc className) {
63             this.className = className;
64         }
65         
66         public boolean equals (Object JavaDoc o) {
67             return o instanceof ParseResult &&
68                 className.equals (((ParseResult) o).className) &&
69                 arguments.equals (((ParseResult) o).arguments);
70         }
71         
72         public String JavaDoc toString () {
73             return "[ParseResult: " + className + ", " + arguments + "]";
74         }
75     }
76     
77     protected static Argument nextArgument (String JavaDoc string, int startPos, String JavaDoc filename, int line) {
78         if (string.charAt (startPos) == ')') {
79             return null;
80         }
81         
82         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
83         
84         int i = startPos;
85         int depth = 0;
86         while (!( (string.charAt (i) == ',' || string.charAt (i) == ')') && depth == 0)) {
87             switch (string.charAt (i)) {
88             case '[':
89             case '{':
90             case '(': depth++; break;
91                 
92             case ']':
93             case '}':
94             case ')': depth--; break;
95                 
96             case '\'':
97             case '"': {
98                     // handle string literals
99
char endChar = string.charAt (i);
100                     sb.append (string.charAt (i));
101                     i++;
102                     while (true) {
103                         char ch = string.charAt (i);
104                         if (ch == '\\') {
105                             sb.append (ch);
106                             i++;
107                             ch = string.charAt (i);
108                             sb.append (ch);
109                         } else {
110                             if (ch == endChar) {
111                                 break;
112                             }
113                             sb.append (ch);
114                         }
115                         i++;
116                     }
117                 }
118             }
119             
120             sb.append (string.charAt (i));
121             i++;
122             if (i == string.length ()) {
123                 throw new BuildException (filename + ":" + line + ": Unterminated argument: " + string);
124             }
125         }
126         
127         if (string.charAt (i) == ',') {
128             i++;
129         }
130         
131         String JavaDoc text = sb.toString ();
132         String JavaDoc field = null;
133         int eqPos = text.indexOf ('=');
134         if (eqPos > -1) {
135             boolean identifier = true;
136             for (int j = 0; j < eqPos; j++) {
137                 char ch = text.charAt (j);
138                 
139                 if (Character.isJavaIdentifierPart (ch) || ch == ' ') {
140                 } else {
141                     identifier = false;
142                 }
143             }
144             
145             if (identifier) {
146                 field = text.substring (0, eqPos).trim ();
147                 text = text.substring (eqPos + 1).trim ();
148             }
149         }
150         
151         Argument arg = new Argument (field, text, i - startPos);
152         
153         return arg;
154     }
155     
156     public static ParseResult parse (String JavaDoc string, String JavaDoc filename, int line) {
157         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
158         int i = 0;
159         while (i < string.length () && (Character.isJavaIdentifierPart (string.charAt (i)) || string.charAt (i) == '.' || string.charAt (i) == ' ')) {
160             sb.append (string.charAt (i));
161             i++;
162         }
163         
164         if (i == string.length () || string.charAt (i) != '(') {
165             throw new BuildException (filename + ":" + line + ": Illegal expression: " + string);
166         }
167         
168         ParseResult result = new ParseResult (sb.toString ());
169         
170         i++;
171         Argument arg = null;
172         boolean seenField = false;
173         while ((arg = nextArgument (string, i, filename, line)) != null) {
174             if (arg.field != null) {
175                 seenField = true;
176             }
177             
178             if (seenField && arg.field == null) {
179                 throw new BuildException (filename + ":" + line + ": Un-named parameters must come before the named parameters: " + string);
180             }
181             
182             result.arguments.add (arg);
183             i += arg.length;
184         }
185         
186         return result;
187     }
188 }
Popular Tags