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.trolltech;
21  
22  import java.io.IOException;
23  import java.io.Reader;
24  
25  import com.github.maven_nar.cpptasks.parser.AbstractParser;
26  import com.github.maven_nar.cpptasks.parser.AbstractParserState;
27  import com.github.maven_nar.cpptasks.parser.LetterState;
28  import com.github.maven_nar.cpptasks.parser.WhitespaceOrLetterState;
29  
30  /**
31   * Scans a source file for Q_OBJECT.
32   *
33   * @author Curt Arnold
34   */
35  public final class MetaObjectParser extends AbstractParser {
36    /**
37     * Parser state that matches file T character.
38     */
39    private static final class FinalTState extends AbstractParserState {
40      /**
41       * Parser.
42       */
43      private final MetaObjectParser mocParser;
44  
45      /**
46       * Constructor.
47       * 
48       * @param parser
49       *          MetaObjectParser parser
50       */
51      public FinalTState(final MetaObjectParser parser) {
52        super(parser);
53        this.mocParser = parser;
54      }
55  
56      /**
57       * Consumes a character and returns the next state for the parser.
58       *
59       * @param ch
60       *          next character
61       * @return the configured nextState if ch is the expected character or the
62       *         configure noMatchState otherwise.
63       */
64      @Override
65      public AbstractParserState consume(final char ch) {
66        if (ch == 'T') {
67          this.mocParser.setQObject(true);
68          return null;
69        }
70        if (ch == '\n') {
71          getParser().getNewLineState();
72        }
73        return null;
74      }
75    }
76  
77    /**
78     * Determines if source file contains Q_OBJECT.
79     * 
80     * @param reader
81     *          Reader source reader
82     * @throws IOException
83     *           if unable to read source file
84     * @return boolean true if source contains Q_OBJECT
85     */
86    public static boolean hasQObject(final Reader reader) throws IOException {
87      final MetaObjectParser parser = new MetaObjectParser();
88      parser.parse(reader);
89      return parser.hasQObject;
90  
91    }
92  
93    /**
94     * Has Q_OBJECT been encountered.
95     */
96    private boolean hasQObject = false;
97  
98    /**
99     * Parser state for start of new line.
100    */
101   private final AbstractParserState newLineState;
102 
103   /**
104    * Constructor.
105    *
106    */
107   private MetaObjectParser() {
108     //
109     // search for Q_OBJECT
110     //
111     final AbstractParserState t = new FinalTState(this);
112     final AbstractParserState c = new LetterState(this, 'C', t, null);
113     final AbstractParserState e = new LetterState(this, 'E', c, null);
114     final AbstractParserState j = new LetterState(this, 'J', e, null);
115     final AbstractParserState b = new LetterState(this, 'B', j, null);
116     final AbstractParserState o = new LetterState(this, 'O', b, null);
117     final AbstractParserState underline = new LetterState(this, '_', o, null);
118     this.newLineState = new WhitespaceOrLetterState(this, 'Q', underline);
119   }
120 
121   /**
122    * Adds a filename to the list of included files.
123    *
124    * @param filename
125    *          filename to be added
126    */
127   @Override
128   protected void addFilename(final String filename) {
129 
130   }
131 
132   /**
133    * Gets new line state.
134    * 
135    * @return AbstractParserState new line state.
136    */
137   @Override
138   public AbstractParserState getNewLineState() {
139     return this.newLineState;
140   }
141 
142   /**
143    * Parse input file.
144    * 
145    * @param reader
146    *          Reader source file
147    * @throws IOException
148    *           if error reading source file
149    */
150   @Override
151   public void parse(final Reader reader) throws IOException {
152     this.hasQObject = false;
153     super.parse(reader);
154   }
155 
156   /**
157    * Called FinalTState to set that Q_OBJECT was found.
158    * 
159    * @param value
160    *          boolean new value for hasQObject
161    */
162   public void setQObject(final boolean value) {
163     this.hasQObject = value;
164   }
165 }