View Javadoc

1   /*
2    * #%L
3    * Native ARchive plugin for Maven
4    * %%
5    * Copyright (C) 2002 - 2014 NAR Maven Plugin developers.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   * http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.github.maven_nar.cpptasks.parser;
21  
22  /**
23   * This parser state checks consumed characters against a specific character
24   * (case insensitive).
25   *
26   * @author Curt Arnold
27   */
28  public final class CaseInsensitiveLetterState extends AbstractParserState {
29    /**
30     * Next state if a match is found.
31     */
32    private final AbstractParserState nextState;
33  
34    /**
35     * Next state if not match is found.
36     */
37    private final AbstractParserState noMatchState;
38  
39    /**
40     * Lower case version of character to match.
41     */
42    private final char lowerLetter;
43  
44    /**
45     * Lower case version of character to match.
46     */
47    private final char upperLetter;
48  
49    /**
50     * Constructor.
51     *
52     * @param parser
53     *          parser
54     * @param matchLetter
55     *          letter to match
56     * @param nextStateArg
57     *          next state if a match on the letter
58     * @param noMatchStateArg
59     *          state if no match on letter
60     */
61    public CaseInsensitiveLetterState(final AbstractParser parser, final char matchLetter,
62        final AbstractParserState nextStateArg, final AbstractParserState noMatchStateArg) {
63      super(parser);
64      this.lowerLetter = Character.toLowerCase(matchLetter);
65      this.upperLetter = Character.toUpperCase(matchLetter);
66      this.nextState = nextStateArg;
67      this.noMatchState = noMatchStateArg;
68    }
69  
70    /**
71     * Consumes a character and returns the next state for the parser.
72     *
73     * @param ch
74     *          next character
75     * @return the configured nextState if ch is the expected character or the
76     *         configure noMatchState otherwise.
77     */
78    @Override
79    public AbstractParserState consume(final char ch) {
80      if (ch == this.lowerLetter || ch == this.upperLetter) {
81        return this.nextState;
82      }
83      if (ch == '\n') {
84        getParser().getNewLineState();
85      }
86      return this.noMatchState;
87    }
88  }