1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.myfaces.commons.util;
20
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24
25 import java.text.DateFormat;
26 import java.text.ParseException;
27 import java.text.SimpleDateFormat;
28
29 import java.util.Date;
30 import java.util.Locale;
31
32 import javax.faces.application.Application;
33 import javax.faces.context.FacesContext;
34 import javax.faces.el.ValueBinding;
35
36
37
38 /**
39 * Utility class for Tag classes
40 *
41 * @version $Name: $ ($Revision: adfrt/faces/adf-faces-impl/src/main/java/oracle/adfinternal/view/faces/taglib/util/TagUtils.java#1 $) $Date: 11-nov-2005.14:59:38 $
42 *
43 */
44 public final class TagUtils
45 {
46 private static final Log LOG = LogFactory.getLog(TagUtils.class);
47
48 private TagUtils()
49 {
50 }
51
52 public static ValueBinding getValueBinding(String valueBindingExpression)
53 {
54 FacesContext context = FacesContext.getCurrentInstance();
55 Application app = context.getApplication();
56 return app.createValueBinding(valueBindingExpression);
57 }
58
59 public static void assertNotNull(Object object)
60 {
61 if (null == object)
62 throw new NullPointerException();
63 }
64
65 // Helpful with tag auto generation. Though this isn't really required.
66 /**
67 * Return the same string. It is there for convenience and makes life easy
68 * while auto generating tags.
69 * @param value
70 * @return
71 */
72 public static String getString(
73 String value)
74 {
75 return value;
76 }
77
78 /**
79 * String --> boolean
80 * @param value
81 * @return
82 */
83 public static boolean getBoolean(
84 String value)
85 {
86 return Boolean.valueOf(value).booleanValue();
87 }
88
89 /**
90 * String --> int
91 * @param value
92 * @return
93 */
94 public static int getInteger(
95 String value)
96 {
97 return Integer.valueOf(value).intValue();
98
99 }
100
101 /**
102 * String --> long
103 * @param value
104 * @return
105 */
106 public static long getLong(
107 String value)
108 {
109 return Long.valueOf(value).longValue();
110
111 }
112
113 /**
114 * String --> long
115 * @param value
116 * @return
117 */
118 public static double getDouble(
119 String value)
120 {
121 return Double.valueOf(value).doubleValue();
122
123 }
124
125 /**
126 * String --> long
127 * @param value
128 * @return
129 */
130 public static float getFloat(
131 String value)
132 {
133 return Float.valueOf(value).floatValue();
134
135 }
136
137 /**
138 * These are normally NMTOKEN type in attributes
139 * String --> String[]
140 * @param value
141 * @return
142
143 public static String[] getStringArray(
144 String value) throws ParseException
145 {
146 return getTokensArray(value);
147 }
148 */
149
150 /**
151 * ISO Date String --> Date
152 * @param value
153 * @return
154 */
155 public static Date getDate(
156 String value)
157 {
158 return parseISODate(value);
159 }
160
161 /**
162 * String --> Locale
163 * @param value
164 * @return
165 */
166 public static Locale getLocale(
167 String value)
168 {
169 return getLocaleInternal(value);
170 }
171
172 /**
173 * String --> TimeZone
174 * @param value
175 * @return
176
177 public static TimeZone getTimeZone(
178 String value)
179 {
180 return DateUtils.getSupportedTimeZone(value);
181 }
182 */
183
184 public static boolean isValueReference(String expression)
185 {
186 if (null != expression)
187 {
188 int start = expression.indexOf("#{");
189 if ((start >= 0) && (expression.indexOf('}', start + 1) >= 0))
190 return true;
191 }
192
193 return false;
194 }
195
196
197
198 /**
199 * Takes a string that is a composite of tokens, extracts tokens delimited
200 * by any whitespace character sequence combination and returns a String
201 * array of such tokens.
202 * @throws ParseException In case of invalid character in the specified
203 * composite. The only invalid character is a comma (',').
204
205 private static String[] _getTokensArray(String tokenComposite)
206 throws ParseException
207 {
208 if (tokenComposite == null || "".equals(tokenComposite))
209 return null;
210
211 return XMLUtils.parseNameTokens(tokenComposite);
212 }
213 */
214
215 /**
216 * Parse a string into a java.util.Date object. The
217 * string must be in ISO 9601 format (yyyy-MM-dd).
218 * @todo why not throw the exception in a different format?
219 * why do we kill it here and return null?
220 */
221 static private final Date parseISODate(String stringValue)
222 {
223 try
224 {
225 return getDateFormat().parse(stringValue);
226 }
227 catch (ParseException pe)
228 {
229 if (LOG.isInfoEnabled()) {
230 LOG.info("CANNOT_PARSE_VALUE_INTO_DATE_WITH_YYYY_MM_DD_PATTERN "+ stringValue, pe);
231 }
232 return null;
233 }
234 }
235
236 private static Locale getLocaleInternal(String locale)
237 {
238 String localeStr = locale.replace('-','_');
239 String[] tokens = localeStr.split("[_]", 3);
240 Locale locl = null;
241
242 if ( tokens.length == 1)
243 {
244 locl = new Locale(tokens[0]); //lang
245 }
246 else if (tokens.length == 2)
247 {
248 locl = new Locale(tokens[0], tokens[1]); // lang + country
249 }
250 else if (tokens.length == 3 )
251 {
252 locl = new Locale(tokens[0], tokens[1], tokens[2]); // lang + country + variant
253 }
254 else
255 {
256 if(LOG.isWarnEnabled())
257 LOG.warn("tokens length should not be greater than 3.");
258 }
259 return locl;
260 }
261
262 // We rely strictly on ISO 8601 formats
263 private static DateFormat getDateFormat()
264 {
265 return new SimpleDateFormat("yyyy-MM-dd");
266 }
267 }