001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.template;
054:
055: import java.lang.reflect.Array;
056: import java.util.ArrayList;
057: import java.util.Collection;
058: import java.util.Iterator;
059: import java.util.Map;
060: import freemarker.ext.dom.NodeModel;
061:
062: /**
063: * <p>The default implementation of the ObjectWrapper
064: * interface.
065: *
066: * @version $Id: DefaultObjectWrapper.java,v 1.23 2005/06/08 00:06:19 revusky Exp $
067: */
068: public class DefaultObjectWrapper extends
069: freemarker.ext.beans.BeansWrapper {
070:
071: static final DefaultObjectWrapper instance = new DefaultObjectWrapper();
072:
073: static private Class W3C_DOM_NODE_CLASS, JYTHON_OBJ_CLASS;
074:
075: static private ObjectWrapper JYTHON_WRAPPER;
076:
077: static {
078: try {
079: W3C_DOM_NODE_CLASS = Class.forName("org.w3c.dom.Node");
080: } catch (Exception e) {
081: }
082: try {
083: JYTHON_OBJ_CLASS = Class
084: .forName("org.python.core.PyObject");
085: JYTHON_WRAPPER = freemarker.ext.jython.JythonWrapper.INSTANCE;
086: } catch (Exception e) {
087: }
088: }
089:
090: public TemplateModel wrap(Object obj) throws TemplateModelException {
091: if (obj == null) {
092: return super .wrap(null);
093: }
094: if (obj instanceof TemplateModel) {
095: return (TemplateModel) obj;
096: }
097: if (obj instanceof String) {
098: return new SimpleScalar((String) obj);
099: }
100: if (obj instanceof Number) {
101: return new SimpleNumber((Number) obj);
102: }
103: if (obj instanceof java.util.Date) {
104: if (obj instanceof java.sql.Date) {
105: return new SimpleDate((java.sql.Date) obj);
106: }
107: if (obj instanceof java.sql.Time) {
108: return new SimpleDate((java.sql.Time) obj);
109: }
110: if (obj instanceof java.sql.Timestamp) {
111: return new SimpleDate((java.sql.Timestamp) obj);
112: }
113: return new SimpleDate((java.util.Date) obj,
114: getDefaultDateType());
115: }
116: if (obj.getClass().isArray()) {
117: obj = convertArray(obj);
118: }
119: if (obj instanceof Collection) {
120: return new SimpleSequence((Collection) obj, this );
121: }
122: if (obj instanceof Map) {
123: return new SimpleHash((Map) obj, this );
124: }
125: if (obj instanceof Boolean) {
126: return obj.equals(Boolean.TRUE) ? TemplateBooleanModel.TRUE
127: : TemplateBooleanModel.FALSE;
128: }
129: if (obj instanceof Iterator) {
130: return new SimpleCollection((Iterator) obj, this );
131: }
132: return handleUnknownType(obj);
133: }
134:
135: /**
136: * Called if an unknown type is passed in.
137: * Since 2.3, this falls back on XML wrapper and BeansWrapper functionality.
138: */
139: protected TemplateModel handleUnknownType(Object obj)
140: throws TemplateModelException {
141: if ((W3C_DOM_NODE_CLASS != null && W3C_DOM_NODE_CLASS
142: .isInstance(obj))) {
143: return wrapDomNode(obj);
144: }
145: if (JYTHON_WRAPPER != null && JYTHON_OBJ_CLASS.isInstance(obj)) {
146: return JYTHON_WRAPPER.wrap(obj);
147: }
148: return super .wrap(obj);
149: }
150:
151: public TemplateModel wrapDomNode(Object obj) {
152: return NodeModel.wrap((org.w3c.dom.Node) obj);
153: }
154:
155: /**
156: * Converts an array to a java.util.List
157: */
158: protected Object convertArray(Object arr) {
159: final int size = Array.getLength(arr);
160: ArrayList list = new ArrayList(size);
161: for (int i = 0; i < size; i++) {
162: list.add(Array.get(arr, i));
163: }
164: return list;
165: }
166: }
|