jsilver-1.0.0

xception
  {
    Appendable output = new StringBuilder(8192);
    renderFromContent(content, data, output);
    return output.toString();
  }
  
  private String loadResource(String filename, ResourceLoader resourceLoader)
    throws IOException
  {
    Reader reader = resourceLoader.open(filename);
    if (reader == null) {
      throw new FileNotFoundException(filename);
    }
    StringBuilder sb = new StringBuilder();
    char[] buf = new char['?'];
    int count;
    while ((count = reader.read(buf)) != -1) {
      sb.append(buf, 0, count);
    }
    return sb.toString();
  }
}

/* Location:
 * Qualified Name:     com.google.clearsilver.jsilver.compatibility.ClearsilverRenderer
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.google.clearsilver.jsilver.compatibility;

import com.google.clearsilver.jsilver.resourceloader.ResourceLoader;
import java.io.IOException;
import org.clearsilver.CSFileLoader;
import org.clearsilver.HDF;

class ClearsilverRenderer$1
  implements CSFileLoader
{
  ClearsilverRenderer$1(ClearsilverRenderer paramClearsilverRenderer, ResourceLoader paramResourceLoader) {}
  
  public String load(HDF hdf, String filename)
    throws IOException
  {
    return ClearsilverRenderer.access$000(this$0, filename, val$resourceLoader);
  }
}

/* Location:
 * Qualified Name:     com.google.clearsilver.jsilver.compatibility.ClearsilverRenderer.1
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.google.clearsilver.jsilver.compiler;

import com.google.clearsilver.jsilver.autoescape.AutoEscapeOptions;
import com.google.clearsilver.jsilver.autoescape.EscapeMode;
import com.google.clearsilver.jsilver.functions.FunctionExecutor;
import com.google.clearsilver.jsilver.interpreter.TemplateFactory;
import com.google.clearsilver.jsilver.resourceloader.ResourceLoader;
import com.google.clearsilver.jsilver.syntax.TemplateSyntaxTree;
import com.google.clearsilver.jsilver.template.DelegatingTemplateLoader;
import com.google.clearsilver.jsilver.template.Template;
import com.google.clearsilver.jsilver.template.TemplateLoader;
import java.io.StringWriter;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject;

public class TemplateCompiler
  implements DelegatingTemplateLoader
{
  private static final Logger logger = Logger.getLogger(TemplateCompiler.class.getName());
  private static final String PACKAGE_NAME = "com.google.clearsilver.jsilver.compiler";
  private static final String CLASS_NAME = "$CompiledTemplate";
  private final TemplateFactory templateFactory;
  private final FunctionExecutor globalFunctionExecutor;
  private final AutoEscapeOptions autoEscapeOptions;
  private TemplateLoader templateLoaderDelegate = this;
  
  public TemplateCompiler(TemplateFactory templateFactory, FunctionExecutor globalFunctionExecutor, AutoEscapeOptions autoEscapeOptions)
  {
    this.templateFactory = templateFactory;
    this.globalFunctionExecutor = globalFunctionExecutor;
    this.autoEscapeOptions = autoEscapeOptions;
  }
  
  public void setTemplateLoaderDelegate(TemplateLoader templateLoaderDelegate)
  {
    this.templateLoaderDelegate = templateLoaderDelegate;
  }
  
  public Template load(String templateName, ResourceLoader resourceLoader, EscapeMode escapeMode)
  {
    return compile(templateFactory.find(templateName, resourceLoader, escapeMode), templateName, escapeMode);
  }
  
  public Template createTemp(String name, String content, EscapeMode escapeMode)
  {
    return compile(templateFactory.createTemp(content, escapeMode), name, escapeMode);
  }
  
  private Template compile(TemplateSyntaxTree ast, String templateName, EscapeMode mode)
  {
    CharSequence javaSource = translateAstToJavaSource(ast, mode);
    
    String errorMessage = "Could not compile template: " + templateName;
    Class<?> templateClass = compileAndLoad(javaSource, errorMessage);
    try
    {
      BaseCompiledTemplate compiledTemplate = (BaseCompiledTemplate)templateClass.newInstance();
      compiledTemplate.setFunctionExecutor(globalFunctionExecutor);
      compiledTemplate.setTemplateName(templateName);
      compiledTemplate.setTemplateLoader(templateLoaderDelegate);
      compiledTemplate.setEscapeMode(mode);
      compiledTemplate.setAutoEscapeOptions(autoEscapeOptions);
      return compiledTemplate;
    }
    catch (InstantiationException e)
    {
      throw new Error(e);
    }
    catch (IllegalAccessException e)
    {
      throw new Error(e);
    }
  }
  
  private CharSequence translateAstToJavaSource(TemplateSyntaxTree ast, EscapeMode mode)
  {
    StringWriter sourceBuffer = new StringWriter(256);
    boolean propagateStatus = (autoEscapeOptions.getPropagateEscapeStatus()) && (mode.isAutoEscapingMode());
    
    ast.apply(new TemplateTranslator("com.google.clearsilver.jsilver.compiler", "$CompiledTemplate", sourceBuffer, propagateStatus));
    StringBuffer javaSource = sourceBuffer.getBuffer();
    logger.log(Level.FINEST, "Compiled template:\n{0}", javaSource);
    return javaSource;
  }
  
  private Class<?> compileAndLoad(CharSequence javaSource, String errorMessage)
    throws JSilverCompilationException
  {
    ClassLoader parentClassLoader = getClass().getClassLoader();
    
    DiagnosticCollector<JavaFileObject> diagnosticCollector = new DiagnosticCollector();
    try
    {
      CompilingClassLoader templateClassLoader = new CompilingClassLoader(parentClassLoader, "$CompiledTemplate", javaSource, diagnosticCollector);
      
      return templateClassLoader.loadClass("com.google.clearsilver.jsilver.compiler.$CompiledTemplate");
    }
    catch (Exception e)
    {
      throwExceptionWithLotsOfDiagnosticInfo(javaSource, errorMessage, diagnosticCollector.getDiagnostics(), e);
    }
    return null;
  }
  
  private void throwExceptionWithLotsOfDiagnosticInfo(CharSequence javaSource, String errorMessage, List<Diagnostic<? extends JavaFileObject>> diagnostics, Exception cause)
    throws JSilverCompilationException
  {
    StringBuilder message = new StringBuilder(errorMessage).append('\n');
    message.append("------ Source code ------\n").append(javaSource);
    message.append("------ Compiler messages ------\n");
    for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
      message.append(diagnostic).append('\n');
    }
    message.append("------ ------\n");
    throw new JSilverCompilationException(message.toString(), cause);
  }
}

/* Location:
 * Qualified Name:     com.google.clearsilver.jsilver.compiler.TemplateCompiler
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.google.clearsilver.jsilver.compiler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Map;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileManager.Location;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;

class CompilingClassLoader$InMemoryFileManager
  extends ForwardingJavaFileManager<JavaFileManager>
{
  public CompilingClassLoader$InMemoryFileManager(CompilingClassLoader paramCompilingClassLoader, JavaFileManager fileManager)
  {
    super(fileManager);
  }
  
  public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, final String className, JavaFileObject.Kind kind, FileObject sibling)
    throws IOException
  {
    new SimpleJavaFileObject(CompilingClassLoader.access$000(), kind)
    {
      public OutputStream openOutputStream()
        throws IOException
      {
        ByteArrayOutputStream outputStream = (ByteArrayOutputStream)CompilingClassLoader.access$100(this$0).get(className);
        if (outputStream != null) {
          throw new IllegalStateException("Cannot write more than once");
        }
        outputStream = new ByteArrayOutputStream(256);
        CompilingClassLoader.access$100(this$0).put(className, outputStream);
        return outputStream;
      }
    };
  }
}

/* Location:
 * Qualified Name:     com.google.clearsilver.jsilver.compiler.CompilingClassLoader.InMemoryFileManager
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.google.clearsilver.jsilver.compiler;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.tools.DiagnosticListener;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileManager.Location;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;

public class CompilingClassLoader
  extends ClassLoader
{
  public static class CompilerException
    extends Exception
  {
    public CompilerException(String message)
    {
      super();
    }
  }
  
  private Map<String, ByteArrayOutputStream> byteCodeForClasses = new HashMap();
  private static final URI EMPTY_URI;
  
  static
  {
    try
    {
      EMPTY_URI = new URI("");
    }
    catch (URISyntaxException e)
    {
      throw new Error(e);
    }
  }
  
  public CompilingClassLoader(ClassLoader parent, String className, CharSequence sourceCode, DiagnosticListener<JavaFileObject> diagnosticListener)
    throws CompilingClassLoader.CompilerException
  {
    super(parent);
    if (!compileSourceCodeToByteCode(className, sourceCode, diagnosticListener)) {
      throw new CompilerException("Could not compile " + className);
    }
  }
  
  public Class findClass(String name)
    throws ClassNotFoundException
  {
    ByteArrayOutputStream byteCode = (ByteArrayOutputStream)byteCodeForClasses.get(name);
    if (byteCode == null) {
      throw new ClassNotFoundException(name);
    }
    return defineClass(name, byteCode.toByteArray(), 0, byteCode.size());
  }
  
  private boolean compileSourceCodeToByteCode(String className, CharSequence sourceCode, DiagnosticListener<JavaFileObject> diagnosticListener)
  {
    JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
    
    InMemoryFileManager fileManager = new InMemoryFileManager(javaCompiler.getStandardFileManager(null, null, null));
    
    JavaFileObject javaFile = new InMemoryJavaFile(className, sourceCode);
    
    System.setProperty("useJavaUtilZip", "true");
    List<String> options = new LinkedList();
    
    options.add("-XDuseJavaUtilZip");
    
    JavaCompiler.CompilationTask compilationTask = javaCompiler.getTask(null, fileManager, diagnosticListener, options, null, Collections.singleton(javaFile));
    
    return compilationTask.call().booleanValue();
  }
  
  private class InMemoryFileManager
    extends ForwardingJavaFileManager<JavaFileManager>
  {
    public InMemoryFileManager(JavaFileManager fileManager)
    {
      super();
    }
    
    public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, final String className, JavaFileObject.Kind kind, FileObject sibling)
      throws IOException
    {
      new SimpleJavaFileObject(CompilingClassLoader.EMPTY_URI, kind)
      {
        public OutputStream openOutputStream()
          throws IOException
        {
          ByteArrayOutputStream outputStream = (ByteArrayOutputStream)byteCodeForClasses.get(className);
          if (outputStream != null) {
            throw new IllegalStateException("Cannot write more than once");
          }
          outputStream = new ByteArrayOutputStream(256);
          byteCodeForClasses.put(className, outputStream);
          return outputStream;
        }
      };
    }
  }
  
  private static class InMemoryJavaFile
    extends SimpleJavaFileObject
  {
    private final CharSequence sourceCode;
    
    public InMemoryJavaFile(String className, CharSequence sourceCode)
    {
      super(JavaFileObject.Kind.SOURCE);
      this.sourceCode = sourceCode;
    }
    
    private static URI makeUri(String className)
    {
      try
      {
        return new URI(className.replaceAll("\\.", "/") + SOURCEextension);
      }
      catch (URISyntaxException e)
      {
        throw new RuntimeException(e);
      }
    }
    
    public CharSequence getCharContent(boolean ignoreEncodingErrors)
      throws IOException
    {
      return sourceCode;
    }
  }
}

/* Location:
 * Qualified Name:     com.google.clearsilver.jsilver.compiler.CompilingClassLoader
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.google.clearsilver.jsilver.compiler;

import com.google.clearsilver.jsilver.syntax.node.ADefCommand;

class TemplateTranslator$MacroInfo
{
  JavaExpression symbol;
  ADefCommand defNode;
}

/* Location:
 * Qualified Name:     com.google.clearsilver.jsilver.compiler.TemplateTranslator.MacroInfo
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.google.clearsilver.jsilver.compiler;

 enum JavaExpression$Type$3
{
  JavaExpression$Type$3(String x0)
  {
    super(paramString, paramInt, x0, null);
  }
  
  protected JavaExpression cast(JavaExpression expression)
  {
    if (expression.getType() == VAR_NAME) {
      expression = expression.cast(DATA);
    }
    return JavaExpression.call(JavaExpression.Type.BOOLEAN, "asBoolean", new JavaExpression[] { expression });
  }
}

/* Location:
 * Qualified Name:     com.google.clearsilver.jsilver.compiler.JavaExpression.Type.3
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.google.clearsilver.jsilver.compiler;

 enum JavaExpression$Type$8
{
  JavaExpression$Type$8(String x0)
  {
    super(paramString, paramInt, x0, null);
  }
  
  protected JavaExpression cast(JavaExpression expression)
  {
    throw new JSilverCompilationException("Cannot cast to 'Macro' for expression:\n" + expression.toString());
  }
}

/* Location:
 * Qualified Name:     com.google.clearsilver.jsilver.compiler.JavaExpression.Type.8
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.google.clearsilver.jsilver.compiler;

import com.google.clearsilver.jsilver.syntax.analysis.DepthFirstAdapter;
import com.google.clearsilver.jsilver.syntax.node.AAddExpression;
import com.google.clearsilver.jsilver.syntax.node.AAndExpression;
import com.google.clearsilver.jsilver.syntax.node.ADecimalExpression;
import com.google.clearsilver.jsilver.syntax.node.ADescendVariable;
import com.google.clearsilver.jsilver.syntax.node.ADivideExpression;
import com.google.clearsilver.jsilver.syntax.node.AEqExpression;
import com.google.clearsilver.jsilver.syntax.node.AExistsExpression;
import com.google.clearsilver.jsilver.syntax.node.AFunctionExpression;
import com.google.clearsilver.jsilver.syntax.node.AGtExpression;
import com.google.clearsilver.jsilver.syntax.node.AGteExpression;
import com.google.clearsilver.jsilver.syntax.node.AHexExpression;
import com.google.clearsilver.jsilver.syntax.node.ALtExpression;
import com.google.clearsilver.jsilver.syntax.node.ALteExpression;
import com.google.clearsilver.jsilver.syntax.node.AModuloExpression;
import com.google.clearsilver.jsilver.syntax.node.AMultiplyExpression;
import com.google.clearsilver.jsilver.syntax.node.ANameVariable;
import com.google.clearsilver.jsilver.syntax.node.ANeExpression;
import com.google.clearsilver.jsilver.syntax.node.ANegativeExpression;
import com.google.clearsilver.jsilver.syntax.node.ANotExpression;
import com.google.clearsilver.jsilver.syntax.node.ANumericAddExpression;
import com.google.clearsilver.jsilver.syntax.node.ANumericEqExpression;
import com.google.clearsilver.jsilver.syntax.node.ANumericExpression;
import com.google.clearsilver.jsilver.syntax.node.ANumericNeExpression;
import com.google.clearsilver.jsilver.syntax.node.AOrExpression;
import com.google.clearsilver.jsilver.syntax.node.AStringExpression;
import com.google.clearsilver.jsilver.syntax.node.ASubtractExpression;
import com.google.clearsilver.jsilver.syntax.node.AVariableExpression;
import com.google.clearsilver.jsilver.syntax.node.PExpression;
import com.google.clearsilver.jsilver.syntax.node.PVariable;
import com.google.clearsilver.jsilver.syntax.node.TWord;
import java.util.LinkedList;

public class EscapingEvaluator
  extends DepthFirstAdapter
{
  private JavaExpression currentEscapingExpression;
  private boolean propagateEscapeStatus;
  private final VariableTranslator variableTranslator;
  
  public EscapingEvaluator(VariableTranslator variableTranslator)
  {
    this.variableTranslator = variableTranslator;
  }
  
  public JavaExpression computeIfExemptFromEscaping(PExpression expression, boolean propagateEscapeStatus)
  {
    if (propagateEscapeStatus) {
      return computeForPropagateStatus(expression);
    }
    return computeEscaping(expression, propagateEscapeStatus);
  }
  
  private JavaExpression computeForPropagateStatus(PExpression expression)
  {
    JavaExpression escapeMode = computeEscaping(expression, true);
    JavaExpression partiallyEscaped = computeEscaping(expression, false);
    
    JavaExpression escapeModeCheck = JavaExpression.infix(JavaExpression.Type.BOOLEAN, "!=", escapeMode, JavaExpression.symbol("EscapeMode.ESCAPE_NONE"));
    
    return JavaExpression.infix(JavaExpression.Type.BOOLEAN, "||", escapeModeCheck, partiallyEscaped);
  }
  
  public JavaExpression computeEscaping(PExpression expression, boolean propagateEscapeStatus)
  {
    try
    {
      assert (currentEscapingExpression == null) : "Not reentrant";
      this.propagateEscapeStatus = propagateEscapeStatus;
      expression.apply(this);
      assert (currentEscapingExpression != null) : "No escaping calculated";
      return currentEscapingExpression;
    }
    finally
    {
      currentEscapingExpression = null;
    }
  }
  
  private void setEscaping(JavaExpression escaping)
  {
    currentEscapingExpression = escaping;
  }
  
  public void caseAAddExpression(AAddExpression node)
  {
    node.getLeft().apply(this);
    JavaExpression left = currentEscapingExpression;
    node.getRight().apply(this);
    JavaExpression right = currentEscapingExpression;
    
    setEscaping(or(left, right));
  }
  
  public void caseAFunctionExpression(AFunctionExpression node)
  {
    LinkedList<PExpression> argsList = node.getArgs();
    PExpression[] args = (PExpression[])argsList.toArray(new PExpression[argsList.size()]);
    
    final StringBuilder fullFunctionName = new StringBuilder();
    node.getName().apply(new DepthFirstAdapter()
    {
      public void caseANameVariable(ANameVariable node11)
      {
        fullFunctionName.append(node11.getWord().getText());
      }
      
      public void caseADescendVariable(ADescendVariable node12)
      {
        node12.getParent().apply(this);
        fullFunctionName.append('.');
        node12.getChild().apply(this);
      }
    });
    setEscaping(function(fullFunctionName.toString(), args));
  }
  
  private JavaExpression function(String name, PExpression... csExpressions)
  {
    if (propagateEscapeStatus) {
      return JavaExpression.inlineIf(JavaExpression.Type.UNKNOWN, JavaExpression.callOn(JavaExpression.Type.BOOLEAN, TemplateTranslator.CONTEXT, "isEscapingFunction", new JavaExpression[] { JavaExpression.string(name) }), JavaExpression.symbol("EscapeMode.ESCAPE_IS_CONSTANT"), JavaExpression.symbol("EscapeMode.ESCAPE_NONE"));
    }
    JavaExpression finalExpression = JavaExpression.BooleanLiteralExpression.FALSE;
    for (int i = 0; i < csExpressions.length; i++)
    {
      csExpressions[i].apply(this);
      finalExpression = or(finalExpression, currentEscapingExpression);
    }
    JavaExpression funcExpr = JavaExpression.callOn(JavaExpression.Type.BOOLEAN, TemplateTranslator.CONTEXT, "isEscapingFunction", new JavaExpression[] { JavaExpression.string(name) });
    
    return or(finalExpression, funcExpr);
  }
  
  private JavaExpression or(JavaExpression first, JavaExpression second)
  {
    if (propagateEscapeStatus) {
      return JavaExpression.callOn(JavaExpression.symbol("EscapeMode"), "combineModes", new JavaExpression[] { first, second });
    }
    if ((first instanceof JavaExpression.BooleanLiteralExpression))
    {
      JavaExpression.BooleanLiteralExpression expr = (JavaExpression.BooleanLiteralExpression)first;
      if (expr.getValue()) {
        return expr;
      }
      return second;
    }
    if ((second instanceof JavaExpression.BooleanLiteralExpression))
    {
      JavaExpression.BooleanLiteralExpression expr = (JavaExpression.BooleanLiteralExpression)second;
      if (expr.getValue()) {
        return expr;
      }
      return first;
    }
    return JavaExpression.infix(JavaExpression.Type.BOOLEAN, "||", first, second);
  }
  
  public void caseAVariableExpression(AVariableExpression node)
  {
    if (propagateEscapeStatus)
    {
      JavaExpression varName = variableTranslator.translate(node.getVariable());
      setEscaping(JavaExpression.callOn(TemplateTranslator.DATA_CONTEXT, "findVariableEscapeMode", new JavaExpression[] { varName }));
    }
    else
    {
      setDefaultEscaping();
    }
  }
  
  private void setDefaultEscaping()
  {
    if (propagateEscapeStatus) {
      setEscaping(JavaExpression.symbol("EscapeMode.ESCAPE_IS_CONSTANT"));
    } else {
      setEscaping(JavaExpression.BooleanLiteralExpression.FALSE);
    }
  }
  
  public void caseAStringExpression(AStringExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseADecimalExpression(ADecimalExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseAHexExpression(AHexExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseANumericExpression(ANumericExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseANotExpression(ANotExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseAExistsExpression(AExistsExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseAEqExpression(AEqExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseANumericEqExpression(ANumericEqExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseANeExpression(ANeExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseANumericNeExpression(ANumericNeExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseALtExpression(ALtExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseAGtExpression(AGtExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseALteExpression(ALteExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseAGteExpression(AGteExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseAAndExpression(AAndExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseAOrExpression(AOrExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseANumericAddExpression(ANumericAddExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseASubtractExpression(ASubtractExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseAMultiplyExpression(AMultiplyExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseADivideExpression(ADivideExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseAModuloExpression(AModuloExpression node)
  {
    setDefaultEscaping();
  }
  
  public void caseANegativeExpression(ANegativeExpression node)
  {
    setDefaultEscaping();
  }
}

/* Location:
 * Qualified Name:     com.google.clearsilver.jsilver.compiler.EscapingEvaluator
 * Java Class Version: 5 (49.0)
 * JD-Core Version:    0.7.1
 */
package com.google.clearsilver.jsilver.compiler;

import com.google.clearsilver.jsilver.autoescape.EscapeMode;
import com.google.clearsilver.jsilver.data.Data;
import com.google.clearsilver.jsilver.data.DataContext;
import com.google.clearsilver.jsilver.functions.Function;
import com.google.clearsilver.jsilver.functions.FunctionExecutor;
import com.google.clearsilver.jsilver.syntax.analysis.DepthFirstAdapter;
import com.google.clearsilver.jsilver.syntax.node.AAltCommand;
import com.google.clearsilver.jsilver.syntax.node.AAutoescapeCommand;
import com.google.clearsilver.jsilver.syntax.node.ACallCommand;
import com.google.clearsilver.jsilver.syntax.node.ADataCommand;
import com.google.clearsilver.jsilver.syntax.node.ADefCommand;
import com.google.clearsilver.jsilver.syntax.node.AEachCommand;
import com.google.clearsilver.jsilver.syntax.node.AEscapeCommand;
import com.google.clearsilver.jsilver.syntax.node.AEvarCommand;
import com.google.clearsilver.jsilver.syntax.node.AHardIncludeCommand;
import com.google.clearsilver.jsilver.syntax.node.AHardLincludeCommand;
import com.google.clearsilver.jsilver.syntax.node.AIfCommand;
import com.google.clearsilver.jsilver.syntax.node.AIncludeCommand;
import com.google.clearsilver.jsilver.syntax.node.ALincludeCommand;
import com.google.clearsilver.jsilver.syntax.node.ALoopCommand;
import com.google.clearsilver.jsilver.syntax.node.ALoopIncCommand;
import com.google.clearsilver.jsilver.syntax.node.ALoopToCommand;
import com.google.clearsilver.jsilver.syntax.node.ALvarCommand;
import com.google.clearsilver.jsilver.syntax.node.ANameCommand;
import com.google.clearsilver.jsilver.syntax.node.ANoopCommand;
import com.google.clearsilver.jsilver.syntax.node.ASetCommand;
import com.google.clearsilver.jsilver.syntax.node.AUvarCommand;
import com.google.clearsilver.jsilver.syntax.node.AVarCommand;
import com.google.clearsilver.jsilver.syntax.node.AWithCommand;
import com.google.clearsilver.jsilver.syntax.node.PCommand;
import com.google.clearsilver.jsilver.syntax.node.PExpression;
import com.google.clearsilver.jsilver.syntax.node.PPosition;
import com.google.clearsilver.jsilver.syntax.node.PVariable;
import com.google.clearsilver.jsilver.syntax.node.Start;
import com.google.clearsilver.jsilver.syntax.node.TCsOpen;
import com.google.clearsilver.jsilver.syntax.node.TData;
import com.google.clearsilver.jsilver.syntax.node.TWord;
import com.google.clearsilver.jsilver.template.Macro;
import com.google.clearsilver.jsilver.template.RenderingContext;
import com.google.clearsilver.jsilver.template.Template;
import com.google.clearsilver.jsilver.values.Value;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;

public class TemplateTranslator
  extends DepthFirstAdapter
{
  public static final JavaExpression DATA = JavaExpression.symbol(JavaExpression.Type.DATA, "data");
  public static final JavaExpression CONTEXT = JavaExpression.symbol("context");
  public static final JavaExpression DATA_CONTEXT = JavaExpression.symbol(JavaExpression.Type.DATA_CONTEXT, "dataContext");
  public static final JavaExpression NULL = JavaExpression.symbol("null");
  public static final JavaExpression RESOURCE_LOADER = JavaExpression.callOn(CONTEXT, "getResourceLoader", new JavaExpression[0]);
  public static final JavaExpression TEMPLATE_LOADER = JavaExpression.symbol("getTemplateLoader()");
  public static final JavaExpression THIS_TEMPLATE = JavaExpression.symbol("this");
  private final JavaSourceWriter java;
  private final String packageName;
  private final String className;
  private final ExpressionTranslator expressionTranslator = new ExpressionTranslator();
  private final VariableTranslator variableTranslator = new VariableTranslator(expressionTranslator);
  private final EscapingEvaluator escapingEvaluator = new EscapingEvaluator(variableTranslator);
  private static final Method RENDER_METHOD;
  private int tempVariable = 0;
  private boolean propagateEscapeStatus;
  private final Map<String, MacroInfo> macroMap = new HashMap();
  private final Queue<MacroInfo> macroQueue = new LinkedList();
  
  private void addMacro(String name, JavaExpression symbol, ADefCommand defNode)
  {
    if (macroMap.get(name) != null) {}
    MacroInfo info = new MacroInfo(null);
    symbol = symbol;
    defNode = defNode;
    macroMap.put(name, info);
    macroQueue.add(info);
    
    java.writeStatement(JavaExpression.callOn(CONTEXT, "registerMacro", new JavaExpression[] { JavaExpression.string(name), symbol }));
  }
  
  static
  {
    try
    {
      RENDER_METHOD = Template.class.getMethod("render", new Class[] { RenderingContext.class });
    }
    catch (NoSuchMethodException e)
    {
      throw new Error("Cannot find CompiledTemplate.render() method! Has signature changed?", e);
    }
  }
  
  public TemplateTranslator(String packageName, String className, Writer output, boolean propagateEscapeStatus)
  {
    this.packageName = packageName;
    this.className = className;
    java = new JavaSourceWriter(output);
    this.propagateEscapeStatus = propagateEscapeStatus;
  }
  
  public void caseStart(Start node)
  {
    java.writeComment("This class is autogenerated by JSilver. Do not edit.");
    java.writePackage(packageName);
    java.writeImports(new Class[] { BaseCompiledTemplate.class, Template.class, Macro.class, RenderingContext.class, Data.class, DataContext.class, Function.class, FunctionExecutor.class, Value.class, EscapeMode.class, IOException.class });
    
    java.startClass(className, BaseCompiledTemplate.class.getSimpleName(), new String[0]);
    
    java.startMethod(RENDER_METHOD, new String[] { "context" });
    java.writeStatement(JavaExpression.declare(JavaExpression.Type.DATA_CONTEXT, "dataContext", JavaExpression.callOn(CONTEXT, "getDataContext", new JavaExpression[0])));
    
    java.writeStatement(JavaExpression.callOn(CONTEXT, "pushExecutionContext", new JavaExpression[] { THIS_TEMPLATE }));
    super.caseStart(node);
    java.writeStatement(JavaExpression.callOn(CONTEXT, "popExecutionContext", new JavaExpression[0]));
    java.endMethod();
    
    MacroTransformer macroTransformer = new MacroTransformer(null);
    while (!macroQueue.isEmpty())
    {
      MacroInfo curr = (MacroInfo)macroQueue.remove();
      macroTransformer.parseDefNode(symbol, defNode);
    }
    java.endClass();
  }
  
  public void caseADataCommand(ADataCommand node)
  {
    String content = node.getData().getText();
    java.writeStatement(JavaExpression.callOn(CONTEXT, "writeUnescaped", new JavaExpression[] { JavaExpression.string(content) }));
  }
  
  public void caseAVarCommand(AVarCommand node)
  {
    capturePosition(node.getPosition());
    
    String tempVariableName = generateTempVariable("result");
    JavaExpression result = JavaExpression.symbol(JavaExpression.Type.STRING, tempVariableName);
    java.writeStatement(JavaExpression.declare(JavaExpression.Type.STRING, tempVariableName, expressionTranslator.translateToString(node.getExpression())));
    
    JavaExpression escaping = escapingEvaluator.computeIfExemptFromEscaping(node.getExpression(), propagateEscapeStatus);
    
    writeVariable(result, escaping);
  }
  
  public void caseAUvarCommand(AUvarCommand node)
  {
    capturePosition(node.getPosition());
    java.writeStatement(JavaExpression.callOn(CONTEXT, "writeUnescaped", new JavaExpression[] { expressionTranslator.translateToString(node.getExpression()) }));
  }
  
  public void caseASetCommand(ASetCommand node)
  {
    capturePosition(node.getPosition());
    String tempVariableName = generateTempVariable("setNode");
    
    JavaExpression setNode = JavaExpression.symbol(JavaExpression.Type.DATA, tempVariableName);
    java.writeStatement(JavaExpression.declare(JavaExpression.Type.DATA, tempVariableName, JavaExpression.callFindVariable(variableTranslator.translate(node.getVariable()), true)));
    
    java.writeStatement(JavaExpression.callOn(setNode, "setValue", new JavaExpression[] { expressionTranslator.translateToString(node.getExpression()) }));
    if (propagateEscapeStatus) {
      java.writeStatement(JavaExpression.callOn(setNode, "setEscapeMode", new JavaExpression[] { escapingEvaluator.computeEscaping(node.getExpression(), propagateEscapeStatus) }));
    }
  }
  
  public void caseANameCommand(ANameCommand node)
  {
    capturePosition(node.getPosition());
    JavaExpression readNode = JavaExpression.callFindVariable(variableTranslator.translate(node.getVariable()), false);
    
    java.writeStatement(JavaExpression.callOn(CONTEXT, "writeEscaped", new JavaExpression[] { JavaExpression.call("getNodeName", new JavaExpression[] { readNode }) }));
  }
  
  public void caseAIfCommand(AIfCommand node)
  {
    capturePosition(node.getPosition());
    
    java.startIfBlock(expressionTranslator.translateToBoolean(node.getExpression()));
    node.getBlock().apply(this);
    if (!(node.getOtherwise() instanceof ANoopCommand))
    {
      java.endIfStartElseBlock();
      node.getOtherwise().apply(this);
    }
    java.endIfBlock();
  }
  
  public void caseAEachCommand(AEachCommand node)
  {
    capturePosition(node.getPosition());
    
    JavaExpression parent = expressionTranslator.translateToData(node.getExpression());
    writeEach(node.getVariable(), parent, node.getCommand());
  }
  
  public void caseAWithCommand(AWithCommand node)
  {
    capturePosition(node.getPosition());
    
    java.startScopedBlock();
    java.writeComment("with:");
    
    JavaExpression value = expressionTranslator.translateUntyped(node.getExpression());
    String methodName = null;
    if (value.getType() == JavaExpression.Type.VAR_NAME)
    {
      String withValueName = generateTempVariable("withValue");
      java.writeStatement(JavaExpression.declare(JavaExpression.Type.STRING, withValueName, value));
      value = JavaExpression.symbol(JavaExpression.Type.VAR_NAME, withValueName);
      methodName = "createLocalVariableByPath";
      
      java.startIfBlock(JavaExpression.infix(JavaExpression.Type.BOOLEAN, "!=", value.cast(JavaExpression.Type.DATA), JavaExpression.literal(JavaExpression.Type.DATA, "null")));
    }
    else
    {
      value = value.cast(JavaExpression.Type.STRING);
      methodName = "createLocalVariableByValue";
    }
    JavaExpression itemKey = variableTranslator.translate(node.getVariable());
    
    java.writeStatement(JavaExpression.callOn(DATA_CONTEXT, "pushVariableScope", new JavaExpression[0]));
    
    java.writeStatement(JavaExpression.callOn(DATA_CONTEXT, methodName, new JavaExpression[] { itemKey, value }));
    node.getCommand().apply(this);
    
    java.writeStatement(JavaExpression.callOn(DATA_CONTEXT, "popVariableScope", new JavaExpression[0]));
    if (value.getType() == JavaExpression.Type.VAR_NAME) {
      java.endIfBlock();
    }
    java.endScopedBlock();
  }
  
  public void caseALoopToCommand(ALoopToCommand node)
  {
    capturePosition(node.getPosition());
    
    JavaExpression start = JavaExpression.integer(0);
    JavaExpression end = expressionTranslator.translateToNumber(node.getExpression());
    JavaExpression incr = JavaExpression.integer(1);
    writeLoop(node.getVariable(), start, end, incr, node.getCommand());
  }
  
  public void caseALoopCommand(ALoopCommand node)
  {
    capturePosition(node.getPosition());
    
    JavaExpression start = expressionTranslator.translateToNumber(node.getStart());
    JavaExpression end = expressionTranslator.translateToNumber(node.getEnd());
    JavaExpression incr = JavaExpression.integer(1);
    writeLoop(node.getVariable(), start, end, incr, node.getCommand());
  }
  
  public void caseALoopIncCommand(ALoopIncCommand node)
  {
    capturePosition(node.getPosition());
    
    JavaExpression start = expressionTranslator.translateToNumber(node.getStart());
    JavaExpression end = expressionTranslator.translateToNumber(node.getEnd());
    JavaExpression incr = expressionTranslator.translateToNumber(node.getIncrement());
    writeLoop(node.getVariable(), start, end, incr, node.getCommand());
  }
  
  private void writeLoop(PVariable itemVariable, JavaExpression start, JavaExpression end, JavaExpression incr, PCommand command)
  {
    java.startScopedBlock();
    
    String startVarName = generateTempVariable("start");
    java.writeStatement(JavaExpression.declare(JavaExpression.Type.INT, startVarName, start));
    JavaExpression startVar = JavaExpression.symbol(JavaExpression.Type.INT, startVarName);
    
    String endVarName = generateTempVariable("end");
    java.writeStatement(JavaExpression.declare(JavaExpression.Type.INT, endVarName, end));
    JavaExpression endVar = JavaExpression.symbol(JavaExpression.Type.INT, endVarName);
    
    String incrVarName = generateTempVariable("incr");
    java.writeStatement(JavaExpression.declare(JavaExpression.Type.INT, incrVarName, incr));
    JavaExpression incrVar = JavaExpression.symbol(JavaExpression.Type.INT, incrVarName);
    
    java.startIfBlock(JavaExpression.call(JavaExpression.Type.BOOLEAN, "validateLoopArgs", new JavaExpression[] { startVar, endVar, incrVar }));
    
    JavaExpression itemKey = variableTranslator.translate(itemVariable);
    
    java.writeStatement(JavaExpression.callOn(DATA_CONTEXT, "pushVariableScope", new JavaExpression[0]));
    
    String loopVariable = generateTempVariable("loop");
    JavaExpression loopVar = JavaExpression.symbol(JavaExpression.Type.INT, loopVariable);
    JavaExpression ifStart = JavaExpression.declare(JavaExpression.Type.INT, loopVariable, startVar);
    JavaExpression ifEnd = JavaExpression.inlineIf(JavaExpression.Type.BOOLEAN, JavaExpression.infix(JavaExpression.Type.BOOLEAN, ">=", incrVar, JavaExpression.integer(0)), JavaExpression.infix(JavaExpression.Type.BOOLEAN, "<=", loopVar, endVar), JavaExpression.infix(JavaExpression.Type.BOOLEAN, ">=", loopVar, endVar));
    
    java.startForLoop(ifStart, ifEnd, JavaExpression.increment(JavaExpression.Type.INT, loopVar, incrVar));
    
    java.writeStatement(JavaExpression.callOn(DATA_CONTEXT, "createLocalVariableByValue", new JavaExpression[] { itemKey, JavaExpression.symbol(loopVariable).cast(JavaExpression.Type.STRING), JavaExpression.infix(JavaExpression.Type.BOOLEAN, "==", JavaExpression.symbol(loopVariable), startVar), JavaExpression.infix(JavaExpression.Type.BOOLEAN, "==", JavaExpression.symbol(loopVariable), endVar) }));
    
    command.apply(this);
    
    java.endLoop();
    
    java.writeStatement(JavaExpression.callOn(DATA_CONTEXT, "popVariableScope", new JavaExpression[0]));
    
    java.endIfBlock();
    java.endScopedBlock();
  }
  
  private void writeEach(PVariable itemVariable, JavaExpression parentData, PCommand command)
  {
    JavaExpression itemKey = variableTranslator.translate(itemVariable);
    
    java.writeStatement(JavaExpression.callOn(DATA_CONTEXT, "pushVariableScope", new JavaExpression[0]));
    
    String childDataVariable = generateTempVariable("child");
    java.startIterableForLoop("Data", childDataVariable, JavaExpression.call("getChildren", new JavaExpression[] { parentData }));
    
    java.writeStatement(JavaExpression.callOn(DATA_CONTEXT, "createLocalVariableByPath", new JavaExpression[] { itemKey, JavaExpression.callOn(JavaExpression.Type.STRING, JavaExpression.symbol(childDataVariable), "getFullPath", new JavaExpression[0]) }));
    
    command.apply(this);
    
    java.endLoop();
    
    java.writeStatement(JavaExpression.callOn(DATA_CONTEXT, "popVariableScope", new JavaExpression[0]));
  }
  
  public void caseAAltCommand(AAltCommand node)
  {
    capturePosition(node.getPosition());
    String tempVariableName = generateTempVariable("altVar");
    
    JavaExpression declaration = expressionTranslator.declareAsVariable(tempVariableName, node.getExpression());
    
    JavaExpression reference = JavaExpression.symbol(declaration.getType(), tempVariableName);
    java.writ
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd