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.EscapeTool;
019    
020    import java.io.IOException;
021    import java.io.InputStream;
022    import java.io.OutputStream;
023    import java.io.OutputStreamWriter;
024    import java.io.Reader;
025    import java.io.Writer;
026    
027    import java.net.URL;
028    
029    public class Options extends OptionMapImpl
030    {
031        private static final char OPERATOR = '=';
032        private static final String NEWLINE = "\n";
033        private Config _config;
034    
035        public Options()
036        {
037            _config = Config.getGlobal().clone();
038            _config.setEmptyOption(true);
039        }
040    
041        public Options(Reader input) throws IOException, InvalidIniFormatException
042        {
043            this();
044            load(input);
045        }
046    
047        public Options(InputStream input) throws IOException, InvalidIniFormatException
048        {
049            this();
050            load(input);
051        }
052    
053        public Options(URL input) throws IOException, InvalidIniFormatException
054        {
055            this();
056            load(input);
057        }
058    
059        public void setConfig(Config value)
060        {
061            _config = value;
062        }
063    
064        public void load(InputStream input) throws IOException, InvalidIniFormatException
065        {
066            OptionParser.newInstance(getConfig()).parse(input, new Builder());
067        }
068    
069        public void load(Reader input) throws IOException, InvalidIniFormatException
070        {
071            OptionParser.newInstance(getConfig()).parse(input, new Builder());
072        }
073    
074        public void load(URL input) throws IOException, InvalidIniFormatException
075        {
076            OptionParser.newInstance(getConfig()).parse(input, new Builder());
077        }
078    
079        public void store(OutputStream output) throws IOException
080        {
081            format(new OutputStreamWriter(output));
082        }
083    
084        public void store(Writer output) throws IOException
085        {
086            format(output);
087        }
088    
089        protected Config getConfig()
090        {
091            return _config;
092        }
093    
094        protected String escape(String input)
095        {
096            return getConfig().isEscape() ? EscapeTool.getInstance().escape(input) : input;
097        }
098    
099        protected void format(Writer output) throws IOException
100        {
101            for (String name : keySet())
102            {
103                int n = getConfig().isMultiOption() ? length(name) : 1;
104    
105                for (int i = 0; i < n; i++)
106                {
107                    String value = get(name, i);
108    
109                    if ((value != null) || getConfig().isEmptyOption())
110                    {
111                        output.append(escape(name));
112                        output.append(OPERATOR);
113                        if (value != null)
114                        {
115                            output.append(escape(value));
116                        }
117    
118                        output.append(NEWLINE);
119                    }
120                }
121            }
122    
123            output.flush();
124        }
125    
126        private class Builder implements OptionHandler
127        {
128            @Override public void handleOption(String name, String value)
129            {
130                if (getConfig().isMultiOption())
131                {
132                    add(name, value);
133                }
134                else
135                {
136                    put(name, value);
137                }
138            }
139        }
140    }