Package it.halfone.parser.token.impl

Source Code of it.halfone.parser.token.impl.ModifierTokenParser

/**
*
*/
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;

/**
* BracketTokenParser - 03/ott/2011
*
* @author Andrea La Rosa
*/
public class ModifierTokenParser implements TokenParser{

  private static final Pattern pattern = Pattern.compile("^.*?" + 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 + index);
      Regex r = regexList.get(Integer.parseInt(matcher.group(1)));
      if (matcher.group(2).equals("*")) {
        regexList.add(r.star());
      } else if (matcher.group(2).equals("+")) {
        regexList.add(r.atLeastOnce());
      } else if (matcher.group(2).equals("?")) {
        regexList.add(r.optional());
      }
     
      ctx.setValue("token", token);
      ctx.setValue("index", ++index);
      }
     
      return retVal;
    }

}
TOP

Related Classes of it.halfone.parser.token.impl.ModifierTokenParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.