1 16 package com.google.gwt.i18n.rebind.util; 17 18 import java.util.ArrayList ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Locale ; 23 import java.util.MissingResourceException ; 24 import java.util.Set ; 25 26 42 public abstract class AbstractResource { 43 47 public static final int REPORT_KEYS_THRESHOLD = 30; 48 49 private final List alternativeParents = new ArrayList (); 50 51 private Set keySet; 52 53 private Locale locale; 54 55 private String path; 56 57 private AbstractResource primaryParent; 58 59 62 public Locale getLocale() { 63 return locale; 64 } 65 66 69 public final Object getObject(String key) { 70 Object s = getObjectAux(key, true); 71 if (s == null) { 72 String msg = "Cannot find '" + key + "' in " + this; 73 Set keys = this.keySet(); 74 if (keys.size() < REPORT_KEYS_THRESHOLD) { 75 msg = msg + ", keys found:\n\t" + keys; 76 } 77 throw new MissingResourceException (msg, key, key); 78 } 79 return s; 80 } 81 82 85 public final String getString(String key) { 86 return (String ) getObject(key); 87 } 88 89 94 public Set keySet() { 95 if (keySet == null) { 96 keySet = new HashSet (); 97 addToKeySet(keySet); 98 if (primaryParent != null) { 99 primaryParent.addToKeySet(keySet); 100 } 101 for (int i = 0; i < alternativeParents.size(); i++) { 102 AbstractResource element = (AbstractResource) alternativeParents.get(i); 103 keySet.addAll(element.keySet()); 104 } 105 } 106 return keySet; 107 } 108 109 public String toString() { 110 return "resource for " + path; 111 } 112 113 118 public String toVerboseString() { 119 StringBuffer b = new StringBuffer (); 120 toVerboseStringAux(0, b); 121 return b.toString(); 122 } 123 124 void addAlternativeParent(AbstractResource parent) { 125 if (parent != null) { 126 alternativeParents.add(parent); 127 } 128 } 129 130 abstract void addToKeySet(Set s); 131 132 void checkKeys() { 133 if (primaryParent == null) { 136 return; 137 } 138 Iterator keys = this.keySet().iterator(); 139 while (keys.hasNext()) { 140 String key = (String ) keys.next(); 141 if (primaryParent.getObjectAux(key, true) == null) { 142 for (int i = 0; i < alternativeParents.size(); i++) { 143 AbstractResource alt = (AbstractResource) alternativeParents.get(i); 144 if (alt.getObjectAux(key, true) != null) { 145 break; 146 } 147 } 148 149 throw new IllegalArgumentException ( 150 key 151 + " is not a valid resource key as it does not occur in the default version of " 152 + this + " nor in any of " + alternativeParents); 153 } 154 } 155 } 156 157 final Object getObjectAux(String key, boolean useAlternativeParents) { 158 Object s = handleGetObject(key); 159 if (s != null) { 160 return s; 161 } 162 AbstractResource parent = this.getPrimaryParent(); 163 if (parent != null) { 164 s = parent.getObjectAux(key, false); 166 } 167 if ((s == null) && (alternativeParents.size() > 0) 168 && (useAlternativeParents)) { 169 for (int i = 0; (i < alternativeParents.size()) && (s == null); i++) { 170 AbstractResource altParent = (AbstractResource) alternativeParents.get(i); 172 s = altParent.getObjectAux(key, true); 173 } 174 } 175 return s; 176 } 177 178 String getPath() { 179 return path; 180 } 181 182 AbstractResource getPrimaryParent() { 183 return primaryParent; 184 } 185 186 abstract Object handleGetObject(String key); 187 188 void setLocale(Locale locale) { 189 this.locale = locale; 190 } 191 192 void setPath(String path) { 193 this.path = path; 194 } 195 196 void setPrimaryParent(AbstractResource primaryParent) { 197 if (primaryParent == null) { 198 return; 199 } 200 this.primaryParent = primaryParent; 201 } 202 203 private void newLine(int indent, StringBuffer buf) { 204 buf.append("\n"); 205 for (int i = 0; i < indent; i++) { 206 buf.append("\t"); 207 } 208 } 209 210 private void toVerboseStringAux(int indent, StringBuffer buf) { 211 newLine(indent, buf); 212 buf.append(toString()); 213 if (primaryParent != null) { 214 newLine(indent, buf); 215 buf.append("Primary Parent: "); 216 primaryParent.toVerboseStringAux(indent + 1, buf); 217 } 218 for (int i = 0; i < alternativeParents.size(); i++) { 219 newLine(indent, buf); 220 buf.append("Alternate Parent: "); 221 AbstractResource element = (AbstractResource) alternativeParents.get(i); 222 element.toVerboseStringAux(indent + 1, buf); 223 } 224 } 225 } 226 | Popular Tags |