| 1 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 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 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 52 public MadLibFactory() { 53 } 55 56 57 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 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 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 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 String format = null; 155 try { 156 format = config.getString(type + ".format"); 157 } catch (MissingResourceException mre) { 158 } 160 161 fillin = new MadLibFillin(isWord, fillinIndex, caption, format, index, 163 endIndex); 164 allFillins.add(fillin); 165 166 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 185 public List getMadLibList() { 186 return titles; 187 } 188 } | Popular Tags |