KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > ext > jython > JythonModel


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.ext.jython;
54
55 import java.util.Iterator JavaDoc;
56 import java.util.List JavaDoc;
57
58 import org.python.core.Py;
59 import org.python.core.PyException;
60 import org.python.core.PyObject;
61
62 import freemarker.ext.util.ModelFactory;
63 import freemarker.ext.util.WrapperTemplateModel;
64 import freemarker.template.AdapterTemplateModel;
65 import freemarker.template.ObjectWrapper;
66 import freemarker.template.TemplateBooleanModel;
67 import freemarker.template.TemplateHashModel;
68 import freemarker.template.TemplateMethodModelEx;
69 import freemarker.template.TemplateModel;
70 import freemarker.template.TemplateModelException;
71 import freemarker.template.TemplateScalarModel;
72
73 /**
74  * Generic model for arbitrary Jython objects.
75  * @version $Id: JythonModel.java,v 1.14 2005/06/12 19:03:06 szegedia Exp $
76  * @author Attila Szegedi
77  */

78 public class JythonModel
79 implements TemplateBooleanModel, TemplateScalarModel, TemplateHashModel,
80 TemplateMethodModelEx, AdapterTemplateModel, WrapperTemplateModel
81 {
82     protected final PyObject object;
83     protected final JythonWrapper wrapper;
84     
85     static final ModelFactory FACTORY =
86         new ModelFactory()
87         {
88             public TemplateModel create(Object JavaDoc object, ObjectWrapper wrapper)
89             {
90                 return new JythonModel((PyObject)object, (JythonWrapper)wrapper);
91             }
92         };
93         
94     public JythonModel(PyObject object, JythonWrapper wrapper)
95     {
96         this.object = object;
97         this.wrapper = wrapper;
98     }
99
100     /**
101      * Returns the value of {@link PyObject#__nonzero__()}.
102      */

103     public boolean getAsBoolean() throws TemplateModelException
104     {
105         try
106         {
107             return object.__nonzero__();
108         }
109         catch(PyException e)
110         {
111             throw new TemplateModelException(e);
112         }
113     }
114
115     /**
116      * Returns the value of {@link Object#toString()}.
117      */

118     public String JavaDoc getAsString() throws TemplateModelException
119     {
120         try
121         {
122             return object.toString();
123         }
124         catch(PyException e)
125         {
126             throw new TemplateModelException(e);
127         }
128     }
129
130     /**
131      * Calls {@link PyObject#__findattr__(java.lang.String)}, then if it
132      * returns null calls {@link PyObject#__finditem__(java.lang.String)}.
133      * If {@link JythonWrapper#setAttributesShadowItems(boolean)} was called
134      * with <code>false</code>, the order of calls is reversed (that is, item
135      * lookup takes precedence over attribute lookup).
136      */

137     public TemplateModel get(String JavaDoc key)
138     throws
139         TemplateModelException
140     {
141         if(key != null)
142         {
143             key = key.intern();
144         }
145         
146         PyObject obj = null;
147         
148         try
149         {
150             if(wrapper.isAttributesShadowItems())
151             {
152                 obj = object.__findattr__(key);
153                 if(obj == null)
154                 {
155                     obj = object.__finditem__(key);
156                 }
157             }
158             else
159             {
160                 obj = object.__finditem__(key);
161                 if(obj == null)
162                 {
163                     obj = object.__findattr__(key);
164                 }
165             }
166         }
167         catch(PyException e)
168         {
169             throw new TemplateModelException(e);
170         }
171
172         return wrapper.wrap(obj);
173     }
174     
175     /**
176      * Returns {@link PyObject#__len__()}<code> == 0</code>.
177      */

178     public boolean isEmpty() throws TemplateModelException
179     {
180         try
181         {
182             return object.__len__() == 0;
183         }
184         catch(PyException e)
185         {
186             throw new TemplateModelException(e);
187         }
188     }
189
190     /**
191      * @see freemarker.template.TemplateMethodModel#exec(List)
192      */

193     public Object JavaDoc exec(List JavaDoc arguments) throws TemplateModelException
194     {
195         int size = arguments.size();
196         try
197         {
198             switch(size)
199             {
200                 case 0:
201                 {
202                     return wrapper.wrap(object.__call__());
203                 }
204                 case 1:
205                 {
206                     return wrapper.wrap(object.__call__(wrapper.unwrap(
207                         (TemplateModel)arguments.get(0))));
208                 }
209                 default:
210                 {
211                     PyObject[] pyargs = new PyObject[size];
212                     int i = 0;
213                     for (Iterator JavaDoc arg = arguments.iterator(); arg.hasNext();)
214                     {
215                         pyargs[i++] = wrapper.unwrap(
216                             (TemplateModel) arg.next());
217                     }
218                     return wrapper.wrap(object.__call__(pyargs));
219                 }
220             }
221         }
222         catch(PyException e)
223         {
224             throw new TemplateModelException(e);
225         }
226     }
227
228     public Object JavaDoc getAdaptedObject(Class JavaDoc hint) {
229         if(object == null) {
230             return null;
231         }
232         Object JavaDoc view = object.__tojava__(hint);
233         if(view == Py.NoConversion) {
234             view = object.__tojava__(Object JavaDoc.class);
235         }
236         return view;
237     }
238     
239     public Object JavaDoc getWrappedObject() {
240         return object == null ? null : object.__tojava__(Object JavaDoc.class);
241     }
242 }
243
Popular Tags