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.IniBuilder;
019    import org.ini4j.spi.IniFormatter;
020    import org.ini4j.spi.IniHandler;
021    import org.ini4j.spi.IniParser;
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 Ini extends BasicProfile implements Persistable, Configurable
037    {
038        private static final long serialVersionUID = -6029486578113700585L;
039        private Config _config;
040        private File _file;
041    
042        public Ini()
043        {
044            _config = Config.getGlobal();
045        }
046    
047        public Ini(Reader input) throws IOException, InvalidFileFormatException
048        {
049            this();
050            load(input);
051        }
052    
053        public Ini(InputStream input) throws IOException, InvalidFileFormatException
054        {
055            this();
056            load(input);
057        }
058    
059        public Ini(URL input) throws IOException, InvalidFileFormatException
060        {
061            this();
062            load(input);
063        }
064    
065        public Ini(File input) throws IOException, InvalidFileFormatException
066        {
067            this();
068            _file = input;
069            load();
070        }
071    
072        @Override public Config getConfig()
073        {
074            return _config;
075        }
076    
077        @Override public void setConfig(Config value)
078        {
079            _config = value;
080        }
081    
082        @Override public File getFile()
083        {
084            return _file;
085        }
086    
087        @Override public void setFile(File value)
088        {
089            _file = value;
090        }
091    
092        @Override public void load() throws IOException, InvalidFileFormatException
093        {
094            if (_file == null)
095            {
096                throw new FileNotFoundException();
097            }
098    
099            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    }