You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jaxme-dev@ws.apache.org by jo...@apache.org on 2004/01/18 13:17:52 UTC
cvs commit: ws-jaxme/src/xs/org/apache/ws/jaxme/xs/junit FormatTest.java
jochen 2004/01/18 04:17:52
Added: src/xs/org/apache/ws/jaxme/xs/util XsTimeFormat.java
XsDateTimeFormat.java XsDateFormat.java
src/xs/org/apache/ws/jaxme/xs/junit FormatTest.java
Log:
Added instances of java.text.format for proper parsing and formating of xs:date, xs:dateTime, and xs:time.
Revision Changes Path
1.1 ws-jaxme/src/xs/org/apache/ws/jaxme/xs/util/XsTimeFormat.java
Index: XsTimeFormat.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.apache.ws.jaxme.xs.util;
/** <p>An instance of {@link Format}, which may be used to parse
* and format <code>xs:dateTime</code> values.</p>
*
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
*/
public class XsTimeFormat extends XsDateTimeFormat {
public XsTimeFormat() {
super(false, true);
}
}
1.1 ws-jaxme/src/xs/org/apache/ws/jaxme/xs/util/XsDateTimeFormat.java
Index: XsDateTimeFormat.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.apache.ws.jaxme.xs.util;
import java.text.FieldPosition;
import java.text.Format;
import java.text.ParsePosition;
import java.util.Calendar;
import java.util.TimeZone;
/** <p>An instance of {@link Format}, which may be used to parse
* and format <code>xs:dateTime</code> values.</p>
*
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
*/
public class XsDateTimeFormat extends Format {
final boolean parseDate;
final boolean parseTime;
XsDateTimeFormat(boolean pParseDate, boolean pParseTime) {
parseDate = pParseDate;
parseTime = pParseTime;
}
public XsDateTimeFormat() {
this(true, true);
}
private int parseInt(String pString, int pOffset, StringBuffer pDigits) {
int length = pString.length();
pDigits.setLength(0);
while (pOffset < length) {
char c = pString.charAt(pOffset);
if (Character.isDigit(c)) {
pDigits.append(c);
++pOffset;
} else {
break;
}
}
return pOffset;
}
public Object parseObject(String pString, ParsePosition pParsePosition) {
if (pString == null) {
throw new NullPointerException("The String argument must not be null.");
}
if (pParsePosition == null) {
throw new NullPointerException("The ParsePosition argument must not be null.");
}
int offset = pParsePosition.getIndex();
int length = pString.length();
boolean isMinus = false;
StringBuffer digits = new StringBuffer();
int year, month, mday;
if (parseDate) {
// Sign
if (offset < length) {
char c = pString.charAt(offset);
if (c == '+') {
++offset;
} else if (c == '-') {
++offset;
isMinus = true;
}
}
offset = parseInt(pString, offset, digits);
if (digits.length() < 4) {
pParsePosition.setErrorIndex(offset);
return null;
}
year = Integer.parseInt(digits.toString());
if (offset < length || pString.charAt(offset) != '-') {
pParsePosition.setErrorIndex(offset);
++offset;
}
offset = parseInt(pString, offset, digits);
if (digits.length() != 2) {
pParsePosition.setErrorIndex(offset);
return null;
}
month = Integer.parseInt(digits.toString());
if (offset < length || pString.charAt(offset) != '-') {
pParsePosition.setErrorIndex(offset);
++offset;
}
offset = parseInt(pString, offset, digits);
if (digits.length() != 2) {
pParsePosition.setErrorIndex(offset);
return null;
}
mday = Integer.parseInt(digits.toString());
if (parseTime) {
if (offset < length || pString.charAt(offset) != 'T') {
pParsePosition.setErrorIndex(offset);
++offset;
}
}
} else {
year = month = mday = 0;
}
int hour, minute, second, millis;
if (parseTime) {
offset = parseInt(pString, offset, digits);
if (digits.length() != 2) {
pParsePosition.setErrorIndex(offset);
return null;
}
hour = Integer.parseInt(digits.toString());
if (offset < length || pString.charAt(offset) != ':') {
pParsePosition.setErrorIndex(offset);
++offset;
}
offset = parseInt(pString, offset, digits);
if (digits.length() != 2) {
pParsePosition.setErrorIndex(offset);
return null;
}
minute = Integer.parseInt(digits.toString());
if (offset < length || pString.charAt(offset) != ':') {
pParsePosition.setErrorIndex(offset);
++offset;
}
offset = parseInt(pString, offset, digits);
if (digits.length() != 2) {
pParsePosition.setErrorIndex(offset);
return null;
}
second = Integer.parseInt(digits.toString());
if (offset < length || pString.charAt(offset) != '.') {
pParsePosition.setErrorIndex(offset);
++offset;
offset = parseInt(pString, offset, digits);
if (digits.length() > 0) {
millis = Integer.parseInt(digits.toString());
} else {
millis = 0;
}
} else {
millis = 0;
}
} else {
hour = minute = second = millis = 0;
}
digits.setLength(0);
digits.append("GMT");
if (offset < length) {
char c = pString.charAt(offset);
if (c == 'Z') {
// Ignore UTC, it is the default
++offset;
} else if (c == '+' || c == '-') {
digits.append(c);
++offset;
for (int i = 0; i < 5; i++) {
if (offset >= length) {
pParsePosition.setErrorIndex(offset);
return null;
}
c = pString.charAt(offset);
if ((i != 2 && Character.isDigit(c)) ||
(i == 2 && c == ':')) {
digits.append(c);
} else {
pParsePosition.setErrorIndex(offset);
return null;
}
++offset;
}
}
}
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(digits.toString()));
cal.set(isMinus ? -year : year, month, mday, hour, minute, second);
if (millis != 0) {
cal.set(Calendar.MILLISECOND, millis);
}
pParsePosition.setIndex(offset);
return cal;
}
private void append(StringBuffer pBuffer, int pNum, int pMinLen) {
String s = Integer.toString(pNum);
for (int i = s.length(); i < pMinLen; i++) {
pBuffer.append('0');
}
pBuffer.append(s);
}
public StringBuffer format(Object pCalendar, StringBuffer pBuffer, FieldPosition pPos) {
if (pCalendar == null) {
throw new NullPointerException("The Calendar argument must not be null.");
}
if (pBuffer == null) {
throw new NullPointerException("The StringBuffer argument must not be null.");
}
if (pPos == null) {
throw new NullPointerException("The FieldPosition argument must not be null.");
}
Calendar cal = (Calendar) pCalendar;
if (parseDate) {
int year = cal.get(Calendar.YEAR);
if (year < 0) {
pBuffer.append('-');
year = -year;
}
append(pBuffer, year, 4);
pBuffer.append('-');
append(pBuffer, cal.get(Calendar.MONTH), 2);
pBuffer.append('-');
append(pBuffer, cal.get(Calendar.DAY_OF_MONTH), 2);
if (parseTime) {
pBuffer.append('T');
}
}
if (parseTime) {
append(pBuffer, cal.get(Calendar.HOUR_OF_DAY), 2);
pBuffer.append(':');
append(pBuffer, cal.get(Calendar.MINUTE), 2);
pBuffer.append(':');
append(pBuffer, cal.get(Calendar.SECOND), 2);
int millis = cal.get(Calendar.MILLISECOND);
if (millis > 0) {
pBuffer.append('.');
append(pBuffer, millis, 3);
}
}
TimeZone tz = cal.getTimeZone();
int offset = tz.getOffset(cal.getTimeInMillis());
if (offset == 0) {
pBuffer.append('Z');
} else {
if (offset < 0) {
pBuffer.append('-');
offset = -offset;
} else {
pBuffer.append('+');
}
int minutes = offset / (60*1000);
int hours = minutes / 60;
minutes -= hours * 60;
append(pBuffer, hours, 2);
pBuffer.append(':');
append(pBuffer, minutes, 2);
}
return pBuffer;
}
}
1.1 ws-jaxme/src/xs/org/apache/ws/jaxme/xs/util/XsDateFormat.java
Index: XsDateFormat.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.apache.ws.jaxme.xs.util;
/** <p>An instance of {@link Format}, which may be used to parse
* and format <code>xs:dateTime</code> values.</p>
*
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
*/
public class XsDateFormat extends XsDateTimeFormat {
public XsDateFormat() {
super(true, false);
}
}
1.1 ws-jaxme/src/xs/org/apache/ws/jaxme/xs/junit/FormatTest.java
Index: FormatTest.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2004 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.apache.ws.jaxme.xs.junit;
import java.text.ParseException;
import java.util.Calendar;
import java.util.TimeZone;
import org.apache.ws.jaxme.xs.util.XsDateFormat;
import org.apache.ws.jaxme.xs.util.XsDateTimeFormat;
import org.apache.ws.jaxme.xs.util.XsTimeFormat;
import junit.framework.TestCase;
/** <p>Test case for the various instances of {@link java.text.Format},
* which are being used to parse special types like <code>xs:dateTime</code>.</p>
*
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
*/
public class FormatTest extends TestCase {
public FormatTest(String pName) {
super(pName);
}
private Calendar getCalendar(TimeZone pTimeZone) {
Calendar cal = Calendar.getInstance(pTimeZone);
cal.set(2004, 01, 14, 03, 12, 07);
cal.set(Calendar.MILLISECOND, 027);
return cal;
}
public void testFormatDateTime() {
Calendar cal = getCalendar(TimeZone.getTimeZone("GMT"));
assertEquals(23, cal.get(Calendar.MILLISECOND));
XsDateTimeFormat format = new XsDateTimeFormat();
String got = format.format(cal);
String expect = "2004-01-14T03:12:07.023Z";
assertEquals(expect, got);
cal = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
assertEquals(23, cal.get(Calendar.MILLISECOND));
got = format.format(cal);
expect = "2004-01-14T03:12:07.023-03:00";
assertEquals(expect, got);
}
public void testParseDateTime() throws ParseException {
String[] dateTimes = new String[]{
"2004-01-14T03:12:07.023Z",
"2004-01-14T03:12:07",
"2004-01-14T03:12:07-00:00",
"2004-01-14T03:12:07+00:00",
};
XsDateTimeFormat format = new XsDateTimeFormat();
Calendar expect = getCalendar(TimeZone.getTimeZone("GMT"));
for (int i = 0; i < dateTimes.length; i++) {
Calendar got = (Calendar) format.parseObject(dateTimes[0]);
assertEquals(expect.getTimeInMillis(), got.getTimeInMillis());
}
String dateTime = "2004-01-14T03:12:07.023-03:00";
expect = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
Calendar got = (Calendar) format.parseObject(dateTime);
assertEquals(expect.getTimeInMillis(), got.getTimeInMillis());
}
public void testFormatDate() {
Calendar cal = getCalendar(TimeZone.getTimeZone("GMT"));
assertEquals(23, cal.get(Calendar.MILLISECOND));
XsDateFormat format = new XsDateFormat();
String got = format.format(cal);
String expect = "2004-01-14Z";
assertEquals(expect, got);
cal = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
assertEquals(23, cal.get(Calendar.MILLISECOND));
got = format.format(cal);
expect = "2004-01-14-03:00";
assertEquals(expect, got);
}
protected void assertEqualDate(Calendar pExpect, Calendar pGot) {
assertEquals(pExpect.get(Calendar.YEAR), pGot.get(Calendar.YEAR));
assertEquals(pExpect.get(Calendar.MONTH), pGot.get(Calendar.MONTH));
assertEquals(pExpect.get(Calendar.DAY_OF_MONTH), pGot.get(Calendar.DAY_OF_MONTH));
assertEquals(pExpect.getTimeZone(), pGot.getTimeZone());
}
protected void assertEqualTime(Calendar pExpect, Calendar pGot) {
assertEquals(pExpect.get(Calendar.HOUR_OF_DAY), pGot.get(Calendar.HOUR_OF_DAY));
assertEquals(pExpect.get(Calendar.MINUTE), pGot.get(Calendar.MINUTE));
assertEquals(pExpect.get(Calendar.SECOND), pGot.get(Calendar.SECOND));
assertEquals(pExpect.get(Calendar.MILLISECOND), pGot.get(Calendar.MILLISECOND));
assertEquals(pExpect.getTimeZone(), pGot.getTimeZone());
}
public void testParseDate() throws ParseException {
String[] dateTimes = new String[]{
"2004-01-14Z",
"2004-01-14",
"2004-01-14+00:00",
"2004-01-14-00:00",
};
XsDateFormat format = new XsDateFormat();
Calendar expect = getCalendar(TimeZone.getTimeZone("GMT"));
for (int i = 0; i < dateTimes.length; i++) {
Calendar got = (Calendar) format.parseObject(dateTimes[0]);
assertEqualDate(expect, got);
}
String dateTime = "2004-01-14-03:00";
expect = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
Calendar got = (Calendar) format.parseObject(dateTime);
assertEqualDate(expect, got);
}
public void testFormatTime() {
Calendar cal = getCalendar(TimeZone.getTimeZone("GMT"));
assertEquals(23, cal.get(Calendar.MILLISECOND));
XsTimeFormat format = new XsTimeFormat();
String got = format.format(cal);
String expect = "03:12:07.023Z";
assertEquals(expect, got);
cal = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
assertEquals(23, cal.get(Calendar.MILLISECOND));
got = format.format(cal);
expect = "03:12:07.023-03:00";
assertEquals(expect, got);
}
public void testParseTime() throws ParseException {
String[] dateTimes = new String[]{
"03:12:07.023Z",
"03:12:07",
"03:12:07-00:00",
"03:12:07+00:00",
};
XsTimeFormat format = new XsTimeFormat();
Calendar expect = getCalendar(TimeZone.getTimeZone("GMT"));
for (int i = 0; i < dateTimes.length; i++) {
Calendar got = (Calendar) format.parseObject(dateTimes[0]);
assertEqualTime(expect, got);
}
String dateTime = "03:12:07.023-03:00";
expect = getCalendar(TimeZone.getTimeZone("GMT-03:00"));
Calendar got = (Calendar) format.parseObject(dateTime);
assertEqualTime(expect, got);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: jaxme-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: jaxme-dev-help@ws.apache.org