1 16 66 package org.outerj.daisy.repository.commonimpl.schema; 67 68 import org.outerx.daisy.x10.LabelsDocument; 69 import org.outerx.daisy.x10.DescriptionsDocument; 70 import org.outerj.daisy.repository.LocaleHelper; 71 72 import java.util.Locale ; 73 import java.util.Set ; 74 import java.util.Iterator ; 75 import java.util.Map ; 76 77 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 78 79 88 public class LocaleMap { 89 90 private ConcurrentReaderHashMap map = new ConcurrentReaderHashMap(); 91 92 private ConcurrentReaderHashMap searchMap = new ConcurrentReaderHashMap(); 93 private static final String NO_RESULT = "(no result: you should never see this)"; 94 95 100 public Object get(Locale locale) { 101 if (map.size() == 0) 102 return null; 103 104 String full = getFullKey(locale); 105 106 if (!searchMap.containsKey(full)) { 107 if (map.containsKey(full)) { 108 Object object = map.get(full); 109 searchMap.put(full, object); 110 return object; 111 } 112 113 String altKey = locale.getLanguage() + '-' + locale.getCountry(); 114 Object object = map.get(altKey); 115 if (object != null) { 116 searchMap.put(full, object); 117 return object; 118 } 119 120 altKey = locale.getLanguage(); 121 object = map.get(altKey); 122 if (object != null) { 123 searchMap.put(full, object); 124 return object; 125 } 126 127 object = map.get(""); 128 if (object != null) { 129 searchMap.put(full, object); 130 return object; 131 } 132 133 searchMap.put(full, NO_RESULT); 134 } 135 136 Object result = searchMap.get(full); 137 return result == NO_RESULT ? null : result; 138 } 139 140 public Object getExact(Locale locale) { 141 return map.get(LocaleHelper.getString(locale)); 142 } 143 144 public void clear() { 145 map.clear(); 146 searchMap.clear(); 147 } 148 149 public void remove(Locale locale) { 150 put(locale, null); 151 } 152 153 public Locale [] getLocales() { 154 String [] localeNames = (String [])map.keySet().toArray(new String [0]); 155 Locale [] locales = new Locale [localeNames.length]; 156 for (int i = 0; i < locales.length; i++) 157 locales[i] = LocaleHelper.parseLocale(localeNames[i]); 158 return locales; 159 } 160 161 public Set entrySet() { 162 return map.entrySet(); 163 } 164 165 private String getFullKey(Locale locale) { 166 return locale.getLanguage() + '-' + locale.getCountry() + '-' + locale.getVariant(); 167 } 168 169 public void put(Locale locale, Object object) { 170 if (object == null) 171 map.remove(LocaleHelper.getString(locale)); 172 else 173 map.put(LocaleHelper.getString(locale), object); 174 searchMap.clear(); 175 } 176 177 public LabelsDocument.Labels getAsLabelsXml() { 178 LabelsDocument.Labels labels = LabelsDocument.Factory.newInstance().addNewLabels(); 179 Iterator labelIt = map.entrySet().iterator(); 180 while (labelIt.hasNext()) { 181 Map.Entry entry = (Map.Entry )labelIt.next(); 182 LabelsDocument.Labels.Label label = labels.addNewLabel(); 183 label.setLocale((String )entry.getKey()); 184 label.setStringValue((String )entry.getValue()); 185 } 186 return labels; 187 } 188 189 public DescriptionsDocument.Descriptions getAsDescriptionsXml() { 190 DescriptionsDocument.Descriptions descriptions = DescriptionsDocument.Factory.newInstance().addNewDescriptions(); 191 Iterator descriptionIt = map.entrySet().iterator(); 192 while (descriptionIt.hasNext()) { 193 Map.Entry entry = (Map.Entry )descriptionIt.next(); 194 DescriptionsDocument.Descriptions.Description description = descriptions.addNewDescription(); 195 description.setLocale((String )entry.getKey()); 196 description.setStringValue((String )entry.getValue()); 197 } 198 return descriptions; 199 } 200 201 public void readFromLabelsXml(LabelsDocument.Labels labelsXml) { 202 LabelsDocument.Labels.Label[] labels = labelsXml.getLabelArray(); 203 for (int i = 0; i < labels.length; i++) { 204 String locale = labels[i].getLocale(); 205 String value = labels[i].getStringValue(); 206 map.put(locale, value); 207 } 208 searchMap.clear(); 209 } 210 211 public void readFromDescriptionsXml(DescriptionsDocument.Descriptions descriptionsAsXml) { 212 DescriptionsDocument.Descriptions.Description[] descriptions = descriptionsAsXml.getDescriptionArray(); 213 for (int i = 0; i < descriptions.length; i++) { 214 String locale = descriptions[i].getLocale(); 215 String value = descriptions[i].getStringValue(); 216 map.put(locale, value); 217 } 218 searchMap.clear(); 219 } 220 221 public boolean isEmpty() { 222 return map.isEmpty(); 223 } 224 } 225 | Popular Tags |