001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019 */
020package org.sonar.channel;
021
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025/**
026 * The RegexChannel can be used to be called each time the next characters in the character stream match a regular expression
027 */
028public abstract class RegexChannel<OUTPUT> extends Channel<OUTPUT> {
029
030  private final StringBuilder tmpBuilder = new StringBuilder();
031  private final Matcher matcher;
032  private final String regex;
033
034  /**
035   * Create a RegexChannel object with the required regular expression
036   * 
037   * @param regex
038   *          regular expression to be used to try matching the next characters in the stream
039   */
040  public RegexChannel(String regex) {
041    matcher = Pattern.compile(regex).matcher("");
042    this.regex = regex;
043  }
044
045  @Override
046  public final boolean consume(CodeReader code, OUTPUT output) {
047    try {
048      if (code.popTo(matcher, tmpBuilder) > 0) {
049        consume(tmpBuilder, output);
050        tmpBuilder.delete(0, tmpBuilder.length());
051        return true;
052      }
053      return false;
054    } catch (StackOverflowError e) {
055      throw new IllegalArgumentException(
056          "The regular expression "
057              + regex
058              + " has led to a stack overflow error. "
059              + "This error is certainly due to an inefficient use of alternations. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5050507",
060          e);
061    }
062  }
063
064  /**
065   * The consume method is called each time the regular expression used to create the RegexChannel object matches the next characters in the
066   * character streams.
067   * 
068   * @param token
069   *          the token consumed in the character stream and matching the regular expression
070   * @param the
071   *          OUPUT object which can be optionally fed
072   */
073  protected abstract void consume(CharSequence token, OUTPUT output);
074}