import java.util.List; import java.util.Map; import java.util.Scanner; /** * The way to use this class with your code: * *
* class Entry represents one object (a "paragraph") taken from a gRUe-database. * See http://ru-itec120.pbwiki.com/grue-data-2007fall * for a gRUe-database file (which include comments explaining its syntax). *
** If you want to modify class Entry, here is some info on how it's organized: *
* ** Vocabulary: *
* You can ask an Entry what its type is, and you can also give it a * key (a String, like "description") and have it return * the associated value (another string, like "fa la la"). * This lookup is implemented by java.util.Map (built just for such tasks). *
* *
* Map stuffFromFile = new java.util.HashMap();
* List allStuff = getEntries();
* Entry e3 = allStuff.get(3);
* if (e3.getType().equals("treasure")) {
* double wt = Double.valueOf( e3.get("weight") );
* Treasure t = new Treasure( e3.get("name"), e3.get("description"), wt );
* stuffFromFile.put( e3.get("id"), t ); // I may want to look up this Treasure later via its id.
* }
*
*
* @author Ian Barland
* @version 2007.Nov.28
* (updated to give better error message if a Treasure entry's weight isn't numeric.)
*/
public class Entry {
/** A URL or filename for reading the data from, if no other file/URL provided: {@value} */
public static final String DEFAULT_DATABASE_FILENAME = "http://ru-itec120.pbwiki.com/grue-data-2007fall";
/** Text which only occurs in a line immediately before the actual data, in a URL/file. */
private static final String BEGIN_GRUE_DATA_MARKER = "BEGIN GRUE DATA";
/** Text which only occurs in a line immediately after the actual data, in a URL/file. */
private static final String END_GRUE_DATA_MARKER = "END GRUE DATA";
/** a total hack -- a field to help hasNextGrueDatum. */
static boolean seenEndOfData;
/** The list of valid types: room, treasure, connection, biconnection */
public static final List".
* @Return the next line from the scanner, removing any trailing "
" or "
". */ private static String myNextLine( java.util.Scanner s ) { if (!buffer.empty()) { return buffer.pop(); } String answer = s.nextLine(); if (answer.contains( "
" )) { buffer.push( "" ); } for ( String suffix : removableSuffixes ) { if (answer.endsWith(suffix)) { answer = answer.substring(0,answer.length()-suffix.length()); } } // Okay, I *could* push answer, and then make a recursive call. // Will hand-optimize: return answer; } private static final String[] removableSuffixes = { "" }; private static boolean justSawParaEnd = false; /** Skip over any comments (and possibly blank lines) in the input. * @param blankLinesToo If true, also skip over blank lines in addition to comments. */ private static void skipComments( Scanner s, boolean blankLinesToo ) { // We *would* just call s's method "skip" with a regular expression, // except that when we are parsing a wiki file w/ html cruft, we need // to realize that "
" is really a blank line. So we have our own // look which calls (our own) myNextLine. boolean skipping = true; String n = "dummy Value to appease compiler"; while (skipping && hasNextGrueDatum(s)) { n = myNextLine(s); skipping = ((n.equals("")) && blankLinesToo) || n.startsWith( COMMENT_START ); } if (hasNextGrueDatum(s)) { // We've gone too far! buffer.push(n); } } /** Default information to use, if there is an error elsewhere. * @param errMsg A message that is embedded into a treasure in the default info. * @return A string formatted as expected, for a gRUe-database. */ public static String fallbackGrueData( String errMsg ) { return BEGIN_GRUE_DATA_MARKER + "\n" + "Treasure\n id = note \n name=note \n weight = 0.0 \n description = The exact error message is \"" + errMsg.replaceAll("\n","; ") + "\"\n" + "\n" + "Room \n id = nexus \n name=the celestial void\n description = A sign says \"Sorry, the wiki is out of order; come again later\"\n treasures=note\n" + "
\n" + "Treasure\n id = feather\n name=a feather \n description = It seems to fall quickly, for a feather.\n weight = 44.0\n" + "
\n" + "Room \n id = pit\n name=the bottomless pit\n description = You are falling, falling, falling, falling, falling. You are bored.\n treasures=feather\n" + "
\n" + "Connection\n from = nexus\n to = pit\n" + "
\n" + "Connection\n from = pit\n to = pit\n" + "
\n" + END_GRUE_DATA_MARKER; } }