scg/ch14/util/ReadAndWrite

From FANG

Jump to: navigation, search

001 package scg.ch14.util;
002 
003 import java.util.ArrayList;
004 import java.util.List;
005 import java.util.Scanner;
006 import java.util.regex.Pattern;
007 
008 /**
009  * Utility class providing input handling methods for reading
010  * attribute-value pairs and formatting output to a fixed width. All
011  * methods are static.
012  */
013 public class ReadAndWrite {
014   /**
015    * Read in an attribute. An attribute is delimited by whitespace in
016    * front of it and whitespace OR an = after. The delimiter of in is
017    * set to [\s=], the set of \s char and = char= is just that symbol;
018    * \s is any whitespace char. The delimiter must be reset after the
019    * call to next().
020    *
021    @param   in  the {@link Scanner} from which to read
022    *
023    @return  next attribute token: skip whitespace,
024    */
025   public static String readAttribute(Scanner in{
026     String retval = "";
027     in.skip("\\s*");// unlimited whitespace
028     Pattern oldDelimiter = in.delimiter();
029     in.useDelimiter("[\\s=]");// stop at whitespace or =
030     retval = in.next();// next using new delimiter
031     in.useDelimiter(oldDelimiter);// reset delimiter
032     return retval;
033   }
034 
035   /**
036    * Read the next non-whitespace character in the current line (just
037    * one character).
038    *
039    @param   in  the input from which to read
040    *
041    @return  {@link String} containing the next non-whitespace
042    *          character
043    */
044   public static String readChar(Scanner in{
045     return in.findInLine("\\S");
046   }
047 
048   /**
049    * Read in the current line until the given string (pattern) is found.
050    *
051    @param   in     the {@link Scanner} from which to read
052    @param   match  the string to match
053    *
054    @return  null if there is no match, the matching string otherwise.
055    */
056   public static String readMatch(Scanner in, String match{
057     return in.findInLine(match);
058   }
059 
060   /**
061    * Read a value from a {@link Scanner}. A value is either the rest of
062    * the current line _or_ it is an arbitrary number of lines with the
063    * value enclosed in curly braces. The method checks if the next
064    * non-whitespace character is an opening curly brace. If it is, then
065    * we read a curly brace enclosed value; if it is not, then we read to
066    * the end of the current line.
067    *
068    @param   in  the {@link Scanner}, positioned at the beginning of
069    *              the value
070    *
071    @return  {@link String} containing the contents of the value: from
072    *          read position to end of the current line if next character
073    *          is not '{'; the value between (but not including) ' {' and
074    *          ' }' otherwise. Note: it goes to the very next ' }' so it
075    *          does not support escapes or nested groups.
076    */
077   public static String readValue(Scanner in{
078     String retval = "";
079     String openMark = readMatch(in, "<");
080     if (openMark == null{
081       retval = in.nextLine();
082     } else {
083       retval = in.findWithinHorizon("[^>]*"0);
084       in.skip(">[\r\n]*");// consume end of line (and blank lines)
085     }
086     return retval.trim();
087   }
088 
089   /**
090    * Wrap the string original into the given field width, breaking only
091    * at word boundaries. Return a list of lines containing the words
092    * from original but wrapped to the given width.
093    *
094    @param   original    the {@link String} to split into the given
095    *                      field width
096    @param   fieldWidth  width, in characters, of the output.
097    *
098    @return  {@link List} of strings containing same info as original
099    *          but wrapped to the given width
100    */
101   public static ArrayList<String> wrap(String original,
102     int fieldWidth{
103     // the lines will be returned here
104     ArrayList<String> wrappedText = new ArrayList<String>();
105     // trim off leading/trailing whitespace and hook up a scanner
106     Scanner wordScanner = new Scanner(original.trim());
107     StringBuffer line = (wordScanner.hasNext())
108       new StringBuffer(wordScanner.next()) new StringBuffer();
109     while (wordScanner.hasNext()) {
110       String word = wordScanner.next();
111       if ((line.length() + word.length() + 1<= fieldWidth{
112         line.append(" " + word);
113       } else {
114         wrappedText.add(line.toString());
115         line = new StringBuffer(word);
116       }
117     }
118     // line has something in it
119     wrappedText.add(line.toString());
120     return wrappedText;
121   }
122 }
123 
124 //Uploaded on Mon Mar 29 21:39:06 EDT 2010


Download/View scg/ch14/util/ReadAndWrite.java





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter

Games
Games