source: trunk/gli/src/org/greenstone/gatherer/cdm/CommandTokenizer.java@ 4293

Last change on this file since 4293 was 4293, checked in by jmt12, 21 years ago

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37
38
39
40
41
42
43/* GPL_HEADER */
44package org.greenstone.gatherer.cdm;
45/**************************************************************************************
46 * Title: Gatherer
47 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
48 * Company: The University of Waikato
49 * Written: 07/05/02
50 * Revised: 03/10/02 - Commented
51 **************************************************************************************/
52import java.util.StringTokenizer;
53/** 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: <br>format Search '&lt;table&gt;&lt;img src=... &lt;/table&gt;'<br> 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).
54 * @author John Thompson, Greenstone Digital Library, University of Waikato
55 * @version 2.3
56 */
57public class CommandTokenizer
58 extends StringTokenizer {
59
60 private int last_type = -1;
61
62 static final public int BRACKET_ENCLOSED = 0;
63 static final public int DOUBLE_QUOTE_ENCLOSED = 1;
64 static final public int NORMAL = 2;
65 static final public int QUOTE_ENCLOSED = 3;
66
67 /** Constructor.
68 * @param command The command <strong>String</strong> you wish to tokenize.
69 */
70 public CommandTokenizer(String command) {
71 super(command);
72 }
73
74 public int getLastType() {
75 return last_type;
76 }
77
78 public boolean isComment() {
79 return (last_type == DOUBLE_QUOTE_ENCLOSED || last_type == QUOTE_ENCLOSED);
80 }
81
82 /** Method to retrieve the next token from the command, taking care to group tokens enclosed in speech marks.
83 * @return A <strong>String</strong> containing the next token from the command.
84 */
85 public String nextToken() {
86 String result = null;
87 if(hasMoreTokens()) {
88 StringBuffer buffer = new StringBuffer(super.nextToken());
89 switch(buffer.charAt(0)) {
90 case '\"':
91 while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != '\"') && hasMoreTokens()) {
92 buffer.append(" ");
93 buffer.append(super.nextToken());
94 ///ystem.err.println("Current Buffer = '" + buffer.toString() + "'");
95 }
96 ///ystem.err.println("Final Buffer = '" + buffer.toString() + "'");
97 last_type = DOUBLE_QUOTE_ENCLOSED;
98 break;
99 case '\'':
100 while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != '\'') && hasMoreTokens()) {
101 buffer.append(" ");
102 buffer.append(super.nextToken());
103 }
104 last_type = QUOTE_ENCLOSED;
105 break;
106 case '[':
107 while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != ']') && hasMoreTokens()) {
108 buffer.append(" ");
109 buffer.append(super.nextToken());
110 }
111 last_type = BRACKET_ENCLOSED;
112 break;
113 default:
114 last_type = NORMAL;
115 }
116 result = buffer.toString();
117 }
118 return result;
119 }
120 /** Unfortunately the StringBuffer doesn't have a built in endsWith method, so I'll just have to implement my own.
121 * @param str The <strong>StringBuffer</strong> we are checking the end of.
122 * @param target The <strong>String</strong> fragment we are searching for.
123 * @return <i>true</i> if str ends with target, <i>false</i> otherwise.
124 */
125 private boolean endsWith(StringBuffer str, String target) {
126 String temp = str.toString();
127 if(temp.endsWith(target) != (str.lastIndexOf(target) == str.length() - target.length())) {
128 ///ystem.err.println("Holy error that'll crash the HFile creator if it happens twice, Batman!");
129 ///ystem.err.println("String = '" + temp + "'");
130 ///ystem.err.println("Target = '" + target + "'");
131 }
132 return str.lastIndexOf(target) == str.length() - target.length();
133 }
134}
135
136
Note: See TracBrowser for help on using the repository browser.