dirkv 2004/03/21 13:10:18
Modified: scaffold/src/java/org/apache/commons/scaffold/text
ConvertUtils.java Merge.java
Log:
Bugzilla Bug 27689: [scaffold] changing to the Apache 2.0 license removed source
file contents.
Revision Changes Path
1.11 +1007 -15
jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/text/ConvertUtils.java
Index: ConvertUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/text/ConvertUtils.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- ConvertUtils.java 28 Feb 2004 03:35:45 -0000 1.10
+++ ConvertUtils.java 21 Mar 2004 21:10:18 -0000 1.11
@@ -1,5 +1,20 @@
-package org.apache.commons.scaffold.text;
+/*
+ * Copyright 2002,2004 The Apache Software Foundation.
+ *
+ * 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 org.apache.commons.scaffold.text;
import java.text.DateFormat;
import java.text.DecimalFormat;
@@ -19,19 +34,996 @@
import java.sql.Timestamp;
+/**
+ * An <b>experimental</b> class with some standard conversion
+ * utilities. Needs more proof of concept and unit testing.
+ *
+ * @author Ted Husted
+ * @author OK State DEQ
+ * @author WXXI Public Broadcasting Council
+ * @version $Revision$ $Date$
+ */
+public class ConvertUtils {
/*
- * Copyright 2002,2004 The Apache Software Foundation.
- *
- * 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.
- */
+ protected Locale[] availableLocales = null;
+ public static Locale[] getAvailableLocales() {
+ return availableLocales;
+ }
+ public static void setAvailableLocales(Locale[] _availableLocales) {
+ availableLocales = _availableLocales;
+ }
+
+*/
+
+
+ /**
+ * This is an all-static utility class.
+ * A private constructor prevents inadvertent instantiation.
+ */
+ private ConvertUtils() {
+ ; // empty
+ }
+
+
+// ---------------------------------------------------- Text Separators
+
+ /**
+ * An empty string.
+ */
+ public static String STRING_EMPTY = "";
+
+
+ /**
+ * An empty string.
+ * @deprecated Use STRING_EMPTY
+ */
+ public static String EMPTY_STRING = STRING_EMPTY;
+
+
+ /**
+ * An single-space string.
+ */
+ public static final String STRING_SPACE = " ";
+
+
+ /**
+ * Space character.
+ */
+ public static final char SPACE = ' ';
+
+
+ /**
+ * Horizontal tab character.
+ */
+ public static final char HORIZONTAL_TABULATION = '\t'; // (aka u0009)
+
+
+ /**
+ * Line feed character.
+ */
+ public static final char LINE_FEED = '\r'; // (aka u000A);
+
+
+ /**
+ * Vertical tab character.
+ */
+ public static final char VERTICAL_TABULATION = '\u000B';
+
+
+ /**
+ * Form feed character.
+ */
+ public static final char FORM_FEED = '\u000C';
+
+
+ /**
+ * Carriage return character.
+ */
+ public static final char CARRIAGE_RETURN = '\n'; // (aka u000D)
+
+
+ /**
+ * File separator character.
+ */
+ public static final char FILE_SEPARATOR = '\u001C';
+
+
+ /**
+ * Group separator character.
+ */
+ public static final char GROUP_SEPARATOR = '\u001D';
+
+
+ /**
+ * Record separator character.
+ */
+ public static final char RECORD_SEPARATOR = '\u001E';
+
+
+ /**
+ * Unit separator character.
+ */
+ public static final char UNIT_SEPARATOR = '\u001F';
+
+
+ /**
+ * Array of line separator characters.
+ *
http://java.sun.com/j2se/1.3/docs/api/java/lang/Character.html#isWhitespace(char)
+ */
+ public static final char[] SEPARATORS = {
+ HORIZONTAL_TABULATION,
+ LINE_FEED,
+ VERTICAL_TABULATION,
+ FORM_FEED,
+ CARRIAGE_RETURN,
+ FILE_SEPARATOR,
+ GROUP_SEPARATOR,
+ RECORD_SEPARATOR,
+ UNIT_SEPARATOR
+ };
+
+// --------------------------------------------------------- Tokenizers
+
+
+ /**
+ * Return array of tokens,
+ * using the result of <code>getTokeSep()</code> as the
+ * separator.
+ * Blanks are trimmed from tokens.
+ *
+ * @param parameter The string to tokenize into an array
+ */
+ public static String[] tokensToArray(String tokens, String separator) {
+
+ StringTokenizer tokenizer =
+ new StringTokenizer(tokens,separator);
+ int i = 0;
+ String[] array = new String[tokenizer.countTokens()];
+ while (tokenizer.hasMoreTokens()) {
+ String token = tokenizer.nextToken().trim();
+ if ((token==null) || (token.length()==0)) continue;
+ array[i++] = token;
+ }
+ return array;
+
+ } // end tokensToArray
+
+
+ /**
+ * Return list of tokens,
+ * using the result of <code>getTokeSep()</code> as the
+ * separator.
+ * Blanks are trimmed from tokens.
+ *
+ * @param parameter The string to tokenize into an array
+ */
+ public static List tokensToList(String tokens, String separator) {
+
+ StringTokenizer tokenizer =
+ new StringTokenizer(tokens,separator);
+ List list = new java.util.ArrayList(tokenizer.countTokens());
+ while (tokenizer.hasMoreTokens()) {
+ String token = tokenizer.nextToken().trim();
+ if ((token==null) || (token.length()==0)) continue;
+ list.add(token);
+ }
+ return list;
+
+ } // end tokensToList
+
+
+// ------------------------------------------------------- Text Methods
+
+
+ /**
+ * Returns true if null or trims to an empty string.
+ * @deprecated Use blank instead.
+ */
+ public static boolean isBlank(String s) {
+ return blank(s);
+ }
+
+
+ /**
+ * Returns true if null or zero.
+ * @deprecated Use blank instead.
+ */
+ public static boolean isBlank(Integer key) {
+ return blank(key);
+ }
+
+
+ /**
+ * Returns true if null or trims to an empty string.
+ * @deprecated Use blankValue instead.
+ */
+ public static boolean isBlankValue(String s) {
+ return blankValue(s);
+ }
+
+
+ /**
+ * Returns true if null or trims to an empty string.
+ * @deprecated Use blank instead.
+ */
+ public static boolean blank(String s) {
+ return ((null==s) || (STRING_EMPTY.equals(s.trim())));
+ }
+
+
+ /**
+ * Returns true if null or zero.
+ * @deprecated Use blank instead.
+ */
+ public static boolean blank(Number key) {
+ return ((null==key) || (0==key.byteValue()));
+ }
+
+
+ /**
+ * Returns true if null, trims to an empty string,
+ * or to "0".
+ */
+ public static boolean blankValue(String s) {
+ if (null==s) return true;
+ String _s = s.trim();
+ return ((STRING_EMPTY.equals(_s)) || (STRING_ZERO.equals(_s)));
+ }
+
+
+ /**
+ * Return a trimmed or empty string (but not null).
+ */
+ public static String toTrimOrEmpty(String string) {
+ if (null==string) return STRING_EMPTY;
+ return string.trim();
+ }
+
+
+ /**
+ * Returns null or a trimmed uppercase string.
+ */
+ public static String toUpperOrNull(String string) {
+ if (null!=string)
+ return string.toUpperCase().trim();
+ return null;
+ }
+
+
+ /**
+ * The token that signifies the begnning of a query string ["?"].
+ */
+ public static String QS_START = "?";
+
+
+ /**
+ * The token that delimits two or more attributes of a query string ["&"].
+ */
+ public static String QS_DELIM = "&";
+
+
+ /**
+ * The token that seperates an attribute name and value.
+ */
+ public static String QS_SEP = "=";
+
+
+ /**
+ * Appends name=value parameter.
+ */
+ public static String addParam(String path, String name, String value) {
+ StringBuffer uri = new StringBuffer(path);
+ boolean isQuery = (path.indexOf(QS_START)>=0);
+ if (isQuery)
+ uri.append(QS_DELIM);
+ else
+ uri.append(QS_START);
+ uri.append(name);
+ uri.append(QS_SEP);
+ uri.append(value);
+ return uri.toString();
+
+ }
+
+
+ /**
+ * Appends name=value parameters to path from Map.
+ */
+ public static String addParams(String path, Map parameters) {
+
+ if (null==path) path = new String();
+
+ if ((null==parameters) || (parameters.isEmpty())) return path;
+
+ StringBuffer uri = new StringBuffer(path);
+ boolean isQuery = (path.indexOf(QS_START)>=0);
+ if (isQuery)
+ uri.append(QS_DELIM);
+ else
+ uri.append(QS_START);
+
+ Set entries = parameters.entrySet();
+ for (Iterator i = entries.iterator(); i.hasNext(); ) {
+ Entry e = (Entry) i.next();
+ uri.append(e.getKey());
+ uri.append(QS_SEP);
+ uri.append(e.getValue());
+ }
+ return uri.toString();
+
+ }
+
+
+ /**
+ * Returns parameters as a series of hidden HTML fields.
+ */
+ public static String renderHiddenFields(Map parameters) {
+
+ if ((null==parameters) || (parameters.isEmpty())) return new String();
+
+ StringBuffer html = new StringBuffer();
+ Set entries = parameters.entrySet();
+ for (Iterator i = entries.iterator(); i.hasNext(); ) {
+ html.append("<input type='hidden' name='");
+ Entry e = (Entry) i.next();
+ html.append(e.getKey());
+ html.append("' value='");
+ html.append(e.getValue());
+ html.append("' />");
+ }
+ return html.toString();
+
+ }
+
+
+ /**
+ * Replace line returns with spaces.
+ */
+ private static String stripLn(String string) {
+
+ // :FIXME: Better way to buffer the interim strings?
+ for (int i=0; i<SEPARATORS.length; i++) {
+ string = string.replace(
+ SEPARATORS[i],
+ SPACE
+ );
+ }
+ return string;
+ }
+
+
+
+// ----------------------------------------------------- Numeric Values
+
+
+ /**
+ * An Double 0.
+ */
+ public static Double DOUBLE_ZERO = new Double(0);
+
+
+ /**
+ * An Double 1.
+ */
+ public static Double DOUBLE_ONE = new Double((double) 1.0);
+
+
+ /**
+ * An Integer 0.
+ */
+ public static Integer INTEGER_ZERO = new Integer(0);
+
+
+ /**
+ * An Integer 1.
+ */
+ public static Integer INTEGER_ONE = new Integer(1);
+
+
+ /**
+ * A Short 0.
+ */
+ public static Short SHORT_ZERO = new Short((short) 0);
+
+
+ /**
+ * A Short 1.
+ */
+ public static Short SHORT_ONE = new Short((short) 1);
+
+
+ /**
+ * A String 0.
+ */
+ public static String STRING_ZERO = "0";
+
+
+ /**
+ * A String 1.
+ */
+ public static String STRING_ONE = "1";
+
+
+
+
+// ---------------------------------------------------- Numeric Methods
+
+
+ /**
+ * Return String with of digits only (0..9).
+ * http://java.sun.com/j2se/1.4/docs/api/java/lang/Character.html
+ */
+ public static String getDigits(String s) {
+ if (s==null) return null;
+ int n = s.length();
+ StringBuffer sb = new StringBuffer(n);
+ for (int i = 0; i < n; i++) {
+ char c = s.charAt(i);
+ if (Character.isDigit(c)) sb.append(c);
+ }
+ return (sb.toString());
+ }
+
+
+ /**
+ * Returns number formatted for default or given locale.
+ */
+ public static String getNumber(Number value, Locale locale) {
+ if (locale==null)
+ return (NumberFormat.getInstance().format(value));
+ return (NumberFormat.getInstance(locale).format(value));
+ }
+
+
+ /**
+ * Returns percent formatted for default or given locale.
+ */
+ public static String getPercent(Number value, Locale locale) {
+ if (locale==null)
+ return (NumberFormat.getPercentInstance().format(value));
+ return (NumberFormat.getPercentInstance(locale).format(value));
+ }
+
+
+ /* -- Is there a use case for this?
+
+ public static String getInteger(Number value, Locale locale) {
+ if (locale==null)
+ return (NumberFormat.getIntegerInstance().format(value));
+ return (NumberFormat.getIntegerInstance(locale).format(value));
+ }
+
+ */
+
+
+ /*
+ * Returns whether the last digit of numeric string is a parity
+ * check on the others per the "primes of nines" method.
+ * Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
+ * @param Number - Number to check.
+ * Must be all digits and not null.
+ */
+ public static boolean luhnCheck(String number) {
+ int no_digit = number.length();
+ int oddoeven = no_digit & 1;
+ long sum = 0;
+ for (int count = 0; count < no_digit; count++) {
+ int digit = Integer.parseInt(
+ String.valueOf(number.charAt(count)));
+ if ( ( (count & 1) ^ oddoeven) ==0 ) { // not
+ digit *= 2;
+ if (digit > 9) digit -= 9;
+ };
+ sum += digit;
+ };
+ if (sum == 0) return false;
+ if (sum % 10 == 0) return true;
+ return false;
+ }
+
+
+ /**
+ * Returns number with the appropriate digit appended
+ * so that is passes a "luhnCheck".
+ * @param Number - Number to process.
+ * Must be all digits and not null.
+ */
+ public static String addLuhnDigit(String number) {
+ // I don't actually understand the alogorithm
+ // so we just use brute force to find the digit.
+ char[] digits = {'1','2','3','4','5','6','7','8','9','0'};
+ int c = number.length();
+ StringBuffer tryNumber = new StringBuffer(number + digits[0]);
+ int i;
+ for (i=0; i<10; i++) {
+ tryNumber.setCharAt(c,digits[i]);
+ if (luhnCheck(tryNumber.toString()))
+ break;
+ }
+ return tryNumber.toString();
+ }
+
+
+// ------------------------------------------------------------ Decimal
+
+
+ /**
+ * Default decimal pattern.
+ * http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html
+ */
+ public static String DECIMAL_PATTERN ="###,###.###";
+
+
+ /**
+ * Symbols that can be used in a decimal pattern.
+ */
+ public static DecimalFormatSymbols getGenericDecimal(Locale locale) {
+ DecimalFormatSymbols symbols =
+ new DecimalFormatSymbols(locale);
+ symbols.setGroupingSeparator('`'); // :FIXME: Want apostrophe here
+ return symbols;
+ }
+
+
+ /**
+ * Return decimal number formatted for default or given locale.
+ */
+ public static String getDecimal(Number value, Locale locale) {
+ if (locale==null)
+ return (DecimalFormat.getInstance().format(value));
+ return (DecimalFormat.getInstance().format(value));
+ }
+
+
+ /**
+ * Return decimal number formatted for default or given locale
+ * using given pattern.
+ */
+ public static String getDecimal(Number value, Locale locale, String pattern) {
+ NumberFormat formatter;
+ if (locale==null)
+ formatter = new java.text.DecimalFormat(pattern);
+ else {
+ formatter = NumberFormat.getNumberInstance(locale);
+ DecimalFormat df = (DecimalFormat) formatter;
+ df.applyPattern(pattern);
+ return df.format(value);
+ }
+ return (formatter.format(value));
+ }
+
+
+ /**
+ * Return decimal for default locale using standard pattern.
+ */
+ public static String getDecimal(Number value) {
+ return getDecimal(value,(Locale) null,DECIMAL_PATTERN);
+ }
+
+
+// ----------------------------------------------------------- Currency
+
+
+ /**
+ * Standard currency pattern.
+ * http://java.sun.com/docs/books/tutorial/i18n/format/numberpattern.html
+ */
+ public static String CURRENCY_PATTERN ="$" + DECIMAL_PATTERN;
+
+
+ /**
+ * Return currency for default locale using standard pattern.
+ */
+ public static String getCurrency(Number value) {
+ return getDecimal(value,null,CURRENCY_PATTERN);
+ }
+
+
+// --------------------------------------------------------------- Date
+
+
+ /**
+ * Default style for dates and times.
+ */
+ public static int DEFAULT = DateFormat.DEFAULT;
+
+
+ /**
+ * Short style for dates and times.
+ */
+ public static int SHORT = DateFormat.SHORT;
+
+
+ /**
+ * Medium style for dates and times.
+ */
+ public static int MEDIUM = DateFormat.MEDIUM;
+
+
+ /**
+ * Long style for dates and times.
+ */
+ public static int LONG = DateFormat.LONG;
+
+
+ /**
+ * Full style for dates and times.
+ */
+ public static int FULL = DateFormat.FULL;
+
+
+ /**
+ * Default lenient setting for getDate.
+ */
+ private static boolean LENIENT_DATE = false;
+
+
+ /**
+ * A "default" date format.
+ */
+ public static String ESCAPE_DATE_PATTERN = "yyyy-mm-dd";
+
+
+ /**
+ * Convert String to Date using given format.
+ * Returns null if conversion fails.
+ * Set lenient=false to disallow dates like 2001-9-32.
+ * http://java.sun.com/j2se/1.4/docs/api/java/text/SimpleDateFormat.html
+ * @author Hal Deadman
+ */
+ public static Date getDate(String dateDisplay,
+ String format, boolean lenient) {
+ if (dateDisplay == null) {
+ return null;
+ }
+ DateFormat df = null;
+ try {
+ if (format==null) {
+ df = new SimpleDateFormat();
+ }
+ else {
+ df = new SimpleDateFormat(format);
+ }
+ // setLenient avoids allowing dates like 9/32/2001
+ // which would otherwise parse to 10/2/2001
+ df.setLenient(false);
+ return df.parse(dateDisplay);
+ }
+ catch(ParseException e) {
+ return null;
+ }
+ }
+
+
+ /**
+ * Convert String to Date using given format.
+ * Returns null if conversion fails.
+ * Uses "strict" coNversion (lenient=false).
+ * @author Hal Deadman
+ */
+ public static Date getDate(String dateDisplay, String format) {
+ return getDate(dateDisplay,format,LENIENT_DATE);
+ }
+
+
+ /**
+ * Convert String to Date using a medium (weekday day month year) format.
+ * Returns null if conversion fails.
+ * Uses "strict" coNversion (lenient=false).
+ * @author Hal Deadman
+ */
+ public static Date getDate(String dateDisplay) {
+ return getDate(dateDisplay,null,LENIENT_DATE);
+ }
+
+
+ /**
+ * Return Date value using a String.
+ * Null or conversion error returns null.
+ * @param String representing Date
+ */
+ public static Date toDate(String string) {
+ if (string==null)
+ return null;
+ else try {
+ return getDate(string);
+ } catch (Throwable t) {
+ return null;
+ }
+ }
+
+
+ /**
+ * Convert date to String for given locale in given style.
+ * A null locale will return the default locale.
+ */
+ public static String getDate(Date date, Locale locale, int style) {
+ if (locale==null)
+ return (DateFormat.getDateInstance(style).format(date));
+ return (DateFormat.getDateInstance(style,locale).format(date));
+ }
+
+
+ /**
+ * Convert date to String for default locale in given style.
+ * A null locale will return the default locale.
+ */
+ public static String getDate(Date date, int style) {
+ return getDate(date,(Locale) null,style);
+ }
+
+
+ /**
+ * Convert date to String for default locale in DEFAULT style.
+ * A null locale will return the default locale.
+ */
+ public static String getDate(Date date) {
+ return getDate(date,(Locale) null,DEFAULT);
+ }
+
+
+ /**
+ * Return String value representing Date.
+ * Null returns null.
+ * @param Date
+ */
+ public static String toString(Date date) {
+ if (date==null)
+ return null;
+ else
+ return getDate(date);
+ }
+
+
+ /**
+ * Return String value representing Date.
+ * Null returns null.
+ * @param Date
+ */
+ public static String toEscape(Date date) {
+ if (date==null)
+ return null;
+ DateFormat df = null;
+ try {
+ df = new SimpleDateFormat(ESCAPE_DATE_PATTERN);
+ } catch (Throwable t) {
+ return null;
+ }
+ df.setLenient(false);
+ return df.format(date);
+ }
+
+
+// ---------------------------------------------------------- Timestamp
+
+ /**
+ * Date separator ["-"].
+ */
+ public static final String DATE_SEPARATOR = "-";
+
+
+ /**
+ * Time separator [":"].
+ */
+ public static final String TIME_SEPARATOR = ":";
+
+
+ /**
+ * Date Time separator [" "].
+ */
+ public static final String DATE_TIME_SEPARATOR = STRING_SPACE;
+
+
+ /**
+ * String to prepend to time [HH:MM:SS.d]
+ * to create a Timestamp escape string ["0002-11-30"].
+ */
+ public static final String TIMESTAMP_DATE_ZERO = "0002-11-30";
+
+
+ /**
+ * String to append to date [YEAR-MM-DD]
+ * to create a Timestamp escape string [" 00:00:00.0"].
+ * Note: includes leading space.
+ */
+ public static final String TIMESTAMP_TIME_ZERO = " 00:00:00.0";
+
+
+ /**
+ * Escape string representing
+ * "November 30, 0002 00:00:00".
+ */
+ public static String ZERO_TIMESTAMP_DISPLAY = "2-11-30 00:00:00";
+
+
+ /**
+ * Timestamp representing ""November 30, 0002 00:00:00".
+ */
+ public static Timestamp ZERO_TIMESTAMP = new Timestamp((long) 00000000000000);
+
+
+
+ /**
+ * Escape string to create Timestamp representing
+ * "January 1, 1970 00:00:00".
+ */
+ public static String NULL_TIMESTAMP_DISPLAY = "1970-01-01 00:00:00";
+
+
+ /**
+ * @deprecated Use NULL_TIMESTAMP_DISPLAY.
+ */
+ public static String NULL_TIMESTAMP_TEXT = "NULL_TIMESTAMP_DISPLAY";
+
+
+ /**
+ * Value needed to create Timestamp representing
+ * "January 1, 1970 00:00:00".
+ * From the documentation, you would think this would be
+ * Timestamp(0), but empirical tests show it to be
+ * Timestamp(18000000).
+ */
+ public static long NULL_TIME = (long) 18000000;
+
+
+ /**
+ * Timestamp representing "January 1, 1970 00:00:00".
+ */
+ public static Timestamp NULL_TIMESTAMP = new Timestamp(NULL_TIME);
+
+
+
+ /**
+ * Timestamp representing "December 31, 2029, 23:59:59.9"
+ */
+ public static Timestamp MAX_TIMESTAMP = Timestamp.valueOf("2029-12-31
23:59:59.999");
+
+
+ /**
+ * Return the String representing "January 1, 1970 00:00:00".
+ */
+ public static String getTimestampDisplayNull() {
+ return NULL_TIMESTAMP_DISPLAY;
+ }
+
+
+ /**
+ * Return the String representing the current timestamp;
+ */
+ public static String getTimestampDisplay() {
+ return getTimestamp(new Timestamp(System.currentTimeMillis()));
+ }
+
+
+ /**
+ * @deprecated Use getTimestampDisplay.
+ */
+ public static String getTimestampText() {
+ return getTimestampDisplay();
+ }
+
+
+ /**
+ * Return null if timestamp is null or equals
+ * "January 1, 1970 00:00:00".
+ */
+ public static boolean isNull(Timestamp timestamp) {
+ return ((timestamp==null) || (timestamp.getTime()==NULL_TIME));
+ }
+
+
+ /**
+ * Factory method to return timestamp initialized to
+ * current system time.
+ * For timestamp as a String in the default format,
+ * use <code>getTimestamp().toString()</code>.
+ */
+ public static Timestamp getTimestamp() {
+ return new Timestamp(System.currentTimeMillis());
+ }
+
+
+ /**
+ * Convert timestamp to String for given locale in given style.
+ * A null locale will return the default locale.
+ */
+ public static String getTimestamp(Timestamp timestamp,
+ Locale locale, int style) {
+ Date date = (Date) timestamp;
+ if (locale==null)
+ return (DateFormat.getDateTimeInstance(style,style).
+ format(date));
+ return (DateFormat.getDateTimeInstance(style,style,locale).
+ format(date));
+ }
+
+
+ /**
+ * Convert date to String for default locale in given style.
+ * A null locale will return the default locale.
+ */
+ public static String getTimestamp(Timestamp timestamp,
+ int style) {
+ return getTimestamp(timestamp,(Locale) null,style);
+ }
+
+
+ /**
+ * Convert date to String for default locale in DEFAULT style.
+ * A null locale will return the default locale.
+ */
+ public static String getTimestamp(Timestamp timestamp) {
+ return getTimestamp(timestamp,(Locale) null,DEFAULT);
+ }
+
+
+ /**
+ * Return Timestamp value using a String.
+ * Null or conversion error returns null.
+ * @param String representing Timestamp
+ */
+ public static Timestamp toTimestamp(String string) {
+ if (string==null)
+ return null;
+ else try {
+ return Timestamp.valueOf(string);
+ } catch (Throwable t) {
+ return null;
+ }
+ }
+
+
+ /**
+ * Return a Timestamp based on the parameters.
+ * Any nulls or conversion error returns null.
+ * @param year The year
+ * @param month The month
+ * @param day The day
+ * @returns Timestamp for year-month-day
+ */
+ public static Timestamp toTimestamp(String year, String month, String day) {
+
+ if ((null==year) || (null==month) || (null==day)) return null;
+
+ StringBuffer sb = new StringBuffer();
+ // YEAR-MM-DD 00:00:00.0
+ sb.append(year); sb.append(DATE_SEPARATOR);
+ sb.append(month); sb.append(DATE_SEPARATOR);
+ sb.append(day);
+
+ sb.append(TIMESTAMP_TIME_ZERO);
+
+ return toTimestamp(sb.toString());
+ }
+
+
+ /**
+ * Return String value representing Timestamp.
+ * Null returns null.
+ * @param Timestamp
+ */
+ public static String toString(Timestamp timestamp) {
+ if (timestamp==null)
+ return null;
+ else
+ return timestamp.toString();
+ }
+
+
+// ----------------------------------------------------------- Internet
+
+
+ /**
+ * Return the integer value of an IP address in dotted octet form,
+ * like that returned by request.getRemotehost).
+ * :FIXME: Needs to be implemented; just returns zero.
+ * @param Timestamp
+ */
+ public static Integer ipNode(String ipAddress) {
+ return INTEGER_ZERO;
+ }
+
+
+}
+
1.2 +17 -3
jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/text/Merge.java
Index: Merge.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/scaffold/src/java/org/apache/commons/scaffold/text/Merge.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Merge.java 14 Aug 2002 17:52:00 -0000 1.1
+++ Merge.java 21 Mar 2004 21:10:18 -0000 1.2
@@ -1,8 +1,22 @@
-package org.apache.commons.scaffold.text;
+/*
+ * Copyright 2001,2004 The Apache Software Foundation.
+ *
+ * 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 org.apache.commons.scaffold.text;
import java.util.Map;
-
/**
* Merge utilities.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]