Package anvil.script.statements

Source Code of anvil.script.statements.PrintStatement

/*
* $Id: PrintStatement.java,v 1.19 2002/09/16 08:05:06 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.script.statements;

import anvil.core.Any;
import anvil.Location;
import anvil.parser.Tag;
import anvil.codec.Code;
import anvil.ErrorListener;
import anvil.script.compiler.ByteCompiler;
import anvil.script.Context;
import anvil.script.expression.Node;
import anvil.script.expression.Expression;
import anvil.script.expression.ExpressionList;
import anvil.script.parser.TemplateParser;
import anvil.script.parser.ExpressionParser;
import anvil.script.Grammar;
import anvil.util.Conversions;
import java.io.IOException;

/**
* class PrintStatement
*
* @author: Jani Lehtim�ki
*/
public class PrintStatement extends Statement
{
  protected Expression[] _expressions = null;
  protected Converter _converters = new Converter();
  protected int _conversions = 0;
  protected boolean _newline = false;


  public PrintStatement(Statement parent, Location location)
  {
    super(parent, location);
  }


  public PrintStatement(Statement parent, Location location, Expression[] expressions, boolean newline)
  {
    super(parent, location);
    _expressions = expressions;
    _newline = newline;
  }


  public PrintStatement(Statement parent, Location location, Expression expr)
  {
    super(parent, location);
    _expressions = new Expression[] { expr };
  }
 

  public int typeOf()
  {
    return ST_PRINT;
  }


  public String name()
  {
    return "print";
  }


  public String toString()
  {
    StringBuffer buffer = new StringBuffer();
    final int n = _expressions.length;
    buffer.append(_newline ? "println " : "print");
    for(int i=0; i<n; i++)  {
      if (i>0) {
        buffer.append(", ");
      }
      buffer.append(_expressions[i].toString());
    }
    buffer.append(';');
    return buffer.toString();
  }




  public void parse(TemplateParser parser, Tag tag)
  {
    String expr = tag.getValue("value");
    if (expr != null) {
      ExpressionParser p = new ExpressionParser(parser, getLocation(), expr);
      ExpressionList list = p.parseExpressionList();
      int n = list.childs();
      _expressions = new Expression[n];
      for(int i=0; i<n; i++) {
        _expressions[i] = new Expression(list.getChild(i), getLocation());
      }
    }

    int n = tag.getLength();
    String name;
    for(int i=0; i<n; i++) {
      name = tag.getName(i);
      if (name.equalsIgnoreCase("quote")) {
        _conversions++;
        _converters = new HtmlConverter(_converters);
      } else if (name.equalsIgnoreCase("meta")) {
        _conversions++;
        _converters = new MetaConverter(_converters);
      } else if (name.equalsIgnoreCase("text")) {
        _conversions++;
        _converters = new TextConverter(_converters);
      } else if (name.equalsIgnoreCase("nowrap")) {
        _conversions++;
        _converters = new NoWrapConverter(_converters);
      } else if (name.equalsIgnoreCase("nl2br")) {
        _conversions++;
        _converters = new NewlineToBreakConverter(_converters);
      } else if (name.equalsIgnoreCase("compress")) {
        _conversions++;
        _converters = new CompressConverter(_converters);
      } else if (name.equalsIgnoreCase("caps")) {
        _conversions++;
        _converters = new CapitalizeConverter(_converters);
      } else if (name.equalsIgnoreCase("capsfirst")) {
        _conversions++;
        _converters = new CapitalizeFirstConverter(_converters);
      } else if (name.equalsIgnoreCase("encode")) {
        _conversions++;
        _converters = new URLEncodeConverter(_converters);
      } else if (name.equalsIgnoreCase("decode")) {
        _conversions++;
        _converters = new URLDecodeConverter(_converters);
      } else if (name.equalsIgnoreCase("uppercase")) {
        _conversions++;
        _converters = new UpperCaseConverter(_converters);
      } else if (name.equalsIgnoreCase("lowercase")) {
        _conversions++;
        _converters = new LowerCaseConverter(_converters);
      } else if (name.equalsIgnoreCase("trim")) {
        _conversions++;
        _converters = new TrimConverter(_converters);
      }

    }
  }


  public void check(ErrorListener context)
  {
    if (_expressions != null) {
      int n = _expressions.length;
      for(int i=0; i<n; i++) {
        _expressions[i].check(context);
      }
    }
  }


  public void compile(ByteCompiler context)
  {
    Code code = context.getCode();
    if (_expressions != null) {
      Expression expr;
      int n = _expressions.length;
      if (n>0) {
        for(int i=0; i<n; i++) {
          expr = _expressions[i];
          boolean newline = _newline && i==n-1;

          if (expr.isConstant()) {
            context.text(code, _converters.convert(expr.constant().toString()), newline);
          } else {
            if (expr.needLineNumbers()) {
              context.location(expr.getLocation());     
            }
            code.aload_first();
            if (_conversions == 0) {
              expr.compile(context, Expression.GET);
              code.invokevirtual(code.getPool().addMethodRef(context.TYPE_CONTEXT,
                newline ? "println" : "print" , "(Lanvil/core/Any;)V"));
            } else {
              _converters.compile(context, expr);
              code.invokevirtual(code.getPool().addMethodRef(context.TYPE_CONTEXT,
                newline ? "println" : "print" , "(Ljava/lang/String;)V"));
            }
          }
        }
      } else if (_newline) {
        code.aload_first();
        code.invokevirtual(code.getPool().addMethodRef(context.TYPE_CONTEXT, "println", "()V"));
      }
    }
  }


  public void onCharacters(TemplateParser parser, String cdata)
  {
    //ignore
  }
 

  public boolean onTag(TemplateParser parser, int type, Tag tag)
  {
    return false;
  }


  protected class Converter {
    protected Converter _converter;

    protected Converter()
    {
      _converter = null;
    }
   
    protected Converter(Converter converter)
    {
      _converter = converter;
    }
   
    protected String convert(String value)
    {
      return value;
    }
   
    protected void compile(ByteCompiler context, Expression expr)
    {
      expr.compile(context, Node.GET);
      context.getCode().invokevirtual(context.getPool().addMethodRef(
        "java/lang/Object", "toString", "()Ljava/lang/String;"));
    }
  }
 
  protected class HtmlConverter extends Converter {
 
    protected HtmlConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.encodeEntities(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "encodeEntities", "(Ljava/lang/String;)Ljava/lang/String;"));
    }

  }

  protected class MetaConverter extends Converter {
 
    protected MetaConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.encodeMetaEntities(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "encodeMetaEntities", "(Ljava/lang/String;)Ljava/lang/String;"));
    }

  }

  protected class TextConverter extends Converter {
 
    protected TextConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.encodeText(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "encodeText", "(Ljava/lang/String;)Ljava/lang/String;"));
    }

  }
 
  protected class NoWrapConverter extends Converter {
 
    protected NoWrapConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.nowrap(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "nowrap", "(Ljava/lang/String;)Ljava/lang/String;"));
    }
  }

  protected class NewlineToBreakConverter extends Converter {
 
    protected NewlineToBreakConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.nl2br(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "nl2br", "(Ljava/lang/String;)Ljava/lang/String;"));
    }   
  }

  protected class CompressConverter extends Converter {
 
    protected CompressConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.compress(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "compress", "(Ljava/lang/String;)Ljava/lang/String;"));
    }     
  }
 
  protected class CapitalizeConverter extends Converter {
 
    protected CapitalizeConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.capitalizeAllWords(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "capitalizeAllWords", "(Ljava/lang/String;)Ljava/lang/String;"));
    }  
  } 
 
  protected class CapitalizeFirstConverter extends Converter {
 
    protected CapitalizeFirstConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.capitalizeFirstWord(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "capitalizeFirstWord", "(Ljava/lang/String;)Ljava/lang/String;"));
    }  
   
  } 

  protected class URLEncodeConverter extends Converter {
 
    protected URLEncodeConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.URLEncode(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "URLEncode", "(Ljava/lang/String;)Ljava/lang/String;"));
    }  
   
  } 

  protected class URLDecodeConverter extends Converter {
 
    protected URLDecodeConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return Conversions.URLDecode(_converter.convert(value));
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokestatic(context.getPool().addMethodRef(
        "anvil/util/Conversions", "URLDecode", "(Ljava/lang/String;)Ljava/lang/String;"));
    }  
  } 

  protected class UpperCaseConverter extends Converter {
 
    protected UpperCaseConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return _converter.convert(value).toUpperCase();
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokevirtual(context.getPool().addMethodRef(
        "java/lang/String", "toUpperCase", "()Ljava/lang/String;"));
   
  } 

  protected class LowerCaseConverter extends Converter {
 
    protected LowerCaseConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return _converter.convert(value).toLowerCase();
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokevirtual(context.getPool().addMethodRef(
        "java/lang/String", "toLowerCase", "()Ljava/lang/String;"));
    }   
  } 


  protected class TrimConverter extends Converter {
 
    protected TrimConverter(Converter converter)
    {
      super(converter);
    }
   
    protected String convert(String value)
    {
      return _converter.convert(value).trim();
    }

    protected void compile(ByteCompiler context, Expression expr)
    {
      _converter.compile(context, expr);
      context.getCode().invokevirtual(context.getPool().addMethodRef(
        "java/lang/String", "trim", "()Ljava/lang/String;"));
    }

  } 


}

  
TOP

Related Classes of anvil.script.statements.PrintStatement

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.