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.OptionsBuilder;
019 import org.ini4j.spi.OptionsFormatter;
020 import org.ini4j.spi.OptionsHandler;
021 import org.ini4j.spi.OptionsParser;
022
023 import java.io.File;
024 import java.io.FileNotFoundException;
025 import java.io.FileOutputStream;
026 import java.io.IOException;
027 import java.io.InputStream;
028 import java.io.InputStreamReader;
029 import java.io.OutputStream;
030 import java.io.OutputStreamWriter;
031 import java.io.Reader;
032 import java.io.Writer;
033
034 import java.net.URL;
035
036 public class Options extends BasicOptionMap implements Persistable, Configurable
037 {
038 private static final long serialVersionUID = -1119753444859181822L;
039 private String _comment;
040 private Config _config;
041 private File _file;
042
043 public Options()
044 {
045 _config = Config.getGlobal().clone();
046 _config.setEmptyOption(true);
047 }
048
049 public Options(Reader input) throws IOException, InvalidFileFormatException
050 {
051 this();
052 load(input);
053 }
054
055 public Options(InputStream input) throws IOException, InvalidFileFormatException
056 {
057 this();
058 load(input);
059 }
060
061 public Options(URL input) throws IOException, InvalidFileFormatException
062 {
063 this();
064 load(input);
065 }
066
067 public Options(File input) throws IOException, InvalidFileFormatException
068 {
069 this();
070 _file = input;
071 load();
072 }
073
074 public String getComment()
075 {
076 return _comment;
077 }
078
079 public void setComment(String value)
080 {
081 _comment = value;
082 }
083
084 @Override public Config getConfig()
085 {
086 return _config;
087 }
088
089 @Override public void setConfig(Config value)
090 {
091 _config = value;
092 }
093
094 @Override public File getFile()
095 {
096 return _file;
097 }
098
099 @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 }