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.IniHandler;
019    
020    import java.io.OutputStream;
021    import java.io.OutputStreamWriter;
022    import java.io.PrintWriter;
023    import java.io.Writer;
024    
025    public class XMLFormatter implements IniHandler
026    {
027        private PrintWriter _output;
028    
029        public static XMLFormatter newInstance(Writer out)
030        {
031            XMLFormatter instance = newInstance();
032    
033            instance.setOutput(new PrintWriter(out));
034    
035            return instance;
036        }
037    
038        public static XMLFormatter newInstance(OutputStream out)
039        {
040            return newInstance(new OutputStreamWriter(out));
041        }
042    
043        public void endIni()
044        {
045            getOutput().println("</ini>");
046            getOutput().flush();
047        }
048    
049        public void endSection()
050        {
051            getOutput().println(" </section>");
052        }
053    
054        public void handleOption(String optionName, String optionValue)
055        {
056            getOutput().print("  <option key='");
057            getOutput().print(optionName);
058            getOutput().print("' value='");
059            getOutput().print(optionValue);
060            getOutput().println("'/>");
061        }
062    
063        public void startIni()
064        {
065            getOutput().println("<ini version='1.0'>");
066        }
067    
068        public void startSection(String sectionName)
069        {
070            getOutput().print(" <section key='");
071            getOutput().print(sectionName);
072            getOutput().println("'>");
073        }
074    
075        protected static XMLFormatter newInstance()
076        {
077            return ServiceFinder.findService(XMLFormatter.class);
078        }
079    
080        protected PrintWriter getOutput()
081        {
082            return _output;
083        }
084    
085        protected void setOutput(PrintWriter value)
086        {
087            _output = value;
088        }
089    }