/**
*
*/
package it.halfone.parser.token.impl;
import it.halfone.exception.InvalidDocumentException;
import it.halfone.hava.container.Context;
import it.halfone.parser.token.TokenParser;
import it.halfone.regex.Regex;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* OrTokenParser - 03/ott/2011
*
* @author Andrea La Rosa
*/
public class OrTokenParser implements TokenParser{
private static final Pattern pattern = Pattern.compile("^.*?" + TokenParser.ID + "" +
"([\\d]+)\\|" + TokenParser.ID + "([\\d]+).*$");
/* (non-Javadoc)
* @see it.halfone.parser.token.TokenParser#parseToken(it.halfone.hava.Context)
*/
@Override
public boolean parseToken(Context ctx) throws InvalidDocumentException {
boolean retVal;
int index = ctx.getValue("index");
String token = ctx.getValue("token");
List<Regex> regexList = ctx.getValue("regexList");
Matcher matcher = pattern.matcher(token);
if(retVal = matcher.find()){
token = token.replaceFirst(TokenParser.ID + "([\\d]+)\\|" + TokenParser.ID + "([\\d]+)", TokenParser.ID + index);
Regex a = regexList.get(Integer.parseInt(matcher.group(1)));
Regex b = regexList.get(Integer.parseInt(matcher.group(2)));
regexList.add(a.or(b));
ctx.setValue("token", token);
ctx.setValue("index", ++index);
}
return retVal;
}
}