View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.ini4j.spi;
17  
18  import org.ini4j.Config;
19  import org.ini4j.InvalidFileFormatException;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.Reader;
24  
25  import java.net.URL;
26  
27  public class OptionsParser extends AbstractParser
28  {
29      private static final String COMMENTS = "!#";
30      private static final String OPERATORS = ":=";
31  
32      public OptionsParser()
33      {
34          super(OPERATORS, COMMENTS);
35      }
36  
37      public static OptionsParser newInstance()
38      {
39          return ServiceFinder.findService(OptionsParser.class);
40      }
41  
42      public static OptionsParser newInstance(Config config)
43      {
44          OptionsParser instance = newInstance();
45  
46          instance.setConfig(config);
47  
48          return instance;
49      }
50  
51      public void parse(InputStream input, OptionsHandler handler) throws IOException, InvalidFileFormatException
52      {
53          parse(newIniSource(input, handler), handler);
54      }
55  
56      public void parse(Reader input, OptionsHandler handler) throws IOException, InvalidFileFormatException
57      {
58          parse(newIniSource(input, handler), handler);
59      }
60  
61      public void parse(URL input, OptionsHandler handler) throws IOException, InvalidFileFormatException
62      {
63          parse(newIniSource(input, handler), handler);
64      }
65  
66      private void parse(IniSource source, OptionsHandler handler) throws IOException, InvalidFileFormatException
67      {
68          handler.startOptions();
69          for (String line = source.readLine(); line != null; line = source.readLine())
70          {
71              parseOptionLine(line, handler, source.getLineNumber());
72          }
73  
74          handler.endOptions();
75      }
76  }