KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > template > utility > DeepUnwrap


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.utility;
54
55 import java.util.ArrayList JavaDoc;
56 import java.util.HashMap JavaDoc;
57
58 import freemarker.ext.util.WrapperTemplateModel;
59 import freemarker.template.AdapterTemplateModel;
60 import freemarker.template.TemplateBooleanModel;
61 import freemarker.template.TemplateCollectionModel;
62 import freemarker.template.TemplateDateModel;
63 import freemarker.template.TemplateHashModelEx;
64 import freemarker.template.TemplateModel;
65 import freemarker.template.TemplateModelException;
66 import freemarker.template.TemplateModelIterator;
67 import freemarker.template.TemplateNumberModel;
68 import freemarker.template.TemplateScalarModel;
69 import freemarker.template.TemplateSequenceModel;
70
71 /**
72  * Utility methods for unwrapping {@link TemplateModel}-s.
73  * @author Attila Szegedi
74  * @version $Id: DeepUnwrap.java,v 1.6 2005/06/16 18:13:58 ddekany Exp $
75  */

76 public class DeepUnwrap
77 {
78     private static final Class JavaDoc OBJECT_CLASS = Object JavaDoc.class;
79     /**
80      * Unwraps {@link TemplateModel}-s recursively.
81      * The converting of the {@link TemplateModel} object happens with the following rules:
82      * <ol>
83      * <li>If the object implements {@link AdapterTemplateModel}, then the result
84      * of {@link AdapterTemplateModel#getAdaptedObject(Class)} for <tt>Object.class</tt> is returned.
85      * <li>If the object implements {@link WrapperTemplateModel}, then the result
86      * of {@link WrapperTemplateModel#getWrappedObject()} is returned.
87      * <li>If the object implements {@link TemplateScalarModel}, then the result
88      * of {@link TemplateScalarModel#getAsString()} is returned.
89      * <li>If the object implements {@link TemplateNumberModel}, then the result
90      * of {@link TemplateNumberModel#getAsNumber()} is returned.
91      * <li>If the object implements {@link TemplateDateModel}, then the result
92      * of {@link TemplateDateModel#getAsDate()} is returned.
93      * <li>If the object implements {@link TemplateBooleanModel}, then the result
94      * of {@link TemplateBooleanModel#getAsBoolean()} is returned.
95      * <li>If the object implements {@link TemplateSequenceModel} or
96      * {@link TemplateCollectionModel}, then a <code>java.util.ArrayList</code> is
97      * constructed from the subvariables, and each subvariable is unwrapped with
98      * the rules described here (recursive unwrapping).
99      * <li>If the object implements {@link TemplateHashModelEx}, then a
100      * <code>java.util.HashMap</code> is constructed from the subvariables, and each
101      * subvariable is unwrapped with the rules described here (recursive unwrapping).
102      * <li>Throw a <code>TemplateModelException</code>, because it doesn't know how to
103      * unwrapp the object.
104      * </ol>
105      */

106     public static Object JavaDoc unwrap(TemplateModel model) throws TemplateModelException {
107         return unwrap(model, false);
108     }
109
110     /**
111      * Same as {@link #unwrap(TemplateModel)}, but it doesn't throw exception if
112      * it doesn't know how to unwrap
113      * the object, but returns it as is.
114      */

115     public static Object JavaDoc premissiveUnwrap(TemplateModel model) throws TemplateModelException {
116         return unwrap(model, true);
117     }
118     
119     private static Object JavaDoc unwrap(TemplateModel model, boolean permissive) throws TemplateModelException {
120         if(model instanceof AdapterTemplateModel) {
121             return ((AdapterTemplateModel)model).getAdaptedObject(OBJECT_CLASS);
122         }
123         if (model instanceof WrapperTemplateModel) {
124             return ((WrapperTemplateModel)model).getWrappedObject();
125         }
126         if(model instanceof TemplateScalarModel) {
127             return ((TemplateScalarModel)model).getAsString();
128         }
129         if(model instanceof TemplateNumberModel) {
130             return ((TemplateNumberModel)model).getAsNumber();
131         }
132         if(model instanceof TemplateDateModel) {
133             return ((TemplateDateModel)model).getAsDate();
134         }
135         if(model instanceof TemplateBooleanModel) {
136             return ((TemplateBooleanModel)model).getAsBoolean() ? Boolean.TRUE : Boolean.FALSE;
137         }
138         if(model instanceof TemplateSequenceModel) {
139             TemplateSequenceModel seq = (TemplateSequenceModel)model;
140             ArrayList JavaDoc list = new ArrayList JavaDoc(seq.size());
141             for(int i = 0; i < seq.size(); ++i) {
142                 list.add(unwrap(seq.get(i)));
143             }
144             return list;
145         }
146         if(model instanceof TemplateCollectionModel) {
147             TemplateCollectionModel coll = (TemplateCollectionModel)model;
148             ArrayList JavaDoc list = new ArrayList JavaDoc();
149             TemplateModelIterator it = coll.iterator();
150             while(it.hasNext()) {
151                 list.add(unwrap(it.next()));
152             }
153             return list;
154         }
155         if(model instanceof TemplateHashModelEx) {
156             TemplateHashModelEx hash = (TemplateHashModelEx)model;
157             HashMap JavaDoc map = new HashMap JavaDoc();
158             TemplateModelIterator keys = hash.keys().iterator();
159             while(keys.hasNext()) {
160                 String JavaDoc key = (String JavaDoc)unwrap(keys.next());
161                 map.put(key, unwrap(hash.get(key)));
162             }
163             return map;
164         }
165         if (permissive) {
166             return model;
167         }
168         throw new TemplateModelException("Cannot deep-unwrap model of type " + model.getClass().getName());
169     }
170 }
Popular Tags