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

stream.util.parser.GenericTurboParser Maven / Gradle / Ivy

The newest version!
/*
 *  streams library
 *
 *  Copyright (C) 2011-2012 by Christian Bockermann, Hendrik Blom
 * 
 *  streams is a library, API and runtime environment for processing high
 *  volume data streams. It is composed of three submodules "stream-api",
 *  "stream-core" and "stream-runtime".
 *
 *  The streams library (and its submodules) is free software: you can 
 *  redistribute it and/or modify it under the terms of the 
 *  GNU Affero General Public License as published by the Free Software 
 *  Foundation, either version 3 of the License, or (at your option) any 
 *  later version.
 *
 *  The stream.ai library (and its submodules) is distributed in the hope
 *  that it will be useful, but WITHOUT ANY WARRANTY; without even the implied 
 *  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/.
 */
package stream.util.parser;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GenericTurboParser extends MParser implements
		Parser> {
	static Logger log = LoggerFactory.getLogger(GenericTurboParser.class);
	Token[] tokens;
	Map defaults = new LinkedHashMap();

	public GenericTurboParser(List token) {
		this.tokens = token.toArray(new Token[token.size()]);
		// for (Token t : tokens) {
		// log.info("Token: '{}'", t.value);
		// }
	}

	@Override
	public Map parse(String str) throws ParseException {
		this.pos = 0; // reset();
		Map reads = new LinkedHashMap();

		if (tokens.length < 1)
			return reads;

		Token token = tokens[0];
		Token next = null;

		String values[] = new String[tokens.length];
		int cur = 0;
		for (int i = 1; i < tokens.length + 1; i++) {
			if (i != tokens.length)
				next = tokens[i];
			else
				next = null;

			if (token.isVariable) {
				if (next == null) {
					if (pos >= 0 && pos < str.length())
						values[cur] = str.substring(pos);
					else
						values[cur] = "";
					break;
				}

				if (next.isVariable) {
					int idx = str.indexOf(next.value, pos);
					if (idx >= 0)
						values[cur] = str.substring(idx + next.length);
				} else {
					int idx = str.indexOf(next.value, pos);
					if (idx < 0)
						idx = str.length();
					values[cur] = str.substring(pos, idx);
					pos = idx;
				}
			} else {
				this.pos += token.length;
				values[cur] = null;
			}
			token = next;
			cur++;
		}

		for (int i = 0; i < values.length; i++) {
			if (values[i] != null) {
				reads.put(tokens[i].name, values[i]);
			}
		}

		return reads;
	}

	@Override
	public Map getDefaults() {
		return defaults;
	}

	@Override
	public void setDefaults(Map defaults) {
		this.defaults.clear();
		this.defaults.putAll(defaults);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy