EvaluatingFilterWriter.java

biz/hammurapi/eval/EvaluatingFilterWriter.java

Violations

Inspector Message Severity Location
Java Inspector 031 Switch statement case without 'break' 1 135:29
Java Inspector 031 Switch statement case without 'break' 1 210:17
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 070-B Cyclomatic complexity is too high: 39, maximum allowed is 20 1 94:5
Java Inspector 070-A Cyclomatic complexity is too high: 39, maximum allowed is 12 2 94:5
Java Inspector 073 [java.lang.StringBuffer] In Java 5 use StringBuilder instead of StringBuffer if access is single-threaded, e.g. StringBuffer is used as a local variable . 2 76:5
Java Inspector 087 Chain exceptions. If exception is thrown from an exception handler (wrapping exceptions), pass the cause exception to the new exception constructor. 2 173:59
Java Inspector 087 Chain exceptions. If exception is thrown from an exception handler (wrapping exceptions), pass the cause exception to the new exception constructor. 2 175:55
Java Inspector 089 Type is not documented 2 42:1
Java Inspector 089 Undocumented constructor 2 53:5
Java Inspector 089 Undocumented field 2 86:5
Java Inspector 089 Undocumented field 2 88:5
Java Inspector 089 Undocumented field 2 90:5
Java Inspector 089 Undocumented method 2 94:5
Java Inspector 089 Undocumented method 2 240:5
Java Inspector 089 Undocumented method 2 256:5
Java Inspector 089 Undocumented method 2 262:5
Java Inspector 005 Classes, interfaces, methods, and variables should be named according to Sun's naming conventions. 3 49:5
Java Inspector 024 Avoid hardwired character literals 3 96:22
Java Inspector 024 Avoid hardwired character literals 3 114:22
Java Inspector 024 Avoid hardwired character literals 3 130:22
Java Inspector 024 Avoid hardwired character literals 3 141:47
Java Inspector 024 Avoid hardwired character literals 3 142:47
Java Inspector 024 Avoid hardwired character literals 3 143:47
Java Inspector 024 Avoid hardwired character literals 3 157:63
Java Inspector 024 Avoid hardwired character literals 3 187:22
Java Inspector 024 Avoid hardwired character literals 3 201:47
Java Inspector 024 Avoid hardwired character literals 3 242:23
Java Inspector 024 Avoid hardwired character literals 3 244:23
Java Inspector 024 Avoid hardwired character literals 3 245:23
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 111:55
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 127:55
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 153:59
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 164:65
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 165:63
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 165:79
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 168:65
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 169:63
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 169:107
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 172:66
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 173:75
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 173:115
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 175:71
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 184:55
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 207:55
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 224:55
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 53:5

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.eval;
24
25import java.io.FilterWriter;
26import java.io.IOException;
27import java.io.Writer;
28import java.util.HashSet;
29import java.util.Iterator;
30import java.util.Set;
31
32import org.apache.log4j.Logger;
33
34import biz.hammurapi.config.Context;
35import biz.hammurapi.convert.Converter;
36
37
38/**
39 * @author Pavel Vlasov
40 * @version $Revision$
41 */
42public class EvaluatingFilterWriter extends FilterWriter {
43 private int mode;
44
45 private Context context;
46
47 private Converter converter;
48
49 private static final Logger logger = Logger.getLogger(EvaluatingFilterWriter.class);
50
51 private Set keySet;
52
53 public EvaluatingFilterWriter(
54 Writer out,
55 int mode,
56 Context context,
57 Converter converter) {
58 this(out,mode,context, converter,new HashSet());
59 }
60
61 private EvaluatingFilterWriter(
62 Writer out,
63 int mode,
64 Context context,
65 Converter converter,
66 Set keySet) {
67 super(out);
68 this.mode = mode;
69 this.context = context;
70 this.converter = converter;
71 this.keySet=keySet;
72 }
73
74 private int lastChar;
75
76 private StringBuffer expressionBuffer;
77
78 private static final int STATE_NORMAL = 0;
79
80 private static final int STATE_DOLLAR = 1;
81
82 private static final int STATE_EXPRESSION = 2;
83
84 private static final int STATE_QUOTE = 3;
85
86 public static final int MODE_LENIENT = 0;
87
88 public static final int MODE_MESSAGE = 1;
89
90 public static final int MODE_STRICT = 2;
91
92 private int state = STATE_NORMAL;
93
94 public void write(int c) throws IOException {
95 switch (c) {
96 case '$':
97 switch (state) {
98 case STATE_NORMAL:
99 state = STATE_DOLLAR;
100 break;
101 case STATE_DOLLAR:
102 state = STATE_NORMAL;
103 out.write(c);
104// out.write(b);
105 break;
106 case STATE_EXPRESSION:
107 case STATE_QUOTE:
108 toBuffer(c);
109 break;
110 default:
111 throw new IOException("Invalid lexer state: " + state);
112 }
113 break;
114 case '{':
115 switch (state) {
116 case STATE_EXPRESSION:
117 case STATE_QUOTE:
118 toBuffer(c);
119 break;
120 case STATE_NORMAL:
121 out.write(c);
122 break;
123 case STATE_DOLLAR:
124 state = STATE_EXPRESSION;
125 break;
126 default:
127 throw new IOException("Invalid lexer state: " + state);
128 }
129 break;
130 case '}':
131 switch (state) {
132 case STATE_NORMAL:
133 out.write(c);
134 break;
135 case STATE_DOLLAR:
136 state = STATE_NORMAL;
137 out.write(c);
138 case STATE_EXPRESSION:
139 state = STATE_NORMAL;
140 if (expressionBuffer == null) {
141 out.write('$');
142 out.write('{');
143 out.write('}');
144 } else {
145 String expression = expressionBuffer.toString();
146 expressionBuffer = null;
147 try {
148 Evaluator evaluator = new Evaluator(expression, null, converter);
149 Iterator it = evaluator.evaluate(context).iterator();
150 while (it.hasNext()) {
151 Object object = it.next();
152 if (object == null) {
153 out.write("(null)");
154 } else {
155 out.write(object.toString());
156 if (it.hasNext()) {
157 out.write(' ');
158 }
159 }
160 }
161 } catch (EvaluationException e) {
162 switch (mode) {
163 case MODE_LENIENT:
164 logger.warn("Evaluation failed for: " + expression, e);
165 out.write("${"+expression+"}");
166 break;
167 case MODE_MESSAGE:
168 logger.warn("Evaluation failed for: " + expression, e);
169 out.write(" *** Evaluation failed for: " + expression+" *** ");
170 break;
171 case MODE_STRICT:
172 logger.error("Evaluation failed for: " + expression, e);
173 throw new IOException("Evaluation failed for: '" + expression+"' - " + e);
174 default:
175 throw new IOException("Invalid mode: "+mode);
176 }
177 }
178 }
179 break;
180 case STATE_QUOTE:
181 toBuffer(c);
182 break;
183 default:
184 throw new IOException("Invalid lexer state: " + state);
185 }
186 break;
187 case '"':
188 switch (state) {
189 case STATE_NORMAL:
190 out.write(c);
191 break;
192 case STATE_DOLLAR:
193 state=STATE_NORMAL;
194 out.write(c);
195 break;
196 case STATE_EXPRESSION:
197 state=STATE_QUOTE;
198 toBuffer(c);
199 break;
200 case STATE_QUOTE:
201 if (lastChar!='\\') {
202 state=STATE_EXPRESSION;
203 }
204 toBuffer(c);
205 break;
206 default:
207 throw new IOException("Invalid lexer state: " + state);
208 }
209 break;
210 default:
211 switch (state) {
212 case STATE_NORMAL:
213 out.write(c);
214 break;
215 case STATE_DOLLAR:
216 state=STATE_NORMAL;
217 out.write(c);
218 break;
219 case STATE_EXPRESSION:
220 case STATE_QUOTE:
221 toBuffer(c);
222 break;
223 default:
224 throw new IOException("Invalid lexer state: " + state);
225 }
226 }
227 lastChar = c;
228 }
229
230 /**
231 * @param b
232 */
233 private void toBuffer(int b) {
234 if (expressionBuffer == null) {
235 expressionBuffer = new StringBuffer();
236 }
237 expressionBuffer.append((char) b);
238 }
239
240 public void close() throws IOException {
241 if (state == STATE_DOLLAR) {
242 out.write('$');
243 } else if (state == STATE_EXPRESSION || state == STATE_QUOTE) {
244 out.write('$');
245 out.write('{');
246 }
247
248 if (expressionBuffer != null) {
249 out.write(expressionBuffer.toString());
250 expressionBuffer = null;
251 }
252 out.close();
253 super.close();
254 }
255
256 public void write(char[] cbuf, int off, int len) throws IOException {
257 for (int i=0; i<len; i++) {
258 write(cbuf[i+off]);
259 }
260 }
261
262 public void write(String str, int off, int len) throws IOException {
263 for (int i=0; i<len; i++) {
264 write(str.charAt(i+off));
265 }
266 }
267}
268