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.OptionsBuilder;
19 import org.ini4j.spi.OptionsFormatter;
20 import org.ini4j.spi.OptionsHandler;
21 import org.ini4j.spi.OptionsParser;
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 Options extends BasicOptionMap implements Persistable, Configurable
37 {
38 private static final long serialVersionUID = -1119753444859181822L;
39 private String _comment;
40 private Config _config;
41 private File _file;
42
43 public Options()
44 {
45 _config = Config.getGlobal().clone();
46 _config.setEmptyOption(true);
47 }
48
49 public Options(Reader input) throws IOException, InvalidFileFormatException
50 {
51 this();
52 load(input);
53 }
54
55 public Options(InputStream input) throws IOException, InvalidFileFormatException
56 {
57 this();
58 load(input);
59 }
60
61 public Options(URL input) throws IOException, InvalidFileFormatException
62 {
63 this();
64 load(input);
65 }
66
67 public Options(File input) throws IOException, InvalidFileFormatException
68 {
69 this();
70 _file = input;
71 load();
72 }
73
74 public String getComment()
75 {
76 return _comment;
77 }
78
79 public void setComment(String value)
80 {
81 _comment = value;
82 }
83
84 @Override public Config getConfig()
85 {
86 return _config;
87 }
88
89 @Override public void setConfig(Config value)
90 {
91 _config = value;
92 }
93
94 @Override public File getFile()
95 {
96 return _file;
97 }
98
99 @Override public void setFile(File value)
100 {
101 _file = value;
102 }
103
104 @Override public void load() throws IOException, InvalidFileFormatException
105 {
106 if (_file == null)
107 {
108 throw new FileNotFoundException();
109 }
110
111 load(_file);
112 }
113
114 @Override public void load(InputStream input) throws IOException, InvalidFileFormatException
115 {
116 load(new InputStreamReader(input, getConfig().getFileEncoding()));
117 }
118
119 @Override public void load(Reader input) throws IOException, InvalidFileFormatException
120 {
121 OptionsParser.newInstance(getConfig()).parse(input, newBuilder());
122 }
123
124 @Override public void load(URL input) throws IOException, InvalidFileFormatException
125 {
126 OptionsParser.newInstance(getConfig()).parse(input, newBuilder());
127 }
128
129 @Override public void load(File input) throws IOException, InvalidFileFormatException
130 {
131 load(input.toURI().toURL());
132 }
133
134 @Override public void store() throws IOException
135 {
136 if (_file == null)
137 {
138 throw new FileNotFoundException();
139 }
140
141 store(_file);
142 }
143
144 @Override public void store(OutputStream output) throws IOException
145 {
146 store(new OutputStreamWriter(output, getConfig().getFileEncoding()));
147 }
148
149 @Override public void store(Writer output) throws IOException
150 {
151 store(OptionsFormatter.newInstance(output, getConfig()));
152 }
153
154 @Override public void store(File output) throws IOException
155 {
156 OutputStream stream = new FileOutputStream(output);
157
158 store(stream);
159 stream.close();
160 }
161
162 protected OptionsHandler newBuilder()
163 {
164 return OptionsBuilder.newInstance(this);
165 }
166
167 protected void store(OptionsHandler formatter) throws IOException
168 {
169 formatter.startOptions();
170 storeComment(formatter, _comment);
171 for (String name : keySet())
172 {
173 storeComment(formatter, getComment(name));
174 int n = getConfig().isMultiOption() ? length(name) : 1;
175
176 for (int i = 0; i < n; i++)
177 {
178 String value = get(name, i);
179
180 formatter.handleOption(name, value);
181 }
182 }
183
184 formatter.endOptions();
185 }
186
187 @Override boolean isPropertyFirstUpper()
188 {
189 return getConfig().isPropertyFirstUpper();
190 }
191
192 private void storeComment(OptionsHandler formatter, String comment)
193 {
194 formatter.handleComment(comment);
195 }
196 }