Home » freemarker-2.3.13 » freemarker.debug.impl » [javadoc | source]

    1   package freemarker.debug.impl;
    2   
    3   import java.rmi.RemoteException;
    4   import java.rmi.server.UnicastRemoteObject;
    5   import java.util.ArrayList;
    6   import java.util.Date;
    7   import java.util.List;
    8   
    9   import freemarker.debug.DebugModel;
   10   import freemarker.template.TemplateBooleanModel;
   11   import freemarker.template.TemplateCollectionModel;
   12   import freemarker.template.TemplateDateModel;
   13   import freemarker.template.TemplateHashModel;
   14   import freemarker.template.TemplateHashModelEx;
   15   import freemarker.template.TemplateMethodModel;
   16   import freemarker.template.TemplateMethodModelEx;
   17   import freemarker.template.TemplateModel;
   18   import freemarker.template.TemplateModelException;
   19   import freemarker.template.TemplateModelIterator;
   20   import freemarker.template.TemplateNumberModel;
   21   import freemarker.template.TemplateScalarModel;
   22   import freemarker.template.TemplateSequenceModel;
   23   import freemarker.template.TemplateTransformModel;
   24   
   25   /**
   26    * @author Attila Szegedi
   27    * @version $Id: RmiDebugModelImpl.java,v 1.2.2.1 2006/11/27 07:54:49 szegedia Exp $
   28    */
   29   class RmiDebugModelImpl extends UnicastRemoteObject implements DebugModel
   30   {
   31       private static final long serialVersionUID = 1L;
   32   
   33       private final TemplateModel model;
   34       private final int type;
   35       
   36       RmiDebugModelImpl(TemplateModel model, int extraTypes) throws RemoteException
   37       {
   38           super();
   39           this.model = model;
   40           type = calculateType(model) + extraTypes;
   41       }
   42   
   43       private static DebugModel getDebugModel(TemplateModel tm) throws RemoteException
   44       {
   45           return (DebugModel)RmiDebuggedEnvironmentImpl.getCachedWrapperFor(tm);
   46       }
   47       public String getAsString() throws TemplateModelException
   48       {
   49           return ((TemplateScalarModel)model).getAsString();
   50       }
   51   
   52       public Number getAsNumber() throws TemplateModelException
   53       {
   54           return ((TemplateNumberModel)model).getAsNumber();
   55       }
   56   
   57       public Date getAsDate() throws TemplateModelException
   58       {
   59           return ((TemplateDateModel)model).getAsDate();
   60       }
   61   
   62       public int getDateType()
   63       {
   64           return ((TemplateDateModel)model).getDateType();
   65       }
   66   
   67       public boolean getAsBoolean() throws TemplateModelException
   68       {
   69           return ((TemplateBooleanModel)model).getAsBoolean();
   70       }
   71   
   72       public int size() throws TemplateModelException
   73       {
   74           if(model instanceof TemplateSequenceModel)
   75           {
   76               return ((TemplateSequenceModel)model).size();
   77           }
   78           return ((TemplateHashModelEx)model).size();
   79       }
   80   
   81       public DebugModel get(int index) throws TemplateModelException, RemoteException
   82       {
   83           return getDebugModel(((TemplateSequenceModel)model).get(index));
   84       }
   85       
   86       public DebugModel[] get(int fromIndex, int toIndex) throws TemplateModelException, RemoteException
   87       {
   88           DebugModel[] dm = new DebugModel[toIndex - fromIndex];
   89           TemplateSequenceModel s = (TemplateSequenceModel)model;
   90           for (int i = fromIndex; i < toIndex; i++)
   91           {
   92               dm[i - fromIndex] = getDebugModel(s.get(i));
   93           }
   94           return dm;
   95       }
   96   
   97       public DebugModel[] getCollection() throws TemplateModelException, RemoteException
   98       {
   99           List list = new ArrayList();
  100           TemplateModelIterator i = ((TemplateCollectionModel)model).iterator();
  101           while(i.hasNext())
  102           {
  103               list.add(getDebugModel(i.next()));
  104           }
  105           return (DebugModel[])list.toArray(new DebugModel[list.size()]);
  106       }
  107       
  108       public DebugModel get(String key) throws TemplateModelException, RemoteException
  109       {
  110           return getDebugModel(((TemplateHashModel)model).get(key));
  111       }
  112       
  113       public DebugModel[] get(String[] keys) throws TemplateModelException, RemoteException
  114       {
  115           DebugModel[] dm = new DebugModel[keys.length];
  116           TemplateHashModel h = (TemplateHashModel)model;
  117           for (int i = 0; i < keys.length; i++)
  118           {
  119               dm[i] = getDebugModel(h.get(keys[i]));
  120           }
  121           return dm;
  122       }
  123   
  124       public String[] keys() throws TemplateModelException
  125       {
  126           TemplateHashModelEx h = (TemplateHashModelEx)model;
  127           List list = new ArrayList();
  128           TemplateModelIterator i = h.keys().iterator();
  129           while(i.hasNext())
  130           {
  131               list.add(((TemplateScalarModel)i.next()).getAsString());
  132           }
  133           return (String[])list.toArray(new String[list.size()]);
  134       }
  135   
  136       public int getModelTypes()
  137       {
  138           return type;
  139       }
  140       
  141       private static int calculateType(TemplateModel model)
  142       {
  143           int type = 0;
  144           if(model instanceof TemplateScalarModel) type += TYPE_SCALAR;
  145           if(model instanceof TemplateNumberModel) type += TYPE_NUMBER;
  146           if(model instanceof TemplateDateModel) type += TYPE_DATE;
  147           if(model instanceof TemplateBooleanModel) type += TYPE_BOOLEAN;
  148           if(model instanceof TemplateSequenceModel) type += TYPE_SEQUENCE;
  149           if(model instanceof TemplateCollectionModel) type += TYPE_COLLECTION;
  150           if(model instanceof TemplateHashModelEx) type += TYPE_HASH_EX;
  151           else if(model instanceof TemplateHashModel) type += TYPE_HASH;
  152           if(model instanceof TemplateMethodModelEx) type += TYPE_METHOD_EX;
  153           else if(model instanceof TemplateMethodModel) type += TYPE_METHOD;
  154           if(model instanceof TemplateTransformModel) type += TYPE_TRANSFORM;
  155           return type;
  156       }
  157   }

Home » freemarker-2.3.13 » freemarker.debug.impl » [javadoc | source]