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.spi;
017    
018    import org.ini4j.Config;
019    import org.ini4j.IniHandler;
020    import org.ini4j.IniParser;
021    
022    import java.io.OutputStream;
023    import java.io.OutputStreamWriter;
024    import java.io.PrintWriter;
025    import java.io.Writer;
026    
027    public class IniFormatter implements IniHandler
028    {
029        private static final char OPERATOR = '=';
030        private static final char SPACE = ' ';
031        private Config _config = Config.getGlobal();
032        private PrintWriter _output;
033    
034        public static IniFormatter newInstance(Writer out)
035        {
036            IniFormatter instance = newInstance();
037    
038            instance.setOutput(new PrintWriter(out));
039    
040            return instance;
041        }
042    
043        public static IniFormatter newInstance(OutputStream out)
044        {
045            return newInstance(new OutputStreamWriter(out));
046        }
047    
048        public static IniFormatter newInstance(Writer out, Config config)
049        {
050            IniFormatter instance = newInstance(out);
051    
052            instance.setConfig(config);
053    
054            return instance;
055        }
056    
057        public static IniFormatter newInstance(OutputStream out, Config config)
058        {
059            return newInstance(new OutputStreamWriter(out), config);
060        }
061    
062        public Config getConfig()
063        {
064            return _config;
065        }
066    
067        @Override public void endIni()
068        {
069            getOutput().flush();
070        }
071    
072        @Override public void endSection()
073        {
074            getOutput().println();
075        }
076    
077        @Override public void handleOption(String optionName, String optionValue)
078        {
079            if (getConfig().isStrictOperator())
080            {
081                if (getConfig().isEmptyOption() || (optionValue != null))
082                {
083                    getOutput().print(escape(optionName));
084                    getOutput().print(OPERATOR);
085                }
086    
087                if (optionValue != null)
088                {
089                    getOutput().print(escape(optionValue));
090                }
091    
092                if (getConfig().isEmptyOption() || (optionValue != null))
093                {
094                    getOutput().println();
095                }
096            }
097            else
098            {
099                String value = ((optionValue == null) && getConfig().isEmptyOption()) ? "" : optionValue;
100    
101                if (value != null)
102                {
103                    getOutput().print(escape(optionName));
104                    getOutput().print(SPACE);
105                    getOutput().print(OPERATOR);
106                    getOutput().print(SPACE);
107                    getOutput().println(escape(value));
108                }
109            }
110        }
111    
112        @Override public void startIni()
113        {
114            assert true;
115        }
116    
117        @Override public void startSection(String sectionName)
118        {
119            getOutput().print(IniParser.SECTION_BEGIN);
120            getOutput().print(escape(sectionName));
121            getOutput().println(IniParser.SECTION_END);
122        }
123    
124        protected static IniFormatter newInstance()
125        {
126            return ServiceFinder.findService(IniFormatter.class);
127        }
128    
129        protected void setConfig(Config value)
130        {
131            _config = value;
132        }
133    
134        protected PrintWriter getOutput()
135        {
136            return _output;
137        }
138    
139        protected void setOutput(PrintWriter value)
140        {
141            _output = value;
142        }
143    
144        protected String escape(String input)
145        {
146            return getConfig().isEscape() ? EscapeTool.getInstance().escape(input) : input;
147        }
148    }