KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > examples > madlib > MadLibFactory


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.examples.madlib;
8
9 import java.util.ArrayList;
10 import java.util.Enumeration;
11 import java.util.List;
12 import java.util.MissingResourceException;
13 import java.util.ResourceBundle;
14
15
16 /**
17  * <p>
18  * This builds MadLib Objects from the madlib.properties file
19  * that must be in the class path. This file contains MadLib
20  * information such as the number of words, the number of
21  * numbers and the raw text of the MadLib in java.text format.
22  * </p>
23  *
24  * @author Brian Pontarelli
25  */

26 public class MadLibFactory {
27
28     private static ResourceBundle bundle = ResourceBundle.getBundle(
29         "com.inversoft.verge.examples.madlib.madlibs");
30     private static ResourceBundle config = ResourceBundle.getBundle(
31         "com.inversoft.verge.examples.madlib.madlib-config");
32     private static List titles = new ArrayList();
33
34
35     /**
36      * Static constructor that builds the titles list form the resource bundle
37      */

38     static {
39         Enumeration enum = bundle.getKeys();
40         String key;
41         while (enum.hasMoreElements()) {
42             key = enum.nextElement().toString();
43             if (key.indexOf(".") == -1) {
44                 titles.add(key);
45             }
46         }
47     }
48
49     /**
50      * Public, even though it is a static class, so that it can be used on JSPs
51      */

52     public MadLibFactory() {
53         // Empty
54
}
55
56
57     /**
58      * Constructs the MadLib that has the given title. If the MadLib can not be
59      * found or is incomplete, this returns null but logs an error to System.err.
60      *
61      * @param title The title of the MadLib to build
62      * @return The MadLib or null if it is not found or incomplete
63      */

64     public static MadLib buildMadLib(String title) throws MadLibException {
65
66         try {
67             String text = bundle.getString(title);
68             List fillins = parseText(text);
69
70             return new MadLib(text, fillins);
71         } catch (MissingResourceException mre) {
72             System.err.println("Madlib properties file is missing a full defintion" +
73                 " for MadLib named: " + title);
74             return null;
75         } catch (NumberFormatException nfe) {
76             System.err.println("Madlib properties file has an invalid integer" +
77                 " value for numberOfNumbers or numberOfWords");
78             return null;
79         }
80     }
81
82     public static List parseText(String text) throws MadLibException {
83
84         int index = text.indexOf('{');
85         if (index == -1) {
86             throw new MadLibException("MadLib contains no fill in spaces");
87         }
88
89         int endIndex = text.indexOf('}', index);
90         if (endIndex == -1) {
91             throw new MadLibException("Invalid MadLib raw text String");
92         }
93
94         List allFillins = new ArrayList();
95         MadLibFillin fillin = null;
96         while (index != -1) {
97             String info = text.substring(index + 1, endIndex);
98             if (info.trim().length() == 0) {
99                 throw new MadLibException("Invalid fill in definition");
100             }
101
102             int comma = info.indexOf(',');
103             if (comma == -1) {
104                 throw new MadLibException("Invalid MadLib fill in definition");
105             }
106
107             // Get the index
108
int fillinIndex;
109             try {
110                 fillinIndex = Integer.parseInt(info.substring(0, comma).trim());
111             } catch (NumberFormatException nfe) {
112                 throw new MadLibException("Invalid MadLib fill in definition", nfe);
113             }
114
115             if ((comma + 1) >= info.length()) {
116                 throw new MadLibException("Invalid MadLib fill in definition");
117             }
118
119             String type = info.substring(comma + 1).trim();
120             if (type.trim().length() == 0) {
121                 throw new MadLibException("Invalid type");
122             }
123
124             // Get the base type (word or number)
125
String typeType = null;
126             try {
127                 typeType = config.getString(type + ".type");
128             } catch (MissingResourceException mre) {
129                 throw new MadLibException("Type not found: " + type, mre);
130             }
131
132             boolean isWord;
133             if (typeType.equals("word")) {
134                 isWord = true;
135             } else if (typeType.equals("number")) {
136                 isWord = false;
137             } else {
138                 throw new MadLibException("Unsupported type: " + typeType);
139             }
140
141             // Get the caption
142
String caption = null;
143             try {
144                 caption = config.getString(type + ".caption");
145             } catch (MissingResourceException mre) {
146                 throw new MadLibException("Caption for type not found: " + type, mre);
147             }
148
149             if (caption.trim().length() == 0) {
150                 throw new MadLibException("Invalid caption");
151             }
152
153             // Get the format (if any)
154
String format = null;
155             try {
156                 format = config.getString(type + ".format");
157             } catch (MissingResourceException mre) {
158                 // Smother, okay
159
}
160
161             // Create the object and add it to the list
162
fillin = new MadLibFillin(isWord, fillinIndex, caption, format, index,
163                 endIndex);
164             allFillins.add(fillin);
165
166             // Get the next position, if any
167
index = text.indexOf('{', endIndex);
168             if (index != -1) {
169                 endIndex = text.indexOf('}', index);
170                 if (endIndex == -1) {
171                     throw new MadLibException("Invalid MadLib raw text String");
172                 }
173             }
174         }
175
176         return allFillins;
177     }
178
179
180     /**
181      * Returns the list of MadLib titles
182      *
183      * @return The List of MadLib titles
184      */

185     public List getMadLibList() {
186         return titles;
187     }
188 }
Popular Tags