001 /*
002 * Copyright 2005,2009 Ivan SZKIBA
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.ini4j;
017
018 import org.ini4j.spi.EscapeTool;
019
020 import java.io.IOException;
021 import java.io.InputStream;
022 import java.io.Reader;
023
024 import java.net.URL;
025
026 import java.util.Locale;
027
028 public abstract class AbstractParser
029 {
030 private final String _comments;
031 private Config _config = Config.getGlobal();
032 private final String _operators;
033
034 protected AbstractParser(String operators, String comments)
035 {
036 _operators = operators;
037 _comments = comments;
038 }
039
040 public void setConfig(Config value)
041 {
042 _config = value;
043 }
044
045 protected Config getConfig()
046 {
047 return _config;
048 }
049
050 protected int indexOfOperator(String line)
051 {
052 int idx = -1;
053
054 for (char c : _operators.toCharArray())
055 {
056 int index = line.indexOf(c);
057
058 if ((index >= 0) && ((idx == -1) || (index < idx)))
059 {
060 idx = index;
061 }
062 }
063
064 return idx;
065 }
066
067 protected void parseError(String line, int lineNumber) throws InvalidIniFormatException
068 {
069 throw new InvalidIniFormatException("parse error (at line: " + lineNumber + "): " + line);
070 }
071
072 protected void parseOptionLine(String line, OptionHandler handler, int lineNumber) throws InvalidIniFormatException
073 {
074 int idx = indexOfOperator(line);
075 String name = null;
076 String value = null;
077
078 if (idx < 0)
079 {
080 if (getConfig().isEmptyOption())
081 {
082 name = line;
083 }
084 else
085 {
086 parseError(line, lineNumber);
087 }
088 }
089 else
090 {
091 name = unescape(line.substring(0, idx)).trim();
092 value = unescape(line.substring(idx + 1)).trim();
093 }
094
095 if (name.length() == 0)
096 {
097 parseError(line, lineNumber);
098 }
099
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 }