KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > i18n > rebind > util > AbstractResource


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.i18n.rebind.util;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashSet JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Locale JavaDoc;
23 import java.util.MissingResourceException JavaDoc;
24 import java.util.Set JavaDoc;
25
26 /**
27  * AbstractResource serves the same purpose as java
28  * ResourceBundle/PropertyResourceBundle.
29  * <p>
30  * Each <code>Resource</code> belongs to a resource tree, indicated by the
31  * path attribute.
32  * <p>
33  * AbstractResource uses a Factory pattern rather than a single static method to
34  * load itself given an abstract string path.
35  * <p>
36  * One advanced feature which should not be used outside the core GWT system is
37  * that resources can have more than one parent, for instance pets_en_US could
38  * have pets_en as one parent and animals_en_US as another. The alternative
39  * parents have lower precedence than any primary parent. Each alternative
40  * parent is associated with a separate resource tree.
41  */

42 public abstract class AbstractResource {
43   /**
44    * Error messages concerning missing keys should include the defined keys if
45    * the number of keys is below this threshold.
46    */

47   public static final int REPORT_KEYS_THRESHOLD = 30;
48
49   private final List JavaDoc alternativeParents = new ArrayList JavaDoc();
50
51   private Set JavaDoc keySet;
52
53   private Locale JavaDoc locale;
54
55   private String JavaDoc path;
56
57   private AbstractResource primaryParent;
58
59   /**
60    * @see java.util.ResourceBundle#getLocale()
61    */

62   public Locale JavaDoc getLocale() {
63     return locale;
64   }
65
66   /**
67    * @see java.util.ResourceBundle#getObject(java.lang.String)
68    */

69   public final Object JavaDoc getObject(String JavaDoc key) {
70     Object JavaDoc s = getObjectAux(key, true);
71     if (s == null) {
72       String JavaDoc msg = "Cannot find '" + key + "' in " + this;
73       Set JavaDoc keys = this.keySet();
74       if (keys.size() < REPORT_KEYS_THRESHOLD) {
75         msg = msg + ", keys found:\n\t" + keys;
76       }
77       throw new MissingResourceException JavaDoc(msg, key, key);
78     }
79     return s;
80   }
81
82   /**
83    * @see java.util.ResourceBundle#getString(java.lang.String)
84    */

85   public final String JavaDoc getString(String JavaDoc key) {
86     return (String JavaDoc) getObject(key);
87   }
88
89   /**
90    * Keys associated with this resource.
91    *
92    * @return keys
93    */

94   public Set JavaDoc keySet() {
95     if (keySet == null) {
96       keySet = new HashSet JavaDoc();
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 JavaDoc toString() {
110     return "resource for " + path;
111   }
112
113   /**
114    * A multi-line representation of this object.
115    *
116    * @return verbose string
117    */

118   public String JavaDoc toVerboseString() {
119     StringBuffer JavaDoc b = new StringBuffer JavaDoc();
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 JavaDoc s);
131
132   void checkKeys() {
133     // If I don't have a parent, then I am a default node so do not need to
134
// conform
135
if (primaryParent == null) {
136       return;
137     }
138     Iterator JavaDoc keys = this.keySet().iterator();
139     while (keys.hasNext()) {
140       String JavaDoc key = (String JavaDoc) 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 JavaDoc(
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 JavaDoc getObjectAux(String JavaDoc key, boolean useAlternativeParents) {
158     Object JavaDoc s = handleGetObject(key);
159     if (s != null) {
160       return s;
161     }
162     AbstractResource parent = this.getPrimaryParent();
163     if (parent != null) {
164       // Primary parents should not look at their alternative parents
165
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         // Alternate parents may look at their alternative parents.
171
AbstractResource altParent = (AbstractResource) alternativeParents.get(i);
172         s = altParent.getObjectAux(key, true);
173       }
174     }
175     return s;
176   }
177
178   String JavaDoc getPath() {
179     return path;
180   }
181
182   AbstractResource getPrimaryParent() {
183     return primaryParent;
184   }
185
186   abstract Object JavaDoc handleGetObject(String JavaDoc key);
187
188   void setLocale(Locale JavaDoc locale) {
189     this.locale = locale;
190   }
191
192   void setPath(String JavaDoc 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 JavaDoc 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 JavaDoc 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