KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > xslt > FormatHelper


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.view.xslt;
18
19 import java.text.DateFormat JavaDoc;
20 import java.text.NumberFormat JavaDoc;
21 import java.text.SimpleDateFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
26
27 import org.springframework.core.NestedRuntimeException;
28 import org.w3c.dom.Document JavaDoc;
29 import org.w3c.dom.Element JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31
32 /**
33  * Xalan extension functions to provide date and currency formatting
34  * beyond the capabilities of XSLT 1.0 or 1.1.
35  *
36  * <p>Note that all extension functions are static.
37  * These extension functions must be declared to use this class.
38  *
39  * <p>Based on an example by Taylor Cowan.
40  *
41  * @author Rod Johnson
42  * @see AbstractXsltView
43  */

44 public class FormatHelper {
45
46     /**
47      * Creates a formatted-date node with the given ISO language and country strings.
48      * If either the language or country parameters are null, the system default
49      * <code>Locale</code> will be used.
50      *
51      * @param time the time in ms since the epoch that should be used to obtain
52      * the formatted date
53      * @param language the two character language code required to construct a
54      * <code>java.util.Locale</code>
55      * @param country the two character country code required to construct a
56      * <code>java.util.Locale</code>
57      * @return a W3C Node containing child Elements with the formatted date-time fragments
58      */

59     public static Node JavaDoc dateTimeElement(long time, String JavaDoc language, String JavaDoc country) {
60         Locale JavaDoc locale = getLocale(language, country);
61         return dateTimeElement(time, locale);
62     }
63
64     /**
65      * Creates a formatted-date node with system default <code>Locale</code>
66      *
67      * @param time the time in ms since the epoch that should be used to obtain
68      * the formatted date
69      * @return a W3C Node containing child Elements with the formatted date-time fragments
70      */

71     public static Node JavaDoc dateTimeElement(long time) {
72         return dateTimeElement(time, Locale.getDefault());
73     }
74
75     /**
76      * Creates a formatted-date node with the given <code>Locale</code>
77      *
78      * @param time the time in ms since the epoch that should be used to obtain
79      * the formatted date
80      * @param locale the <code>java.util.Locale</code> to determine the date formatting
81      * @return a W3C Node containing child Elements with the formatted date-time fragments
82      */

83     public static Node JavaDoc dateTimeElement(long time, Locale JavaDoc locale) {
84         try {
85             Document JavaDoc doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
86             Element JavaDoc dateNode = doc.createElement("formatted-date");
87
88             // Works in most locales
89
SimpleDateFormat JavaDoc df = (SimpleDateFormat JavaDoc) DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale);
90
91             Date JavaDoc d = new Date JavaDoc(time);
92             df.applyPattern("MMMM");
93             addChild(dateNode, "month", df.format(d));
94             df.applyPattern("EEEE");
95             addChild(dateNode, "day-of-week", df.format(d));
96             df.applyPattern("yyyy");
97             addChild(dateNode, "year", df.format(d));
98             df.applyPattern("dd");
99             addChild(dateNode, "day-of-month", df.format(d));
100             df.applyPattern("h");
101             addChild(dateNode, "hours", df.format(d));
102             df.applyPattern("mm");
103             addChild(dateNode, "minutes", df.format(d));
104             df.applyPattern("a");
105             addChild(dateNode, "am-pm", df.format(d));
106             return dateNode;
107         }
108         catch (Exception JavaDoc ex) {
109             throw new XsltFormattingException("Failed to create XML date element", ex);
110         }
111     }
112
113     /**
114      * Format a currency amount in a given locale.
115      *
116      * @param amount the currency value to format
117      * @param locale the <code>java.util.Locale</code> to use to format the amount
118      * @return a formatted <code>String</code> representing the amount
119      */

120     public static String JavaDoc currency(double amount, Locale JavaDoc locale) {
121         NumberFormat JavaDoc nf = NumberFormat.getCurrencyInstance(locale);
122         return nf.format(amount);
123     }
124
125     /**
126      * Format a currency amount for a given language and country.
127      *
128      * @param amount the currency value to format
129      * @param language the two character language code required to construct a
130      * <code>java.util.Locale</code>
131      * @param country the two character country code required to construct a
132      * <code>java.util.Locale</code>
133      * @return a formatted <code>String</code> representing the amount
134      */

135     public static String JavaDoc currency(double amount, String JavaDoc language, String JavaDoc country) {
136         Locale JavaDoc locale = getLocale(language, country);
137         return currency(amount, locale);
138     }
139
140     /**
141      * Utility method for adding text nodes.
142      */

143     private static void addChild(Node JavaDoc parent, String JavaDoc name, String JavaDoc text) {
144         Element JavaDoc child = parent.getOwnerDocument().createElement(name);
145         child.appendChild(parent.getOwnerDocument().createTextNode(text));
146         parent.appendChild(child);
147     }
148     
149     /**
150      * Utility method to guarantee a Locale.
151      */

152     private static Locale JavaDoc getLocale(String JavaDoc language, String JavaDoc country) {
153         Locale JavaDoc locale = null;
154         if (language == null || country == null) {
155             locale = Locale.getDefault();
156         }
157         else {
158             locale = new Locale JavaDoc(language, country);
159         }
160         return locale;
161     }
162
163
164     /**
165      * XsltFormattingException
166      *
167      * @author Rod Johnson
168      */

169     public static class XsltFormattingException extends NestedRuntimeException {
170         public XsltFormattingException(String JavaDoc msg, Throwable JavaDoc ex) {
171             super(msg, ex);
172         }
173     }
174
175 }
176
Popular Tags