001: /*
002: * $Id: PrintStatement.java,v 1.19 2002/09/16 08:05:06 jkl Exp $
003: *
004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
005: *
006: * Use is subject to license terms, as defined in
007: * Anvil Sofware License, Version 1.1. See LICENSE
008: * file, or http://njet.org/license-1.1.txt
009: */
010: package anvil.script.statements;
011:
012: import anvil.core.Any;
013: import anvil.Location;
014: import anvil.parser.Tag;
015: import anvil.codec.Code;
016: import anvil.ErrorListener;
017: import anvil.script.compiler.ByteCompiler;
018: import anvil.script.Context;
019: import anvil.script.expression.Node;
020: import anvil.script.expression.Expression;
021: import anvil.script.expression.ExpressionList;
022: import anvil.script.parser.TemplateParser;
023: import anvil.script.parser.ExpressionParser;
024: import anvil.script.Grammar;
025: import anvil.util.Conversions;
026: import java.io.IOException;
027:
028: /**
029: * class PrintStatement
030: *
031: * @author: Jani Lehtimäki
032: */
033: public class PrintStatement extends Statement {
034:
035: protected Expression[] _expressions = null;
036: protected Converter _converters = new Converter();
037: protected int _conversions = 0;
038: protected boolean _newline = false;
039:
040: public PrintStatement(Statement parent, Location location) {
041: super (parent, location);
042: }
043:
044: public PrintStatement(Statement parent, Location location,
045: Expression[] expressions, boolean newline) {
046: super (parent, location);
047: _expressions = expressions;
048: _newline = newline;
049: }
050:
051: public PrintStatement(Statement parent, Location location,
052: Expression expr) {
053: super (parent, location);
054: _expressions = new Expression[] { expr };
055: }
056:
057: public int typeOf() {
058: return ST_PRINT;
059: }
060:
061: public String name() {
062: return "print";
063: }
064:
065: public String toString() {
066: StringBuffer buffer = new StringBuffer();
067: final int n = _expressions.length;
068: buffer.append(_newline ? "println " : "print");
069: for (int i = 0; i < n; i++) {
070: if (i > 0) {
071: buffer.append(", ");
072: }
073: buffer.append(_expressions[i].toString());
074: }
075: buffer.append(';');
076: return buffer.toString();
077: }
078:
079: public void parse(TemplateParser parser, Tag tag) {
080: String expr = tag.getValue("value");
081: if (expr != null) {
082: ExpressionParser p = new ExpressionParser(parser,
083: getLocation(), expr);
084: ExpressionList list = p.parseExpressionList();
085: int n = list.childs();
086: _expressions = new Expression[n];
087: for (int i = 0; i < n; i++) {
088: _expressions[i] = new Expression(list.getChild(i),
089: getLocation());
090: }
091: }
092:
093: int n = tag.getLength();
094: String name;
095: for (int i = 0; i < n; i++) {
096: name = tag.getName(i);
097: if (name.equalsIgnoreCase("quote")) {
098: _conversions++;
099: _converters = new HtmlConverter(_converters);
100: } else if (name.equalsIgnoreCase("meta")) {
101: _conversions++;
102: _converters = new MetaConverter(_converters);
103: } else if (name.equalsIgnoreCase("text")) {
104: _conversions++;
105: _converters = new TextConverter(_converters);
106: } else if (name.equalsIgnoreCase("nowrap")) {
107: _conversions++;
108: _converters = new NoWrapConverter(_converters);
109: } else if (name.equalsIgnoreCase("nl2br")) {
110: _conversions++;
111: _converters = new NewlineToBreakConverter(_converters);
112: } else if (name.equalsIgnoreCase("compress")) {
113: _conversions++;
114: _converters = new CompressConverter(_converters);
115: } else if (name.equalsIgnoreCase("caps")) {
116: _conversions++;
117: _converters = new CapitalizeConverter(_converters);
118: } else if (name.equalsIgnoreCase("capsfirst")) {
119: _conversions++;
120: _converters = new CapitalizeFirstConverter(_converters);
121: } else if (name.equalsIgnoreCase("encode")) {
122: _conversions++;
123: _converters = new URLEncodeConverter(_converters);
124: } else if (name.equalsIgnoreCase("decode")) {
125: _conversions++;
126: _converters = new URLDecodeConverter(_converters);
127: } else if (name.equalsIgnoreCase("uppercase")) {
128: _conversions++;
129: _converters = new UpperCaseConverter(_converters);
130: } else if (name.equalsIgnoreCase("lowercase")) {
131: _conversions++;
132: _converters = new LowerCaseConverter(_converters);
133: } else if (name.equalsIgnoreCase("trim")) {
134: _conversions++;
135: _converters = new TrimConverter(_converters);
136: }
137:
138: }
139: }
140:
141: public void check(ErrorListener context) {
142: if (_expressions != null) {
143: int n = _expressions.length;
144: for (int i = 0; i < n; i++) {
145: _expressions[i].check(context);
146: }
147: }
148: }
149:
150: public void compile(ByteCompiler context) {
151: Code code = context.getCode();
152: if (_expressions != null) {
153: Expression expr;
154: int n = _expressions.length;
155: if (n > 0) {
156: for (int i = 0; i < n; i++) {
157: expr = _expressions[i];
158: boolean newline = _newline && i == n - 1;
159:
160: if (expr.isConstant()) {
161: context.text(code, _converters.convert(expr
162: .constant().toString()), newline);
163: } else {
164: if (expr.needLineNumbers()) {
165: context.location(expr.getLocation());
166: }
167: code.aload_first();
168: if (_conversions == 0) {
169: expr.compile(context, Expression.GET);
170: code.invokevirtual(code.getPool()
171: .addMethodRef(
172: context.TYPE_CONTEXT,
173: newline ? "println"
174: : "print",
175: "(Lanvil/core/Any;)V"));
176: } else {
177: _converters.compile(context, expr);
178: code.invokevirtual(code.getPool()
179: .addMethodRef(
180: context.TYPE_CONTEXT,
181: newline ? "println"
182: : "print",
183: "(Ljava/lang/String;)V"));
184: }
185: }
186: }
187: } else if (_newline) {
188: code.aload_first();
189: code.invokevirtual(code.getPool().addMethodRef(
190: context.TYPE_CONTEXT, "println", "()V"));
191: }
192: }
193: }
194:
195: public void onCharacters(TemplateParser parser, String cdata) {
196: //ignore
197: }
198:
199: public boolean onTag(TemplateParser parser, int type, Tag tag) {
200: return false;
201: }
202:
203: protected class Converter {
204: protected Converter _converter;
205:
206: protected Converter() {
207: _converter = null;
208: }
209:
210: protected Converter(Converter converter) {
211: _converter = converter;
212: }
213:
214: protected String convert(String value) {
215: return value;
216: }
217:
218: protected void compile(ByteCompiler context, Expression expr) {
219: expr.compile(context, Node.GET);
220: context.getCode().invokevirtual(
221: context.getPool().addMethodRef("java/lang/Object",
222: "toString", "()Ljava/lang/String;"));
223: }
224: }
225:
226: protected class HtmlConverter extends Converter {
227:
228: protected HtmlConverter(Converter converter) {
229: super (converter);
230: }
231:
232: protected String convert(String value) {
233: return Conversions
234: .encodeEntities(_converter.convert(value));
235: }
236:
237: protected void compile(ByteCompiler context, Expression expr) {
238: _converter.compile(context, expr);
239: context.getCode().invokestatic(
240: context.getPool().addMethodRef(
241: "anvil/util/Conversions", "encodeEntities",
242: "(Ljava/lang/String;)Ljava/lang/String;"));
243: }
244:
245: }
246:
247: protected class MetaConverter extends Converter {
248:
249: protected MetaConverter(Converter converter) {
250: super (converter);
251: }
252:
253: protected String convert(String value) {
254: return Conversions.encodeMetaEntities(_converter
255: .convert(value));
256: }
257:
258: protected void compile(ByteCompiler context, Expression expr) {
259: _converter.compile(context, expr);
260: context.getCode().invokestatic(
261: context.getPool().addMethodRef(
262: "anvil/util/Conversions",
263: "encodeMetaEntities",
264: "(Ljava/lang/String;)Ljava/lang/String;"));
265: }
266:
267: }
268:
269: protected class TextConverter extends Converter {
270:
271: protected TextConverter(Converter converter) {
272: super (converter);
273: }
274:
275: protected String convert(String value) {
276: return Conversions.encodeText(_converter.convert(value));
277: }
278:
279: protected void compile(ByteCompiler context, Expression expr) {
280: _converter.compile(context, expr);
281: context.getCode().invokestatic(
282: context.getPool().addMethodRef(
283: "anvil/util/Conversions", "encodeText",
284: "(Ljava/lang/String;)Ljava/lang/String;"));
285: }
286:
287: }
288:
289: protected class NoWrapConverter extends Converter {
290:
291: protected NoWrapConverter(Converter converter) {
292: super (converter);
293: }
294:
295: protected String convert(String value) {
296: return Conversions.nowrap(_converter.convert(value));
297: }
298:
299: protected void compile(ByteCompiler context, Expression expr) {
300: _converter.compile(context, expr);
301: context.getCode().invokestatic(
302: context.getPool().addMethodRef(
303: "anvil/util/Conversions", "nowrap",
304: "(Ljava/lang/String;)Ljava/lang/String;"));
305: }
306: }
307:
308: protected class NewlineToBreakConverter extends Converter {
309:
310: protected NewlineToBreakConverter(Converter converter) {
311: super (converter);
312: }
313:
314: protected String convert(String value) {
315: return Conversions.nl2br(_converter.convert(value));
316: }
317:
318: protected void compile(ByteCompiler context, Expression expr) {
319: _converter.compile(context, expr);
320: context.getCode().invokestatic(
321: context.getPool().addMethodRef(
322: "anvil/util/Conversions", "nl2br",
323: "(Ljava/lang/String;)Ljava/lang/String;"));
324: }
325: }
326:
327: protected class CompressConverter extends Converter {
328:
329: protected CompressConverter(Converter converter) {
330: super (converter);
331: }
332:
333: protected String convert(String value) {
334: return Conversions.compress(_converter.convert(value));
335: }
336:
337: protected void compile(ByteCompiler context, Expression expr) {
338: _converter.compile(context, expr);
339: context.getCode().invokestatic(
340: context.getPool().addMethodRef(
341: "anvil/util/Conversions", "compress",
342: "(Ljava/lang/String;)Ljava/lang/String;"));
343: }
344: }
345:
346: protected class CapitalizeConverter extends Converter {
347:
348: protected CapitalizeConverter(Converter converter) {
349: super (converter);
350: }
351:
352: protected String convert(String value) {
353: return Conversions.capitalizeAllWords(_converter
354: .convert(value));
355: }
356:
357: protected void compile(ByteCompiler context, Expression expr) {
358: _converter.compile(context, expr);
359: context.getCode().invokestatic(
360: context.getPool().addMethodRef(
361: "anvil/util/Conversions",
362: "capitalizeAllWords",
363: "(Ljava/lang/String;)Ljava/lang/String;"));
364: }
365: }
366:
367: protected class CapitalizeFirstConverter extends Converter {
368:
369: protected CapitalizeFirstConverter(Converter converter) {
370: super (converter);
371: }
372:
373: protected String convert(String value) {
374: return Conversions.capitalizeFirstWord(_converter
375: .convert(value));
376: }
377:
378: protected void compile(ByteCompiler context, Expression expr) {
379: _converter.compile(context, expr);
380: context.getCode().invokestatic(
381: context.getPool().addMethodRef(
382: "anvil/util/Conversions",
383: "capitalizeFirstWord",
384: "(Ljava/lang/String;)Ljava/lang/String;"));
385: }
386:
387: }
388:
389: protected class URLEncodeConverter extends Converter {
390:
391: protected URLEncodeConverter(Converter converter) {
392: super (converter);
393: }
394:
395: protected String convert(String value) {
396: return Conversions.URLEncode(_converter.convert(value));
397: }
398:
399: protected void compile(ByteCompiler context, Expression expr) {
400: _converter.compile(context, expr);
401: context.getCode().invokestatic(
402: context.getPool().addMethodRef(
403: "anvil/util/Conversions", "URLEncode",
404: "(Ljava/lang/String;)Ljava/lang/String;"));
405: }
406:
407: }
408:
409: protected class URLDecodeConverter extends Converter {
410:
411: protected URLDecodeConverter(Converter converter) {
412: super (converter);
413: }
414:
415: protected String convert(String value) {
416: return Conversions.URLDecode(_converter.convert(value));
417: }
418:
419: protected void compile(ByteCompiler context, Expression expr) {
420: _converter.compile(context, expr);
421: context.getCode().invokestatic(
422: context.getPool().addMethodRef(
423: "anvil/util/Conversions", "URLDecode",
424: "(Ljava/lang/String;)Ljava/lang/String;"));
425: }
426: }
427:
428: protected class UpperCaseConverter extends Converter {
429:
430: protected UpperCaseConverter(Converter converter) {
431: super (converter);
432: }
433:
434: protected String convert(String value) {
435: return _converter.convert(value).toUpperCase();
436: }
437:
438: protected void compile(ByteCompiler context, Expression expr) {
439: _converter.compile(context, expr);
440: context.getCode().invokevirtual(
441: context.getPool().addMethodRef("java/lang/String",
442: "toUpperCase", "()Ljava/lang/String;"));
443: }
444: }
445:
446: protected class LowerCaseConverter extends Converter {
447:
448: protected LowerCaseConverter(Converter converter) {
449: super (converter);
450: }
451:
452: protected String convert(String value) {
453: return _converter.convert(value).toLowerCase();
454: }
455:
456: protected void compile(ByteCompiler context, Expression expr) {
457: _converter.compile(context, expr);
458: context.getCode().invokevirtual(
459: context.getPool().addMethodRef("java/lang/String",
460: "toLowerCase", "()Ljava/lang/String;"));
461: }
462: }
463:
464: protected class TrimConverter extends Converter {
465:
466: protected TrimConverter(Converter converter) {
467: super (converter);
468: }
469:
470: protected String convert(String value) {
471: return _converter.convert(value).trim();
472: }
473:
474: protected void compile(ByteCompiler context, Expression expr) {
475: _converter.compile(context, expr);
476: context.getCode().invokevirtual(
477: context.getPool().addMethodRef("java/lang/String",
478: "trim", "()Ljava/lang/String;"));
479: }
480:
481: }
482:
483: }
|