001: package freemarker.debug.impl;
002:
003: import java.rmi.RemoteException;
004: import java.rmi.server.UnicastRemoteObject;
005: import java.util.ArrayList;
006: import java.util.Date;
007: import java.util.List;
008:
009: import freemarker.debug.DebugModel;
010: import freemarker.template.TemplateBooleanModel;
011: import freemarker.template.TemplateCollectionModel;
012: import freemarker.template.TemplateDateModel;
013: import freemarker.template.TemplateHashModel;
014: import freemarker.template.TemplateHashModelEx;
015: import freemarker.template.TemplateMethodModel;
016: import freemarker.template.TemplateMethodModelEx;
017: import freemarker.template.TemplateModel;
018: import freemarker.template.TemplateModelException;
019: import freemarker.template.TemplateModelIterator;
020: import freemarker.template.TemplateNumberModel;
021: import freemarker.template.TemplateScalarModel;
022: import freemarker.template.TemplateSequenceModel;
023: import freemarker.template.TemplateTransformModel;
024:
025: /**
026: * @author Attila Szegedi
027: * @version $Id: RmiDebugModelImpl.java,v 1.2.2.1 2006/11/27 07:54:49 szegedia Exp $
028: */
029: class RmiDebugModelImpl extends UnicastRemoteObject implements
030: DebugModel {
031: private static final long serialVersionUID = 1L;
032:
033: private final TemplateModel model;
034: private final int type;
035:
036: RmiDebugModelImpl(TemplateModel model, int extraTypes)
037: throws RemoteException {
038: super ();
039: this .model = model;
040: type = calculateType(model) + extraTypes;
041: }
042:
043: private static DebugModel getDebugModel(TemplateModel tm)
044: throws RemoteException {
045: return (DebugModel) RmiDebuggedEnvironmentImpl
046: .getCachedWrapperFor(tm);
047: }
048:
049: public String getAsString() throws TemplateModelException {
050: return ((TemplateScalarModel) model).getAsString();
051: }
052:
053: public Number getAsNumber() throws TemplateModelException {
054: return ((TemplateNumberModel) model).getAsNumber();
055: }
056:
057: public Date getAsDate() throws TemplateModelException {
058: return ((TemplateDateModel) model).getAsDate();
059: }
060:
061: public int getDateType() {
062: return ((TemplateDateModel) model).getDateType();
063: }
064:
065: public boolean getAsBoolean() throws TemplateModelException {
066: return ((TemplateBooleanModel) model).getAsBoolean();
067: }
068:
069: public int size() throws TemplateModelException {
070: if (model instanceof TemplateSequenceModel) {
071: return ((TemplateSequenceModel) model).size();
072: }
073: return ((TemplateHashModelEx) model).size();
074: }
075:
076: public DebugModel get(int index) throws TemplateModelException,
077: RemoteException {
078: return getDebugModel(((TemplateSequenceModel) model).get(index));
079: }
080:
081: public DebugModel[] get(int fromIndex, int toIndex)
082: throws TemplateModelException, RemoteException {
083: DebugModel[] dm = new DebugModel[toIndex - fromIndex];
084: TemplateSequenceModel s = (TemplateSequenceModel) model;
085: for (int i = fromIndex; i < toIndex; i++) {
086: dm[i - fromIndex] = getDebugModel(s.get(i));
087: }
088: return dm;
089: }
090:
091: public DebugModel[] getCollection() throws TemplateModelException,
092: RemoteException {
093: List list = new ArrayList();
094: TemplateModelIterator i = ((TemplateCollectionModel) model)
095: .iterator();
096: while (i.hasNext()) {
097: list.add(getDebugModel(i.next()));
098: }
099: return (DebugModel[]) list.toArray(new DebugModel[list.size()]);
100: }
101:
102: public DebugModel get(String key) throws TemplateModelException,
103: RemoteException {
104: return getDebugModel(((TemplateHashModel) model).get(key));
105: }
106:
107: public DebugModel[] get(String[] keys)
108: throws TemplateModelException, RemoteException {
109: DebugModel[] dm = new DebugModel[keys.length];
110: TemplateHashModel h = (TemplateHashModel) model;
111: for (int i = 0; i < keys.length; i++) {
112: dm[i] = getDebugModel(h.get(keys[i]));
113: }
114: return dm;
115: }
116:
117: public String[] keys() throws TemplateModelException {
118: TemplateHashModelEx h = (TemplateHashModelEx) model;
119: List list = new ArrayList();
120: TemplateModelIterator i = h.keys().iterator();
121: while (i.hasNext()) {
122: list.add(((TemplateScalarModel) i.next()).getAsString());
123: }
124: return (String[]) list.toArray(new String[list.size()]);
125: }
126:
127: public int getModelTypes() {
128: return type;
129: }
130:
131: private static int calculateType(TemplateModel model) {
132: int type = 0;
133: if (model instanceof TemplateScalarModel)
134: type += TYPE_SCALAR;
135: if (model instanceof TemplateNumberModel)
136: type += TYPE_NUMBER;
137: if (model instanceof TemplateDateModel)
138: type += TYPE_DATE;
139: if (model instanceof TemplateBooleanModel)
140: type += TYPE_BOOLEAN;
141: if (model instanceof TemplateSequenceModel)
142: type += TYPE_SEQUENCE;
143: if (model instanceof TemplateCollectionModel)
144: type += TYPE_COLLECTION;
145: if (model instanceof TemplateHashModelEx)
146: type += TYPE_HASH_EX;
147: else if (model instanceof TemplateHashModel)
148: type += TYPE_HASH;
149: if (model instanceof TemplateMethodModelEx)
150: type += TYPE_METHOD_EX;
151: else if (model instanceof TemplateMethodModel)
152: type += TYPE_METHOD;
153: if (model instanceof TemplateTransformModel)
154: type += TYPE_TRANSFORM;
155: return type;
156: }
157: }
|