| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | 96 | super(OPERATORS, COMMENTS); |
| 39 | 96 | } |
| 40 | |
|
| 41 | |
public static IniParser newInstance() |
| 42 | |
{ |
| 43 | 85 | return ServiceFinder.findService(IniParser.class); |
| 44 | |
} |
| 45 | |
|
| 46 | |
public static IniParser newInstance(Config config) |
| 47 | |
{ |
| 48 | 84 | IniParser instance = newInstance(); |
| 49 | |
|
| 50 | 84 | instance.setConfig(config); |
| 51 | |
|
| 52 | 84 | return instance; |
| 53 | |
} |
| 54 | |
|
| 55 | |
public void parse(InputStream input, IniHandler handler) throws IOException, InvalidFileFormatException |
| 56 | |
{ |
| 57 | 9 | parse(newIniSource(input, handler), handler); |
| 58 | 0 | } |
| 59 | |
|
| 60 | |
public void parse(Reader input, IniHandler handler) throws IOException, InvalidFileFormatException |
| 61 | |
{ |
| 62 | 75 | parse(newIniSource(input, handler), handler); |
| 63 | 71 | } |
| 64 | |
|
| 65 | |
public void parse(URL input, IniHandler handler) throws IOException, InvalidFileFormatException |
| 66 | |
{ |
| 67 | 17 | parse(newIniSource(input, handler), handler); |
| 68 | 16 | } |
| 69 | |
|
| 70 | |
private void parse(IniSource source, IniHandler handler) throws IOException, InvalidFileFormatException |
| 71 | |
{ |
| 72 | 101 | handler.startIni(); |
| 73 | 101 | String sectionName = null; |
| 74 | |
|
| 75 | 3307 | for (String line = source.readLine(); line != null; line = source.readLine()) |
| 76 | |
{ |
| 77 | 3220 | if (line.charAt(0) == SECTION_BEGIN) |
| 78 | |
{ |
| 79 | 569 | if (sectionName != null) |
| 80 | |
{ |
| 81 | 474 | handler.endSection(); |
| 82 | |
} |
| 83 | |
|
| 84 | 569 | sectionName = parseSectionLine(line, source, handler); |
| 85 | |
} |
| 86 | |
else |
| 87 | |
{ |
| 88 | 2651 | if (sectionName == null) |
| 89 | |
{ |
| 90 | 4 | if (getConfig().isGlobalSection()) |
| 91 | |
{ |
| 92 | 1 | sectionName = getConfig().getGlobalSectionName(); |
| 93 | 1 | handler.startSection(sectionName); |
| 94 | |
} |
| 95 | |
else |
| 96 | |
{ |
| 97 | 3 | parseError(line, source.getLineNumber()); |
| 98 | |
} |
| 99 | |
} |
| 100 | |
|
| 101 | 2648 | parseOptionLine(line, handler, source.getLineNumber()); |
| 102 | |
} |
| 103 | |
} |
| 104 | |
|
| 105 | 87 | if (sectionName != null) |
| 106 | |
{ |
| 107 | 85 | handler.endSection(); |
| 108 | |
} |
| 109 | |
|
| 110 | 87 | handler.endIni(); |
| 111 | 87 | } |
| 112 | |
|
| 113 | |
private String parseSectionLine(String line, IniSource source, IniHandler handler) throws InvalidFileFormatException |
| 114 | |
{ |
| 115 | |
String sectionName; |
| 116 | |
|
| 117 | 569 | if (line.charAt(line.length() - 1) != SECTION_END) |
| 118 | |
{ |
| 119 | 5 | parseError(line, source.getLineNumber()); |
| 120 | |
} |
| 121 | |
|
| 122 | 564 | sectionName = unescapeFilter(line.substring(1, line.length() - 1).trim()); |
| 123 | 564 | if ((sectionName.length() == 0) && !getConfig().isUnnamedSection()) |
| 124 | |
{ |
| 125 | 2 | parseError(line, source.getLineNumber()); |
| 126 | |
} |
| 127 | |
|
| 128 | 562 | if (getConfig().isLowerCaseSection()) |
| 129 | |
{ |
| 130 | 106 | sectionName = sectionName.toLowerCase(Locale.getDefault()); |
| 131 | |
} |
| 132 | |
|
| 133 | 562 | handler.startSection(sectionName); |
| 134 | |
|
| 135 | 562 | return sectionName; |
| 136 | |
} |
| 137 | |
} |