Save This Page
Home » freemarker-2.3.13 » freemarker.core » [javadoc | source]
    1   /*
    2    * Copyright (c) 2003 The Visigoth Software Society. All rights
    3    * reserved.
    4    *
    5    * Redistribution and use in source and binary forms, with or without
    6    * modification, are permitted provided that the following conditions
    7    * are met:
    8    *
    9    * 1. Redistributions of source code must retain the above copyright
   10    *    notice, this list of conditions and the following disclaimer.
   11    *
   12    * 2. Redistributions in binary form must reproduce the above copyright
   13    *    notice, this list of conditions and the following disclaimer in
   14    *    the documentation and/or other materials provided with the
   15    *    distribution.
   16    *
   17    * 3. The end-user documentation included with the redistribution, if
   18    *    any, must include the following acknowledgement:
   19    *       "This product includes software developed by the
   20    *        Visigoth Software Society (http://www.visigoths.org/)."
   21    *    Alternately, this acknowledgement may appear in the software itself,
   22    *    if and wherever such third-party acknowledgements normally appear.
   23    *
   24    * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the 
   25    *    project contributors may be used to endorse or promote products derived
   26    *    from this software without prior written permission. For written
   27    *    permission, please contact visigoths@visigoths.org.
   28    *
   29    * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
   30    *    nor may "FreeMarker" or "Visigoth" appear in their names
   31    *    without prior written permission of the Visigoth Software Society.
   32    *
   33    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   34    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   35    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   36    * DISCLAIMED.  IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
   37    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   38    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   39    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   40    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   41    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   42    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   43    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44    * SUCH DAMAGE.
   45    * ====================================================================
   46    *
   47    * This software consists of voluntary contributions made by many
   48    * individuals on behalf of the Visigoth Software Society. For more
   49    * information on the Visigoth Software Society, please see
   50    * http://www.visigoths.org/
   51    */
   52   
   53   package freemarker.core;
   54   
   55   import java.util.ArrayList;
   56   import java.util.Collections;
   57   import java.util.Iterator;
   58   import java.util.List;
   59   import java.util.ListIterator;
   60   import freemarker.template;
   61   import freemarker.template.utility.Collections12;
   62   import java.io.IOException;
   63   
   64   final class ListLiteral extends Expression {
   65   
   66       final ArrayList values;
   67   
   68       ListLiteral(ArrayList values) {
   69           this.values = values;
   70           values.trimToSize();
   71       }
   72   
   73       TemplateModel _getAsTemplateModel(Environment env) throws TemplateException {
   74           SimpleSequence list = new SimpleSequence(values.size());
   75           for (Iterator it = values.iterator(); it.hasNext();) {
   76               Expression exp = (Expression) it.next();
   77               TemplateModel tm = exp.getAsTemplateModel(env);
   78               assertNonNull(tm, exp, env);
   79               list.add(tm);
   80           }
   81           return list;
   82       }
   83   
   84       /**
   85        * For the benefit of method calls, return the list of arguments as a list
   86        * of values.
   87        */
   88       List getValueList(Environment env) throws TemplateException {
   89           int size = values.size();
   90           switch(size) {
   91               case 0: {
   92                   return Collections.EMPTY_LIST;
   93               }
   94               case 1: {
   95                   return Collections12.singletonList(((Expression)values.get(0)).getStringValue(env));
   96               }
   97               default: {
   98                   List result = new ArrayList(values.size());
   99                   for (ListIterator iterator = values.listIterator(); iterator.hasNext();) {
  100                       Expression exp = (Expression)iterator.next();
  101                       result.add(exp.getStringValue(env));
  102                   }
  103                   return result;
  104               }
  105           }
  106       }
  107   
  108       /**
  109        * For the benefit of extended method calls, return the list of arguments as a
  110        * list of template models.
  111        */
  112       List getModelList(Environment env) throws TemplateException {
  113           int size = values.size();
  114           switch(size) {
  115               case 0: {
  116                   return Collections.EMPTY_LIST;
  117               }
  118               case 1: {
  119                   return Collections12.singletonList(((Expression)values.get(0)).getAsTemplateModel(env));
  120               }
  121               default: {
  122                   List result = new ArrayList(values.size());
  123                   for (ListIterator iterator = values.listIterator(); iterator.hasNext();) {
  124                       Expression exp = (Expression)iterator.next();
  125                       result.add(exp.getAsTemplateModel(env));
  126                   }
  127                   return result;
  128               }
  129           }
  130       }
  131   
  132       public String getCanonicalForm() {
  133           StringBuffer buf = new StringBuffer("[");
  134           int size = values.size();
  135           for (int i = 0; i<size; i++) {
  136               Expression value = (Expression) values.get(i);
  137               buf.append(value.getCanonicalForm());
  138               if (i != size-1) {
  139                   buf.append(",");
  140               }
  141           }
  142           buf.append("]");
  143           return buf.toString();
  144       }
  145   
  146       boolean isLiteral() {
  147           if (constantValue != null) {
  148               return true;
  149           }
  150           for (int i = 0; i<values.size(); i++) {
  151               Expression exp = (Expression) values.get(i);
  152               if (!exp.isLiteral()) {
  153                   return false;
  154               }
  155           }
  156           return true;
  157       }
  158       
  159       // A hacky routine used by VisitNode and RecurseNode
  160       
  161       TemplateSequenceModel evaluateStringsToNamespaces(Environment env) throws TemplateException {
  162           TemplateSequenceModel val = (TemplateSequenceModel) getAsTemplateModel(env);
  163           SimpleSequence result = new SimpleSequence(val.size());
  164           for (int i=0; i<values.size(); i++) {
  165               if (values.get(i) instanceof StringLiteral) {
  166                   String s = ((StringLiteral) values.get(i)).getAsString();
  167                   try {
  168                       Environment.Namespace ns = env.importLib(s, null);
  169                       result.add(ns);
  170                   } 
  171                   catch (IOException ioe) {
  172                       throw new TemplateException("Could not import library '" + s + "', " + ioe.getMessage(), env); 
  173                   }
  174               }
  175               else {
  176                   result.add(val.get(i));
  177               }
  178           }
  179           return result;
  180       }
  181       
  182       Expression _deepClone(String name, Expression subst) {
  183   		ArrayList clonedValues = (ArrayList)values.clone();
  184   		for (ListIterator iter = clonedValues.listIterator(); iter.hasNext();) {
  185               iter.set(((Expression)iter.next()).deepClone(name, subst));
  186           }
  187           return new ListLiteral(clonedValues);
  188       }
  189   
  190   }

Save This Page
Home » freemarker-2.3.13 » freemarker.core » [javadoc | source]