Save This Page
Home » freemarker-2.3.13 » freemarker.template » [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.template;
   54   
   55   import java.lang.reflect.Array;
   56   import java.util.ArrayList;
   57   import java.util.Collection;
   58   import java.util.Iterator;
   59   import java.util.Map;
   60   import freemarker.ext.dom.NodeModel;
   61   
   62   /**
   63    * <p>The default implementation of the ObjectWrapper
   64    * interface.
   65    *
   66    * @version $Id: DefaultObjectWrapper.java,v 1.23 2005/06/08 00:06:19 revusky Exp $
   67    */
   68   public class DefaultObjectWrapper extends freemarker.ext.beans.BeansWrapper {
   69       
   70       static final DefaultObjectWrapper instance = new DefaultObjectWrapper();
   71       
   72       static private Class W3C_DOM_NODE_CLASS, 
   73                            JYTHON_OBJ_CLASS;
   74       
   75       static private ObjectWrapper JYTHON_WRAPPER;
   76       
   77       static {
   78           try {
   79               W3C_DOM_NODE_CLASS = Class.forName("org.w3c.dom.Node");
   80           } catch (Exception e) {}
   81           try {
   82               JYTHON_OBJ_CLASS = Class.forName("org.python.core.PyObject");
   83               JYTHON_WRAPPER = freemarker.ext.jython.JythonWrapper.INSTANCE;
   84           } catch (Exception e) {}
   85       }
   86       
   87   
   88       public TemplateModel wrap(Object obj) throws TemplateModelException {
   89           if (obj == null) {
   90               return super.wrap(null);
   91           }
   92           if (obj instanceof TemplateModel) {
   93               return (TemplateModel) obj;
   94           }
   95           if (obj instanceof String) {
   96               return new SimpleScalar((String) obj);
   97           }
   98           if (obj instanceof Number) {
   99               return new SimpleNumber((Number) obj);
  100           }
  101           if (obj instanceof java.util.Date) {
  102               if(obj instanceof java.sql.Date) {
  103                   return new SimpleDate((java.sql.Date) obj);
  104               }
  105               if(obj instanceof java.sql.Time) {
  106                   return new SimpleDate((java.sql.Time) obj);
  107               }
  108               if(obj instanceof java.sql.Timestamp) {
  109                   return new SimpleDate((java.sql.Timestamp) obj);
  110               }
  111               return new SimpleDate((java.util.Date) obj, getDefaultDateType());
  112           }
  113           if (obj.getClass().isArray()) {
  114               obj = convertArray(obj);
  115           }
  116           if (obj instanceof Collection) {
  117               return new SimpleSequence((Collection) obj, this);
  118           }
  119           if (obj instanceof Map) {
  120               return new SimpleHash((Map) obj, this);
  121           }
  122           if (obj instanceof Boolean) {
  123               return obj.equals(Boolean.TRUE) ? TemplateBooleanModel.TRUE : TemplateBooleanModel.FALSE;
  124           }
  125           if (obj instanceof Iterator) {
  126               return new SimpleCollection((Iterator) obj, this);
  127           }
  128           return handleUnknownType(obj);
  129       }
  130       
  131       
  132       /**
  133        * Called if an unknown type is passed in.
  134        * Since 2.3, this falls back on XML wrapper and BeansWrapper functionality.
  135        */
  136       protected TemplateModel handleUnknownType(Object obj) throws TemplateModelException {
  137           if ((W3C_DOM_NODE_CLASS != null && W3C_DOM_NODE_CLASS.isInstance(obj)))
  138           {
  139               return wrapDomNode(obj);
  140           }
  141           if (JYTHON_WRAPPER != null  && JYTHON_OBJ_CLASS.isInstance(obj)) {
  142               return JYTHON_WRAPPER.wrap(obj);
  143           }
  144           return super.wrap(obj); 
  145       }
  146   
  147       
  148       public TemplateModel wrapDomNode(Object obj) {
  149           return NodeModel.wrap((org.w3c.dom.Node) obj);
  150       }
  151   
  152       /**
  153        * Converts an array to a java.util.List
  154        */
  155       protected Object convertArray(Object arr) {
  156           final int size = Array.getLength(arr);
  157           ArrayList list = new ArrayList(size);
  158           for (int i=0;i<size; i++) {
  159               list.add(Array.get(arr, i));
  160           }
  161           return list;
  162       }
  163   }

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