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.IniBuilder; |
19 | |
import org.ini4j.spi.IniFormatter; |
20 | |
import org.ini4j.spi.IniHandler; |
21 | |
import org.ini4j.spi.IniParser; |
22 | |
|
23 | |
import java.io.File; |
24 | |
import java.io.FileNotFoundException; |
25 | |
import java.io.FileOutputStream; |
26 | |
import java.io.IOException; |
27 | |
import java.io.InputStream; |
28 | |
import java.io.InputStreamReader; |
29 | |
import java.io.OutputStream; |
30 | |
import java.io.OutputStreamWriter; |
31 | |
import java.io.Reader; |
32 | |
import java.io.Writer; |
33 | |
|
34 | |
import java.net.URL; |
35 | |
|
36 | |
public class Ini extends BasicProfile implements Persistable, Configurable |
37 | |
{ |
38 | |
private static final long serialVersionUID = -6029486578113700585L; |
39 | |
private Config _config; |
40 | |
private File _file; |
41 | |
|
42 | |
public Ini() |
43 | 109 | { |
44 | 109 | _config = Config.getGlobal(); |
45 | 109 | } |
46 | |
|
47 | |
public Ini(Reader input) throws IOException, InvalidFileFormatException |
48 | |
{ |
49 | 9 | this(); |
50 | 9 | load(input); |
51 | 9 | } |
52 | |
|
53 | |
public Ini(InputStream input) throws IOException, InvalidFileFormatException |
54 | |
{ |
55 | 8 | this(); |
56 | 8 | load(input); |
57 | 8 | } |
58 | |
|
59 | |
public Ini(URL input) throws IOException, InvalidFileFormatException |
60 | |
{ |
61 | 5 | this(); |
62 | 5 | load(input); |
63 | 5 | } |
64 | |
|
65 | |
public Ini(File input) throws IOException, InvalidFileFormatException |
66 | |
{ |
67 | 4 | this(); |
68 | 4 | _file = input; |
69 | 4 | load(); |
70 | 4 | } |
71 | |
|
72 | |
@Override public Config getConfig() |
73 | |
{ |
74 | 5957 | return _config; |
75 | |
} |
76 | |
|
77 | |
@Override public void setConfig(Config value) |
78 | |
{ |
79 | 60 | _config = value; |
80 | 60 | } |
81 | |
|
82 | |
@Override public File getFile() |
83 | |
{ |
84 | 1 | return _file; |
85 | |
} |
86 | |
|
87 | |
@Override public void setFile(File value) |
88 | |
{ |
89 | 6 | _file = value; |
90 | 6 | } |
91 | |
|
92 | |
@Override public void load() throws IOException, InvalidFileFormatException |
93 | |
{ |
94 | 10 | if (_file == null) |
95 | |
{ |
96 | 1 | throw new FileNotFoundException(); |
97 | |
} |
98 | |
|
99 | 9 | load(_file); |
100 | 9 | } |
101 | |
|
102 | |
@Override public void load(InputStream input) throws IOException, InvalidFileFormatException |
103 | |
{ |
104 | 16 | load(new InputStreamReader(input, getConfig().getFileEncoding())); |
105 | 15 | } |
106 | |
|
107 | |
@Override public void load(Reader input) throws IOException, InvalidFileFormatException |
108 | |
{ |
109 | 56 | IniParser.newInstance(getConfig()).parse(input, newBuilder()); |
110 | 52 | } |
111 | |
|
112 | |
@Override public void load(File input) throws IOException, InvalidFileFormatException |
113 | |
{ |
114 | 8 | load(input.toURI().toURL()); |
115 | 8 | } |
116 | |
|
117 | |
@Override public void load(URL input) throws IOException, InvalidFileFormatException |
118 | |
{ |
119 | 16 | IniParser.newInstance(getConfig()).parse(input, newBuilder()); |
120 | 15 | } |
121 | |
|
122 | |
@Override public void store() throws IOException |
123 | |
{ |
124 | 3 | if (_file == null) |
125 | |
{ |
126 | 1 | throw new FileNotFoundException(); |
127 | |
} |
128 | |
|
129 | 2 | store(_file); |
130 | 2 | } |
131 | |
|
132 | |
@Override public void store(OutputStream output) throws IOException |
133 | |
{ |
134 | 6 | store(new OutputStreamWriter(output, getConfig().getFileEncoding())); |
135 | 6 | } |
136 | |
|
137 | |
@Override public void store(Writer output) throws IOException |
138 | |
{ |
139 | 16 | store(IniFormatter.newInstance(output, getConfig())); |
140 | 16 | } |
141 | |
|
142 | |
@Override public void store(File output) throws IOException |
143 | |
{ |
144 | 3 | OutputStream stream = new FileOutputStream(output); |
145 | |
|
146 | 3 | store(stream); |
147 | 3 | stream.close(); |
148 | 3 | } |
149 | |
|
150 | |
protected IniHandler newBuilder() |
151 | |
{ |
152 | 72 | return IniBuilder.newInstance(this); |
153 | |
} |
154 | |
|
155 | |
@Override protected void store(IniHandler formatter, Profile.Section section) |
156 | |
{ |
157 | 50 | if (getConfig().isEmptySection() || (section.size() != 0)) |
158 | |
{ |
159 | 50 | super.store(formatter, section); |
160 | |
} |
161 | 50 | } |
162 | |
|
163 | |
@Override protected void store(IniHandler formatter, Profile.Section section, String option, int index) |
164 | |
{ |
165 | 268 | if (getConfig().isMultiOption() || (index == (section.length(option) - 1))) |
166 | |
{ |
167 | 265 | super.store(formatter, section, option, index); |
168 | |
} |
169 | 268 | } |
170 | |
|
171 | |
@Override boolean isTreeMode() |
172 | |
{ |
173 | 413 | return getConfig().isTree(); |
174 | |
} |
175 | |
|
176 | |
@Override char getPathSeparator() |
177 | |
{ |
178 | 1299 | return getConfig().getPathSeparator(); |
179 | |
} |
180 | |
|
181 | |
@Override boolean isPropertyFirstUpper() |
182 | |
{ |
183 | 853 | return getConfig().isPropertyFirstUpper(); |
184 | |
} |
185 | |
} |