1 5 package xdoclet.modules.externalizer; 6 7 import java.text.MessageFormat ; 8 import java.util.*; 9 10 import xjavadoc.*; 11 12 import xdoclet.TemplateSubTask; 13 import xdoclet.XDocletException; 14 15 26 public class ExternalizerSubTask extends TemplateSubTask 27 { 28 public final static String GENERATED_FILE_NAME = "{0}Messages{1}.properties"; 29 30 private static String DEFAULT_TEMPLATE_FILE = "resources/properties.xdt"; 31 32 private String tagName = "msg.message"; 33 private String 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 getTagName() 45 { 46 return tagName; 47 } 48 49 public String getValueParamName() 50 { 51 return valueParamName; 52 } 53 54 public Combination getCurrentCombination() 55 { 56 return currentCombination; 57 } 58 59 public String getKeyParamName() 60 { 61 return ""; 62 } 63 64 65 public void setTagName(String tagName) 66 { 67 this.tagName = tagName; 68 } 69 70 public void setValueParamName(String valueParamName) 71 { 72 this.valueParamName = valueParamName; 73 } 74 75 public void setKeyParamName(String 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 getBundleKey(XClass clazz) 92 { 93 String result = MessageFormat.format(getDestinationFile(), new Object []{clazz.getQualifiedName().replace('.', '/'), ""}); 94 95 return result.substring(0, result.lastIndexOf('.')); 96 } 97 98 protected String getGeneratedFileName(XClass clazz) throws XDocletException 99 { 100 String result = MessageFormat.format(getDestinationFile(), new Object []{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 language = tag.getAttributeValue("language"); 139 String country = tag.getAttributeValue("country"); 140 String variant = tag.getAttributeValue("variant"); 141 String 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 } 157 } 158 } 159 160 163 final static class Combination 164 { 165 public String language; 166 public String country; 167 public String variant; 168 public List keys = new ArrayList(); 169 public List values = new ArrayList(); 170 171 public Combination(String language, String country, String variant) 172 { 173 this.country = country; 174 this.language = language; 175 this.variant = variant; 176 } 177 178 public String getCombinationNameAsResourceBundleName() 179 { 180 String 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 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 |