001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.j2me.payment;
028:
029: import java.io.InputStream;
030: import java.io.IOException;
031: import java.io.UnsupportedEncodingException;
032:
033: import java.util.TimeZone;
034: import java.util.Calendar;
035: import java.util.Date;
036:
037: import com.sun.midp.util.Properties;
038: import com.sun.midp.util.DateParser;
039:
040: import com.sun.midp.installer.JadProperties;
041:
042: import com.sun.midp.i18n.Resource;
043: import com.sun.midp.i18n.ResourceConstants;
044:
045: /**
046: * This class implements utility methods used in the
047: * <code>com.sun.j2me.payment</code> package.
048: *
049: * @version
050: */
051: public class CldcUtils extends Utils {
052:
053: /** The offset value between Utils and ResourceConstants resource keys. */
054: public static final int PAYMENT_ID_OFFSET = Utils.PAYMENT_PROV_SEL_DLG_NEVER
055: - ResourceConstants.PAYMENT_PROV_SEL_DLG_NEVER;
056:
057: /** Creates a new instance of <code>CldcUtils</code>. */
058: public CldcUtils() {
059: }
060:
061: /**
062: * Parses properties from the given input stream and returns them as an
063: * instance of the <code>Properties</code> class. The <code>charset</code>
064: * parameter contains the actual character encoding of the input stream.
065: *
066: * @param is the input stream which contains properties
067: * @param charset the character set of the input stream
068: * @return the properties read from the stream
069: * @throws UnsupportedEncodingException if <code>charset</code> is not
070: * supported by the implementation
071: * @throws IOException if the input stream doesn't have properties in the
072: * correct format
073: */
074: public Properties loadProperties(InputStream is, String charset)
075: throws UnsupportedEncodingException, IOException {
076: JadProperties props = new JadProperties();
077: props.load(is, charset);
078:
079: return props;
080: }
081:
082: /**
083: * Returns a resource string for the given key.
084: *
085: * @param key the key
086: * @return the string assigned to the key
087: */
088: public String getString(int key) {
089: if (key >= PAYMENT_ID_OFFSET) {
090: key -= PAYMENT_ID_OFFSET;
091: }
092: return Resource.getString(key);
093: }
094:
095: /**
096: * Returns a modified resource string for the given key. It replaces any
097: * occurence of %1, %2, ... in the original resource string with the first,
098: * second, ... string from <code>values</code>.
099: *
100: * @param key the key
101: * @param values the replacement strings
102: * @return the modified resource string
103: */
104: public String getString(int key, String[] values) {
105: if (key >= PAYMENT_ID_OFFSET) {
106: key -= PAYMENT_ID_OFFSET;
107: }
108: return Resource.getString(key, values);
109: }
110:
111: /**
112: * Parses a date string according to the ISO 8601 standard.
113: *
114: * @param date the date string in the format YYYY-MM-DDTHH:MM[:SS][[+|-]
115: * HH[MM]]
116: * @return the number of milliseconds elapsed since 1970-1-1 GMT to this
117: * date
118: * @throws IllegalArgumentException if the format of the date string is
119: * incorrect or the date is invalid
120: */
121: public long parseISODate(String date) {
122: return DateParser.parseISO(date);
123: }
124:
125: /**
126: * Converts the given date to the string formatted according to the
127: * ISO 8601 standard.
128: *
129: * @param date the date as the number of milliseconds elapsed since
130: * 1970-1-1 GMT
131: * @return the date string
132: */
133: public String formatISODate(long date) {
134: TimeZone gmtTimeZone = TimeZone.getTimeZone("GMT");
135: Calendar gmtCalendar = Calendar.getInstance(gmtTimeZone);
136:
137: gmtCalendar.setTime(new Date(date));
138:
139: StringBuffer buffer = new StringBuffer();
140: char[] temp = new char[4];
141:
142: formatNumber(temp, gmtCalendar.get(Calendar.YEAR), 4);
143: buffer.append(temp, 0, 4);
144: buffer.append('-');
145: formatNumber(temp, gmtCalendar.get(Calendar.MONTH) + 1, 2);
146: buffer.append(temp, 0, 2);
147: buffer.append('-');
148: formatNumber(temp, gmtCalendar.get(Calendar.DAY_OF_MONTH), 2);
149: buffer.append(temp, 0, 2);
150: buffer.append(' ');
151:
152: formatNumber(temp, gmtCalendar.get(Calendar.HOUR_OF_DAY), 2);
153: buffer.append(temp, 0, 2);
154: buffer.append(':');
155: formatNumber(temp, gmtCalendar.get(Calendar.MINUTE), 2);
156: buffer.append(temp, 0, 2);
157: buffer.append(':');
158: formatNumber(temp, gmtCalendar.get(Calendar.SECOND), 2);
159: buffer.append(temp, 0, 2);
160:
161: buffer.append('Z');
162:
163: return buffer.toString();
164: }
165:
166: private void formatNumber(char[] dest, int number, int digits) {
167: int index = digits;
168: while (index > 0) {
169: dest[--index] = (char) ((number % 10) + '0');
170: number = number / 10;
171: }
172: }
173:
174: }
|