/** *######################################################################### * * A component of the Gatherer application, part of the Greenstone digital * library suite from the New Zealand Digital Library Project at the * University of Waikato, New Zealand. * *

* * Author: John Thompson, Greenstone Digital Library, University of Waikato * *

* * Copyright (C) 1999 New Zealand Digital Library Project * *

* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * *

* * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * *

* * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *######################################################################## */ /* GPL_HEADER */ package org.greenstone.gatherer.cdm; /************************************************************************************** * Title: Gatherer * Description: The Gatherer: a tool for gathering and enriching a digital collection. * Company: The University of Waikato * Written: 07/05/02 * Revised: 03/10/02 - Commented **************************************************************************************/ import java.util.StringTokenizer; /** This class provides an extension to the standard StringTokenizer in that it recognizes quotes (or some form of bracketting) enclose a single token so in something like:
format Search '<table><img src=... </table>'
the formatting string is parsed as a single token. Unfortunately this makes countTokens() unreliable for exact measurement of tokens remaining, and only useful for determining if there are tokens left to be processed (includes any that have already been read into command buffer). * @author John Thompson, Greenstone Digital Library, University of Waikato * @version 2.3 */ public class CommandTokenizer extends StringTokenizer { private int last_type = -1; static final public int BRACKET_ENCLOSED = 0; static final public int DOUBLE_QUOTE_ENCLOSED = 1; static final public int NORMAL = 2; static final public int QUOTE_ENCLOSED = 3; /** Constructor. * @param command The command String you wish to tokenize. */ public CommandTokenizer(String command) { super(command); } public int getLastType() { return last_type; } public boolean isComment() { return (last_type == DOUBLE_QUOTE_ENCLOSED || last_type == QUOTE_ENCLOSED); } /** Method to retrieve the next token from the command, taking care to group tokens enclosed in speech marks. * @return A String containing the next token from the command. */ public String nextToken() { String result = null; if(hasMoreTokens()) { StringBuffer buffer = new StringBuffer(super.nextToken()); switch(buffer.charAt(0)) { case '\"': while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != '\"') && hasMoreTokens()) { buffer.append(" "); buffer.append(super.nextToken()); ///ystem.err.println("Current Buffer = '" + buffer.toString() + "'"); } ///ystem.err.println("Final Buffer = '" + buffer.toString() + "'"); last_type = DOUBLE_QUOTE_ENCLOSED; break; case '\'': while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != '\'') && hasMoreTokens()) { buffer.append(" "); buffer.append(super.nextToken()); } last_type = QUOTE_ENCLOSED; break; case '[': while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != ']') && hasMoreTokens()) { buffer.append(" "); buffer.append(super.nextToken()); } last_type = BRACKET_ENCLOSED; break; default: last_type = NORMAL; } result = buffer.toString(); } return result; } /** Unfortunately the StringBuffer doesn't have a built in endsWith method, so I'll just have to implement my own. * @param str The StringBuffer we are checking the end of. * @param target The String fragment we are searching for. * @return true if str ends with target, false otherwise. */ private boolean endsWith(StringBuffer str, String target) { String temp = str.toString(); if(temp.endsWith(target) != (str.lastIndexOf(target) == str.length() - target.length())) { ///ystem.err.println("Holy error that'll crash the HFile creator if it happens twice, Batman!"); ///ystem.err.println("String = '" + temp + "'"); ///ystem.err.println("Target = '" + target + "'"); } return str.lastIndexOf(target) == str.length() - target.length(); } }