KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > swing > datefield > DateFormatParser


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.swing.datefield;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Calendar JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * Parses date format strings into {@link org.openharmonise.swing.datefield.DateFormatBlock}
28  * objects. The format string is based on the {@link java.text.SimpleDateFormat} one with some
29  * caveats:
30  *
31  * 1) Only numerical representations of dates are allowed, except for the Era
32  * field with can only be represented as "AD" or "BC".
33  *
34  * 2) The format length for each field needs to be the same length as the expected
35  * input. So for a 4 digit year you must use "yyyy", where as the {@link java.text.SimpleDateFormat}
36  * class will accept "yyy" and "yyyyy". The only excpetion to this rule is the Era field
37  * which must be represented as "G".
38  *
39  * @author Matthew Large
40  * @version $Revision: 1.1 $
41  *
42  */

43 public class DateFormatParser {
44
45     /**
46      * Full date format string.
47      */

48     private String JavaDoc m_sFormat = null;
49     
50     /**
51      * List of {@link DateFormatBlock} objects.
52      */

53     private ArrayList JavaDoc m_formatBlocks = new ArrayList JavaDoc();
54     
55     /**
56      * Map of pattern characters to {@link Calendar} date fields.
57      */

58     private static final HashMap JavaDoc m_mPatternInfo = new HashMap JavaDoc();
59     
60     static {
61         m_mPatternInfo.put("G", new Integer JavaDoc(Calendar.ERA));
62         m_mPatternInfo.put("y", new Integer JavaDoc(Calendar.YEAR));
63         m_mPatternInfo.put("M", new Integer JavaDoc(Calendar.MONTH));
64         m_mPatternInfo.put("w", new Integer JavaDoc(Calendar.WEEK_OF_YEAR));
65         m_mPatternInfo.put("W", new Integer JavaDoc(Calendar.WEEK_OF_MONTH));
66         m_mPatternInfo.put("D", new Integer JavaDoc(Calendar.DAY_OF_YEAR));
67         m_mPatternInfo.put("d", new Integer JavaDoc(Calendar.DAY_OF_MONTH));
68         m_mPatternInfo.put("F", new Integer JavaDoc(Calendar.DAY_OF_WEEK_IN_MONTH));
69         m_mPatternInfo.put("E", new Integer JavaDoc(Calendar.DAY_OF_WEEK));
70         m_mPatternInfo.put("a", new Integer JavaDoc(Calendar.AM_PM));
71         m_mPatternInfo.put("H", new Integer JavaDoc(Calendar.HOUR_OF_DAY));
72         m_mPatternInfo.put("k", new Integer JavaDoc(Calendar.HOUR_OF_DAY));
73         m_mPatternInfo.put("K", new Integer JavaDoc(Calendar.HOUR));
74         m_mPatternInfo.put("h", new Integer JavaDoc(Calendar.HOUR));
75         m_mPatternInfo.put("m", new Integer JavaDoc(Calendar.MINUTE));
76         m_mPatternInfo.put("s", new Integer JavaDoc(Calendar.SECOND));
77         m_mPatternInfo.put("S", new Integer JavaDoc(Calendar.MILLISECOND));
78         m_mPatternInfo.put("z", new Integer JavaDoc(Calendar.ZONE_OFFSET));
79         m_mPatternInfo.put("Z", new Integer JavaDoc(Calendar.ZONE_OFFSET));
80     }
81
82     /**
83      * Constructs a new date format parser.
84      *
85      * @param sFormat Date format
86      */

87     public DateFormatParser(String JavaDoc sFormat) {
88         super();
89         this.m_sFormat = sFormat;
90         this.setup();
91     }
92     
93     /**
94      * Configures this date format parser.
95      *
96      */

97     private void setup() {
98         int nPos = 0;
99         int nInputPosition = 0;
100         
101         while(nPos<=this.m_sFormat.length()-1) {
102             DateFormatBlock block = this.getNextBlock(nPos, nInputPosition);
103             this.m_formatBlocks.add(block);
104             nPos = nPos + block.getFormat().length();
105             nInputPosition = nInputPosition + block.getInputLength();
106         }
107     }
108     
109     /**
110      * Returns the dateformat blocks.
111      *
112      * @return List of {@link DateFormatBlock} objects
113      */

114     public List JavaDoc getFormatBlocks() {
115         return this.m_formatBlocks;
116     }
117     
118     /**
119      * Returns the next date format pattern in the date format string.
120      *
121      * @param nPosition Current position in date format string
122      * @param nInputPosition Current input date value string position
123      * @return Next date format block
124      */

125     private DateFormatBlock getNextBlock(int nPosition, int nInputPosition) {
126         DateFormatBlock block = null;
127         boolean bFetchingActiveBlock = false;
128         String JavaDoc sFormatBlock = new String JavaDoc();
129         
130         int nCurrentPosition = nPosition;
131         
132         String JavaDoc sFirst = this.m_sFormat.substring(nPosition, nPosition+1);
133         bFetchingActiveBlock = DateFormatParser.m_mPatternInfo.keySet().contains(sFirst);
134         
135         int nCalendarField = -1;
136         if(bFetchingActiveBlock) {
137             nCalendarField = ((Integer JavaDoc)DateFormatParser.m_mPatternInfo.get(sFirst)).intValue();
138         }
139         
140         String JavaDoc sTemp = sFirst;
141         boolean bEnd = false;
142         
143         while((bFetchingActiveBlock && DateFormatParser.m_mPatternInfo.keySet().contains(sTemp))
144                 || ((!bFetchingActiveBlock && !DateFormatParser.m_mPatternInfo.keySet().contains(sTemp)))) {
145             sFormatBlock = sFormatBlock + sTemp;
146             nCurrentPosition++;
147             if(nCurrentPosition<=this.m_sFormat.length()-1) {
148                 sTemp = this.m_sFormat.substring(nCurrentPosition, nCurrentPosition+1);
149             } else {
150                 break;
151             }
152         }
153         
154         int nInputLength = -1;
155         
156         if(sFormatBlock.equalsIgnoreCase("G")) {
157             nInputLength = 2;
158         } else if(!bFetchingActiveBlock) {
159             String JavaDoc sFormatTemp = sFormatBlock.replaceAll("'", "");
160             nInputLength = sFormatTemp.length();
161         } else {
162             nInputLength = sFormatBlock.length();
163         }
164         
165         String JavaDoc sClearedValue = null;
166         String JavaDoc sEntryValue = null;
167         
168         if(bFetchingActiveBlock) {
169             if(sFormatBlock.equalsIgnoreCase("G")) {
170                 sClearedValue = "AD";
171                 sEntryValue = "G";
172             } else if(sFormatBlock.equalsIgnoreCase("yyyy")) {
173                 nInputLength = 8;
174                 sClearedValue = " ccyy";
175                 sEntryValue = " ccyy";
176             } else {
177                 StringBuffer JavaDoc sBuff = new StringBuffer JavaDoc(sFormatBlock);
178                 if(sBuff.length()<nInputLength) {
179                     for(int i=sBuff.length(); i<nInputLength; i++) {
180                         sBuff.append(" ");
181                     }
182                 }
183                 sClearedValue = sBuff.toString();
184                 sEntryValue = sBuff.toString();
185             }
186         } else {
187             sClearedValue = sFormatBlock.replaceAll("'", "");
188             sEntryValue = sFormatBlock.replaceAll("'", "");
189         }
190         
191         block = new DateFormatBlock(sFormatBlock, bFetchingActiveBlock, nInputPosition, nInputLength, sClearedValue, nCalendarField, sEntryValue);
192         
193         return block;
194     }
195     
196     public static void main(String JavaDoc[] args) {
197         DateFormatParser dfp = new DateFormatParser("yyyy-MM-dd G");
198         DateFormatParser dfp2 = new DateFormatParser("yyyy-MM-dd'T'HH:mm:ss G");
199     }
200
201
202 }
203
Popular Tags