package org.jetel.component;

import java.nio.BufferUnderflowException;
import java.nio.CharBuffer;
import java.util.Properties;

import org.jetel.data.parser.MultiLevelSelector;
import org.jetel.exception.ComponentNotReadyException;
import org.jetel.metadata.DataRecordMetadata;

public class HDTNumberSelector implements MultiLevelSelector {

	int skipChars;
	int chosen = -1;
	
	public void choose(CharBuffer arg0)
			throws BufferUnderflowException {
		char c;
		// read characters
		// and discard newlines and comments
		int inCommentType = 0;
		int commentNest = 0;
		int commentChars = 0;
		do {
			c = arg0.get();
			if (c == '\r' || c == '\n') {
				if (inCommentType == 1) {
					inCommentType = 0;
				}
				skipChars++;
				continue;
			} else if ((inCommentType == 1)) {
				skipChars++;
				continue;
			} else if ((c == '#') && inCommentType == 0) {
				skipChars++;
				inCommentType = 1;
				continue;
			} else if ((commentChars == 0) && (c == '/')) { 
				commentChars++;
				skipChars++;
				continue;
			} else if (commentChars == 1) { 
				commentChars = 0;
				if (c == '*') {
					if (commentNest == 0) {
						inCommentType = 2;
					}
					commentNest++;
				}
				skipChars++;
				continue;
			} else if (inCommentType == 2)  {
				if ((commentChars == 0) && (c == '*')) {
					commentChars--;
				} else if (commentChars == -1) {
					commentChars = 0;
					if (c == '/') {
						commentNest--;
						if (commentNest <= 0) {
							inCommentType = 0;
							commentNest = 0;
						}
					}
				}
				skipChars++;
				continue;
			} else if (c == ' ' || c == '\t') {
				skipChars++;
				continue;
			} else {
				break;
			}
		} while (true);

		System.out.println("ExampleSelector: Decision character is : " + c);
		
		switch (c) {
		case '1':
			chosen = 0;
			break;
		case '2':
			chosen = 1;
			break;
		case '3':
			chosen = 2;
			break;
		case 'H' :
		case 'F' :
			chosen = 3;
			break;
		case 'C' :
			chosen = 4;
			break;
		default:
			chosen = -1;
			break;
		}
	}

	public void init(DataRecordMetadata[] arg0, Properties arg1)
			throws ComponentNotReadyException {
	}

	public int lookAheadCharacters() {
		return 1;
	}

	public int nextRecordOffset() {
		return skipChars;
	}

	public void reset() {
		skipChars = 0;
		chosen = -1;
	}

	public int nextRecordMetadataIndex() {
		return chosen;
	}


}

@@ -2455,13 +2455,166 @@
 %0D%0A%09%7D%0D%0A%0D%0A
+%09public boolean recoverToNextRecord(CharBuffer data)%0D%0A%09%09%09throws BufferUnderflowException %7B%0D%0A%09%09// TODO Auto-generated method stub%0D%0A%09%09return false;%0D%0A%09%7D%0D%0A%0D%0A
 %0D%0A%7D%0D%0A

FIX issue 1300 fixed build fail