From FANG
|
001 packagepackage is used to name the directory or folder a class is in scg.ch14.util;
002
003 importimport means to make the classes and/or packages available in this program java.util.ArrayList;
004 importimport means to make the classes and/or packages available in this program java.util.List;
005 importimport means to make the classes and/or packages available in this program java.util.Scanner;
006 importimport means to make the classes and/or packages available in this program java.util.regex.Pattern;
007
008 /**
009 * Utility classclass is a group of fields and methods used for making objects providing input handling methods forfor is a looping structure for repeatedly executing a block of code reading
010 * attribute-value pairs and formatting output to a fixed width. All
011 * methods are staticstatic means that an instance is not required for access (class level access).
012 */
013 publicpublic is used to indicate unrestricted access (any other class can have access) classclass is a group of fields and methods used for making objects ReadAndWrite {open braces start code blocks and must be matched with a close brace
014 /**
015 * Read in an attribute. An attribute is delimited by whitespace in
016 * front of it and whitespace OR an =this assignment operator makes the left side equal to the right side after. The delimiter of in is
017 * set to [brackets are typically used to declare, initialize and index (indicate which element of) arrays\s=this assignment operator makes the left side equal to the right side]brackets are typically used to declare, initialize and index (indicate which element of) arrays, the set of \s charchar is the type for a single letter or symbol and it is short for character and =this assignment operator makes the left side equal to the right side charchar is the type for a single letter or symbol and it is short for character. =this assignment operator makes the left side equal to the right side is just that symbol;
018 * \s is any whitespace charchar is the type for a single letter or symbol and it is short for character. The delimiter must be reset after the
019 * call to next().
020 *
021 * @paramthis is the Javadoc tag for documenting the purpose of parameters in the {open braces start code blocks and must be matched with a close brace@link Scanner}close braces end code blocks and must match an earlier open brace from which to read
022 *
023 * @returnnull next attribute token: skip whitespace,
024 */
025 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) String readAttribute(Scanner in) {open braces start code blocks and must be matched with a close brace
026 String retval =this assignment operator makes the left side equal to the right side "";
027 in.skip("\\s*");// unlimited whitespace
028 Pattern oldDelimiter =this assignment operator makes the left side equal to the right side in.delimiter();
029 in.useDelimiter("[\\s=]");// stop at whitespace or =
030 retval =this assignment operator makes the left side equal to the right side in.next();// next using new delimiter
031 in.useDelimiter(oldDelimiter);// reset delimiter
032 returnreturn means to provide the result of the method and/or cease execution of the method immediately retval;
033 }close braces end code blocks and must match an earlier open brace
034
035 /**
036 * Read the next non-whitespace character in the current line (just
037 * one character).
038 *
039 * @paramthis is the Javadoc tag for documenting the purpose of parameters in the input from which to read
040 *
041 * @returnnull a {open braces start code blocks and must be matched with a close brace@link String}close braces end code blocks and must match an earlier open brace containing the next non-whitespace
042 * character
043 */
044 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) String readChar(Scanner in) {open braces start code blocks and must be matched with a close brace
045 returnreturn means to provide the result of the method and/or cease execution of the method immediately in.findInLine("\\S");
046 }close braces end code blocks and must match an earlier open brace
047
048 /**
049 * Read in the current line until the given string (pattern) is found.
050 *
051 * @paramthis is the Javadoc tag for documenting the purpose of parameters in the {open braces start code blocks and must be matched with a close brace@link Scanner}close braces end code blocks and must match an earlier open brace from which to read
052 * @paramthis is the Javadoc tag for documenting the purpose of parameters match the string to match
053 *
054 * @returnnull nullnull is the value used to refer to a non-existant object ifif executes the next statement only if the condition in parenthesis evaluates to true there is no match, the matching string otherwise.
055 */
056 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) String readMatch(Scanner in, String match) {open braces start code blocks and must be matched with a close brace
057 returnreturn means to provide the result of the method and/or cease execution of the method immediately in.findInLine(match);
058 }close braces end code blocks and must match an earlier open brace
059
060 /**
061 * Read a value from a {open braces start code blocks and must be matched with a close brace@link Scanner}close braces end code blocks and must match an earlier open brace. 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 ifif executes the next statement only if the condition in parenthesis evaluates to true the next
064 * non-whitespace character is an opening curly brace. If it is, then
065 * we read a curly brace enclosed value; ifif executes the next statement only if the condition in parenthesis evaluates to true it is not, then we read to
066 * the end of the current line.
067 *
068 * @paramthis is the Javadoc tag for documenting the purpose of parameters in the {open braces start code blocks and must be matched with a close brace@link Scanner}close braces end code blocks and must match an earlier open brace, positioned at the beginning of
069 * the value
070 *
071 * @returnnull {open braces start code blocks and must be matched with a close brace@link String}close braces end code blocks and must match an earlier open brace containing the contents of the value: from
072 * read position to end of the current line ifif executes the next statement only if the condition in parenthesis evaluates to true next character
073 * is not '{open braces start code blocks and must be matched with a close brace'; the value between (but not including) ' {open braces start code blocks and must be matched with a close brace' and
074 * ' }close braces end code blocks and must match an earlier open brace' otherwise. Note: it goes to the very next ' }close braces end code blocks and must match an earlier open brace' so it
075 * does not support escapes or nested groups.
076 */
077 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) String readValue(Scanner in) {open braces start code blocks and must be matched with a close brace
078 String retval =this assignment operator makes the left side equal to the right side "";
079 String openMark =this assignment operator makes the left side equal to the right side readMatch(in, "<");
080 ifif executes the next statement only if the condition in parenthesis evaluates to true (openMark ==this is the comparison operator which evaluates to true if both sides are the same nullnull is the value used to refer to a non-existant object) {open braces start code blocks and must be matched with a close brace
081 retval =this assignment operator makes the left side equal to the right side in.nextLine();
082 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
083 retval =this assignment operator makes the left side equal to the right side in.findWithinHorizon("[^>]*", 0);
084 in.skip(">[\r\n]*");// consume end of line (and blank lines)
085 }close braces end code blocks and must match an earlier open brace
086 returnreturn means to provide the result of the method and/or cease execution of the method immediately retval.trim();
087 }close braces end code blocks and must match an earlier open brace
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 * @paramthis is the Javadoc tag for documenting the purpose of parameters original the {open braces start code blocks and must be matched with a close brace@link String}close braces end code blocks and must match an earlier open brace to split into the given
095 * field width
096 * @paramthis is the Javadoc tag for documenting the purpose of parameters fieldWidth width, in characters, of the output.
097 *
098 * @returnnull a {open braces start code blocks and must be matched with a close brace@link List}close braces end code blocks and must match an earlier open brace of strings containing same info as original
099 * but wrapped to the given width
100 */
101 publicpublic is used to indicate unrestricted access (any other class can have access) staticstatic means that an instance is not required for access (class level access) ArrayList<String> wrap(String original,
102 intint is the type for whole numbers and it is short for integer fieldWidth) {open braces start code blocks and must be matched with a close brace
103 // the lines will be returned here
104 ArrayList<String> wrappedText =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor ArrayList<String>();
105 // trim off leading/trailing whitespace and hook up a scanner
106 Scanner wordScanner =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor Scanner(original.trim());
107 StringBuffer line =this assignment operator makes the left side equal to the right side (wordScanner.hasNext())
108 ? newnew is used to create objects by calling the constructor StringBuffer(wordScanner.next()) : newnew is used to create objects by calling the constructor StringBuffer();
109 whilewhile is a looping structure for executing code repeatedly (wordScanner.hasNext()) {open braces start code blocks and must be matched with a close brace
110 String word =this assignment operator makes the left side equal to the right side wordScanner.next();
111 ifif executes the next statement only if the condition in parenthesis evaluates to true ((line.length() +adds two numbers together or concatenates Strings together word.length() +adds two numbers together or concatenates Strings together 1) <=this evaluates to true if the left side is not more than the right side fieldWidth) {open braces start code blocks and must be matched with a close brace
112 line.append(" " +adds two numbers together or concatenates Strings together word);
113 }close braces end code blocks and must match an earlier open brace elseelse is what happens when the if condition is false {open braces start code blocks and must be matched with a close brace
114 wrappedText.add(line.toString());
115 line =this assignment operator makes the left side equal to the right side newnew is used to create objects by calling the constructor StringBuffer(word);
116 }close braces end code blocks and must match an earlier open brace
117 }close braces end code blocks and must match an earlier open brace
118 // line has something in it
119 wrappedText.add(line.toString());
120 returnreturn means to provide the result of the method and/or cease execution of the method immediately wrappedText;
121 }close braces end code blocks and must match an earlier open brace
122 }close braces end code blocks and must match an earlier open brace
123
124 //Uploaded on Mon Mar 29 21:39:06 EDT 2010
|
Download/View scg/ch14/util/ReadAndWrite.java