gate.util.SearchParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gate Show documentation
Show all versions of gate Show documentation
A multipurpose java library
package gate.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class SearchParser implements Iterable
{
private String string;
private final List tokens
= new ArrayList<>();
public SearchParser(String string)
{
if (string != null)
{
int i = 0;
char state = ' ';
StringBuilder token = new StringBuilder();
do
{
switch (string.charAt(i))
{
case ' ':
switch (state)
{
case ' ':
tokens.add(token.toString());
token.setLength(0);
state = ' ';
break;
case '"':
token.append(string.charAt(i));
break;
case '\'':
token.append(string.charAt(i));
break;
}
break;
case '"':
switch (state)
{
case ' ':
state = '"';
break;
case '"':
tokens.add(token.toString());
token.setLength(0);
state = ' ';
break;
case '\'':
token.append(string.charAt(i));
break;
}
break;
case '\'':
switch (state)
{
case ' ':
state = '\'';
break;
case '"':
token.append(string.charAt(i));
break;
case '\'':
tokens.add(token.toString());
token.setLength(0);
state = ' ';
break;
}
break;
default:
token.append(string.charAt(i));
}
} while (++i < string.length());
if (token.length() > 0)
tokens.add(token.toString());
}
}
public List getTokens()
{
return Collections.unmodifiableList(tokens);
}
@Override
public Iterator iterator()
{
return getTokens().iterator();
}
@Override
public String toString()
{
return string;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy