001: package org.igfay.jfig;
002:
003: import java.util.Iterator;
004: import java.util.TreeMap;
005:
006: import org.apache.log4j.Logger;
007:
008: /**
009: * JFigDictionary contains the configuration entries. It is a TreeMap of
010: * TreeMaps. There is a map for each section. Each section map in turn
011: * holds its key/value pairs.
012: *
013: * The dictionary provides methods to set and retrieve the key/value entries.
014: * It can also print itself.
015: *
016: *
017: *@author bconrad
018: *@created April 3, 2001
019: */
020: public class JFigDictionary implements java.io.Serializable {
021:
022: private static Logger log = Logger.getLogger(JFigDictionary.class);
023:
024: private TreeMap dictionaryOfSectionDictionaries;
025: private String hiddenString;
026: private String htmlString;
027:
028: /**
029: * default constructor
030: */
031: public JFigDictionary() {
032: }
033:
034: public void addKeyValueToSection(String section, String key,
035: String value) {
036: log.debug("Section " + section + " Key: " + key + " Value: "
037: + value);
038: getSectionNamed(section, true).put(key, value);
039: }
040:
041: private void addSectionDictionary(String sectionName) {
042: log.debug("section: " + sectionName);
043: TreeMap sectionDictionary = new TreeMap();
044: getDictionaryOfSectionDictionaries().put(sectionName,
045: sectionDictionary);
046: }
047:
048: /**
049: * For a given key, return the value of that key in a given
050: * collection,regardless of case (color or creed).
051: *
052: *@param ht Description of Parameter
053: *@param key Description of Parameter
054: *@return Description of the Returned Value
055: */
056: private Object findInDictionaryIgnoreCase(TreeMap ht, String key) {
057: try {
058: Iterator enum = ht.keySet().iterator();
059: while (enum.hasNext()) {
060: String enumKey = (String) enum.next();
061: //log.info("\n key "+key+"\nenumKey "+enumKey);
062:
063: if (key.equalsIgnoreCase(enumKey)) {
064: return ht.get(enumKey);
065: }
066: }
067: }
068: catch (Exception e) {
069: log.debug("Exception " + e);
070: }
071: return null;
072: }
073:
074: /**
075: * Print the configuration dictionary.
076: * Sections and keys will be in alphabetical order.
077: * @deprecated
078: */
079: public void printConfigurationDictionary() {
080: print();
081: }
082:
083: /**
084: * Print the configuration dictionary.
085: * Sections and keys will be in alphabetical order.
086: *
087: */
088: public void print() {
089: Iterator enum = getDictionaryOfSectionDictionaries().keySet().iterator();
090: log.info("--------------------------------------");
091: while (enum.hasNext()) {
092: String sectionName = (String) enum.next();
093: log.info("");
094: log.info("Section: " + sectionName);
095: TreeMap sectionDictionary = (TreeMap) getDictionaryOfSectionDictionaries().get(sectionName);
096: Iterator enumSection = sectionDictionary.keySet().iterator();
097: while (enumSection.hasNext()) {
098: String key = (String) enumSection.next();
099: String value = null;
100: if (isHidden(key) )
101: value = "********";
102: else
103: value = (String) sectionDictionary.get(key);
104:
105: log.info(" Key: " + key + " Value: " + value);
106: } // while enumSection
107: } // while enum
108: log.info("--------------------------------------");
109: }
110:
111: /**
112: * Return the configuration dictionary as a string embedded with
113: * html.
114: * This method will be replaced with a better jsp.
115: *
116: * @return String
117: */
118: public String getHtmlString() {
119: if (htmlString == null) {
120: log.debug("");
121: StringBuffer buffer = new StringBuffer();
122:
123: String key = null;
124: String value = null;
125:
126: StringBuffer startSectionString = new StringBuffer("<TR><TD> </TD></TR><TR><TD><B>[");
127: StringBuffer endSectionString = new StringBuffer("]</B></TD></TR><TR><TD><B>Key</B></TD><TD><B>Value</B></TD></TR>");
128: StringBuffer startKeyValue = new StringBuffer("<TR><TD>");
129: StringBuffer midKeyValue = new StringBuffer("</TD><TD>");
130: StringBuffer endKeyValue = new StringBuffer("</TD></TR>");
131:
132: Iterator enum = getDictionaryOfSectionDictionaries().keySet().iterator();
133: while (enum.hasNext()) {
134: String sectionName = (String) enum.next();
135: buffer.append(startSectionString).
136: append(sectionName).
137: append(endSectionString);
138: TreeMap sectionHashtable = (TreeMap) getDictionaryOfSectionDictionaries().get(sectionName);
139: Iterator enumSection = sectionHashtable.keySet().iterator();
140: while (enumSection.hasNext()) {
141: key = (String) enumSection.next();
142: value = (String) sectionHashtable.get(key);
143:
144: buffer.append(startKeyValue).
145: append(key).
146: append(midKeyValue).
147: append(value).
148: append(endKeyValue);
149:
150: } // while enumSection
151: } // while enum
152: htmlString = buffer.toString();
153: }
154: return htmlString;
155: }
156:
157: public boolean isHidden(String key) {
158: return getHiddenString().indexOf(" " + key + " ".toUpperCase()) > -1;
159:
160: }
161:
162: /**
163: *
164: * Return a list of keys that should not be printed (ie passwords).
165: * Relies on a special config/isHidden section keyword.
166: * There may be better ways to do this, esp with xml
167: */
168: private String getHiddenString() {
169: if (hiddenString == null) {
170: hiddenString = "";
171: }
172: return hiddenString;
173: }
174:
175: /**
176: * Return the DictionaryOfSectionDictionaries value
177: */
178: public java.util.TreeMap getDictionaryOfSectionDictionaries() {
179: if (dictionaryOfSectionDictionaries == null) {
180: dictionaryOfSectionDictionaries = new TreeMap();
181: }
182: return dictionaryOfSectionDictionaries;
183: }
184:
185: /**
186: * Return the DictionaryOfSectionDictionaries value as an iterator
187: */
188: public Iterator getSectionIterator() {
189: return getDictionaryOfSectionDictionaries().keySet().iterator();
190: }
191:
192: /**
193: * Return a named section dictionary
194: * Does the same job as getSectionNamed but the section
195: * name must match with case sensitivity.
196: * Here for better performance when we are ensured the section will match.
197: */
198: public TreeMap getSectionDictionary(String sectionName) {
199: return (TreeMap) getDictionaryOfSectionDictionaries().get(
200: sectionName);
201: }
202:
203: protected TreeMap getSectionNamed(String sectionName) {
204: return getSectionNamed(sectionName, false);
205: }
206:
207: /**
208: * Return a named section dictionary
209: * Iterate through all the section dictionaries so we can
210: * check without case sensitivity.
211: */
212: protected TreeMap getSectionNamed(String sectionName, boolean addIfNull) {
213: Iterator enum = getDictionaryOfSectionDictionaries().keySet().iterator();
214: //log.debug(getClassName() + ".getSectionNamed(): " + sectionName);
215: while (enum.hasNext()) {
216: Object sectionObject = enum.next();
217: String compareSection = (String) sectionObject;
218: if (sectionName.equalsIgnoreCase(compareSection)) {
219: //log.debug(getClassName() + ".getSectionNamed(): found Section: " + sectionName);
220: return (TreeMap) findInDictionaryIgnoreCase(getDictionaryOfSectionDictionaries(), sectionName);
221: }
222: }
223: if (addIfNull) {
224: addSectionDictionary(sectionName);
225: return getSectionNamed(sectionName, false);
226: }
227: return null;
228: }
229:
230: /**
231: * Return the value for this section and key.
232: * If not found, throw JFigException.
233: *
234: *@param section Description of Parameter
235: *@param key Description of Parameter
236: *@return The Value value
237: *@exception JFigException Description of Exception
238: */
239: public String getValue(String section, String key)
240: throws JFigException {
241:
242: return getValueReally(section, key, "$dummy$");
243: }
244:
245: /**
246: * Return the value for this section and key.
247: * If not found, return defaultValue.
248: *
249: *@param section Description of Parameter
250: *@param key Description of Parameter
251: *@param notFoundValue Description of Parameter
252: *@return The Value value
253: */
254: public String getValue(String section, String key,
255: String notFoundValue) {
256: String value = null;
257: try {
258: value = getValueReally(section, key, notFoundValue);
259: return value;
260: } catch (Exception e) {
261: log.debug("catch Exception value: " + value + " e: "
262: + e.getMessage() + " Will use notFoundValue: "
263: + notFoundValue);
264: }
265: log.debug("returning notFoundValue: " + notFoundValue);
266: return notFoundValue;
267: }
268:
269: private String getValueReally(String section, String key,
270: String defaultKey) throws JFigException {
271: String value = null;
272: TreeMap sectionDictionary = null;
273:
274: // Find the section dictionary in the configDictionary
275: if ((sectionDictionary = (TreeMap) findInDictionaryIgnoreCase(
276: getDictionaryOfSectionDictionaries(), section)) != null) {
277:
278: // Find the value for the key in the section dictionary
279: if ((value = (String) findInDictionaryIgnoreCase(
280: sectionDictionary, key)) != null) {
281: return value;
282: } else {
283: log.debug("Throw Exception key " + key + " == null");
284: throw new JFigException(
285: "ConfigurationDictionary.getValueReally() ***Exception *** \nKey: "
286: + key + " not found in section: "
287: + section);
288: }
289: } // sectionDictionary != null
290: else {
291: log.debug("Throw Exception sectionDictionary " + section
292: + " == null");
293: throw new JFigException(
294: "ConfigurationDictionary.getValueReally() ***Exception *** \nSection "
295: + section + " not found.");
296: }
297: }
298:
299: /**
300: * Set a value for a given section and key.
301: *
302: *@param sectionName The new ConfigurationValue value
303: *@param keyString The new ConfigurationValue value
304: *@param valueString The new ConfigurationValue value
305: */
306: protected void setConfigurationValue(String sectionName,
307: String keyString, String valueString) {
308: addKeyValueToSection(sectionName, keyString, valueString);
309: }
310:
311: /**
312: * Sets the htmlString.
313: * @param htmlString The htmlString to set
314: */
315: public void setHtmlLString(String htmlString) {
316: this.htmlString = htmlString;
317: }
318:
319: }
|