All Downloads are FREE. Search and download functionalities are using the official Maven repository.

gate.util.SearchParser Maven / Gradle / Ivy

There is a newer version: 12.3.1
Show newest version
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