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

Last change on this file since 5590 was 5590, checked in by mdewsnip, 21 years ago

Could it be I've finished adding tooltips?? Why yes, very nearly... and a big "hallelulah" for that.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 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 */
37package org.greenstone.gatherer.cdm;
38
39/**************************************************************************************
40 * Title: Gatherer
41 * Description: The Gatherer: a tool for gathering and enriching a digital collection.
42 * Company: The University of Waikato
43 * Written: 07/05/02
44 * Revised: 03/10/02 - Commented
45 **************************************************************************************/
46import java.util.StringTokenizer;
47
48/** 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).
49 * @author John Thompson, Greenstone Digital Library, University of Waikato
50 * @version 2.3
51 */
52public class CommandTokenizer
53 extends StringTokenizer {
54
55 private int last_type = -1;
56
57 static final public int BRACKET_ENCLOSED = 0;
58 static final public int DOUBLE_QUOTE_ENCLOSED = 1;
59 static final public int NORMAL = 2;
60 static final public int QUOTE_ENCLOSED = 3;
61
62 /** Constructor.
63 * @param command The command <strong>String</strong> you wish to tokenize.
64 */
65 public CommandTokenizer(String command) {
66 super(command);
67 }
68
69 public int getLastType() {
70 return last_type;
71 }
72
73 public boolean isComment() {
74 return (last_type == DOUBLE_QUOTE_ENCLOSED || last_type == QUOTE_ENCLOSED);
75 }
76
77 /** Method to retrieve the next token from the command, taking care to group tokens enclosed in speech marks.
78 * @return A <strong>String</strong> containing the next token from the command.
79 */
80 public String nextToken() {
81 String result = null;
82 if(hasMoreTokens()) {
83 StringBuffer buffer = new StringBuffer(super.nextToken());
84 switch(buffer.charAt(0)) {
85 case '\"':
86 while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != '\"') && hasMoreTokens()) {
87 buffer.append(" ");
88 buffer.append(super.nextToken());
89 ///ystem.err.println("Current Buffer = '" + buffer.toString() + "'");
90 }
91 ///ystem.err.println("Final Buffer = '" + buffer.toString() + "'");
92 last_type = DOUBLE_QUOTE_ENCLOSED;
93 break;
94 case '\'':
95 while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != '\'') && hasMoreTokens()) {
96 buffer.append(" ");
97 buffer.append(super.nextToken());
98 }
99 last_type = QUOTE_ENCLOSED;
100 break;
101 case '[':
102 while((buffer.length() == 1 || buffer.charAt(buffer.length() - 1) != ']') && hasMoreTokens()) {
103 buffer.append(" ");
104 buffer.append(super.nextToken());
105 }
106 last_type = BRACKET_ENCLOSED;
107 break;
108 default:
109 last_type = NORMAL;
110 }
111 result = buffer.toString();
112 }
113 return result;
114 }
115
116 /** Unfortunately the StringBuffer doesn't have a built in endsWith method, so I'll just have to implement my own.
117 * @param str The <strong>StringBuffer</strong> we are checking the end of.
118 * @param target The <strong>String</strong> fragment we are searching for.
119 * @return <i>true</i> if str ends with target, <i>false</i> otherwise.
120 */
121 private boolean endsWith(StringBuffer str, String target) {
122 String temp = str.toString();
123 if(temp.endsWith(target) != (str.lastIndexOf(target) == str.length() - target.length())) {
124 ///ystem.err.println("Holy error that'll crash the HFile creator if it happens twice, Batman!");
125 ///ystem.err.println("String = '" + temp + "'");
126 ///ystem.err.println("Target = '" + target + "'");
127 }
128 return str.lastIndexOf(target) == str.length() - target.length();
129 }
130}
Note: See TracBrowser for help on using the repository browser.