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  import java.io.IOException;
23  import java.io.Reader;
24  
25  /**
26   * An abstract base class for simple parsers
27   *
28   * @author Curt Arnold
29   */
30  public abstract class AbstractParser {
31    /**
32       *
33       *
34       */
35    protected AbstractParser() {
36    }
37  
38    protected abstract void addFilename(String filename);
39  
40    public abstract AbstractParserState getNewLineState();
41  
42    protected void parse(final Reader reader) throws IOException {
43      final char[] buf = new char[4096];
44      final AbstractParserState newLineState = getNewLineState();
45      AbstractParserState state = newLineState;
46      int charsRead = -1;
47      do {
48        charsRead = reader.read(buf, 0, buf.length);
49        if (state == null) {
50          for (int i = 0; i < charsRead; i++) {
51            if (buf[i] == '\n') {
52              state = newLineState;
53              break;
54            }
55          }
56        }
57        if (state != null) {
58          for (int i = 0; i < charsRead; i++) {
59            state = state.consume(buf[i]);
60            //
61            // didn't match a production, skip to a new line
62            //
63            if (state == null) {
64              for (; i < charsRead; i++) {
65                if (buf[i] == '\n') {
66                  state = newLineState;
67                  break;
68                }
69              }
70            }
71          }
72        }
73      } while (charsRead >= 0);
74    }
75  }