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 | * Author: John Thompson, Greenstone Digital Library, University of Waikato
|
---|
9 | *
|
---|
10 | * Copyright (C) 1999 New Zealand Digital Library Project
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
25 | *########################################################################
|
---|
26 | */
|
---|
27 | package org.greenstone.gatherer.util;
|
---|
28 |
|
---|
29 | public class PatternTokenizer {
|
---|
30 | private int index;
|
---|
31 | private String content;
|
---|
32 | private String pattern;
|
---|
33 | public PatternTokenizer(String content, String pattern) {
|
---|
34 | this.content = content;
|
---|
35 | this.index = 0;
|
---|
36 | this.pattern = pattern;
|
---|
37 | }
|
---|
38 | public boolean hasMoreTokens() {
|
---|
39 | return (index != -1 && content.length() - index > 0);
|
---|
40 | }
|
---|
41 | public String nextToken() {
|
---|
42 | // Special case when index is zero
|
---|
43 | if(index == 0) {
|
---|
44 | index = content.indexOf(pattern);
|
---|
45 | // No occurance of pattern, return whole string
|
---|
46 | if(index == -1) {
|
---|
47 | return content;
|
---|
48 | }
|
---|
49 | // Return the fragment from 0 to index
|
---|
50 | else {
|
---|
51 | return content.substring(0, index);
|
---|
52 | }
|
---|
53 | }
|
---|
54 | // Otherwise
|
---|
55 | else {
|
---|
56 | // We must first skip the pattern, as it should be at the head of our current view into content
|
---|
57 | index = index + pattern.length();
|
---|
58 | int end_index = content.indexOf(pattern, index);
|
---|
59 | String fragment;
|
---|
60 | // No occurance of pattern, return fragment from index to length
|
---|
61 | if(end_index == -1) {
|
---|
62 | fragment = content.substring(index);
|
---|
63 | }
|
---|
64 | // Return the fragment from index to end index, then set index to end index
|
---|
65 | else {
|
---|
66 | fragment = content.substring(index, end_index);
|
---|
67 | }
|
---|
68 | index = end_index;
|
---|
69 | return fragment;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|