I would prefer not to have to use java.util.Calendar to represent timestamps. There are a couple of reasons:
* The rest of the application uses java.util.Date for this purpose. Having to use Calendar for the SOAP layer would introduce extra conversions and make the code less consistent. * Date includes both date and time. Mapping it to xs:date loses information. The mapping for other types is not lossy (as far as I can tell). * It would be very easy to accidentally use Date instead of Calendar. This error wouldn't be detected until at runtime, and might easily slip through testing depending on how the dates are being used (sometimes timestamps are stored with both date and time, but only the date portion might be displayed). That said, here's a different patch that doesn't modify how Axis2 formats dates, but would solve the issue for me. This moves the implementation of SimpleTypeMap into a new class SimpleTypeMap, that has instance methods instead of static methods. A service parameter, SimpleTypeMap, can be used to specify a subclass of SimpleTypeMap to use. The implementation of SimpleTypeMap is the same as SimpleTypeMapper was, except that getStringValue has been split into different protected methods and getCurrentMessageContext is no longer called inside getStringValue since AxisService is now available as a member. This allows much more flexibility than the earlier patch, at the cost that the only way to take advantage of it is to write some code (the first patch required only configuration). Potential use cases for this include: * Detailed control of how dates are formatted (should timezone be included? which timezone? use offsets (+00:00) or canonical representation (Z)? map Date to xs:date, xs:dateTime, xs:time or something else? include milliseconds or not?) * Custom types can be mapped to simple types (for example Joda date and time classes can be mapped to xs:dateTime etc., or an Image class might be mapped to base64binary instead of being treated as a complex type). I actually prefer this patch to the first one (for example, the first patch lacked a corresponding option in Java2WSDL, but for this patch such an option already exists: -sg). Regards, Pétur Runólfsson Betware ________________________________________ From: Amila Suriarachchi [amilasuriarach...@gmail.com] Sent: Friday, June 12, 2009 05:30 To: axis-dev@ws.apache.org Subject: Re: [Axis2] Bring back hours, minutes, seconds when Date is formatted in SimpleTypeMapper On Thu, Jun 11, 2009 at 8:02 PM, Pétur Runólfsson <pe...@betware.com<mailto:pe...@betware.com>> wrote: Hi, It seems that the schema type for java.util.Date has changed from xs:dateTime to xs:date in Axis2 1.5. This is preventing me from upgrading, since the application depends on having both the date and time available for many operations. Now the convention is to map java.util.Date to xs:date and java.util.Calendar to xs:datetime is it possible you to convert your app to use Calendar instead of date where you need to have datetime. here the reason is that if you represent something like birthday which represent in java as java.util.date and should map to a xs:date. thanks, Amila. The attached patch adds a configuration option to change the schema type back to xs:dateTime. When the service parameter JavaDateSchemaType is set to xs:dateTime, the format used by SimpleTypeMapper is yyyy-MM-dd'T'HH:mm:ss.SSSZ, otherwise it is yyyy-MM-dd like before. Note that the format is different from Axis2 1.4.1 because of the TimeZone parameter that was added in 1.5. Setting the TimeZone to GMT restores the old behavior completely. This patch only modifies the formatting in SimpleTypeMapper. Java2WSDL still treats Date as xs:date. This doesn't matter for me, since the schema is easy to fix using a custom SchemaGenerator or by running the wsdl through sed. Still, it doesn't seem hard to add the corresponding support to Java2WSDL. This fixes issues AXIS2-4329 and AXIS2-4370. Regards, Pétur Runólfsson Betware The content of this e-mail, together with any of its attachments, is for the exclusive and confidential use of the named addressee(s) and it may contain legally privileged and confidential information and/or copyrighted material. Any other distribution, use or reproduction without the sender's prior consent is unauthorized and strictly prohibited. If you have by coincidence, mistake or without specific authorization received this e-mail in error, please notify the sender by e-mail immediately, uphold strict confidentiality and neither read, copy, transfer, disseminate, disclose nor otherwise make use of its content in any way and delete the material from your computer. The content of the e-mail and its attachments is the liability of the individual sender, if it does not relate to the affairs of Betware. Betware does not assume any civil or criminal liability should the e-mail or it´s attachments be virus infected. -- Amila Suriarachchi WSO2 Inc. blog: http://amilachinthaka.blogspot.com/ The content of this e-mail, together with any of its attachments, is for the exclusive and confidential use of the named addressee(s) and it may contain legally privileged and confidential information and/or copyrighted material. Any other distribution, use or reproduction without the sender's prior consent is unauthorized and strictly prohibited. If you have by coincidence, mistake or without specific authorization received this e-mail in error, please notify the sender by e-mail immediately, uphold strict confidentiality and neither read, copy, transfer, disseminate, disclose nor otherwise make use of its content in any way and delete the material from your computer. The content of the e-mail and its attachments is the liability of the individual sender, if it does not relate to the affairs of Betware. Betware does not assume any civil or criminal liability should the e-mail or it´s attachments be virus infected.
Index: test/org/apache/axis2/databinding/typemapping/SimpleTypeMapperTest.java =================================================================== --- test/org/apache/axis2/databinding/typemapping/SimpleTypeMapperTest.java (revision 0) +++ test/org/apache/axis2/databinding/typemapping/SimpleTypeMapperTest.java (revision 0) @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.databinding.typemapping; + +import org.apache.axis2.context.MessageContext; +import org.apache.axis2.description.AxisService; + +import junit.framework.TestCase; + +import javax.activation.DataHandler; + +import java.awt.datatransfer.Transferable; +import java.util.Calendar; +import java.util.Date; +import java.util.TimeZone; + +public class SimpleTypeMapperTest extends TestCase { + + private MessageContext msgContext; + private AxisService axisService; + private TimeZone prevTimeZone; + + @Override + protected void setUp() throws Exception { + prevTimeZone = TimeZone.getDefault(); + + axisService = new AxisService(); + msgContext = new MessageContext(); + msgContext.setAxisService(axisService); + MessageContext.setCurrentMessageContext(msgContext); + } + + @Override + protected void tearDown() throws Exception { + MessageContext.setCurrentMessageContext(null); + + TimeZone.setDefault(prevTimeZone); + } + + public void testDataHandlerTypes() { + assertTrue(SimpleTypeMapper.isDataHandler(DataHandler.class)); + assertTrue(SimpleTypeMapper.isDataHandler(Transferable.class)); + } + + public void testNonDataHandlerTypes() { + assertFalse(SimpleTypeMapper.isDataHandler(Object.class)); + } + + public void testGetStringValueFromDate() { + Date date = getDate(); + + String result = SimpleTypeMapper.getStringValue(date); + assertEquals("2009-06-10", result); + } + + public void testGetStringValueFromDateWithTimeZone() throws Exception { + TimeZone.setDefault(TimeZone.getTimeZone("Europe/Helsinki")); + + Date date = getDate(); + + axisService.addParameter("TimeZone", "Pacific/Honolulu"); + + String result = SimpleTypeMapper.getStringValue(date); + assertEquals("2009-06-09", result); + } + + public static class CustomFormatMap extends SimpleTypeMap { + @Override + protected String formatDate(Object obj) { + return "Hello, world!"; + } + } + + public void testGetStringValueWithCustomFormat() throws Exception { + Date date = getDate(); + + axisService.addParameter("SimpleTypeMap", CustomFormatMap.class.getName()); + + String result = SimpleTypeMapper.getStringValue(date); + assertEquals("Hello, world!", result); + } + + private Date getDate() { + Calendar cal = Calendar.getInstance(); + cal.clear(); + cal.set(2009, Calendar.JUNE, 10, 9, 16, 37); + return cal.getTime(); + } +} Index: src/org/apache/axis2/databinding/typemapping/SimpleTypeMapper.java =================================================================== --- src/org/apache/axis2/databinding/typemapping/SimpleTypeMapper.java (revision 784097) +++ src/org/apache/axis2/databinding/typemapping/SimpleTypeMapper.java (working copy) @@ -19,297 +19,77 @@ package org.apache.axis2.databinding.typemapping; -import org.apache.axiom.attachments.utils.DataHandlerUtils; import org.apache.axiom.om.OMElement; -import org.apache.axiom.om.OMNode; -import org.apache.axiom.om.OMText; -import org.apache.axis2.databinding.utils.ConverterUtil; -import org.apache.axis2.context.MessageContext; -import org.apache.axis2.description.AxisService; import javax.activation.DataHandler; -import javax.xml.namespace.QName; -import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.HashSet; + public class SimpleTypeMapper { - private static final String STRING = "java.lang.String"; - private static final String W_INT = "java.lang.Integer"; - private static final String W_DOUBLE = "java.lang.Double"; - private static final String W_LONG = "java.lang.Long"; - private static final String W_BYTE = "java.lang.Byte"; - private static final String W_SHORT = "java.lang.Short"; - private static final String W_BOOLEAN = "java.lang.Boolean"; - private static final String W_CHAR = "java.lang.Character"; - private static final String W_FLOAT = "java.lang.Float"; - private static final String W_CALENDAR = "java.util.Calendar"; - private static final String W_DATE = "java.util.Date"; - private static final String INT = "int"; - private static final String BOOLEAN = "boolean"; - private static final String BYTE = "byte"; - private static final String DOUBLE = "double"; - private static final String SHORT = "short"; - private static final String LONG = "long"; - private static final String FLOAT = "float"; - private static final String CHAR = "char"; - - /* - * To support deserialize BigDecimal, BigInteger - * Day, Duration, Month, MonthDay, Time, Year, YearMonth - */ - private static final String BIG_DECIMAL = "java.math.BigDecimal"; - private static final String BIG_INTEGER = "java.math.BigInteger"; - private static final String DAY = "org.apache.axis2.databinding.types.Day"; - private static final String DURATION = "org.apache.axis2.databinding.types.Duration"; - private static final String MONTH = "org.apache.axis2.databinding.types.Month"; - private static final String MONTH_DAY = "org.apache.axis2.databinding.types.MonthDay"; - private static final String TIME = "org.apache.axis2.databinding.types.Time"; - private static final String YEAR = "org.apache.axis2.databinding.types.Year"; - private static final String YEAR_MONTH = "org.apache.axis2.databinding.types.YearMonth"; - + private static SimpleTypeMap getTypeMap() { + return SimpleTypeMap.getInstance(); + } + public static Object getSimpleTypeObject(Class parameter, OMElement value) { - String name = parameter.getName(); - String text = value.getText(); - - if(name.equals(STRING)) { - return text; - } else if (text == null || text.length() == 0) { - return null; - } else if (name.equals(INT)) { - return new Integer(text); - } else if (name.equals(BOOLEAN)) { - return ConverterUtil.convertToBoolean(text); - } else if (name.equals(BYTE)) { - return new Byte(text); - } else if (name.equals(DOUBLE)) { - return new Double(text); - } else if (name.equals(SHORT)) { - return new Short(text); - } else if (name.equals(LONG)) { - return new Long(text); - } else if (name.equals(FLOAT)) { - return new Float(text); - } else if (name.equals(CHAR)) { - return text.toCharArray()[0]; - } else if (name.equals(W_INT)) { - return new Integer(text); - } else if (name.equals(W_BOOLEAN)) { - return Boolean.valueOf(text); - } else if (name.equals(W_BYTE)) { - return new Byte(text); - } else if (name.equals(W_DOUBLE)) { - return new Double(text); - } else if (name.equals(W_SHORT)) { - return new Short(text); - } else if (name.equals(W_LONG)) { - return new Long(text); - } else if (name.equals(W_FLOAT)) { - return new Float(text); - } else if (name.equals(W_CHAR)) { - return text.toCharArray()[0]; - } else if (name.equals(W_CALENDAR)) { - return makeCalendar(text); - } else if (name.equals(W_DATE)) { - return makeDate(text); - }/* - * return the correpsonding object for adding data type - */ - else if(name.equals(BIG_DECIMAL)) { - return new java.math.BigDecimal(text); - } - else if(name.equals(BIG_INTEGER)) { - return new java.math.BigInteger(text); - } - else if(name.equals(DAY)) { - return new org.apache.axis2.databinding.types.Day(text); - } - else if(name.equals(DURATION)) { - return new org.apache.axis2.databinding.types.Duration(text); - } - else if(name.equals(MONTH)) { - return new org.apache.axis2.databinding.types.Month(text); - } - else if(name.equals(MONTH_DAY)) { - return new org.apache.axis2.databinding.types.MonthDay(text); - } - else if(name.equals(TIME)) { - return new org.apache.axis2.databinding.types.Time(text); - } - else if(name.equals(YEAR)) { - return new org.apache.axis2.databinding.types.Year(text); - } - else if(name.equals(YEAR_MONTH)) { - return new org.apache.axis2.databinding.types.YearMonth(text); - } - else { - return null; - } + return getTypeMap().getSimpleTypeObject(parameter, value); } public static ArrayList getArrayList(OMElement element, String localName) { - Iterator childitr = element.getChildrenWithName(new QName(localName)); - ArrayList list = new ArrayList(); - while (childitr.hasNext()) { - Object o = childitr.next(); - list.add(o); - } - return list; + return getTypeMap().getArrayList(element, localName); } public static HashSet getHashSet(OMElement element, String localName) { - Iterator childitr = element.getChildrenWithName(new QName(localName)); - final HashSet list = new HashSet(); - while (childitr.hasNext()) { - OMElement o = (OMElement) childitr.next(); - list.add(o.getText()); - } - return list; + return getTypeMap().getHashSet(element, localName); } public static DataHandler getDataHandler(OMElement element) { - OMNode node = element.getFirstOMChild(); - if (node instanceof OMText) { - OMText txt = (OMText)node; - if (txt.isOptimized()) { - return (DataHandler)txt.getDataHandler(); - } else { - return (DataHandler)DataHandlerUtils.getDataHandlerFromText(txt.getText(), null); - } - } - return null; + return getTypeMap().getDataHandler(element); } public static ArrayList getArrayList(OMElement element) { - Iterator childitr = element.getChildren(); - ArrayList list = new ArrayList(); - while (childitr.hasNext()) { - Object o = childitr.next(); - list.add(o); - } - return list; + return getTypeMap().getArrayList(element); } public static boolean isSimpleType(Object obj) { - String objClassName = obj.getClass().getName(); - return obj instanceof Calendar || obj instanceof Date || isSimpleType(objClassName); + return getTypeMap().isSimpleType(obj); } public static boolean isSimpleType(Class obj) { - String objClassName = obj.getName(); - return isSimpleType(objClassName); + return getTypeMap().isSimpleType(obj); } public static boolean isDataHandler(Class obj) { - return obj.isAssignableFrom(DataHandler.class) && !obj.equals(Object.class); + return getTypeMap().isDataHandler(obj); } public static boolean isHashSet(Class obj) { - return java.util.HashSet.class.isAssignableFrom(obj); + return getTypeMap().isHashSet(obj); } public static boolean isCollection(Class obj) { - return java.util.Collection.class.isAssignableFrom(obj); + return getTypeMap().isCollection(obj); } public static boolean isSimpleType(String objClassName) { - if (objClassName.equals(STRING)) { - return true; - } else if (objClassName.equals(INT)) { - return true; - } else if (objClassName.equals(BOOLEAN)) { - return true; - } else if (objClassName.equals(BYTE)) { - return true; - } else if (objClassName.equals(DOUBLE)) { - return true; - } else if (objClassName.equals(SHORT)) { - return true; - } else if (objClassName.equals(LONG)) { - return true; - } else if (objClassName.equals(FLOAT)) { - return true; - } else if (objClassName.equals(CHAR)) { - return true; - } else if (objClassName.equals(W_INT)) { - return true; - } else if (objClassName.equals(W_BOOLEAN)) { - return true; - } else if (objClassName.equals(W_BYTE)) { - return true; - } else if (objClassName.equals(W_DOUBLE)) { - return true; - } else if (objClassName.equals(W_SHORT)) { - return true; - } else if (objClassName.equals(W_LONG)) { - return true; - } else if (objClassName.equals(W_FLOAT)) { - return true; - } else if (objClassName.equals(W_CALENDAR)) { - return true; - } else if (objClassName.equals(W_DATE)) { - return true; - } /* - * consider BigDecimal, BigInteger, Day, Duration, Month - * MonthDay, Time, Year, YearMonth as simple type - */ - else return objClassName.equals(BIG_DECIMAL) - || objClassName.equals(BIG_INTEGER) - || objClassName.equals(DAY) - || objClassName.equals(DURATION) - || objClassName.equals(MONTH) - || objClassName.equals(MONTH_DAY) - || objClassName.equals(TIME) - || objClassName.equals(YEAR) - || objClassName.equals(YEAR_MONTH) || objClassName.equals(W_CHAR); + return getTypeMap().isSimpleType(objClassName); } public static String getStringValue(Object obj) { - if (obj instanceof Float || - obj instanceof Double) { - double data; - if (obj instanceof Float) { - data = ((Float)obj).doubleValue(); - } else { - data = (Double)obj; - } - if (Double.isNaN(data)) { - return "NaN"; - } else if (data == Double.POSITIVE_INFINITY) { - return "INF"; - } else if (data == Double.NEGATIVE_INFINITY) { - return "-INF"; - } else { - return obj.toString(); - } - } else if (obj instanceof Calendar) { - Calendar calendar = (Calendar) obj; - return ConverterUtil.convertToString(calendar); - } else if (obj instanceof Date) { - SimpleDateFormat zulu = new SimpleDateFormat("yyyy-MM-dd"); - - MessageContext messageContext = MessageContext.getCurrentMessageContext(); - AxisService axisServce = messageContext.getAxisService(); - // if the user has given a pirticualr timezone we use it. - if (axisServce.getParameter("TimeZone") != null){ - zulu.setTimeZone(TimeZone.getTimeZone((String)axisServce.getParameter("TimeZone").getValue())); - } - - return zulu.format(obj); - } - return obj.toString(); + return getTypeMap().getStringValue(obj); } public static Object makeCalendar(String source) { - return ConverterUtil.convertToDateTime(source); + return getTypeMap().makeCalendar(source); } public static Object makeDate(String source) { - return ConverterUtil.convertToDateTime(source).getTime(); + return getTypeMap().makeDate(source); } } Index: src/org/apache/axis2/databinding/typemapping/SimpleTypeMap.java =================================================================== --- src/org/apache/axis2/databinding/typemapping/SimpleTypeMap.java (revision 0) +++ src/org/apache/axis2/databinding/typemapping/SimpleTypeMap.java (revision 0) @@ -0,0 +1,387 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.axis2.databinding.typemapping; + +import org.apache.axiom.attachments.utils.DataHandlerUtils; +import org.apache.axiom.om.OMElement; +import org.apache.axiom.om.OMNode; +import org.apache.axiom.om.OMText; +import org.apache.axis2.AxisFault; +import org.apache.axis2.context.MessageContext; +import org.apache.axis2.databinding.utils.ConverterUtil; +import org.apache.axis2.description.AxisService; +import org.apache.axis2.util.Loader; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.activation.DataHandler; +import javax.xml.namespace.QName; + +import java.text.SimpleDateFormat; +import java.util.*; + +/** + * <p> + * Handles conversion of XML simple types from Java object to strings and back. + * </p> + * <p> + * This can be customized by setting the {...@value #CLASS_PARAM} service parameter. + * </p> + */ +public class SimpleTypeMap { + + public static final String INSTANCE_PARAM = "org.apache.axis2.databinding.typemapping.SimpleTypeMap.instance"; + public static final String CLASS_PARAM = "SimpleTypeMap"; + + private static final String STRING = "java.lang.String"; + private static final String W_INT = "java.lang.Integer"; + private static final String W_DOUBLE = "java.lang.Double"; + private static final String W_LONG = "java.lang.Long"; + private static final String W_BYTE = "java.lang.Byte"; + private static final String W_SHORT = "java.lang.Short"; + private static final String W_BOOLEAN = "java.lang.Boolean"; + private static final String W_CHAR = "java.lang.Character"; + private static final String W_FLOAT = "java.lang.Float"; + private static final String W_CALENDAR = "java.util.Calendar"; + private static final String W_DATE = "java.util.Date"; + private static final String INT = "int"; + private static final String BOOLEAN = "boolean"; + private static final String BYTE = "byte"; + private static final String DOUBLE = "double"; + private static final String SHORT = "short"; + private static final String LONG = "long"; + private static final String FLOAT = "float"; + private static final String CHAR = "char"; + + /* + * To support deserialize BigDecimal, BigInteger + * Day, Duration, Month, MonthDay, Time, Year, YearMonth + */ + private static final String BIG_DECIMAL = "java.math.BigDecimal"; + private static final String BIG_INTEGER = "java.math.BigInteger"; + private static final String DAY = "org.apache.axis2.databinding.types.Day"; + private static final String DURATION = "org.apache.axis2.databinding.types.Duration"; + private static final String MONTH = "org.apache.axis2.databinding.types.Month"; + private static final String MONTH_DAY = "org.apache.axis2.databinding.types.MonthDay"; + private static final String TIME = "org.apache.axis2.databinding.types.Time"; + private static final String YEAR = "org.apache.axis2.databinding.types.Year"; + private static final String YEAR_MONTH = "org.apache.axis2.databinding.types.YearMonth"; + + private static final Log log = LogFactory.getLog(SimpleTypeMap.class); + private static final SimpleTypeMap defaultInstance = new SimpleTypeMap(); + private AxisService axisService; + + public static SimpleTypeMap getInstance() { + MessageContext messageContext = MessageContext.getCurrentMessageContext(); + if (messageContext != null) { + return getInstance(messageContext.getAxisService()); + } else { + return defaultInstance; + } + } + + public static SimpleTypeMap getInstance(AxisService axisService) { + SimpleTypeMap instance = (SimpleTypeMap) axisService.getParameterValue(INSTANCE_PARAM); + if (instance == null) { + String className = (String) axisService.getParameterValue(CLASS_PARAM); + if (className != null) { + className = className.trim(); + try { + Class clazz = Loader.loadClass(axisService.getClassLoader(), className); + instance = (SimpleTypeMap) clazz.newInstance(); + } catch (Exception e) { + throw new RuntimeException("Failed to create SimpleTypeMap instance of type '" + className + "'", e); + } + } else { + instance = new SimpleTypeMap(); + } + instance.setAxisService(axisService); + try { + axisService.addParameter(INSTANCE_PARAM, instance); + } catch (AxisFault e) { + // Probably means somebody else added the parameter concurrently, doesn't really matter. + log.warn("Failed to add SimpleTypeMap to service", e); + } + } + return instance; + } + + public void setAxisService(AxisService axisService) { + this.axisService = axisService; + } + + public AxisService getAxisService() { + return this.axisService; + } + + public Object getSimpleTypeObject(Class parameter, OMElement value) { + + String name = parameter.getName(); + String text = value.getText(); + + if(name.equals(STRING)) { + return text; + } else if (text == null || text.length() == 0) { + return null; + } else if (name.equals(INT)) { + return new Integer(text); + } else if (name.equals(BOOLEAN)) { + return ConverterUtil.convertToBoolean(text); + } else if (name.equals(BYTE)) { + return new Byte(text); + } else if (name.equals(DOUBLE)) { + return new Double(text); + } else if (name.equals(SHORT)) { + return new Short(text); + } else if (name.equals(LONG)) { + return new Long(text); + } else if (name.equals(FLOAT)) { + return new Float(text); + } else if (name.equals(CHAR)) { + return text.toCharArray()[0]; + } else if (name.equals(W_INT)) { + return new Integer(text); + } else if (name.equals(W_BOOLEAN)) { + return Boolean.valueOf(text); + } else if (name.equals(W_BYTE)) { + return new Byte(text); + } else if (name.equals(W_DOUBLE)) { + return new Double(text); + } else if (name.equals(W_SHORT)) { + return new Short(text); + } else if (name.equals(W_LONG)) { + return new Long(text); + } else if (name.equals(W_FLOAT)) { + return new Float(text); + } else if (name.equals(W_CHAR)) { + return text.toCharArray()[0]; + } else if (name.equals(W_CALENDAR)) { + return makeCalendar(text); + } else if (name.equals(W_DATE)) { + return makeDate(text); + }/* + * return the correpsonding object for adding data type + */ + else if(name.equals(BIG_DECIMAL)) { + return new java.math.BigDecimal(text); + } + else if(name.equals(BIG_INTEGER)) { + return new java.math.BigInteger(text); + } + else if(name.equals(DAY)) { + return new org.apache.axis2.databinding.types.Day(text); + } + else if(name.equals(DURATION)) { + return new org.apache.axis2.databinding.types.Duration(text); + } + else if(name.equals(MONTH)) { + return new org.apache.axis2.databinding.types.Month(text); + } + else if(name.equals(MONTH_DAY)) { + return new org.apache.axis2.databinding.types.MonthDay(text); + } + else if(name.equals(TIME)) { + return new org.apache.axis2.databinding.types.Time(text); + } + else if(name.equals(YEAR)) { + return new org.apache.axis2.databinding.types.Year(text); + } + else if(name.equals(YEAR_MONTH)) { + return new org.apache.axis2.databinding.types.YearMonth(text); + } + else { + return null; + } + } + + public ArrayList getArrayList(OMElement element, String localName) { + Iterator childitr = element.getChildrenWithName(new QName(localName)); + ArrayList list = new ArrayList(); + while (childitr.hasNext()) { + Object o = childitr.next(); + list.add(o); + } + return list; + } + + public HashSet getHashSet(OMElement element, String localName) { + Iterator childitr = element.getChildrenWithName(new QName(localName)); + final HashSet list = new HashSet(); + while (childitr.hasNext()) { + OMElement o = (OMElement) childitr.next(); + list.add(o.getText()); + } + return list; + } + + + public DataHandler getDataHandler(OMElement element) { + OMNode node = element.getFirstOMChild(); + if (node instanceof OMText) { + OMText txt = (OMText)node; + if (txt.isOptimized()) { + return (DataHandler)txt.getDataHandler(); + } else { + return (DataHandler)DataHandlerUtils.getDataHandlerFromText(txt.getText(), null); + } + } + return null; + } + + + public ArrayList getArrayList(OMElement element) { + Iterator childitr = element.getChildren(); + ArrayList list = new ArrayList(); + while (childitr.hasNext()) { + Object o = childitr.next(); + list.add(o); + } + return list; + } + + public boolean isSimpleType(Object obj) { + String objClassName = obj.getClass().getName(); + return obj instanceof Calendar || obj instanceof Date || isSimpleType(objClassName); + } + + public boolean isSimpleType(Class obj) { + String objClassName = obj.getName(); + return isSimpleType(objClassName); + } + + public boolean isDataHandler(Class obj) { + return obj.isAssignableFrom(DataHandler.class) && !obj.equals(Object.class); + } + + public boolean isHashSet(Class obj) { + return java.util.HashSet.class.isAssignableFrom(obj); + } + + + public boolean isCollection(Class obj) { + return java.util.Collection.class.isAssignableFrom(obj); + } + + public boolean isSimpleType(String objClassName) { + if (objClassName.equals(STRING)) { + return true; + } else if (objClassName.equals(INT)) { + return true; + } else if (objClassName.equals(BOOLEAN)) { + return true; + } else if (objClassName.equals(BYTE)) { + return true; + } else if (objClassName.equals(DOUBLE)) { + return true; + } else if (objClassName.equals(SHORT)) { + return true; + } else if (objClassName.equals(LONG)) { + return true; + } else if (objClassName.equals(FLOAT)) { + return true; + } else if (objClassName.equals(CHAR)) { + return true; + } else if (objClassName.equals(W_INT)) { + return true; + } else if (objClassName.equals(W_BOOLEAN)) { + return true; + } else if (objClassName.equals(W_BYTE)) { + return true; + } else if (objClassName.equals(W_DOUBLE)) { + return true; + } else if (objClassName.equals(W_SHORT)) { + return true; + } else if (objClassName.equals(W_LONG)) { + return true; + } else if (objClassName.equals(W_FLOAT)) { + return true; + } else if (objClassName.equals(W_CALENDAR)) { + return true; + } else if (objClassName.equals(W_DATE)) { + return true; + } /* + * consider BigDecimal, BigInteger, Day, Duration, Month + * MonthDay, Time, Year, YearMonth as simple type + */ + else return objClassName.equals(BIG_DECIMAL) + || objClassName.equals(BIG_INTEGER) + || objClassName.equals(DAY) + || objClassName.equals(DURATION) + || objClassName.equals(MONTH) + || objClassName.equals(MONTH_DAY) + || objClassName.equals(TIME) + || objClassName.equals(YEAR) + || objClassName.equals(YEAR_MONTH) || objClassName.equals(W_CHAR); + } + + public String getStringValue(Object obj) { + if (obj instanceof Float || + obj instanceof Double) { + return formatFloatingPoint(obj); + } else if (obj instanceof Calendar) { + return formatCalendar(obj); + } else if (obj instanceof Date) { + return formatDate(obj); + } + return obj.toString(); + } + + protected String formatFloatingPoint(Object obj) { + double data; + if (obj instanceof Float) { + data = ((Float)obj).doubleValue(); + } else { + data = (Double)obj; + } + if (Double.isNaN(data)) { + return "NaN"; + } else if (data == Double.POSITIVE_INFINITY) { + return "INF"; + } else if (data == Double.NEGATIVE_INFINITY) { + return "-INF"; + } else { + return obj.toString(); + } + } + + protected String formatCalendar(Object obj) { + Calendar calendar = (Calendar) obj; + return ConverterUtil.convertToString(calendar); + } + + protected String formatDate(Object obj) { + SimpleDateFormat zulu = new SimpleDateFormat("yyyy-MM-dd"); + + // if the user has given a pirticualr timezone we use it. + if (axisService.getParameter("TimeZone") != null){ + zulu.setTimeZone(TimeZone.getTimeZone((String)axisService.getParameter("TimeZone").getValue())); + } + + return zulu.format(obj); + } + + public Object makeCalendar(String source) { + return ConverterUtil.convertToDateTime(source); + } + + public Object makeDate(String source) { + return ConverterUtil.convertToDateTime(source).getTime(); + } +}