| (Originally from SearchDomino post) 
 Paul Cathro, 2004
 
 --------------------------------------------------------------------------------
 
 This is a simple class that emulates the @Word function in Lotus Notes. It uses the StringTokenizer class to parse a string, and uses the NextToken method of StringTokenizer to load a vector with all the tokens of that string.
 
 The vector is then used by the tokenFound method to return the token requested as a parameter to the tokenFound method. If the value passed falls outside the number of tokens, a NULL is returned. The same three constructors found in the StringTokenizer class are included in this class.
 
 
 import java.util.*;
 import java.io.*;
 
 /**
 
 * This is a simple class that emulates the @Word function in Lotus
 Notes. It uses the StringTokenizer class to parse a string and uses the
 NextToken method of StringTokenizer to load a vector with all the tokens of
 that string. The vector is then used by the tokenFound method to return the
 token requested as a parameter to the tokenFound method. If the value passed
 falls outside the number of tokens a NULL is returned. The same three
 constructors found in the StringTokenizer class are included in this class.
 
 The following is one example of the use of JavaWord. The code:
 
 String tmp = "This~is~a~test~of~parsing~text";
 JavaWord jw = new JavaWord(tmp, "~");
 String jwStr = jw.tokenFound("4");
 
 * Prints the following output:
 test
 
 @author Paul V. Cathro
 @version 1.1
 
 */
 
 public class JavaWord {
 private static final PrintStream o = System.out;
 private static int j, tok;
 private static String tokenFound, holdToken, prevToken = "";
 private static StringTokenizer pt1;
 private static Vector token = new Vector();
 private static boolean firstNoDel = false;
 
 /**
 
 * Receives the string to parse. Then it loads a vector with all the tokens in
 the string. This constructor uses " tnr" (blank, tab, newline, and return) as
 delimiters and the delimiters are not returned as tokens.
 
 * @param sStr - String to parse
 */
 public JavaWord(String sStr) {
 token.removeAllElements();
 token.trimToSize();
 pt1 = new StringTokenizer(sStr);
 tok = pt1.countTokens();
 while (pt1.hasMoreTokens()) {
 token.addElement(pt1.nextToken());
 }
 }
 
 /**
 
 * Receives the string to parse and the delimiter to parse on.  Then it loads a
 vector with all the tokens in the string.
 
 * @param sStr - String to parse
 * @param sTok - delimeter to parse on
 */
 public JavaWord(String sStr, String sTok) {
 token.removeAllElements();
 token.trimToSize();
 pt1 = new StringTokenizer(sStr, sTok);
 tok = pt1.countTokens();
 while (pt1.hasMoreTokens()) {
 token.addElement(pt1.nextToken());
 }
 }
 
 /**
 
 * Receives the string to parse, the delimiter to parse on, and Boolean set
 to "true" if there is the possibility having blank tokens or the string
 starting with a blank token. Then it loads a vector with all the tokens in the
 string. If the Boolean is set to "true" then the delimiters are included as
 tokens.
 
 * @param sStr - String to parse
 * @param sTok - delimeter to parse on
 * @param retTok - Boolean, if set to true will check for and handle
 blank tokens
 */
 public JavaWord(String sStr, String sTok, boolean retTok) {
 token.removeAllElements();
 token.trimToSize();
 if (retTok == true) {
 pt1 = new StringTokenizer(sStr, sTok, true);
 tok = pt1.countTokens();
 // if last token is a delimiter then subtract 1 from
 the total token count
 if ((sStr.lastIndexOf("~") + 1) == sStr.length()) {
 tok--;
 }
 int z = 0;
 while (pt1.hasMoreTokens()) {
 holdToken = pt1.nextToken();
 
 if (!holdToken.equals(sTok)) {
 token.addElement(holdToken);
 firstNoDel = true;
 } else if (prevToken.equals(sTok) &&
 holdToken.equals(sTok)) {
 token.addElement("");
 } else if (firstNoDel == false && z == 0 &&
 holdToken.equals(sTok)) {
 token.addElement("");
 z++;
 } else if (firstNoDel == true && z == 0 &&
 holdToken.equals(sTok)) {
 z++;
 } else if (z > 0 && holdToken.equals(sTok)) {
 z++;
 }
 prevToken = holdToken;
 }
 tok = tok - z;
 } else {
 pt1 = new StringTokenizer(sStr, sTok, false);
 tok = pt1.countTokens();
 while (pt1.hasMoreTokens()) {
 token.addElement(pt1.nextToken());
 }
 }
 firstNoDel = false;
 }
 
 /**
 
 * Takes the number passed to this method (tokenFound) and returns the
 corresponding token based on the string passed to the constructor.  If a number
 is passed to this method that is outside of the total token count then a blank
 is returned.
 
 * @param tokWant - number of the token to return
 *
 * @return token requested
 */
 public String tokenFound(String tokWant) {
 Integer tokInt = Integer.valueOf(tokWant);
 int tmpInt = tokInt.intValue() - 1;   // -1 to adjust to the
 StringTokenizer array starting at zero
 if (tmpInt <= tok && tmpInt >= 0 && !token.isEmpty() &&
 tokInt.intValue() <= token.size()) {
 tokenFound = (String)token.elementAt(tmpInt);
 } else {
 tokenFound = "";
 }
 return tokenFound;
 }
 }
 
 
 previous page
 
 
 |