KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > xpath > datamodel > SchemaFormater


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 XQuark Group.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.xpath.datamodel;
24
25 import java.text.DateFormat JavaDoc;
26 import java.text.ParseException JavaDoc;
27 import java.text.SimpleDateFormat JavaDoc;
28 import java.util.Date JavaDoc;
29
30 import org.xquark.schema.SchemaException;
31 import org.xquark.schema.datatypes.PrimitiveType;
32
33 /**
34  * <code>SchemaFormater</code> is a class for formatting Schema primitive types into user-friendly strings and backward.
35  * <P><strong>Examples Using the french Locale:</strong>
36  * <P>
37  * <pre>
38  * Type Schema String User-friendly String
39  * --------------------------------------------------------
40  * date 2001-01-15 >> 15 janvier 2001
41  * 2001-01-15 << 15/01/01
42  * time 13:30:25+02:00 >> 12:30:25 CET
43  * gMonth --01-- >> janvier
44  * --01-- << 01
45  * --01-- << 1
46  * --01-- << janv.
47  * </pre>
48  *
49  * @created 20 novembre 2001
50  */

51 public class SchemaFormater {
52     public static final String JavaDoc RCSRevision = "$Revision: 1.1 $";
53     public static final String JavaDoc RCSName = "$Name: $";
54
55     /**
56      * Used for formatting primitive type <code>date</code> into user-friendly string.
57      */

58     protected static DateFormat JavaDoc[] dateFormats;
59     /**
60      * Used for formatting user-friendly strings into primitive type <code>date</code>.
61      */

62     protected static DateFormat JavaDoc ufDateFormat = DateFormat.getDateInstance(DateFormat.LONG);
63
64     /**
65      * Used for formatting primitive type <code>time</code> into user-friendly string.
66      */

67     protected static DateFormat JavaDoc[] timeFormats;
68     /**
69      * Used for formatting user-friendly strings into primitive type <code>time</code>.
70      */

71     protected static DateFormat JavaDoc ufTimeFormat = DateFormat.getTimeInstance(DateFormat.LONG);
72
73     /**
74      * Used for formatting primitive type <code>dateTime</code> into user-friendly string.
75      */

76     protected static DateFormat JavaDoc[] dateTimeFormats;
77     /**
78      * Used for formatting user-friendly strings into primitive type <code>dateTime</code>.
79      */

80     protected static DateFormat JavaDoc ufDateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG);
81
82     /**
83      * Used for formatting primitive type <code>gMonth</code> into user-friendly string.
84      */

85     protected static DateFormat JavaDoc[] gMonthFormats;
86     /**
87      * Used for formatting user-friendly strings into primitive type <code>gMonth</code>.
88      */

89     protected static DateFormat JavaDoc ufGMonthFormat = new SimpleDateFormat JavaDoc("MMMM");
90
91     /**
92      * Used for formatting primitive type <code>gDay</code> into user-friendly string.
93      */

94     protected static DateFormat JavaDoc[] gDayFormats;
95     /**
96      * Used for formatting user-friendly strings into primitive type <code>gDay</code>.
97      */

98     protected static DateFormat JavaDoc ufGDayFormat = new SimpleDateFormat JavaDoc("d");
99
100     /**
101      * Used for formatting primitive type <code>gYear</code> into user-friendly string.
102      */

103     protected static DateFormat JavaDoc[] gYearFormats;
104
105
106     /**
107      * Formats a string entered by the user into a form conforming the given schema type.
108      *
109      * @param input The string entered by the user.
110      * @param type The given schema type.
111      * @return The formatted string.
112      */

113     public static String JavaDoc toSchemaForm(String JavaDoc input, PrimitiveType type) {
114         switch (type.getType()) {
115             case PrimitiveType.DATE:
116                 for (int i = 0; i < dateFormats.length; i++) {
117                     try {
118                         return type.toXMLString(dateFormats[i].parse(input), null);
119                     }
120                     catch (ParseException JavaDoc e) {
121                     }
122                 }
123                 break;
124             case PrimitiveType.TIME:
125                 for (int i = 0; i < timeFormats.length; i++) {
126                     try {
127                         return type.toXMLString(timeFormats[i].parse(input), null);
128                     }
129                     catch (ParseException JavaDoc e) {
130                     }
131                 }
132                 break;
133             case PrimitiveType.DATE_TIME:
134                 for (int i = 0; i < dateTimeFormats.length; i++) {
135                     try {
136                         return type.toXMLString(dateTimeFormats[i].parse(input), null);
137                     }
138                     catch (ParseException JavaDoc e) {
139                     }
140                 }
141                 break;
142             case PrimitiveType.GMONTH:
143                 for (int i = 0; i < gMonthFormats.length; i++) {
144                     try {
145                         return type.toXMLString(gMonthFormats[i].parse(input), null);
146                     }
147                     catch (ParseException JavaDoc e) {
148                     }
149                 }
150                 break;
151             case PrimitiveType.GDAY:
152                 for (int i = 0; i < gDayFormats.length; i++) {
153                     try {
154                         return type.toXMLString(gDayFormats[i].parse(input), null);
155                     }
156                     catch (ParseException JavaDoc e) {
157                     }
158                 }
159                 break;
160             case PrimitiveType.GYEAR:
161                 for (int i = 0; i < gYearFormats.length; i++) {
162                     try {
163                         return type.toXMLString(gYearFormats[i].parse(input), null);
164                     }
165                     catch (ParseException JavaDoc e) {
166                     }
167                 }
168                 break;
169         }
170         return input;
171     }
172
173
174     /**
175      * Formats the representation of a schema type into a user-friendly string.
176      *
177      * @param input The representation.
178      * @param type The given schema type.
179      * @return The formatted string.
180      */

181     public static String JavaDoc toUserFriendlyForm(String JavaDoc input, PrimitiveType type) {
182         switch (type.getType()) {
183             case PrimitiveType.DATE:
184                 try {
185                     return ufDateFormat.format((Date JavaDoc) type.convert(input, false, null));
186                 }
187                 catch (SchemaException e) {
188                 }
189                 break;
190             case PrimitiveType.TIME:
191                 try {
192                     return ufTimeFormat.format((Date JavaDoc) type.convert(input, false, null));
193                 }
194                 catch (SchemaException e) {
195                 }
196                 break;
197             case PrimitiveType.DATE_TIME:
198                 try {
199                     return ufDateTimeFormat.format((Date JavaDoc) type.convert(input, false, null));
200                 }
201                 catch (SchemaException e) {
202                 }
203                 break;
204             case PrimitiveType.GMONTH:
205                 try {
206                     return ufGMonthFormat.format((Date JavaDoc) type.convert(input, false, null));
207                 }
208                 catch (SchemaException e) {
209                 }
210                 break;
211             case PrimitiveType.GDAY:
212                 try {
213                     return ufGDayFormat.format((Date JavaDoc) type.convert(input, false, null));
214                 }
215                 catch (SchemaException e) {
216                 }
217                 break;
218         }
219         return input;
220     }
221
222     static {
223         int[] styles = new int[]{DateFormat.FULL, DateFormat.LONG, DateFormat.MEDIUM, DateFormat.SHORT};
224         int numStyles = styles.length;
225         int i;
226         int j;
227
228         //dateFormats
229
dateFormats = new DateFormat JavaDoc[numStyles];
230         for (i = 0; i < numStyles; i++) {
231             dateFormats[i] = DateFormat.getDateInstance(styles[i]);
232         }
233
234         //timeFormats
235
timeFormats = new DateFormat JavaDoc[numStyles];
236         for (i = 0; i < numStyles; i++) {
237             timeFormats[i] = DateFormat.getTimeInstance(styles[i]);
238         }
239
240         //dateTimeFormats
241
dateTimeFormats = new DateFormat JavaDoc[numStyles * numStyles];
242         for (i = 0; i < numStyles; i++) {
243             for (j = 0; j < numStyles; j++) {
244                 dateTimeFormats[i + j * numStyles] = DateFormat.getDateTimeInstance(styles[i], styles[j]);
245             }
246         }
247
248         //gMonthFormats
249
gMonthFormats = new DateFormat JavaDoc[]{new SimpleDateFormat JavaDoc("M"), new SimpleDateFormat JavaDoc("MMM"), new SimpleDateFormat JavaDoc("MMMM")};
250
251         //gDayFormats
252
gDayFormats = new DateFormat JavaDoc[]{new SimpleDateFormat JavaDoc("d")};
253
254         //gYearFormats
255
gYearFormats = new DateFormat JavaDoc[]{new SimpleDateFormat JavaDoc("y")};
256
257     }
258
259 }
260
Popular Tags