View Javadoc

1   /*
2    * Copyright 2005,2009 Ivan SZKIBA
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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      {
44          _config = Config.getGlobal();
45      }
46  
47      public Ini(Reader input) throws IOException, InvalidFileFormatException
48      {
49          this();
50          load(input);
51      }
52  
53      public Ini(InputStream input) throws IOException, InvalidFileFormatException
54      {
55          this();
56          load(input);
57      }
58  
59      public Ini(URL input) throws IOException, InvalidFileFormatException
60      {
61          this();
62          load(input);
63      }
64  
65      public Ini(File input) throws IOException, InvalidFileFormatException
66      {
67          this();
68          _file = input;
69          load();
70      }
71  
72      @Override public Config getConfig()
73      {
74          return _config;
75      }
76  
77      @Override public void setConfig(Config value)
78      {
79          _config = value;
80      }
81  
82      @Override public File getFile()
83      {
84          return _file;
85      }
86  
87      @Override public void setFile(File value)
88      {
89          _file = value;
90      }
91  
92      @Override public void load() throws IOException, InvalidFileFormatException
93      {
94          if (_file == null)
95          {
96              throw new FileNotFoundException();
97          }
98  
99          load(_file);
100     }
101 
102     @Override public void load(InputStream input) throws IOException, InvalidFileFormatException
103     {
104         load(new InputStreamReader(input, getConfig().getFileEncoding()));
105     }
106 
107     @Override public void load(Reader input) throws IOException, InvalidFileFormatException
108     {
109         IniParser.newInstance(getConfig()).parse(input, newBuilder());
110     }
111 
112     @Override public void load(File input) throws IOException, InvalidFileFormatException
113     {
114         load(input.toURI().toURL());
115     }
116 
117     @Override public void load(URL input) throws IOException, InvalidFileFormatException
118     {
119         IniParser.newInstance(getConfig()).parse(input, newBuilder());
120     }
121 
122     @Override public void store() throws IOException
123     {
124         if (_file == null)
125         {
126             throw new FileNotFoundException();
127         }
128 
129         store(_file);
130     }
131 
132     @Override public void store(OutputStream output) throws IOException
133     {
134         store(new OutputStreamWriter(output, getConfig().getFileEncoding()));
135     }
136 
137     @Override public void store(Writer output) throws IOException
138     {
139         store(IniFormatter.newInstance(output, getConfig()));
140     }
141 
142     @Override public void store(File output) throws IOException
143     {
144         OutputStream stream = new FileOutputStream(output);
145 
146         store(stream);
147         stream.close();
148     }
149 
150     protected IniHandler newBuilder()
151     {
152         return IniBuilder.newInstance(this);
153     }
154 
155     @Override protected void store(IniHandler formatter, Profile.Section section)
156     {
157         if (getConfig().isEmptySection() || (section.size() != 0))
158         {
159             super.store(formatter, section);
160         }
161     }
162 
163     @Override protected void store(IniHandler formatter, Profile.Section section, String option, int index)
164     {
165         if (getConfig().isMultiOption() || (index == (section.length(option) - 1)))
166         {
167             super.store(formatter, section, option, index);
168         }
169     }
170 
171     @Override boolean isTreeMode()
172     {
173         return getConfig().isTree();
174     }
175 
176     @Override char getPathSeparator()
177     {
178         return getConfig().getPathSeparator();
179     }
180 
181     @Override boolean isPropertyFirstUpper()
182     {
183         return getConfig().isPropertyFirstUpper();
184     }
185 }