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.borland;
21  
22  import java.io.IOException;
23  import java.io.Reader;
24  import java.util.Vector;
25  
26  import com.github.maven_nar.cpptasks.parser.AbstractParser;
27  import com.github.maven_nar.cpptasks.parser.AbstractParserState;
28  import com.github.maven_nar.cpptasks.parser.LetterState;
29  import com.github.maven_nar.cpptasks.parser.WhitespaceOrLetterState;
30  
31  /**
32   * A parser that paths from a borland cfg file
33   *
34   * @author Curt Arnold
35   */
36  public final class BorlandCfgParser extends AbstractParser {
37    private final AbstractParserState newLineState;
38    private final Vector<String> path = new Vector<>();
39  
40    /**
41       *
42       *
43       */
44    public BorlandCfgParser(final char switchChar) {
45      //
46      // a quoted path (-I"some path")
47      // doesn't end till a close quote and will be abandoned
48      // if a new line is encountered first
49      //
50      final AbstractParserState quote = new CfgFilenameState(this, new char[] {
51        '"'
52      });
53      //
54      // an unquoted path (-Ic:\borland\include)
55      // ends at the first space or new line
56      final AbstractParserState unquote = new CfgFilenameState(this, new char[] {
57          ' ', '\n', '\r'
58      });
59      final AbstractParserState quoteBranch = new QuoteBranchState(this, quote, unquote);
60      final AbstractParserState toNextSwitch = new ConsumeToSpaceOrNewLine(this);
61      final AbstractParserState switchState = new LetterState(this, switchChar, quoteBranch, toNextSwitch);
62      this.newLineState = new WhitespaceOrLetterState(this, '-', switchState);
63    }
64  
65    @Override
66    public void addFilename(final String include) {
67      this.path.addElement(include);
68    }
69  
70    @Override
71    public AbstractParserState getNewLineState() {
72      return this.newLineState;
73    }
74  
75    public String[] parsePath(final Reader reader) throws IOException {
76      this.path.setSize(0);
77      super.parse(reader);
78      final String[] retval = new String[this.path.size()];
79      this.path.copyInto(retval);
80      return retval;
81    }
82  }