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;
17  
18  import org.ini4j.spi.EscapeTool;
19  
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.Reader;
23  
24  import java.net.URL;
25  
26  import java.util.Locale;
27  
28  public abstract class AbstractParser
29  {
30      private final String _comments;
31      private Config _config = Config.getGlobal();
32      private final String _operators;
33  
34      protected AbstractParser(String operators, String comments)
35      {
36          _operators = operators;
37          _comments = comments;
38      }
39  
40      public void setConfig(Config value)
41      {
42          _config = value;
43      }
44  
45      protected Config getConfig()
46      {
47          return _config;
48      }
49  
50      protected int indexOfOperator(String line)
51      {
52          int idx = -1;
53  
54          for (char c : _operators.toCharArray())
55          {
56              int index = line.indexOf(c);
57  
58              if ((index >= 0) && ((idx == -1) || (index < idx)))
59              {
60                  idx = index;
61              }
62          }
63  
64          return idx;
65      }
66  
67      protected void parseError(String line, int lineNumber) throws InvalidIniFormatException
68      {
69          throw new InvalidIniFormatException("parse error (at line: " + lineNumber + "): " + line);
70      }
71  
72      protected void parseOptionLine(String line, OptionHandler handler, int lineNumber) throws InvalidIniFormatException
73      {
74          int idx = indexOfOperator(line);
75          String name = null;
76          String value = null;
77  
78          if (idx < 0)
79          {
80              if (getConfig().isEmptyOption())
81              {
82                  name = line;
83              }
84              else
85              {
86                  parseError(line, lineNumber);
87              }
88          }
89          else
90          {
91              name = unescape(line.substring(0, idx)).trim();
92              value = unescape(line.substring(idx + 1)).trim();
93          }
94  
95          if (name.length() == 0)
96          {
97              parseError(line, lineNumber);
98          }
99  
100         if (getConfig().isLowerCaseOption())
101         {
102             name = name.toLowerCase(Locale.getDefault());
103         }
104 
105         handler.handleOption(name, value);
106     }
107 
108     protected String unescape(String line)
109     {
110         return getConfig().isEscape() ? EscapeTool.getInstance().unescape(line) : line;
111     }
112 
113     IniSource newIniSource(InputStream input)
114     {
115         return new IniSource(input, getConfig().isInclude(), _comments);
116     }
117 
118     IniSource newIniSource(Reader input)
119     {
120         return new IniSource(input, getConfig().isInclude(), _comments);
121     }
122 
123     IniSource newIniSource(URL input) throws IOException
124     {
125         return new IniSource(input, getConfig().isInclude(), _comments);
126     }
127 }