From 57ed4837ccc9d11102c130424b87ca0ca5e654cd Mon Sep 17 00:00:00 2001 From: Matti Tahvonen Date: Wed, 30 Apr 2008 08:33:39 +0000 Subject: [PATCH] fixes #1573 svn changeset:4296/svn branch:trunk --- .../terminal/gwt/client/DateLocale.java | 69 ------ .../terminal/gwt/client/DateTimeService.java | 10 - .../terminal/gwt/client/LocaleService.java | 1 + .../terminal/gwt/client/ui/IDateField.java | 14 +- .../terminal/gwt/client/ui/ITextualDate.java | 155 +++++------- .../terminal/gwt/client/util/DateLocale.java | 86 ------- .../terminal/gwt/client/util/Pattern.java | 195 --------------- .../gwt/client/util/SimpleDateFormat.java | 229 ------------------ .../gwt/client/util/SimpleDateParser.java | 174 ------------- 9 files changed, 69 insertions(+), 864 deletions(-) delete mode 100644 src/com/itmill/toolkit/terminal/gwt/client/DateLocale.java delete mode 100644 src/com/itmill/toolkit/terminal/gwt/client/util/DateLocale.java delete mode 100644 src/com/itmill/toolkit/terminal/gwt/client/util/Pattern.java delete mode 100644 src/com/itmill/toolkit/terminal/gwt/client/util/SimpleDateFormat.java delete mode 100644 src/com/itmill/toolkit/terminal/gwt/client/util/SimpleDateParser.java diff --git a/src/com/itmill/toolkit/terminal/gwt/client/DateLocale.java b/src/com/itmill/toolkit/terminal/gwt/client/DateLocale.java deleted file mode 100644 index 481f3ecdec..0000000000 --- a/src/com/itmill/toolkit/terminal/gwt/client/DateLocale.java +++ /dev/null @@ -1,69 +0,0 @@ -/* -@ITMillApache2LicenseForJavaFiles@ - */ - -package com.itmill.toolkit.terminal.gwt.client; - -public class DateLocale extends - com.itmill.toolkit.terminal.gwt.client.util.DateLocale { - - private static String locale; - - public DateLocale() { - locale = LocaleService.getDefaultLocale(); - } - - public static void setLocale(String l) { - if (LocaleService.getAvailableLocales().contains(locale)) { - locale = l; - } else { - // TODO redirect to console - System.out.println("Tried to use an unloaded locale \"" + locale - + "\". Using default in stead (" + locale + ")"); - } - } - - public static String getAM() { - try { - return LocaleService.getAmPmStrings(locale)[0]; - } catch (final LocaleNotLoadedException e) { - // TODO redirect to console - System.out.println("Tried to use an unloaded locale \"" + locale - + "\"."); - return "AM"; - } - } - - public static String getPM() { - try { - return LocaleService.getAmPmStrings(locale)[1]; - } catch (final LocaleNotLoadedException e) { - // TODO redirect to console - System.out.println("Tried to use an unloaded locale \"" + locale - + "\"."); - return "PM"; - } - } - - public String[] getWEEKDAY_LONG() { - try { - return LocaleService.getDayNames(locale); - } catch (final LocaleNotLoadedException e) { - // TODO redirect to console - System.out.println("Tried to use an unloaded locale \"" + locale - + "\"."); - return null; - } - } - - public String[] getWEEKDAY_SHORT() { - try { - return LocaleService.getShortDayNames(locale); - } catch (final LocaleNotLoadedException e) { - // TODO redirect to console - System.out.println("Tried to use an unloaded locale \"" + locale - + "\"."); - return null; - } - } -} diff --git a/src/com/itmill/toolkit/terminal/gwt/client/DateTimeService.java b/src/com/itmill/toolkit/terminal/gwt/client/DateTimeService.java index 1138412fcd..aaa3ebc9b6 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/DateTimeService.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/DateTimeService.java @@ -158,16 +158,6 @@ public class DateTimeService { return start; } - public String getDateFormat() { - try { - return LocaleService.getDateFormat(currentLocale); - } catch (final LocaleNotLoadedException e) { - // TODO redirect to console - System.out.println(e + ":" + e.getMessage()); - } - return "M/d/yy"; - } - public static int getNumberOfDaysInMonth(Date date) { final int month = date.getMonth(); if (month == 1 && true == isLeapYear(date)) { diff --git a/src/com/itmill/toolkit/terminal/gwt/client/LocaleService.java b/src/com/itmill/toolkit/terminal/gwt/client/LocaleService.java index 6b74b2c186..ed5a8432c6 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/LocaleService.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/LocaleService.java @@ -191,6 +191,7 @@ public class LocaleService { } else { throw new LocaleNotLoadedException(locale); } + } } diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/IDateField.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/IDateField.java index bd368810f0..c064164035 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/IDateField.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/IDateField.java @@ -23,13 +23,13 @@ public class IDateField extends FlowPanel implements Paintable { protected boolean immediate; - public static int RESOLUTION_YEAR = 0; - public static int RESOLUTION_MONTH = 1; - public static int RESOLUTION_DAY = 2; - public static int RESOLUTION_HOUR = 3; - public static int RESOLUTION_MIN = 4; - public static int RESOLUTION_SEC = 5; - public static int RESOLUTION_MSEC = 6; + public static final int RESOLUTION_YEAR = 0; + public static final int RESOLUTION_MONTH = 1; + public static final int RESOLUTION_DAY = 2; + public static final int RESOLUTION_HOUR = 3; + public static final int RESOLUTION_MIN = 4; + public static final int RESOLUTION_SEC = 5; + public static final int RESOLUTION_MSEC = 6; protected int currentResolution = RESOLUTION_YEAR; diff --git a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextualDate.java b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextualDate.java index c4eade28d1..69b31c9600 100644 --- a/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextualDate.java +++ b/src/com/itmill/toolkit/terminal/gwt/client/ui/ITextualDate.java @@ -4,24 +4,23 @@ package com.itmill.toolkit.terminal.gwt.client.ui; +import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.user.client.Timer; import com.google.gwt.user.client.ui.ChangeListener; import com.google.gwt.user.client.ui.Widget; import com.itmill.toolkit.terminal.gwt.client.ApplicationConnection; import com.itmill.toolkit.terminal.gwt.client.ContainerResizedListener; -import com.itmill.toolkit.terminal.gwt.client.DateLocale; +import com.itmill.toolkit.terminal.gwt.client.LocaleNotLoadedException; +import com.itmill.toolkit.terminal.gwt.client.LocaleService; import com.itmill.toolkit.terminal.gwt.client.Paintable; import com.itmill.toolkit.terminal.gwt.client.UIDL; -import com.itmill.toolkit.terminal.gwt.client.util.SimpleDateFormat; public class ITextualDate extends IDateField implements Paintable, ChangeListener, ContainerResizedListener { private final ITextField text; - private SimpleDateFormat format; - - private DateLocale dl; + private String formatStr; private String width; @@ -37,62 +36,70 @@ public class ITextualDate extends IDateField implements Paintable, } public void updateFromUIDL(UIDL uidl, ApplicationConnection client) { + int origRes = currentResolution; super.updateFromUIDL(uidl, client); + if (origRes != currentResolution) { + // force recreating format string + formatStr = null; + } buildDate(); } - public void buildDate() { - dl = new DateLocale(); - DateLocale.setLocale(currentLocale); + protected String getFormatString() { + if (formatStr == null) { + if (currentResolution == RESOLUTION_YEAR) { + formatStr = "yyyy"; // force full year + } else { + + try { + String frmString = LocaleService + .getDateFormat(currentLocale); + frmString = cleanFormat(frmString); + String delim = LocaleService + .getClockDelimiter(currentLocale); + + if (currentResolution >= RESOLUTION_HOUR) { + if (dts.isTwelveHourClock()) { + frmString += " hh"; + } else { + frmString += " HH"; + } + if (currentResolution >= RESOLUTION_MIN) { + frmString += ":mm"; + if (currentResolution >= RESOLUTION_SEC) { + frmString += ":ss"; + if (currentResolution >= RESOLUTION_MSEC) { + frmString += ".SSS"; + } + } + } + if (dts.isTwelveHourClock()) { + frmString += " aaa"; + } + + } - com.itmill.toolkit.terminal.gwt.client.util.DateLocale.SUPPORTED_DF_TOKENS = com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKENS_RESOLUTION_YEAR; - if (currentResolution == IDateField.RESOLUTION_MONTH) { - com.itmill.toolkit.terminal.gwt.client.util.DateLocale.SUPPORTED_DF_TOKENS = com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKENS_RESOLUTION_MONTH; - } else if (currentResolution >= IDateField.RESOLUTION_DAY) { - com.itmill.toolkit.terminal.gwt.client.util.DateLocale.SUPPORTED_DF_TOKENS = com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKENS_RESOLUTION_DAY; + formatStr = frmString; + } catch (LocaleNotLoadedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } } + return formatStr; + } - format = new SimpleDateFormat(cleanFormat(dts.getDateFormat())); - format.setLocale(dl); + /** + * + */ + protected void buildDate() { // Create the initial text for the textfield - String dateText = ""; + String dateText; if (date != null) { - dateText = format.format(date); - - if (currentResolution >= IDateField.RESOLUTION_HOUR) { - com.itmill.toolkit.terminal.gwt.client.util.DateLocale.SUPPORTED_DF_TOKENS = com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKENS_RESOLUTION_ALL; - int h = date.getHours(); - if (h > 11 && dts.isTwelveHourClock()) { - h -= 12; - } - final int m = currentResolution > IDateField.RESOLUTION_HOUR ? date - .getMinutes() - : 0; - dateText += " " + (h < 10 ? "0" + h : "" + h) - + dts.getClockDelimeter() + (m < 10 ? "0" + m : "" + m); - } - if (currentResolution >= IDateField.RESOLUTION_SEC) { - final int s = date.getSeconds(); - dateText += dts.getClockDelimeter() - + (s < 10 ? "0" + s : "" + s); - } - if (currentResolution == IDateField.RESOLUTION_MSEC) { - final int ms = getMilliseconds(); - String text = "" + ms; - if (ms < 10) { - text = "00" + text; - } else if (ms < 100) { - text = "0" + text; - } - dateText += "." + text; - } - if (currentResolution >= IDateField.RESOLUTION_HOUR - && dts.isTwelveHourClock()) { - dateText += " " - + (date.getHours() < 12 ? dts.getAmPmStrings()[0] : dts - .getAmPmStrings()[1]); - } + dateText = DateTimeFormat.getFormat(getFormatString()).format(date); + } else { + dateText = ""; } text.setText(dateText); @@ -108,50 +115,9 @@ public class ITextualDate extends IDateField implements Paintable, public void onChange(Widget sender) { if (sender == text) { if (!text.getText().equals("")) { - com.itmill.toolkit.terminal.gwt.client.util.DateLocale.SUPPORTED_DF_TOKENS = com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKENS_RESOLUTION_ALL; - if (currentResolution == IDateField.RESOLUTION_YEAR) { - com.itmill.toolkit.terminal.gwt.client.util.DateLocale.SUPPORTED_DF_TOKENS = com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKENS_RESOLUTION_YEAR; - } else if (currentResolution == IDateField.RESOLUTION_MONTH) { - com.itmill.toolkit.terminal.gwt.client.util.DateLocale.SUPPORTED_DF_TOKENS = com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKENS_RESOLUTION_MONTH; - } else if (currentResolution == IDateField.RESOLUTION_DAY) { - com.itmill.toolkit.terminal.gwt.client.util.DateLocale.SUPPORTED_DF_TOKENS = com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKENS_RESOLUTION_DAY; - } - - String f = cleanFormat(dts.getDateFormat()); - - if (currentResolution >= IDateField.RESOLUTION_HOUR) { - f += " " - + (dts.isTwelveHourClock() ? com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_HOUR_12 - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_HOUR_12 - : com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_HOUR_24 - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_HOUR_24) - + dts.getClockDelimeter() - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_MINUTE - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_MINUTE; - } - if (currentResolution >= IDateField.RESOLUTION_SEC) { - f += dts.getClockDelimeter() - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_SECOND - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_SECOND; - } - if (currentResolution == IDateField.RESOLUTION_MSEC) { - f += "." - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_MILLISECOND - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_MILLISECOND - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_MILLISECOND; - } - if (currentResolution >= IDateField.RESOLUTION_HOUR - && dts.isTwelveHourClock()) { - f += " " - + com.itmill.toolkit.terminal.gwt.client.util.DateLocale.TOKEN_AM_PM; - } - - format = new SimpleDateFormat(f); - DateLocale.setLocale(currentLocale); - format.setLocale(dl); - try { - date = format.parse(text.getText()); + date = DateTimeFormat.getFormat(getFormatString()).parse( + text.getText()); } catch (final Exception e) { ApplicationConnection.getConsole().log(e.getMessage()); text.addStyleName(ITextField.CLASSNAME + "-error"); @@ -166,6 +132,7 @@ public class ITextualDate extends IDateField implements Paintable, } } else { + ApplicationConnection.getConsole().log("jep jep"); date = null; } diff --git a/src/com/itmill/toolkit/terminal/gwt/client/util/DateLocale.java b/src/com/itmill/toolkit/terminal/gwt/client/util/DateLocale.java deleted file mode 100644 index 43e40adfd0..0000000000 --- a/src/com/itmill/toolkit/terminal/gwt/client/util/DateLocale.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.itmill.toolkit.terminal.gwt.client.util; - -import java.util.Arrays; -import java.util.List; - -/** - * Date locale support for the {@link SimpleDateParser}. You are encouraged to - * extend this class and provide implementations for other locales. - * - * @author George Georgovassilis - * - */ -public class DateLocale { - public final static String TOKEN_DAY_OF_WEEK = "E"; - - public final static String TOKEN_DAY_OF_MONTH = "d"; - - public final static String TOKEN_MONTH = "M"; - - public final static String TOKEN_YEAR = "y"; - - public final static String TOKEN_HOUR_12 = "h"; - - public final static String TOKEN_HOUR_24 = "H"; - - public final static String TOKEN_MINUTE = "m"; - - public final static String TOKEN_SECOND = "s"; - - public final static String TOKEN_MILLISECOND = "S"; - - public final static String TOKEN_AM_PM = "a"; - - public final static String AM = "AM"; - - public final static String PM = "PM"; - - public static List SUPPORTED_DF_TOKENS = Arrays.asList(new String[] { - TOKEN_DAY_OF_WEEK, TOKEN_DAY_OF_MONTH, TOKEN_MONTH, TOKEN_YEAR, - TOKEN_HOUR_12, TOKEN_HOUR_24, TOKEN_MINUTE, TOKEN_SECOND, - TOKEN_AM_PM }); - - public static List TOKENS_RESOLUTION_ALL = Arrays.asList(new String[] { - TOKEN_DAY_OF_WEEK, TOKEN_DAY_OF_MONTH, TOKEN_MONTH, TOKEN_YEAR, - TOKEN_HOUR_12, TOKEN_HOUR_24, TOKEN_MINUTE, TOKEN_SECOND, - TOKEN_AM_PM }); - - public static List TOKENS_RESOLUTION_YEAR = Arrays - .asList(new String[] { TOKEN_YEAR }); - - public static List TOKENS_RESOLUTION_MONTH = Arrays.asList(new String[] { - TOKEN_YEAR, TOKEN_MONTH }); - - public static List TOKENS_RESOLUTION_DAY = Arrays.asList(new String[] { - TOKEN_YEAR, TOKEN_MONTH, TOKEN_DAY_OF_MONTH }); - - public String[] MONTH_LONG = { "January", "February", "March", "April", - "May", "June", "July", "August", "September", "October", - "November", "December" }; - - public String[] MONTH_SHORT = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" }; - - public String[] WEEKDAY_LONG = { "Sunday", "Monday", "Tuesday", - "Wednesday", "Thursday", "Friday", "Saturday" }; - - public String[] WEEKDAY_SHORT = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", - "Sat" }; - - public static String getAM() { - return AM; - } - - public static String getPM() { - return PM; - } - - public String[] getWEEKDAY_LONG() { - return WEEKDAY_LONG; - } - - public String[] getWEEKDAY_SHORT() { - return WEEKDAY_SHORT; - } - -} diff --git a/src/com/itmill/toolkit/terminal/gwt/client/util/Pattern.java b/src/com/itmill/toolkit/terminal/gwt/client/util/Pattern.java deleted file mode 100644 index 0e041e02d1..0000000000 --- a/src/com/itmill/toolkit/terminal/gwt/client/util/Pattern.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright 2006 Robert Hanson - * - * Licensed 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 com.itmill.toolkit.terminal.gwt.client.util; - -import java.util.ArrayList; -import java.util.List; - -import com.google.gwt.core.client.JavaScriptObject; - -/** - *

- * Implementation of the {@link java.util.regex.Pattern} class with a wrapper - * aroung the Javascript RegExp - * object. As most of the methods delegate to the JavaScript RegExp object, - * certain differences in the declaration and behaviour of regular expressions - * must be expected. - *

- *

- * Please note that neither the {@link java.util.regex.Pattern#compile(String)} - * method nor {@link java.util.regex.Matcher} instances are supported. For the - * later, consider using {@link Pattern#match(String)}. - *

- * - * @author George Georgovassilis - * - */ -public class Pattern { - - /** - * Declares that regular expressions should be matched across line borders. - */ - public final static int MULTILINE = 1; - - /** - * Declares that characters are matched reglardless of case. - */ - public final static int CASE_INSENSITIVE = 2; - - private final JavaScriptObject regExp; - - private static JavaScriptObject createExpression(String pattern, int flags) { - String sFlags = ""; - if ((flags & MULTILINE) != 0) { - sFlags += "m"; - } - if ((flags & CASE_INSENSITIVE) != 0) { - sFlags += "i"; - } - return _createExpression(pattern, sFlags); - } - - private static native JavaScriptObject _createExpression(String pattern, - String flags)/*-{ - return new RegExp(pattern, flags); - }-*/; - - private native void _match(String text, List matches)/*-{ - var regExp = this.@com.itmill.toolkit.terminal.gwt.client.util.Pattern::regExp; - var result = text.match(regExp); - if (result == null) return; - for (var i=0;itrue if matched. - */ - public static boolean matches(String regex, String input) { - return new Pattern(regex).matches(input); - } - - /** - * Escape a provided string so that it will be interpreted as a literal in - * regular expressions. The current implementation does escape each - * character even if not neccessary, generating verbose literals. - * - * @param input - * @return - */ - public static String quote(String input) { - String output = ""; - for (int i = 0; i < input.length(); i++) { - output += "\\" + input.charAt(i); - } - return output; - } - - /** - * Class constructor - * - * @param pattern - * Regular expression - */ - public Pattern(String pattern) { - this(pattern, 0); - } - - /** - * Class constructor - * - * @param pattern - * Regular expression - * @param flags - */ - public Pattern(String pattern, int flags) { - regExp = createExpression(pattern, flags); - } - - /** - * This method is borrowed from the JavaScript RegExp object. It parses a - * string and returns as an array any assignments to parenthesis groups in - * the pattern's regular expression - * - * @param text - * @return Array of strings following java's Pattern convention for groups: - * Group 0 is the entire input string and the remaining groups are - * the matched parenthesis. In case nothing was matched an empty - * array is returned. - */ - public String[] match(String text) { - final List matches = new ArrayList(); - _match(text, matches); - final String arr[] = new String[matches.size()]; - for (int i = 0; i < matches.size(); i++) { - arr[i] = matches.get(i).toString(); - } - return arr; - } - - /** - * Determines wether a provided text matches the regular expression - * - * @param text - * @return - */ - public native boolean matches(String text)/*-{ - var regExp = this.@com.itmill.toolkit.terminal.gwt.client.util.Pattern::regExp; - return regExp.test(text); - }-*/; - - /** - * Returns the regular expression for this pattern - * - * @return - */ - public native String pattern()/*-{ - var regExp = this.@com.itmill.toolkit.terminal.gwt.client.util.Pattern::regExp; - return regExp.source; - }-*/; - - private native void _split(String input, List results)/*-{ - var regExp = this.@com.itmill.toolkit.terminal.gwt.client.util.Pattern::regExp; - var parts = input.split(regExp); - for (var i=0;i - * - * Licensed 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 com.itmill.toolkit.terminal.gwt.client.util; - -import java.util.Date; - -/** - *
- *
Title: - *
SimpleDateFormat
- *

- *

Description: - *
GWT does not implement any of the java.text package, so this class tries - * to fill the void of the missing java.text.SimpleDateFormat class. This - * version however only supports a subset of the date and time patterns - * supported by its java.text counterpart. The pattern symbols supported by this - * class are: - *
- *
E
- *
Day in a week
- *
d
- *
Day of the month
- *
y
- *
Year
- *
M
- *
Month January, Jan, 01, 1
- *
H
- *
Hour in 24 hour format (0-23)
- *
h
- *
Hour in 12 hour format (1-12)
- *
m
- *
Minute of the hour
- *
s
- *
Seconds of the minute
- *
a
- *
am/pm
- *
- * All characters that are not recognised as a date format character are - * translated literally into the output string.
- *

- *

- *

- * A simple date parsing facility has also been implemented resembling the java - * prototype. You can currently parse most numeric patterns but no temporal - * literals (such as day or month names). - *

- * - * @author Jason Essington - * @author George Georgovassilis - * @version $Revision: 0.0 $ - */ -public class SimpleDateFormat { - private final String format; - private DateLocale locale = new DateLocale(); - - /** - * Gets the support locale for formatting and parsing dates - * - * @return - */ - public DateLocale getLocale() { - return locale; - } - - public void setLocale(DateLocale locale) { - this.locale = locale; - } - - public SimpleDateFormat(String pattern) { - format = pattern; - } - - public String format(Date date) { - String f = ""; - if (format != null && format.length() > 0) { - String lastTokenType = null; - String currentToken = ""; - for (int i = 0; i < format.length(); i++) { - final String thisChar = format.substring(i, i + 1); - final String currentTokenType = DateLocale.SUPPORTED_DF_TOKENS - .contains(thisChar) ? thisChar : ""; - if (currentTokenType.equals(lastTokenType) || i == 0) { - currentToken += thisChar; - lastTokenType = currentTokenType; - } else { - if ("".equals(lastTokenType)) { - f += currentToken; - } else { - f += handleToken(currentToken, date); - } - currentToken = thisChar; - lastTokenType = currentTokenType; - } - } - if ("".equals(lastTokenType)) { - f += currentToken; - } else { - f += handleToken(currentToken, date); - } - } - return f; - } - - /** - * takes a date format string and returns the formatted portion of the date. - * For instance if the token is MMMM then the full month name is returned. - * - * @param token - * date format token - * @param date - * date to format - * @return formatted portion of the date - */ - private String handleToken(String token, Date date) { - String response = token; - final String tc = token.substring(0, 1); - if (DateLocale.TOKEN_DAY_OF_WEEK.equals(tc)) { - if (token.length() > 3) { - response = locale.getWEEKDAY_LONG()[date.getDay()]; - } else { - response = locale.getWEEKDAY_SHORT()[date.getDay()]; - } - } else if (DateLocale.TOKEN_DAY_OF_MONTH.equals(tc)) { - if (token.length() == 1) { - response = Integer.toString(date.getDate()); - } else { - response = twoCharDateField(date.getDate()); - } - } else if (DateLocale.TOKEN_MONTH.equals(tc)) { - switch (token.length()) { - case 1: - response = Integer.toString(date.getMonth() + 1); - break; - case 2: - response = twoCharDateField(date.getMonth() + 1); - break; - case 3: - response = locale.MONTH_SHORT[date.getMonth()]; - break; - default: - response = locale.MONTH_LONG[date.getMonth()]; - break; - } - } else if (DateLocale.TOKEN_YEAR.equals(tc)) { - if (token.length() >= 2) { - response = Integer.toString(date.getYear() + 1900); - } else { - response = twoCharDateField(date.getYear()); - } - } else if (DateLocale.TOKEN_HOUR_12.equals(tc)) { - int h = date.getHours(); - if (h == 0) { - h = 12; - } else if (h > 12) { - h -= 12; - } - // if (token.length() > 1) - response = twoCharDateField(h); - // else - // response = Integer.toString(h); - } else if (DateLocale.TOKEN_HOUR_24.equals(tc)) { - // if (token.length() > 1) - response = twoCharDateField(date.getHours()); - // else - // response = Integer.toString(date.getHours()); - } else if (DateLocale.TOKEN_MINUTE.equals(tc)) { - // if (token.length() > 1) - response = twoCharDateField(date.getMinutes()); - // else - // response = Integer.toString(date.getMinutes()); - } else if (DateLocale.TOKEN_SECOND.equals(tc)) { - // if (token.length() > 1) - response = twoCharDateField(date.getSeconds()); - // else - // response = Integer.toString(date.getSeconds()); - } else if (DateLocale.TOKEN_AM_PM.equals(tc)) { - final int hour = date.getHours(); - if (hour > 11) { - response = DateLocale.getPM(); - } else { - response = DateLocale.getAM(); - } - } - return response; - } - - /** - * This is basically just a sneaky way to guarantee that our 1 or 2 digit - * numbers come out as a 2 character string. we add an arbitrary number - * larger than 100, convert this new number to a string, then take the right - * most 2 characters. - * - * @param num - * @return - */ - private String twoCharDateField(int num) { - String res = Integer.toString(num + 1900); - res = res.substring(res.length() - 2); - return res; - } - - private static Date newDate(long time) { - return new Date(time); - } - - /** - * Parses text and returns the corresponding date object. - * - * @param source - * @return java.util.Date - */ - public Date parse(String source) { - return SimpleDateParser.parse(source, format); - }; - -} diff --git a/src/com/itmill/toolkit/terminal/gwt/client/util/SimpleDateParser.java b/src/com/itmill/toolkit/terminal/gwt/client/util/SimpleDateParser.java deleted file mode 100644 index a0543e7156..0000000000 --- a/src/com/itmill/toolkit/terminal/gwt/client/util/SimpleDateParser.java +++ /dev/null @@ -1,174 +0,0 @@ -package com.itmill.toolkit.terminal.gwt.client.util; - -import java.util.Date; - -/** - * This is a simple regular expression based parser for date notations. While - * our aim is to fully support in the future the JDK date parser, currently only - * numeric notations and literals are supported such as - * dd/MM/yyyy HH:mm:ss.SSSS. Each entity is parsed with the same - * number of digits, i.e. for dd two digits will be parsed while - * for d only one will be parsed. - * - * @author George Georgovassilis - * - */ - -public class SimpleDateParser { - - private final static String DAY_IN_MONTH = "d"; - - private final static String MONTH = "M"; - - private final static String YEAR = "y"; - - private final static String LITERAL = "\\"; - - private final static int DATE_PATTERN = 0; - - private final static int REGEX_PATTERN = 1; - - private final static int COMPONENT = 2; - - private final static int REGEX = 0; - - private final static int INSTRUCTION = 1; - - private final static String[] TOKENS[] = { - { "SSSS", "(\\d\\d\\d\\d)", DateLocale.TOKEN_MILLISECOND }, - { "SSS", "(\\d\\d\\d)", DateLocale.TOKEN_MILLISECOND }, - { "SS", "(\\d\\d)", DateLocale.TOKEN_MILLISECOND }, - { "S", "(\\d)", DateLocale.TOKEN_MILLISECOND }, - { "ss", "(\\d\\d)", DateLocale.TOKEN_SECOND }, - { "s", "(\\d\\d)", DateLocale.TOKEN_SECOND }, - { "mm", "(\\d\\d)", DateLocale.TOKEN_MINUTE }, - { "m", "(\\d\\d)", DateLocale.TOKEN_MINUTE }, - { "HH", "(\\d\\d)", DateLocale.TOKEN_HOUR_24 }, - { "H", "(\\d{1,2})", DateLocale.TOKEN_HOUR_24 }, - { "hh", "(\\d\\d)", DateLocale.TOKEN_HOUR_12 }, - { "h", "(\\d{1,2})", DateLocale.TOKEN_HOUR_12 }, - { "dd", "(\\d\\d)", DateLocale.TOKEN_DAY_OF_MONTH }, - { "d", "(\\d{1,2})", DateLocale.TOKEN_DAY_OF_MONTH }, - { "MM", "(\\d\\d)", DateLocale.TOKEN_MONTH }, - { "M", "(\\d{1,2})", DateLocale.TOKEN_MONTH }, - { "yyyy", "(\\d\\d\\d\\d)", DateLocale.TOKEN_YEAR }, - { "yyy", "(\\d\\d\\d\\d)", DateLocale.TOKEN_YEAR }, - { "yy", "(\\d\\d\\d\\d)", DateLocale.TOKEN_YEAR }, - { "y", "(\\d{1,2})", DateLocale.TOKEN_YEAR }, - { "a", "(\\S{1,4})", DateLocale.TOKEN_AM_PM } }; - - private final Pattern regularExpression; - - private String instructions = ""; - - private static void _parse(String format, String[] args) { - if (format.length() == 0) { - return; - } - if (format.startsWith("'")) { - format = format.substring(1); - final int end = format.indexOf("'"); - if (end == -1) { - throw new IllegalArgumentException("Unmatched single quotes."); - } - args[REGEX] += Pattern.quote(format.substring(0, end)); - format = format.substring(end + 1); - } - for (int i = 0; i < TOKENS.length; i++) { - final String[] row = TOKENS[i]; - final String datePattern = row[DATE_PATTERN]; - if (!format.startsWith(datePattern)) { - continue; - } - format = format.substring(datePattern.length()); - args[REGEX] += row[REGEX_PATTERN]; - args[INSTRUCTION] += row[COMPONENT]; - _parse(format, args); - return; - } - args[REGEX] += Pattern.quote("" + format.charAt(0)); - format = format.substring(1); - _parse(format, args); - } - - private static void load(Date date, String text, String component, - String input, Pattern regex) { - if (component.equals(DateLocale.TOKEN_MILLISECOND)) { - date.setTime(date.getTime() / 1000 * 1000 + Integer.parseInt(text)); - } - - if (component.equals(DateLocale.TOKEN_SECOND)) { - date.setSeconds(Integer.parseInt(text)); - } - - if (component.equals(DateLocale.TOKEN_MINUTE)) { - date.setMinutes(Integer.parseInt(text)); - } - - if (component.equals(DateLocale.TOKEN_HOUR_24)) { - date.setHours(Integer.parseInt(text)); - } - - if (component.equals(DateLocale.TOKEN_HOUR_12)) { - int h = Integer.parseInt(text); - final String token = com.itmill.toolkit.terminal.gwt.client.DateLocale - .getPM(); - final String which = input.substring(input.length() - - token.length()); // Assumes - // both - // AM - // and - // PM - // tokens - // have - // same - // length - if (which.equalsIgnoreCase(token)) { - h += 12; - } - date.setHours(h); - } - - if (component.equals(DateLocale.TOKEN_DAY_OF_MONTH)) { - date.setDate(Integer.parseInt(text)); - } - if (component.equals(DateLocale.TOKEN_MONTH)) { - date.setMonth(Integer.parseInt(text) - 1); - } - if (component.equals(DateLocale.TOKEN_YEAR)) { - // TODO: fix for short patterns - date.setYear(Integer.parseInt(text) - 1900); - } - - } - - public SimpleDateParser(String format) { - final String[] args = new String[] { "", "" }; - _parse(format, args); - regularExpression = new Pattern(args[REGEX]); - instructions = args[INSTRUCTION]; - } - - public Date parse(String input) { - final Date date = new Date(0, 0, 0, 0, 0, 0); - final String matches[] = regularExpression.match(input); - if (matches == null) { - throw new IllegalArgumentException(input + " does not match " - + regularExpression.pattern()); - } - if (matches.length - 1 != instructions.length()) { - throw new IllegalArgumentException("Different group count - " - + input + " does not match " + regularExpression.pattern()); - } - for (int group = 0; group < instructions.length(); group++) { - final String match = matches[group + 1]; - load(date, match, "" + instructions.charAt(group), input, - regularExpression); - } - return date; - } - - public static Date parse(String input, String pattern) { - return new SimpleDateParser(pattern).parse(input); - } -} -- 2.39.5