001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.quercus.function;
031:
032: import com.caucho.quercus.env.*;
033: import com.caucho.quercus.module.ModuleContext;
034: import com.caucho.quercus.lib.file.BinaryInput;
035: import com.caucho.quercus.program.JavaClassDef;
036: import com.caucho.util.L10N;
037: import com.caucho.vfs.Path;
038:
039: import java.io.InputStream;
040: import java.math.BigDecimal;
041: import java.math.BigInteger;
042: import java.net.URL;
043: import java.util.Calendar;
044: import java.util.Collection;
045: import java.util.Date;
046: import java.util.List;
047: import java.util.Map;
048:
049: /**
050: * Code for marshaling (PHP to Java) and unmarshaling (Java to PHP) arguments.
051: */
052: public class MarshalFactory {
053: private static final L10N L = new L10N(MarshalFactory.class);
054:
055: protected ModuleContext _moduleContext;
056:
057: public MarshalFactory(ModuleContext moduleContext) {
058: _moduleContext = moduleContext;
059: }
060:
061: public Marshal create(Class argType) {
062: return create(argType, false);
063: }
064:
065: public Marshal create(Class argType, boolean isNotNull) {
066: return create(argType, isNotNull, false);
067: }
068:
069: public Marshal create(Class argType, boolean isNotNull,
070: boolean isNullAsFalse) {
071: final Marshal marshal;
072:
073: // optimized cases, new types should be added to JavaMarshall
074:
075: if (String.class.equals(argType)) {
076: marshal = StringMarshal.MARSHAL;
077: } else if (boolean.class.equals(argType)) {
078: marshal = BooleanMarshal.MARSHAL;
079: } else if (Boolean.class.equals(argType)) {
080: marshal = BooleanObjectMarshal.MARSHAL;
081: } else if (byte.class.equals(argType)) {
082: marshal = ByteMarshal.MARSHAL;
083: } else if (Byte.class.equals(argType)) {
084: marshal = ByteObjectMarshal.MARSHAL;
085: } else if (short.class.equals(argType)) {
086: marshal = ShortMarshal.MARSHAL;
087: } else if (Short.class.equals(argType)) {
088: marshal = ShortObjectMarshal.MARSHAL;
089: } else if (int.class.equals(argType)) {
090: marshal = IntegerMarshal.MARSHAL;
091: } else if (Integer.class.equals(argType)) {
092: marshal = IntegerObjectMarshal.MARSHAL;
093: } else if (long.class.equals(argType)) {
094: marshal = LongMarshal.MARSHAL;
095: } else if (Long.class.equals(argType)) {
096: marshal = LongObjectMarshal.MARSHAL;
097: } else if (LongValue.class.equals(argType)) {
098: marshal = LongValueMarshal.MARSHAL;
099: } else if (float.class.equals(argType)) {
100: marshal = FloatMarshal.MARSHAL;
101: } else if (Float.class.equals(argType)) {
102: marshal = FloatObjectMarshal.MARSHAL;
103: } else if (double.class.equals(argType)) {
104: marshal = DoubleMarshal.MARSHAL;
105: } else if (Double.class.equals(argType)) {
106: marshal = DoubleObjectMarshal.MARSHAL;
107: } else if (DoubleValue.class.equals(argType)) {
108: marshal = DoubleValueMarshal.MARSHAL;
109: } else if (BigDecimal.class.equals(argType)) {
110: marshal = BigDecimalMarshal.MARSHAL;
111: } else if (BigInteger.class.equals(argType)) {
112: marshal = BigIntegerMarshal.MARSHAL;
113: } else if (char.class.equals(argType)) {
114: marshal = CharacterMarshal.MARSHAL;
115: } else if (Character.class.equals(argType)) {
116: marshal = CharacterObjectMarshal.MARSHAL;
117: } else if (Path.class.equals(argType)) {
118: marshal = PathMarshal.MARSHAL;
119: } else if (Callback.class.equals(argType)) {
120: marshal = CallbackMarshal.MARSHAL;
121: } else if (StringValue.class.equals(argType)) {
122: marshal = StringValueMarshal.MARSHAL;
123: } else if (UnicodeValue.class.equals(argType)) {
124: marshal = UnicodeValueMarshal.MARSHAL;
125: } else if (BinaryValue.class.equals(argType)) {
126: marshal = BinaryValueMarshal.MARSHAL;
127: } else if (BytesValue.class.equals(argType)) {
128: marshal = BinaryValueMarshal.MARSHAL;
129: } else if (InputStream.class.equals(argType)) {
130: marshal = InputStreamMarshal.MARSHAL;
131: } else if (BinaryInput.class.equals(argType)) {
132: marshal = BinaryInputMarshal.MARSHAL;
133: } else if (ArrayValue.class.equals(argType)) {
134: marshal = ArrayValueMarshal.MARSHAL;
135: } else if (Value.class.equals(argType)) {
136: marshal = ValueMarshal.MARSHAL;
137: } else if (Value.class.isAssignableFrom(argType)) {
138: marshal = new ExtValueMarshal(argType);
139: } else if (void.class.equals(argType)) {
140: marshal = VoidMarshal.MARSHAL;
141: } else if (Calendar.class.equals(argType)) {
142: marshal = CalendarMarshal.MARSHAL;
143: } else if (Date.class.equals(argType)) {
144: marshal = DateMarshal.MARSHAL;
145: } else if (URL.class.equals(argType)) {
146: marshal = URLMarshal.MARSHAL;
147: } else if (byte[].class.equals(argType)) {
148: marshal = JavaByteArrayMarshal.MARSHAL;
149: } else if (Byte[].class.equals(argType)) {
150: marshal = JavaByteObjectArrayMarshal.MARSHAL;
151: } else if (char[].class.equals(argType)) {
152: marshal = JavaCharacterArrayMarshal.MARSHAL;
153: } else if (Character[].class.equals(argType)) {
154: marshal = JavaCharacterObjectArrayMarshal.MARSHAL;
155: } else if (argType.isArray()) {
156: marshal = new JavaArrayMarshal(argType);
157: } else if (Map.class.isAssignableFrom(argType)) {
158: String typeName = argType.getName();
159:
160: JavaClassDef javaDef = _moduleContext
161: .getJavaClassDefinition(typeName);
162:
163: marshal = new JavaMapMarshal(javaDef, isNotNull,
164: isNullAsFalse);
165: } else if (List.class.isAssignableFrom(argType)) {
166: String typeName = argType.getName();
167:
168: JavaClassDef javaDef = _moduleContext
169: .getJavaClassDefinition(typeName);
170:
171: marshal = new JavaListMarshal(javaDef, isNotNull,
172: isNullAsFalse);
173: } else if (Collection.class.isAssignableFrom(argType)) {
174: String typeName = argType.getName();
175:
176: JavaClassDef javaDef = _moduleContext
177: .getJavaClassDefinition(typeName);
178:
179: marshal = new JavaCollectionMarshal(javaDef, isNotNull,
180: isNullAsFalse);
181: } else {
182: String typeName = argType.getName();
183:
184: JavaClassDef javaDef = _moduleContext
185: .getJavaClassDefinition(typeName);
186:
187: marshal = new JavaMarshal(javaDef, isNotNull, isNullAsFalse);
188: }
189:
190: if (!isNullAsFalse)
191: return marshal;
192: else {
193: if (Value.class.equals(argType)
194: || Boolean.class.equals(argType)
195: || Byte.class.equals(argType)
196: || Short.class.equals(argType)
197: || Integer.class.equals(argType)
198: || Long.class.equals(argType)
199: || Float.class.equals(argType)
200: || Double.class.equals(argType)
201: || Character.class.equals(argType)) {
202:
203: String shortName = argType.getSimpleName();
204: throw new UnsupportedOperationException(
205: "@ReturnNullAsFalse cannot be used with return type `"
206: + shortName + "'");
207: }
208:
209: return new NullAsFalseMarshal(marshal);
210: }
211: }
212:
213: public Marshal createReference() {
214: return ReferenceMarshal.MARSHAL;
215: }
216: }
|