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 24 28 class RmiDebugModelImpl extends UnicastRemoteObject implements DebugModel 29 { 30 private final TemplateModel model; 31 private final int type; 32 33 RmiDebugModelImpl(TemplateModel model) throws RemoteException 34 { 35 super(); 36 this.model = model; 37 type = calculateType(model); 38 } 39 40 private static DebugModel getDebugModel(TemplateModel tm) throws RemoteException 41 { 42 return (DebugModel)RmiDebuggedEnvironmentImpl.getCachedWrapperFor(tm); 43 } 44 public String getAsString() throws TemplateModelException 45 { 46 return ((TemplateScalarModel)model).getAsString(); 47 } 48 49 public Number getAsNumber() throws TemplateModelException 50 { 51 return ((TemplateNumberModel)model).getAsNumber(); 52 } 53 54 public Date getAsDate() throws TemplateModelException 55 { 56 return ((TemplateDateModel)model).getAsDate(); 57 } 58 59 public int getDateType() 60 { 61 return ((TemplateDateModel)model).getDateType(); 62 } 63 64 public boolean getAsBoolean() throws TemplateModelException 65 { 66 return ((TemplateBooleanModel)model).getAsBoolean(); 67 } 68 69 public int size() throws TemplateModelException 70 { 71 if(model instanceof TemplateSequenceModel) 72 { 73 return ((TemplateSequenceModel)model).size(); 74 } 75 return ((TemplateHashModelEx)model).size(); 76 } 77 78 public DebugModel get(int index) throws TemplateModelException, RemoteException 79 { 80 return getDebugModel(((TemplateSequenceModel)model).get(index)); 81 } 82 83 public DebugModel[] get(int fromIndex, int toIndex) throws TemplateModelException, RemoteException 84 { 85 DebugModel[] dm = new DebugModel[toIndex - fromIndex]; 86 TemplateSequenceModel s = (TemplateSequenceModel)model; 87 for (int i = fromIndex; i < toIndex; i++) 88 { 89 dm[i - fromIndex] = getDebugModel(s.get(i)); 90 } 91 return dm; 92 } 93 94 public DebugModel[] getCollection() throws TemplateModelException, RemoteException 95 { 96 List list = new ArrayList (); 97 TemplateModelIterator i = ((TemplateCollectionModel)model).iterator(); 98 while(i.hasNext()) 99 { 100 list.add(getDebugModel(i.next())); 101 } 102 return (DebugModel[])list.toArray(new DebugModel[list.size()]); 103 } 104 105 public DebugModel get(String key) throws TemplateModelException, RemoteException 106 { 107 return getDebugModel(((TemplateHashModel)model).get(key)); 108 } 109 110 public DebugModel[] get(String [] keys) throws TemplateModelException, RemoteException 111 { 112 DebugModel[] dm = new DebugModel[keys.length]; 113 TemplateHashModel h = (TemplateHashModel)model; 114 for (int i = 0; i < keys.length; i++) 115 { 116 dm[i] = getDebugModel(h.get(keys[i])); 117 } 118 return dm; 119 } 120 121 public String [] keys() throws TemplateModelException 122 { 123 TemplateHashModelEx h = (TemplateHashModelEx)model; 124 List list = new ArrayList (); 125 TemplateModelIterator i = h.keys().iterator(); 126 while(i.hasNext()) 127 { 128 list.add(((TemplateScalarModel)i.next()).getAsString()); 129 } 130 return (String [])list.toArray(new String [list.size()]); 131 } 132 133 public int getModelTypes() 134 { 135 return type; 136 } 137 138 private static int calculateType(TemplateModel model) 139 { 140 int type = 0; 141 if(model instanceof TemplateScalarModel) type += TYPE_SCALAR; 142 if(model instanceof TemplateNumberModel) type += TYPE_NUMBER; 143 if(model instanceof TemplateDateModel) type += TYPE_DATE; 144 if(model instanceof TemplateBooleanModel) type += TYPE_BOOLEAN; 145 if(model instanceof TemplateSequenceModel) type += TYPE_SEQUENCE; 146 if(model instanceof TemplateCollectionModel) type += TYPE_COLLECTION; 147 if(model instanceof TemplateHashModelEx) type += TYPE_HASH_EX; 148 else if(model instanceof TemplateHashModel) type += TYPE_HASH; 149 if(model instanceof TemplateMethodModelEx) type += TYPE_METHOD_EX; 150 else if(model instanceof TemplateMethodModel) type += TYPE_METHOD; 151 return type; 152 } 153 } 154 | Popular Tags |