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  import java.util.Locale;
28  
29  public class IniParser extends AbstractParser
30  {
31      private static final String COMMENTS = ";#";
32      private static final String OPERATORS = ":=";
33      static final char SECTION_BEGIN = '[';
34      static final char SECTION_END = ']';
35  
36      public IniParser()
37      {
38          super(OPERATORS, COMMENTS);
39      }
40  
41      public static IniParser newInstance()
42      {
43          return ServiceFinder.findService(IniParser.class);
44      }
45  
46      public static IniParser newInstance(Config config)
47      {
48          IniParser instance = newInstance();
49  
50          instance.setConfig(config);
51  
52          return instance;
53      }
54  
55      public void parse(InputStream input, IniHandler handler) throws IOException, InvalidFileFormatException
56      {
57          parse(newIniSource(input, handler), handler);
58      }
59  
60      public void parse(Reader input, IniHandler handler) throws IOException, InvalidFileFormatException
61      {
62          parse(newIniSource(input, handler), handler);
63      }
64  
65      public void parse(URL input, IniHandler handler) throws IOException, InvalidFileFormatException
66      {
67          parse(newIniSource(input, handler), handler);
68      }
69  
70      private void parse(IniSource source, IniHandler handler) throws IOException, InvalidFileFormatException
71      {
72          handler.startIni();
73          String sectionName = null;
74  
75          for (String line = source.readLine(); line != null; line = source.readLine())
76          {
77              if (line.charAt(0) == SECTION_BEGIN)
78              {
79                  if (sectionName != null)
80                  {
81                      handler.endSection();
82                  }
83  
84                  sectionName = parseSectionLine(line, source, handler);
85              }
86              else
87              {
88                  if (sectionName == null)
89                  {
90                      if (getConfig().isGlobalSection())
91                      {
92                          sectionName = getConfig().getGlobalSectionName();
93                          handler.startSection(sectionName);
94                      }
95                      else
96                      {
97                          parseError(line, source.getLineNumber());
98                      }
99                  }
100 
101                 parseOptionLine(line, handler, source.getLineNumber());
102             }
103         }
104 
105         if (sectionName != null)
106         {
107             handler.endSection();
108         }
109 
110         handler.endIni();
111     }
112 
113     private String parseSectionLine(String line, IniSource source, IniHandler handler) throws InvalidFileFormatException
114     {
115         String sectionName;
116 
117         if (line.charAt(line.length() - 1) != SECTION_END)
118         {
119             parseError(line, source.getLineNumber());
120         }
121 
122         sectionName = unescapeFilter(line.substring(1, line.length() - 1).trim());
123         if ((sectionName.length() == 0) && !getConfig().isUnnamedSection())
124         {
125             parseError(line, source.getLineNumber());
126         }
127 
128         if (getConfig().isLowerCaseSection())
129         {
130             sectionName = sectionName.toLowerCase(Locale.getDefault());
131         }
132 
133         handler.startSection(sectionName);
134 
135         return sectionName;
136     }
137 }