| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.ini4j; |
| 17 | |
|
| 18 | |
import org.ini4j.spi.ServiceFinder; |
| 19 | |
|
| 20 | |
import org.xml.sax.Attributes; |
| 21 | |
import org.xml.sax.InputSource; |
| 22 | |
import org.xml.sax.SAXException; |
| 23 | |
import org.xml.sax.helpers.DefaultHandler; |
| 24 | |
|
| 25 | |
import java.io.IOException; |
| 26 | |
import java.io.InputStream; |
| 27 | |
import java.io.InputStreamReader; |
| 28 | |
import java.io.Reader; |
| 29 | |
|
| 30 | |
import java.net.URL; |
| 31 | |
|
| 32 | |
import java.util.Locale; |
| 33 | |
|
| 34 | |
import javax.xml.parsers.SAXParserFactory; |
| 35 | |
|
| 36 | |
public class IniParser extends AbstractParser |
| 37 | |
{ |
| 38 | |
private static final String COMMENTS = ";#"; |
| 39 | |
private static final String OPERATORS = ":="; |
| 40 | |
|
| 41 | |
public static final char SECTION_BEGIN = '['; |
| 42 | |
public static final char SECTION_END = ']'; |
| 43 | |
|
| 44 | |
public IniParser() |
| 45 | |
{ |
| 46 | 75 | super(OPERATORS, COMMENTS); |
| 47 | 75 | } |
| 48 | |
|
| 49 | |
public static IniParser newInstance() |
| 50 | |
{ |
| 51 | 69 | return ServiceFinder.findService(IniParser.class); |
| 52 | |
} |
| 53 | |
|
| 54 | |
public static IniParser newInstance(Config config) |
| 55 | |
{ |
| 56 | 66 | IniParser instance = newInstance(); |
| 57 | |
|
| 58 | 66 | instance.setConfig(config); |
| 59 | |
|
| 60 | 66 | return instance; |
| 61 | |
} |
| 62 | |
|
| 63 | |
public void parse(InputStream input, IniHandler handler) throws IOException, InvalidIniFormatException |
| 64 | |
{ |
| 65 | 37 | parse(newIniSource(input), handler); |
| 66 | 24 | } |
| 67 | |
|
| 68 | |
public void parse(Reader input, IniHandler handler) throws IOException, InvalidIniFormatException |
| 69 | |
{ |
| 70 | 26 | parse(newIniSource(input), handler); |
| 71 | 24 | } |
| 72 | |
|
| 73 | |
public void parse(URL input, IniHandler handler) throws IOException, InvalidIniFormatException |
| 74 | |
{ |
| 75 | 17 | parse(newIniSource(input), handler); |
| 76 | 15 | } |
| 77 | |
|
| 78 | |
public void parseXML(InputStream input, IniHandler handler) throws IOException, InvalidIniFormatException |
| 79 | |
{ |
| 80 | 8 | parseXML(new InputStreamReader(input), handler); |
| 81 | 2 | } |
| 82 | |
|
| 83 | |
public void parseXML(Reader input, final IniHandler handler) throws IOException, InvalidIniFormatException |
| 84 | |
{ |
| 85 | 11 | class XmlToIni extends DefaultHandler |
| 86 | |
{ |
| 87 | |
private static final String TAG_SECTION = "section"; |
| 88 | |
private static final String TAG_OPTION = "option"; |
| 89 | |
private static final String TAG_INI = "ini"; |
| 90 | |
private static final String ATTR_KEY = "key"; |
| 91 | |
private static final String ATTR_VALUE = "value"; |
| 92 | |
private static final String ATTR_VERSION = "version"; |
| 93 | |
private static final String CURRENT_VERSION = "1.0"; |
| 94 | |
|
| 95 | |
@Override public void startElement(String uri, String localName, String qname, Attributes attrs) throws SAXException |
| 96 | |
{ |
| 97 | 249 | String key = attrs.getValue(ATTR_KEY); |
| 98 | |
|
| 99 | 249 | if (qname.equals(TAG_INI)) |
| 100 | |
{ |
| 101 | 10 | String ver = attrs.getValue(ATTR_VERSION); |
| 102 | |
|
| 103 | 10 | if ((ver == null) || !ver.equals(CURRENT_VERSION)) |
| 104 | |
{ |
| 105 | 2 | throw new SAXException("Missing or invalid 'version' attribute"); |
| 106 | |
} |
| 107 | |
|
| 108 | 8 | handler.startIni(); |
| 109 | 8 | } |
| 110 | |
else |
| 111 | |
{ |
| 112 | 239 | if (key == null) |
| 113 | |
{ |
| 114 | 2 | throw new SAXException("missing '" + ATTR_KEY + "' attribute"); |
| 115 | |
} |
| 116 | |
|
| 117 | 237 | if (qname.equals(TAG_SECTION)) |
| 118 | |
{ |
| 119 | 39 | handler.startSection(key); |
| 120 | |
} |
| 121 | 198 | else if (qname.equals(TAG_OPTION)) |
| 122 | |
{ |
| 123 | 197 | handler.handleOption(key, attrs.getValue(ATTR_VALUE)); |
| 124 | |
} |
| 125 | |
else |
| 126 | |
{ |
| 127 | 1 | throw new SAXException("Invalid element: " + qname); |
| 128 | |
} |
| 129 | |
} |
| 130 | 244 | } |
| 131 | |
|
| 132 | |
@Override public void endElement(String uri, String localName, String qname) throws SAXException |
| 133 | |
{ |
| 134 | 240 | if (qname.equals(TAG_SECTION)) |
| 135 | |
{ |
| 136 | 38 | handler.endSection(); |
| 137 | |
} |
| 138 | 202 | else if (qname.equals(TAG_INI)) |
| 139 | |
{ |
| 140 | 5 | handler.endIni(); |
| 141 | |
} |
| 142 | 240 | } |
| 143 | |
} |
| 144 | |
|
| 145 | 11 | XmlToIni xmlToini = new XmlToIni(); |
| 146 | |
|
| 147 | |
try |
| 148 | |
{ |
| 149 | 11 | SAXParserFactory.newInstance().newSAXParser().parse(new InputSource(input), xmlToini); |
| 150 | |
} |
| 151 | 6 | catch (Exception x) |
| 152 | |
{ |
| 153 | 6 | throw new InvalidIniFormatException(x); |
| 154 | 5 | } |
| 155 | 5 | } |
| 156 | |
|
| 157 | |
public void parseXML(URL input, IniHandler handler) throws IOException, InvalidIniFormatException |
| 158 | |
{ |
| 159 | 1 | parseXML(input.openStream(), handler); |
| 160 | 1 | } |
| 161 | |
|
| 162 | |
private String parseSectionLine(String line, IniSource source, IniHandler handler) throws InvalidIniFormatException |
| 163 | |
{ |
| 164 | |
String sectionName; |
| 165 | |
|
| 166 | 385 | if (line.charAt(line.length() - 1) != SECTION_END) |
| 167 | |
{ |
| 168 | 6 | parseError(line, source.getLineNumber()); |
| 169 | |
} |
| 170 | |
|
| 171 | 379 | sectionName = unescape(line.substring(1, line.length() - 1).trim()); |
| 172 | 379 | if ((sectionName.length() == 0) && !getConfig().isUnnamedSection()) |
| 173 | |
{ |
| 174 | 2 | parseError(line, source.getLineNumber()); |
| 175 | |
} |
| 176 | |
|
| 177 | 377 | if (getConfig().isLowerCaseSection()) |
| 178 | |
{ |
| 179 | 106 | sectionName = sectionName.toLowerCase(Locale.getDefault()); |
| 180 | |
} |
| 181 | |
|
| 182 | 377 | handler.startSection(sectionName); |
| 183 | |
|
| 184 | 377 | return sectionName; |
| 185 | |
} |
| 186 | |
|
| 187 | |
private void parse(IniSource source, IniHandler handler) throws IOException, InvalidIniFormatException |
| 188 | |
{ |
| 189 | 79 | handler.startIni(); |
| 190 | 79 | String sectionName = null; |
| 191 | |
|
| 192 | 2243 | for (String line = source.readLine(); line != null; line = source.readLine()) |
| 193 | |
{ |
| 194 | |
|
| 195 | 2179 | if (line.charAt(0) == SECTION_BEGIN) |
| 196 | |
{ |
| 197 | 385 | if (sectionName != null) |
| 198 | |
{ |
| 199 | 315 | handler.endSection(); |
| 200 | |
} |
| 201 | |
|
| 202 | 385 | sectionName = parseSectionLine(line, source, handler); |
| 203 | |
|
| 204 | |
} |
| 205 | |
else |
| 206 | |
{ |
| 207 | 1794 | if (sectionName == null) |
| 208 | |
{ |
| 209 | 4 | if (getConfig().isGlobalSection()) |
| 210 | |
{ |
| 211 | 2 | sectionName = getConfig().getGlobalSectionName(); |
| 212 | 2 | handler.startSection(sectionName); |
| 213 | |
} |
| 214 | |
else |
| 215 | |
{ |
| 216 | 2 | parseError(line, source.getLineNumber()); |
| 217 | |
} |
| 218 | |
} |
| 219 | |
|
| 220 | 1792 | parseOptionLine(line, handler, source.getLineNumber()); |
| 221 | |
|
| 222 | |
} |
| 223 | |
} |
| 224 | |
|
| 225 | 63 | if (sectionName != null) |
| 226 | |
{ |
| 227 | 59 | handler.endSection(); |
| 228 | |
} |
| 229 | |
|
| 230 | 63 | handler.endIni(); |
| 231 | 63 | } |
| 232 | |
} |