You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by am...@apache.org on 2007/02/16 05:48:57 UTC
svn commit: r508309 - in /webservices/axis2/trunk/java/modules:
adb-codegen/src/org/apache/axis2/schema/template/
adb-codegen/test/org/apache/axis2/schema/populate/simple/
adb/src/org/apache/axis2/databinding/types/
adb/src/org/apache/axis2/databinding...
Author: amilas
Date: Thu Feb 15 20:48:56 2007
New Revision: 508309
URL: http://svn.apache.org/viewvc?view=rev&rev=508309
Log:
fixed the issues AXIS2-1863 and AXIS2-820.
parse the string date using the available simpleDateFormat class functionalities instead of manually passing them.
Modified:
webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl
webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDatePopulateTest.java
webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDateTimePopulateTest.java
webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeTimePopulateTest.java
webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/Time.java
webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java
webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java
Modified: webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl?view=diff&rev=508309&r1=508308&r2=508309
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl (original)
+++ webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl Thu Feb 15 20:48:56 2007
@@ -493,7 +493,7 @@
<xsl:when test="(@restrictionBaseType)">
<xsl:choose>
<xsl:when test="(@patternFacet)">
- if (param.matches("<xsl:value-of select="$patternFacet"/>")) {
+ if (java.lang.String.valueOf(param).matches("<xsl:value-of select="$patternFacet"/>")) {
this.<xsl:value-of select="$varName"/>=param;
}
else {
@@ -501,7 +501,7 @@
}
</xsl:when>
<xsl:when test="(@lenFacet)">
- if ( param.length() == <xsl:value-of select="@lenFacet"/> ) {
+ if ( java.lang.String.valueOf(param).length() == <xsl:value-of select="@lenFacet"/> ) {
this.<xsl:value-of select="$varName"/>=param;
}
else {
@@ -509,8 +509,8 @@
}
</xsl:when>
<xsl:when test="(@maxLenFacet) or (@minLenFacet)">
- if ( <xsl:if test="(@minLenFacet)"> (<xsl:value-of select="$minLenFacet"/> < param.length())</xsl:if>
- <xsl:if test="(@maxLenFacet)"> <xsl:if test="(@minLenFacet)"> && </xsl:if> (param.length() >= <xsl:value-of select="$maxLenFacet"/>) </xsl:if> ) {
+ if ( <xsl:if test="(@minLenFacet)"> (<xsl:value-of select="$minLenFacet"/> < java.lang.String.valueOf(param).length())</xsl:if>
+ <xsl:if test="(@maxLenFacet)"> <xsl:if test="(@minLenFacet)"> && </xsl:if> (java.lang.String.valueOf(param).length() >= <xsl:value-of select="$maxLenFacet"/>) </xsl:if> ) {
this.<xsl:value-of select="$varName"/>=param;
}
else {
Modified: webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDatePopulateTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDatePopulateTest.java?view=diff&rev=508309&r1=508308&r2=508309
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDatePopulateTest.java (original)
+++ webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDatePopulateTest.java Thu Feb 15 20:48:56 2007
@@ -3,6 +3,7 @@
import org.apache.axis2.databinding.utils.ConverterUtil;
import java.util.Date;
+import java.text.SimpleDateFormat;
/*
* Copyright 2004,2005 The Apache Software Foundation.
*
@@ -21,9 +22,9 @@
public class SimpleTypeDatePopulateTest extends AbstractSimplePopulater{
private String values[]={
- "2002-10-10",
- "2000-12-31",
- "2002-02-28"
+ "2002-10-10Z",
+ "2000-12-31Z",
+ "2002-02-28Z"
} ;
private String xmlString[] = {
"<dateParam xmlns=\"http://soapinterop.org/xsd\">"+values[0]+"</dateParam>",
@@ -38,8 +39,13 @@
// force others to implement this method
public void testPopulate() throws Exception {
+
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddZ");
+ Date date = null;
+
for (int i = 0; i < values.length; i++) {
- checkValue(xmlString[i],values[i]);
+ date = ConverterUtil.convertToDate(values[i]);
+ checkValue(xmlString[i],simpleDateFormat.format(date));
}
}
Modified: webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDateTimePopulateTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDateTimePopulateTest.java?view=diff&rev=508309&r1=508308&r2=508309
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDateTimePopulateTest.java (original)
+++ webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeDateTimePopulateTest.java Thu Feb 15 20:48:56 2007
@@ -3,6 +3,7 @@
import org.apache.axis2.databinding.utils.ConverterUtil;
import java.util.Calendar;
+import java.text.SimpleDateFormat;
/*
* Copyright 2004,2005 The Apache Software Foundation.
*
@@ -20,8 +21,8 @@
*/
public class SimpleTypeDateTimePopulateTest extends AbstractSimplePopulater{
- private String values[] ={"2002-10-10T12:00:00+05:00",
- "2000-12-31T11:59:59-05:00",
+ private String values[] ={"2002-10-10T12:00:00+0500",
+ "2000-12-31T11:59:59-0500",
"2002-10-10T07:00:00Z"
};
private String xmlString[] = {
@@ -31,9 +32,13 @@
};
// force others to implement this method
public void testPopulate() throws Exception {
- for (int i = 0; i < values.length; i++) {
- checkValue(xmlString[i],values[i]);
- }
+
+ Calendar calendar;
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
+ for (int i = 0; i < values.length; i++) {
+ calendar = ConverterUtil.convertToDateTime(values[i]);
+ checkValue(xmlString[i],simpleDateFormat.format(calendar.getTime()));
+ }
}
protected void setUp() throws Exception {
@@ -45,8 +50,5 @@
return ConverterUtil.convertToString((Calendar) o);
}
- protected void compare(String val1, String val2) {
- //do nothing for this comparison. We dont know how to compare these yet
- //todo Fix this comparison
- }
+
}
Modified: webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeTimePopulateTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeTimePopulateTest.java?view=diff&rev=508309&r1=508308&r2=508309
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeTimePopulateTest.java (original)
+++ webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/populate/simple/SimpleTypeTimePopulateTest.java Thu Feb 15 20:48:56 2007
@@ -20,7 +20,8 @@
public class SimpleTypeTimePopulateTest extends AbstractSimplePopulater{
private String values[]= {
- "13:20:00",
+ "13:20:00Z",
+ "23:59:59+0530",
"23:59:59"
};
private String xmlString[] = {
@@ -35,8 +36,11 @@
// force others to implement this method
public void testPopulate() throws Exception {
+
+ Time time;
for (int i = 0; i < 2; i++) {
- checkValue(xmlString[i],values[i]);
+ time = new Time(values[i]);
+ checkValue(xmlString[i],time.toString());
}
}
@@ -44,13 +48,5 @@
return ConverterUtil.convertToString((Time)o);
}
- protected void compare(String val1, String val2) {
- //todo - Find a propr way to compare these time strings
- //check for null for now
- assertNotNull(val1);
- assertNotNull(val2);
- //assertTrue(new Time(val1).equals(new Time(val2)));
-
- }
}
Modified: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/Time.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/Time.java?view=diff&rev=508309&r1=508308&r2=508309
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/Time.java (original)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/types/Time.java Thu Feb 15 20:48:56 2007
@@ -17,6 +17,7 @@
import java.text.SimpleDateFormat;
+import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
@@ -25,30 +26,26 @@
* Class that represents the xsd:time XML Schema type
*/
public class Time implements java.io.Serializable {
-
+
private static final long serialVersionUID = -9022201555535589908L;
- private Calendar _value;
+ private Calendar _value;
+ private boolean isFromString;
+ private String originalString;
/**
* a shared java.text.SimpleDateFormat instance used for parsing the basic
* component of the timestamp
*/
private static SimpleDateFormat zulu =
- new SimpleDateFormat("HH:mm:ss.SSS'Z'");
-
- // We should always format dates in the GMT timezone
- static {
- zulu.setTimeZone(TimeZone.getTimeZone("GMT"));
- }
-
+ new SimpleDateFormat("HH:mm:ss.SSSZ");
/**
* Initializes with a Calender. Year, month and date are ignored.
*/
public Time(Calendar value) {
this._value = value;
- _value.set(0,0,0); // ignore year, month, date
+ _value.set(0, 0, 0); // ignore year, month, date
}
/**
@@ -56,10 +53,13 @@
*/
public Time(String value) throws NumberFormatException {
_value = makeValue(value);
+ this.isFromString = true;
+ this.originalString = value;
}
/**
* Returns the time as a calendar. Ignores the year, month and date fields.
+ *
* @return Returns calendar value; may be null.
*/
public Calendar getAsCalendar() {
@@ -68,160 +68,102 @@
/**
* Sets the time; ignores year, month, date
+ *
* @param date
*/
public void setTime(Calendar date) {
this._value = date;
- _value.set(0,0,0); // ignore year, month, date
+ _value.set(0, 0, 0); // ignore year, month, date
}
/**
* Sets the time from a date instance.
+ *
* @param date
*/
public void setTime(Date date) {
_value.setTime(date);
- _value.set(0,0,0); // ignore year, month, date
+ _value.set(0, 0, 0); // ignore year, month, date
}
/**
* Utility function that parses xsd:time strings and returns a Date object
*/
private Calendar makeValue(String source) throws NumberFormatException {
- Calendar calendar = Calendar.getInstance();
- Date date;
- validateSource(source);
+ // cannonical form of the times is hh ':' mm ':' ss ('.' s+)? (zzzzzz)?
- // convert what we have validated so far
- date = ParseHoursMinutesSeconds(source);
-
- int pos = 8; // The "." in hh:mm:ss.sss
-
- // parse optional milliseconds
- if ( source != null ) {
- if (pos < source.length() && source.charAt(pos)=='.') {
- int milliseconds = 0;
- int start = ++pos;
- while (pos<source.length() &&
- Character.isDigit(source.charAt(pos))) {
- pos++;
- }
+ Calendar calendar = Calendar.getInstance();
+ SimpleDateFormat simpleDateFormat = null;
+ Date date;
+ if ((source != null) && (source.length() >= 8)) {
+ if (source.length() == 8) {
+ // i.e this does not have milisecond values or time zone value
+ simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
+ } else {
+ String rest = source.substring(8);
+ if (rest.startsWith(".")) {
+ // i.e this have the ('.'s+) part
+ if (rest.endsWith("Z")) {
+ // this is in gmt time zone
+ simpleDateFormat = new SimpleDateFormat("HH:mm:ss.SSS'Z'");
+ simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+
+ } else if ((rest.indexOf("+") > 0) || (rest.indexOf("-") > 0)) {
+ // this is given in a general time zione
+ simpleDateFormat = new SimpleDateFormat("HH:mm:ss.SSSZ");
+ } else {
+ // i.e it does not have time zone
+ simpleDateFormat = new SimpleDateFormat("HH:mm:ss.SSS");
+ }
- String decimal=source.substring(start,pos);
- if (decimal.length()==3) {
- milliseconds=Integer.parseInt(decimal);
- } else if (decimal.length() < 3) {
- milliseconds=Integer.parseInt((decimal+"000")
- .substring(0,3));
} else {
- milliseconds=Integer.parseInt(decimal.substring(0,3));
- if (decimal.charAt(3)>='5') {
- ++milliseconds;
+ if (rest.startsWith("Z")) {
+ // this is in gmt time zone
+ simpleDateFormat = new SimpleDateFormat("HH:mm:ss'Z'");
+ simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+ } else if (rest.startsWith("+") || rest.startsWith("-")) {
+ // this is given in a general time zione
+ simpleDateFormat = new SimpleDateFormat("HH:mm:ssZ");
+ } else {
+ throw new NumberFormatException("in valid time zone attribute");
}
}
-
- // add milliseconds to the current date
- date.setTime(date.getTime()+milliseconds);
- }
-
- // parse optional timezone
- if (pos+5 < source.length() &&
- (source.charAt(pos)=='+' || (source.charAt(pos)=='-'))) {
- if (!Character.isDigit(source.charAt(pos+1)) ||
- !Character.isDigit(source.charAt(pos+2)) ||
- source.charAt(pos+3) != ':' ||
- !Character.isDigit(source.charAt(pos+4)) ||
- !Character.isDigit(source.charAt(pos+5)))
- {
- throw new NumberFormatException();
- //Messages.getMessage("badTimezone00"));
- }
-
- int hours = (source.charAt(pos+1)-'0')*10
- +source.charAt(pos+2)-'0';
- int mins = (source.charAt(pos+4)-'0')*10
- +source.charAt(pos+5)-'0';
- int milliseconds = (hours*60+mins)*60*1000;
-
- // subtract milliseconds from current date to obtain GMT
- if (source.charAt(pos)=='+') {
- milliseconds=-milliseconds;
- }
- date.setTime(date.getTime()+milliseconds);
- pos+=6;
- }
-
- if (pos < source.length() && source.charAt(pos)=='Z') {
- pos++;
- calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
- }
-
- if (pos < source.length()) {
- throw new NumberFormatException();
- //Messages.getMessage("badChars00"));
}
+ } else {
+ throw new RuntimeException("invalid message string");
}
- calendar.setTime(date);
- calendar.set(0,0,0); // ignore year, month, date
-
- return calendar;
- }
-
- /**
- * Parses the hours, minutes and seconds of a string, by handing it off to
- * the java runtime.
- * The relevant code returns null if a null string is passed in, so this
- * code may return a null date in response.
- * @param source
- * @return Returns Date.
- * @throws NumberFormatException in the event of trouble
- */
- private static Date ParseHoursMinutesSeconds(String source) {
- Date date;
try {
- synchronized (zulu) {
- String fulltime = source == null ? null :
- (source.substring(0,8)+".000Z");
- date = zulu.parse(fulltime);
- }
- } catch (Exception e) {
- throw new NumberFormatException(e.toString());
+ date = simpleDateFormat.parse(source);
+ calendar.setTime(date);
+ calendar.set(0, 0, 0);
+ } catch (ParseException e) {
+ throw new RuntimeException("invalid message string");
}
- return date;
- }
- /**
- * Validates the source.
- * @param source
- */
- private void validateSource(String source) {
- // validate fixed portion of format
- if ( source != null ) {
- if (source.charAt(2) != ':' || source.charAt(5) != ':') {
- throw new NumberFormatException();
- //Messages.getMessage("badTime00"));
- }
- if (source.length() < 8) {
- throw new NumberFormatException();
- //Messages.getMessage("badTime00"));
- }
- }
+ return calendar;
}
+
/**
* Returns the time as it would be in GMT. This is accurate to the
* seconds. Milliseconds probably gets lost.
+ *
* @return Returns String.
*/
public String toString() {
- if(_value==null) {
+ if (_value == null) {
return "unassigned Time";
}
- synchronized (zulu) {
- return zulu.format(_value.getTime());
+
+ if (isFromString) {
+ return originalString;
+ } else {
+ synchronized (zulu) {
+ return zulu.format(_value.getTime());
+ }
}
}
Modified: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java?view=diff&rev=508309&r1=508308&r2=508309
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java (original)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java Thu Feb 15 20:48:56 2007
@@ -8,6 +8,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
+import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
@@ -120,31 +121,17 @@
}
public static String convertToString(Date value) {
- Calendar calendar = Calendar.getInstance();
- SimpleDateFormat zulu = new SimpleDateFormat("yyyy-MM-dd");
- StringBuffer buf = new StringBuffer();
- synchronized (calendar) {
- if (calendar.get(Calendar.ERA) == GregorianCalendar.BC) {
- buf.append("-");
- calendar.setTime(value);
- calendar.set(Calendar.ERA, GregorianCalendar.AD);
- value = calendar.getTime();
- }
- buf.append(zulu.format(value));
- }
- return buf.toString();
+ // lexical form of the date is '-'? yyyy '-' mm '-' dd zzzzzz?
+ // we have to serialize it with the timezone
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddZ");
+ return simpleDateFormat.format(value);
}
public static String convertToString(Calendar value) {
- SimpleDateFormat zulu =
- new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
- Date date = value.getTime();
-
- // Serialize including convert to GMT
- synchronized (zulu) {
- // Sun JDK bug http://developer.java.sun.com/developer/bugParade/bugs/4229798.html
- return zulu.format(date);
- }
+ // lexical form of the calendar is '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?
+ SimpleDateFormat zulu = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
+ // Sun JDK bug http://developer.java.sun.com/developer/bugParade/bugs/4229798.html
+ return zulu.format(value.getTime());
}
public static String convertToString(Day o) {
@@ -357,48 +344,49 @@
*/
public static Date convertToDate(String source) {
+ // the lexical form of the date is '-'? yyyy '-' mm '-' dd zzzzzz?
Calendar calendar = Calendar.getInstance();
- SimpleDateFormat zulu = new SimpleDateFormat("yyyy-MM-dd");
- // 0123456789 0 123456789
- Date result;
+ SimpleDateFormat simpleDateFormat = null;
boolean bc = false;
-
- // validate fixed portion of format
- if (source != null) {
- if (source.charAt(0) == '+')
- source = source.substring(1);
-
- if (source.charAt(0) == '-') {
- source = source.substring(1);
- bc = true;
- }
-
- if (source.length() < 10)
- throw new NumberFormatException("bad date format");
-
-
- if (source.charAt(4) != '-' || source.charAt(7) != '-')
- throw new NumberFormatException("bad Date format");
-
+ if (source.startsWith("-")) {
+ source = source.substring(1);
+ bc = true;
}
- synchronized (calendar) {
- // convert what we have validated so far
- try {
- result = zulu.parse(source == null ? null :
- (source.substring(0, 10)));
- } catch (Exception e) {
- throw new NumberFormatException(e.toString());
+ if ((source != null) && (source.length() >= 10)) {
+ if (source.length() == 10) {
+ //i.e this stirng has only the compulsory part
+ simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
+ } else {
+ String restpart = source.substring(10);
+ if (restpart.startsWith("Z")) {
+ // this is a gmt time zone value
+ simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'Z'");
+ simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+ } else if (restpart.startsWith("+") || restpart.startsWith("-")) {
+ // this is a specific time format string
+ simpleDateFormat = new SimpleDateFormat("yyyy-MM-ddZ");
+ } else {
+ throw new RuntimeException("In valid string sufix");
+ }
}
+ } else {
+ throw new RuntimeException("In valid string to parse");
+ }
- // support dates before the Christian era
+ Date date;
+ try {
+ date = simpleDateFormat.parse(source);
if (bc) {
- calendar.setTime(result);
+ calendar.setTime(date);
calendar.set(Calendar.ERA, GregorianCalendar.BC);
- result = calendar.getTime();
+ date = calendar.getTime();
}
+ } catch (ParseException e) {
+ throw new RuntimeException("In valid string to parse");
}
- return result;
+
+ return date;
}
public static Time convertToTime(String s) {
@@ -524,109 +512,64 @@
* @return Returns Calendar.
*/
public static Calendar convertToDateTime(String source) {
+
+ // the lexical representation of the date time as follows
+ // '-'? yyyy '-' mm '-' dd 'T' hh ':' mm ':' ss ('.' s+)? (zzzzzz)?
+ SimpleDateFormat simpleDateFormat = null;
+ Date date = null;
Calendar calendar = Calendar.getInstance();
- SimpleDateFormat zulu =
- new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
- Date date;
- boolean bc = false;
- // validate fixed portion of format
- if (source == null || source.length() == 0) {
- throw new NumberFormatException();
-// Messages.getMessage("badDateTime00"));
- }
- if (source.charAt(0) == '+') {
+ if (source.startsWith("-")) {
source = source.substring(1);
+ calendar.set(Calendar.ERA, GregorianCalendar.BC);
}
- if (source.charAt(0) == '-') {
- source = source.substring(1);
- bc = true;
- }
- if (source.length() < 19) {
- throw new NumberFormatException();
-// Messages.getMessage("badDateTime00"));
- }
- if (source.charAt(4) != '-' || source.charAt(7) != '-' ||
- source.charAt(10) != 'T') {
- throw new NumberFormatException();//Messages.getMessage("badDate00"));
- }
- if (source.charAt(13) != ':' || source.charAt(16) != ':') {
- throw new NumberFormatException();//Messages.getMessage("badTime00"));
- }
- // convert what we have validated so far
- try {
- synchronized (zulu) {
- date = zulu.parse(source.substring(0, 19) + ".000Z");
- }
- } catch (Exception e) {
- throw new NumberFormatException(e.toString());
- }
- int pos = 19;
- // parse optional milliseconds
- if (pos < source.length() && source.charAt(pos) == '.') {
- int milliseconds = 0;
- int start = ++pos;
- while (pos < source.length() &&
- Character.isDigit(source.charAt(pos))) {
- pos++;
- }
- String decimal = source.substring(start, pos);
- if (decimal.length() == 3) {
- milliseconds = Integer.parseInt(decimal);
- } else if (decimal.length() < 3) {
- milliseconds = Integer.parseInt((decimal + "000")
- .substring(0, 3));
- } else {
- milliseconds = Integer.parseInt(decimal.substring(0, 3));
- if (decimal.charAt(3) >= '5') {
- ++milliseconds;
+ try {
+ if ((source != null) && (source.length() >= 19)) {
+ if (source.length() == 19) {
+ // i.e. this does not have any additional assume this time in current local
+ simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
+
+ } else {
+ String rest = source.substring(19);
+ if (rest.startsWith(".")) {
+ // i.e this have the ('.'s+) part
+ if (rest.endsWith("Z")) {
+ // this is in gmt time zone
+ simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
+ simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+
+ } else if ((rest.indexOf("+") > 0) || (rest.indexOf("-") > 0)) {
+ // this is given in a general time zione
+ simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
+ } else {
+ // i.e it does not have time zone
+ simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
+ }
+
+ } else {
+ if (rest.startsWith("Z")) {
+ // this is in gmt time zone
+ simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
+ simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+ } else if (rest.startsWith("+") || rest.startsWith("-")) {
+ // this is given in a general time zione
+ simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+ } else {
+ throw new NumberFormatException("in valid time zone attribute");
+ }
+ }
}
- }
+ date = simpleDateFormat.parse(source);
+ calendar.setTime(date);
- // add milliseconds to the current date
- date.setTime(date.getTime() + milliseconds);
- }
-
- // parse optional timezone
- if (pos + 5 < source.length() &&
- (source.charAt(pos) == '+' || (source.charAt(pos) == '-'))) {
- if (!Character.isDigit(source.charAt(pos + 1)) ||
- !Character.isDigit(source.charAt(pos + 2)) ||
- source.charAt(pos + 3) != ':' ||
- !Character.isDigit(source.charAt(pos + 4)) ||
- !Character.isDigit(source.charAt(pos + 5))) {
- throw new NumberFormatException();
- // Messages.getMessage("badTimezone00"));
- }
- int hours = (source.charAt(pos + 1) - '0') * 10
- + source.charAt(pos + 2) - '0';
- int mins = (source.charAt(pos + 4) - '0') * 10
- + source.charAt(pos + 5) - '0';
- int milliseconds = (hours * 60 + mins) * 60 * 1000;
-
- // subtract milliseconds from current date to obtain GMT
- if (source.charAt(pos) == '+') {
- milliseconds = -milliseconds;
+ } else {
+ throw new NumberFormatException("date string can not be less than 19 charactors");
}
- date.setTime(date.getTime() + milliseconds);
- pos += 6;
- }
- if (pos < source.length() && source.charAt(pos) == 'Z') {
- pos++;
- calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
- }
- if (pos < source.length()) {
- throw new NumberFormatException();//Messages.getMessage("badChars00"));
- }
- calendar.setTime(date);
-
- // support dates before the Christian era
- if (bc) {
- calendar.set(Calendar.ERA, GregorianCalendar.BC);
+ } catch (ParseException e) {
+ throw new NumberFormatException(e.getMessage());
}
return calendar;
-
}
/**
@@ -771,7 +714,7 @@
private static void ConvertToArbitraryObjectArray(Object returnArray,
Class baseArrayClass,
List objectList) {
- if(!(ADBBean.class.isAssignableFrom(baseArrayClass))) {
+ if (!(ADBBean.class.isAssignableFrom(baseArrayClass))) {
try {
for (int i = 0; i < objectList.size(); i++) {
Object o = objectList.get(i);
Modified: webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java?view=diff&rev=508309&r1=508308&r2=508309
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java (original)
+++ webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java Thu Feb 15 20:48:56 2007
@@ -2,11 +2,10 @@
import junit.framework.TestCase;
-import java.util.List;
-import java.util.ArrayList;
-import java.util.Arrays;
+import java.util.*;
import java.math.BigInteger;
import java.lang.reflect.Array;
+import java.text.SimpleDateFormat;
/*
* Copyright 2004,2005 The Apache Software Foundation.
*
@@ -28,7 +27,7 @@
/**
* Test conversion of Big Integer
*/
- public void testBigInteger(){
+ public void testBigInteger() {
List l = new ArrayList();
l.add("23445");
l.add("23446");
@@ -36,7 +35,7 @@
l.add("1113646");
Object convertedObj = ConverterUtil.convertToArray(
- BigInteger.class,l);
+ BigInteger.class, l);
assertTrue(convertedObj.getClass().isArray());
assertTrue(convertedObj.getClass().equals(BigInteger[].class));
@@ -46,7 +45,7 @@
/**
* integer arrays
*/
- public void testInt(){
+ public void testInt() {
List l = new ArrayList();
l.add("23445");
l.add("23446");
@@ -54,17 +53,17 @@
l.add("1113646");
Object convertedObj = ConverterUtil.convertToArray(
- int.class,l);
+ int.class, l);
assertTrue(convertedObj.getClass().isArray());
assertTrue(convertedObj.getClass().equals(int[].class));
}
- /**
+ /**
* boolean arrays
*/
- public void testBool(){
+ public void testBool() {
List l = new ArrayList();
l.add("true");
l.add("false");
@@ -72,11 +71,78 @@
l.add("false");
Object convertedObj = ConverterUtil.convertToArray(
- boolean.class,l);
+ boolean.class, l);
assertTrue(convertedObj.getClass().isArray());
assertTrue(convertedObj.getClass().equals(boolean[].class));
+ }
+
+ public void testConvertToDateTime() {
+
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z");
+ Calendar calendar;
+ calendar = ConverterUtil.convertToDateTime("2007-02-15T14:54:29");
+ System.out.println("String ==> " + "2007-02-15T14:54:29");
+ System.out.println("calendar ==> " + simpleDateFormat.format(calendar.getTime()));
+
+ calendar = ConverterUtil.convertToDateTime("2007-02-15T14:54:29.399");
+ System.out.println("String ==> " + "2007-02-15T14:54:29.399");
+ System.out.println("calendar ==> " + simpleDateFormat.format(calendar.getTime()));
+
+ calendar = ConverterUtil.convertToDateTime("2007-02-15T14:54:29+0530");
+ System.out.println("String ==> " + "2007-02-15T14:54:29+0530");
+ System.out.println("calendar ==> " + simpleDateFormat.format(calendar.getTime()));
+
+ calendar = ConverterUtil.convertToDateTime("2007-02-15T14:54:29.399+0530");
+ System.out.println("String ==> " + "2007-02-15T14:54:29.399+0530");
+ System.out.println("calendar ==> " + simpleDateFormat.format(calendar.getTime()));
+
+ calendar = ConverterUtil.convertToDateTime("2007-02-15T14:54:29Z");
+ System.out.println("String ==> " + "2007-02-15T14:54:29Z");
+ System.out.println("calendar ==> " + simpleDateFormat.format(calendar.getTime()));
+
+ calendar = ConverterUtil.convertToDateTime("2007-02-15T14:54:29.399Z");
+ System.out.println("String ==> " + "2007-02-15T14:54:29.399Z");
+ System.out.println("calendar ==> " + simpleDateFormat.format(calendar.getTime()));
+
+ calendar = ConverterUtil.convertToDateTime("2007-02-15T14:54:29.399-0530");
+ System.out.println("String ==> " + "2007-02-15T14:54:29.399-0530");
+ System.out.println("calendar ==> " + simpleDateFormat.format(calendar.getTime()));
+
+
+ calendar = ConverterUtil.convertToDateTime("2006-12-11T23:57:16.625Z");
+ System.out.println("String ==> " + "2006-12-11T23:57:16.625Z");
+ simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+ System.out.println("calendar ==> " + simpleDateFormat.format(calendar.getTime()));
+
+ }
+
+ public void testConvertToDateString(){
+ Date date = new Date();
+ String dateString = ConverterUtil.convertToString(date);
+ System.out.println("Date ==> " + dateString);
+ }
+
+ public void testConvertToDate(){
+
+ Date date;
+ SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd Z");
+// date = ConverterUtil.convertToDate("2007-02-15");
+// System.out.println("String ==> " + "2007-02-15");
+// System.out.println("calendar ==> " + simpleDateFormat.format(date));
+//
+// date = ConverterUtil.convertToDate("2007-02-15Z");
+// System.out.println("String ==> " + "2007-02-15Z");
+// System.out.println("calendar ==> " + simpleDateFormat.format(date));
+//
+// date = ConverterUtil.convertToDate("2007-02-15+0530");
+// System.out.println("String ==> " + "2007-02-15+0530");
+// System.out.println("calendar ==> " + simpleDateFormat.format(date));
+
+ date = ConverterUtil.convertToDate("2007-02-15-1230");
+ System.out.println("String ==> " + "2007-02-15-1230");
+ System.out.println("calendar ==> " + simpleDateFormat.format(date));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org