KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > MessageMap


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35 package com.micronova.jsp.tag;
36
37 import com.micronova.util.*;
38 import java.util.*;
39 import javax.servlet.jsp.*;
40 import javax.servlet.jsp.jstl.fmt.LocaleSupport;
41
42 public class MessageMap extends MapBean
43 {
44     protected PageContext _pageContext;
45     protected String JavaDoc _baseName;
46     protected String JavaDoc _defaultValue;
47
48     public MessageMap(PageContext pageContext)
49     {
50         super();
51
52         _pageContext = pageContext;
53         _baseName = null;
54         _defaultValue = "";
55     }
56
57     public void setBaseName(String JavaDoc baseName)
58     {
59         _baseName = baseName;
60     }
61
62     public void setDefaultValue(String JavaDoc defaultValue)
63     {
64         _defaultValue = defaultValue;
65     }
66
67     public Object JavaDoc evaluate(String JavaDoc expression, Class JavaDoc objectClass) throws Exception JavaDoc
68     {
69         return EL.evaluateExpression(_pageContext, "resourceMapExpression", expression.replaceAll("@\\{", "\\$\\{"), objectClass);
70     }
71
72     /** ObjectSource implementation */
73
74     public Object JavaDoc getObject(Object JavaDoc client, Object JavaDoc key)
75     {
76         PageContext pageContext = _pageContext;
77         String JavaDoc baseName = _baseName;
78
79         Object JavaDoc object = null;
80
81         try
82         {
83             String JavaDoc keyString = key.toString();
84
85             List partList = StringUtil.split(keyString, ':');
86
87             int partCount = partList.size();
88
89             if (partCount < 2)
90             {
91                 String JavaDoc name = (String JavaDoc)evaluate(keyString, String JavaDoc.class);
92
93                 if (baseName != null)
94                 {
95                     object = LocaleSupport.getLocalizedMessage(pageContext, name, baseName);
96                 }
97                 else
98                 {
99                     object = LocaleSupport.getLocalizedMessage(pageContext, name);
100                 }
101             }
102             else
103             {
104                 String JavaDoc name = (String JavaDoc)EL.evaluateExpression(pageContext, "resourceMapExpression", partList.get(0).toString(), String JavaDoc.class);
105
106                 Object JavaDoc[] objectList = new Object JavaDoc[partCount - 1];
107
108                 for (int i = partCount; --i >= 1; )
109                 {
110                     objectList[i - 1] = evaluate(partList.get(i).toString(), Object JavaDoc.class);
111                 }
112
113                 if (baseName != null)
114                 {
115                     object = LocaleSupport.getLocalizedMessage(pageContext, name, objectList, baseName);
116                 }
117                 else
118                 {
119                     object = LocaleSupport.getLocalizedMessage(pageContext, name, objectList);
120                    
121                 }
122             }
123
124             String JavaDoc defaultValue = _defaultValue;
125
126             if (!("*".equals(defaultValue)))
127             {
128                 if (object.equals("???" + keyString + "???"))
129                 {
130                     object = defaultValue;
131                 }
132             }
133         }
134         catch (Exception JavaDoc e)
135         {
136             throw new RuntimeException JavaDoc(e);
137         }
138
139         return object;
140     }
141
142     public Object JavaDoc putObject(Object JavaDoc client, Object JavaDoc key, Object JavaDoc value)
143     {
144         if ("baseName".equals(key))
145         {
146             setBaseName(value.toString());
147         }
148         else if ("defaultValue".equals(key))
149         {
150             setDefaultValue(value.toString());
151         }
152
153         return null;
154     }
155 }
156
Popular Tags