KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > cofax > Glossary


1 /*
2  * Glossary is part of the Cofax content management system library.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Please see http://www.cofax.org for contact information and other related informaion.
19  *
20  * $Header: /cvsroot/cofax/cofax/src/org/cofax/Glossary.java,v 1.4.2.1 2006/12/11 16:28:45 fxrobin Exp $
21  */

22
23 package org.cofax;
24
25 import java.util.*;
26
27 /**
28  * A container object for the glossary variable space. It's main purpose is
29  * making commonly used variables accessable to resources across Cofax.
30  *
31  * @author Karl Martino
32  * @author Hung Dao
33  * @created June 26, 2002
34  */

35 public class Glossary {
36
37     private HashMap keyValues = new HashMap();
38
39     /**
40      * Default constructor
41      */

42     public Glossary() {
43         return;
44     }
45
46     /**
47      * Sets the internal keyValues HashMap
48      *
49      * @param inHashMap
50      * The new keyValues value
51      */

52     public void setKeyValues(HashMap inHashMap) {
53         keyValues = inHashMap;
54     }
55
56     /**
57      * Returns the internal keyValue HashMap
58      *
59      * @return The keyValues value
60      */

61     public HashMap getKeyValues() {
62         return keyValues;
63     }
64
65     /**
66      * Returns a member of the Glossary as a string
67      *
68      * @param fieldName
69      * The key forwhich you want the value in the Glossary
70      * @return The string value mapped to the key
71      */

72     public String JavaDoc getString(String JavaDoc fieldName) {
73
74         Object JavaDoc value = keyValues.get(fieldName);
75         return (value != null) ? value.toString() : "";
76     }
77
78     /**
79      * Sets a Glossary value
80      *
81      * @param fieldName
82      * The key forwhich you want to set the value
83      * @param value
84      * The value forwhich you want mapped to the key
85      */

86     public void putString(String JavaDoc fieldName, String JavaDoc value) {
87
88         keyValues.put(fieldName, value);
89
90     }
91
92     /**
93      * Adds a new set of glossary values, contained in a HashMap, to this
94      * Glossary
95      *
96      * @param newKeyValues
97      * HashMap of keys and values to be added to the Glossary
98      */

99     public void addToGlossary(HashMap newKeyValues) {
100
101         for (Iterator i = newKeyValues.keySet().iterator(); i.hasNext();) {
102             String JavaDoc tag = (String JavaDoc) i.next();
103             String JavaDoc value = newKeyValues.get(tag).toString();
104             keyValues.put(tag, value);
105         }
106
107         return;
108     }
109
110     /**
111      * Prints the Glossary - hack job alert!
112      *
113      * @return an HTML output of the contents of the glossary
114      */

115     public String JavaDoc printGlossary() {
116
117         String JavaDoc printme = "";
118
119         for (Iterator i = keyValues.keySet().iterator(); i.hasNext();) {
120             String JavaDoc key = i.next().toString();
121             String JavaDoc value = getString(key);
122             printme = printme + key + " = " + value + "<br>\n";
123
124         }
125
126         return printme;
127     }
128
129     /**
130      * Prints the Glossary where key item start with specific pattern name
131      *
132      * @param startWithName
133      * Key to start outputting from
134      * @return an HTML output of the contents of the glossary
135      */

136     public String JavaDoc printGlossary(String JavaDoc startWithName) {
137         String JavaDoc printme = new String JavaDoc();
138
139         for (Iterator i = keyValues.keySet().iterator(); i.hasNext();) {
140             String JavaDoc key = i.next().toString();
141             String JavaDoc value = getString(key);
142
143             if (key.startsWith(startWithName)) {
144                 printme = printme + key + " = " + value + "<br>\n";
145             }
146         }
147
148         return printme;
149     }
150
151     /**
152      * Returns the size of the Glossary
153      *
154      * @return Description of the Return Value
155      */

156     public int size() {
157         return keyValues.size();
158     }
159
160     /**
161      * Applies the glossary to a string utlizing backtick syntax
162      *
163      * @param inString
164      * Description of the Parameter
165      * @return Description of the Return Value
166      */

167     public String JavaDoc applyGlossary(String JavaDoc inString) {
168         if (inString == null) {
169             return "";
170         }
171         HashMap params = getKeyValues();
172         if (params == null) {
173             return inString;
174         }
175
176         int firstOc = inString.indexOf("`");
177         String JavaDoc first;
178         String JavaDoc last;
179         String JavaDoc param;
180         int nextOc = 0;
181
182         while (firstOc > -1) {
183             nextOc = inString.indexOf("`", firstOc + 1);
184             if (nextOc > -1) {
185                 first = inString.substring(0, firstOc);
186                 last = inString.substring(nextOc + 1, inString.length());
187
188                 param = inString.substring(firstOc + 1, nextOc);
189
190                 String JavaDoc myValue = "";
191                 if (params.containsKey(param)) {
192                     myValue = params.get(param).toString();
193                 }
194                 if (myValue == null) {
195                     myValue = "";
196                 }
197                 inString = first + myValue + last;
198
199                 firstOc = inString.indexOf("`");
200             } else {
201                 firstOc = -1;
202             }
203         }
204
205         return inString;
206     }
207
208 }
209
Popular Tags