1 7 package com.inversoft.verge.examples.madlib; 8 9 10 import java.util.Iterator ; 11 12 13 22 public class MadLibBuilder { 23 24 27 private MadLibBuilder() { 28 } 30 31 32 41 public static String buildMadLib(MadLibBean mb) throws MadLibException { 42 43 MadLib madlib = mb.getMadLib(); 44 if (madlib == null) { 45 throw new MadLibException("MadLib is null"); 46 } 47 48 String rawText = madlib.getRawText(); 49 StringBuffer buf = new StringBuffer (rawText.length()); 50 int currentIndex = 0; 51 Iterator iter = madlib.getRawMadLibFillins().iterator(); 52 while (iter.hasNext()) { 53 MadLibFillin fillin = (MadLibFillin) iter.next(); 54 55 buf.append(rawText.substring(currentIndex, fillin.getStartIndex())); 56 if (fillin.isWord()) { 57 buf.append(mb.getWord(fillin.getIndex())); 58 } else { 59 buf.append(mb.getNumber(fillin.getIndex())); 60 } 61 62 currentIndex = fillin.getEndIndex() + 1; 63 } 64 65 if (currentIndex < rawText.length()) { 66 buf.append(rawText.substring(currentIndex)); 67 } 68 69 return buf.toString(); 70 } 71 } | Popular Tags |