KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > modules > externalizer > ExternalizerSubTask


1 /*
2  * Copyright (c) 2001, 2002 The XDoclet team
3  * All rights reserved.
4  */

5 package xdoclet.modules.externalizer;
6
7 import java.text.MessageFormat JavaDoc;
8 import java.util.*;
9
10 import xjavadoc.*;
11
12 import xdoclet.TemplateSubTask;
13 import xdoclet.XDocletException;
14
15 /**
16  * Externalizes key-value paired tags to whatever configured file (be it xml or properties file). It's useful for simple
17  * key-value properties or xml files. For now only properties files are supported. It supports i18n and l10n, so
18  * generated files follow {@link java.util.ResourceBundle} naming convention. Parameters "language", "country" and
19  * "variant" are reserved and used for this purpose.
20  *
21  * @author Ara Abrahamian (ara_e@email.com)
22  * @created May 7, 2002
23  * @ant.element display-name="Externalizer" name="externalizer" parent="xdoclet.DocletTask"
24  * @version $Revision: 1.12 $
25  */

26 public class ExternalizerSubTask extends TemplateSubTask
27 {
28     public final static String JavaDoc GENERATED_FILE_NAME = "{0}Messages{1}.properties";
29
30     private static String JavaDoc DEFAULT_TEMPLATE_FILE = "resources/properties.xdt";
31
32     private String JavaDoc tagName = "msg.message";
33     private String JavaDoc valueParamName = "msg";
34     private Map combinations;
35     private Combination currentCombination;
36
37     public ExternalizerSubTask()
38     {
39         setTemplateURL(getClass().getResource(DEFAULT_TEMPLATE_FILE));
40         setDestinationFile(GENERATED_FILE_NAME);
41     }
42
43
44     public String JavaDoc getTagName()
45     {
46         return tagName;
47     }
48
49     public String JavaDoc getValueParamName()
50     {
51         return valueParamName;
52     }
53
54     public Combination getCurrentCombination()
55     {
56         return currentCombination;
57     }
58
59     public String JavaDoc getKeyParamName()
60     {
61         return "";
62     }
63
64
65     public void setTagName(String JavaDoc tagName)
66     {
67         this.tagName = tagName;
68     }
69
70     public void setValueParamName(String JavaDoc valueParamName)
71     {
72         this.valueParamName = valueParamName;
73     }
74
75     public void setKeyParamName(String JavaDoc p)
76     {
77     }
78
79     public void validateOptions() throws XDocletException
80     {
81         super.validateOptions();
82
83         if (getTagName() == null) {
84             throw new XDocletException("tagName parameter is missing.");
85         }
86         if (getValueParamName() == null) {
87             throw new XDocletException("valueParamName parameter is missing.");
88         }
89     }
90
91     protected String JavaDoc getBundleKey(XClass clazz)
92     {
93         String JavaDoc result = MessageFormat.format(getDestinationFile(), new Object JavaDoc[]{clazz.getQualifiedName().replace('.', '/'), ""});
94
95         return result.substring(0, result.lastIndexOf('.'));
96     }
97
98     protected String JavaDoc getGeneratedFileName(XClass clazz) throws XDocletException
99     {
100         String JavaDoc result = MessageFormat.format(getDestinationFile(), new Object JavaDoc[]{clazz.getQualifiedName().replace('.', '/'), currentCombination.getCombinationNameAsResourceBundleName()});
101
102         return result;
103     }
104
105     protected void generateForClass(XClass clazz) throws XDocletException
106     {
107         setupResourceBundleCombinationsForClass(clazz);
108
109         Set entries = combinations.entrySet();
110
111         for (Iterator iterator = entries.iterator(); iterator.hasNext(); ) {
112             Map.Entry entry = (Map.Entry) iterator.next();
113             Combination combination = (Combination) entry.getValue();
114
115             currentCombination = combination;
116
117             super.generateForClass(clazz);
118         }
119
120         currentCombination = null;
121     }
122
123     private void setupResourceBundleCombinationsForClass(XClass clazz) throws XDocletException
124     {
125         combinations = new HashMap();
126
127         Collection fields = clazz.getFields();
128
129         for (Iterator j = fields.iterator(); j.hasNext(); ) {
130             XField field = (XField) j.next();
131             XDoc doc = field.getDoc();
132
133             if (doc != null && doc.hasTag(tagName)) {
134                 Collection tags = doc.getTags(tagName);
135
136                 for (Iterator i = tags.iterator(); i.hasNext(); ) {
137                     XTag tag = (XTag) i.next();
138                     String JavaDoc language = tag.getAttributeValue("language");
139                     String JavaDoc country = tag.getAttributeValue("country");
140                     String JavaDoc variant = tag.getAttributeValue("variant");
141                     String JavaDoc combination_name = language + country + variant;
142                     Combination combination = (Combination) combinations.get(combination_name);
143
144                     if (combination == null) {
145                         combination = new Combination(language, country, variant);
146
147                         combinations.put(combination_name, combination);
148                     }
149
150                     combination.keys.add(field.getName().toLowerCase());
151                     combination.values.add(tag.getAttributeValue(valueParamName));
152                 }
153             }
154             else {
155                 //throw new XDocletException("Couldn't find any field tags named @" + tagName + " in " + field.getContainingClass().getQualifiedName() + "." + field.getQualifiedName());
156
}
157         }
158     }
159
160     /**
161      * @created May 31, 2002
162      */

163     final static class Combination
164     {
165         public String JavaDoc language;
166         public String JavaDoc country;
167         public String JavaDoc variant;
168         public List keys = new ArrayList();
169         public List values = new ArrayList();
170
171         public Combination(String JavaDoc language, String JavaDoc country, String JavaDoc variant)
172         {
173             this.country = country;
174             this.language = language;
175             this.variant = variant;
176         }
177
178         public String JavaDoc getCombinationNameAsResourceBundleName()
179         {
180             String JavaDoc name = "";
181
182             if (language != null) {
183                 name += language;
184             }
185
186             if (country != null) {
187                 name += "_";
188                 name += country;
189             }
190
191             if (variant != null) {
192                 name += "_";
193                 name += variant;
194             }
195
196             if (name.length() > 0)
197                 return "_" + name;
198             else
199                 return name;
200         }
201
202         public int hashCode()
203         {
204             return (language + country + variant).hashCode();
205         }
206
207         public boolean equals(Object JavaDoc obj)
208         {
209             if (!(obj instanceof Combination))
210                 return false;
211
212             Combination other_combination = (Combination) obj;
213
214             return this.language.equals(other_combination.language) &&
215                 this.country.equals(other_combination.country) &&
216                 this.variant.equals(other_combination.variant);
217         }
218     }
219 }
220
Popular Tags