1 package org.codehaus.mojo.natives.parser;
2
3 /*
4 *
5 * Copyright 2001-2004 The Ant-Contrib project
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 */
19
20 import java.io.IOException;
21 import java.io.Reader;
22
23 /**
24 * An abstract base class for simple parsers
25 *
26 * @author Curt Arnold
27 */
28 public abstract class AbstractParser
29 {
30
31 protected AbstractParser()
32 {
33 }
34
35 protected abstract void addFilename( String filename );
36
37 public abstract AbstractParserState getNewLineState();
38
39 protected void parse( Reader reader )
40 throws IOException
41 {
42 char[] buf = new char[4096];
43 AbstractParserState newLineState = getNewLineState();
44 AbstractParserState state = newLineState;
45 int charsRead = -1;
46
47 do
48 {
49 charsRead = reader.read( buf, 0, buf.length );
50 if ( state == null )
51 {
52 for ( int i = 0; i < charsRead; i++ )
53 {
54 if ( buf[i] == '\n' )
55 {
56 state = newLineState;
57 break;
58 }
59 }
60 }
61
62 if ( state != null )
63 {
64 for ( int i = 0; i < charsRead; i++ )
65 {
66 state = state.consume( buf[i] );
67 //
68 // didn't match a production, skip to a new line
69 //
70 if ( state == null )
71 {
72 for ( ; i < charsRead; i++ )
73 {
74 if ( buf[i] == '\n' )
75 {
76 state = newLineState;
77 break;
78 }
79 }
80 }
81 }
82 }
83
84 }
85 while ( charsRead >= 0 );
86 }
87 }