View Javadoc

1   /*
2    * Created on 30.03.2004
3    *
4    * To change the template for this generated file go to
5    * Window - Preferences - Java - Code Generation - Code and Comments
6    */
7   package net.stff.ical.validators;
8   
9   import java.text.DateFormatSymbols;
10  import java.util.List;
11  import java.util.Locale;
12  import java.util.Vector;
13  
14  /***
15   * @author buntekuh
16   *
17   * To change the template for this generated type comment go to
18   * Window - Preferences - Java - Code Generation - Code and Comments
19   */
20  public class DateValidator{
21  	/***
22  	 * @param year
23  	 * @param month
24  	 * @param day
25  	 * @param hour
26  	 * @param minute
27  	 * @return
28  	 */
29  	public static final int YEARFLAG = 1;
30  	public static final int MONTHFLAG = 2;
31  	public static final int DAYFLAG = 4;
32  	public static final int HOURSFLAG = 8;
33  	public static final int MINUTESFLAG = 16;
34  	public static final String INVALIDYEAR = "DateValidator.invalidYear";
35  	public static final String MONTHOUTOFRANGE = "DateValidator.monthOutOfRange";
36  	public static final String MONTHINVALIDNAME = "DateValidator.monthInvalidName";
37  	public static final String DAYOUTOFRANGE = "DateValidator.dayOutOfRange";
38  	public static final String INVALIDDAY = "DateValidator.invalidDay";
39  	public static final String HOURSOUTOFRANGE = "DateValidator.hoursOutOfRange";
40  	public static final String INVALIDHOURS = "DateValidator.invalidHours";
41  	public static final String MINUTESOUTOFRANGE = "DateValidator.minutesOutOfRange";
42  	public static final String INVALIDMINUTES = "DateValidator.invalidMinutes";
43  
44  	public static List validateDate(String year, String month, String day, String hours, String minutes, Locale l, int flags){
45  		Vector errors = new Vector();
46  		String error;
47  
48  		if ((flags & YEARFLAG) > 0){
49  			error = validateYear(year.trim());
50  
51  			if (error != null){
52  				errors.add(error);
53  			}
54  		}
55  
56  		if ((flags & MONTHFLAG) > 0){
57  			error = validateMonth(month.trim(), l);
58  
59  			if (error != null){
60  				errors.add(error);
61  			}
62  		}
63  
64  		if ((flags & DAYFLAG) > 0){
65  			error = validateDay(day.trim());
66  
67  			if (error != null){
68  				errors.add(error);
69  			}
70  		}
71  
72  		if ((flags & HOURSFLAG) > 0){
73  			error = validateHours(hours.trim());
74  
75  			if (error != null){
76  				errors.add(error);
77  			}
78  		}
79  
80  		if ((flags & MINUTESFLAG) > 0){
81  			error = validateMinutes(minutes.trim());
82  
83  			if (error != null){
84  				errors.add(error);
85  			}
86  		}
87  
88  		return errors;
89  	}
90  
91  	public static String validateYear(String y){
92  		try{
93  			Integer.parseInt(y);
94  		}
95  		catch (NumberFormatException e){
96  			//if its not a number its not a year
97  			return INVALIDYEAR;
98  		}
99  
100 		return null;
101 	}
102 
103 	public static String validateMonth(String m, Locale l){
104 		int i;
105 		DateFormatSymbols dfs;
106 
107 		try{
108 			i = Integer.parseInt(m);
109 
110 			if ((i < 1) || (i > 12)){
111 				return MONTHOUTOFRANGE;
112 			}
113 		}
114 		catch (NumberFormatException e){
115 			//if its not a number it might be the name of a Month
116 			if (l == null){
117 				l = Locale.GERMAN;
118 			}
119 
120 			dfs = new DateFormatSymbols(l);
121 
122 			String[] months = dfs.getMonths();
123 
124 			for (int n = 0; n < months.length; n++){
125 				if (months[n].equalsIgnoreCase(m)){
126 					return null;
127 				}
128 			}
129 
130 			return MONTHINVALIDNAME;
131 		}
132 
133 		return null;
134 	}
135 
136 	public static String validateDay(String d){
137 		int i;
138 
139 		try{
140 			i = Integer.parseInt(d);
141 
142 			if ((i < 1) || (i > 31)){
143 				return DAYOUTOFRANGE;
144 			}
145 		}
146 		catch (NumberFormatException e){
147 			//if its not a number its not a day
148 			return INVALIDDAY;
149 		}
150 
151 		return null;
152 	}
153 
154 	public static String validateHours(String h){
155 		int i;
156 
157 		try{
158 			i = Integer.parseInt(h);
159 
160 			if ((i < 0) || (i > 23)){
161 				return HOURSOUTOFRANGE;
162 			}
163 		}
164 		catch (NumberFormatException e){
165 			//if its not a number its not an hour
166 			return INVALIDHOURS;
167 		}
168 
169 		return null;
170 	}
171 
172 	public static String validateMinutes(String m){
173 		int i;
174 
175 		try{
176 			i = Integer.parseInt(m);
177 
178 			if ((i < 0) || (i > 59)){
179 				return MINUTESOUTOFRANGE;
180 			}
181 		}
182 		catch (NumberFormatException e){
183 			//if its not a number its not a minute
184 			return INVALIDMINUTES;
185 		}
186 
187 		return null;
188 	}
189 }