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 */
016package org.ini4j.spi;
017
018import org.ini4j.Config;
019import org.ini4j.Options;
020
021public class OptionsBuilder implements OptionsHandler
022{
023    private boolean _header;
024    private String _lastComment;
025    private Options _options;
026
027    public static OptionsBuilder newInstance(Options opts)
028    {
029        OptionsBuilder instance = newInstance();
030
031        instance.setOptions(opts);
032
033        return instance;
034    }
035
036    public void setOptions(Options value)
037    {
038        _options = value;
039    }
040
041    @Override public void endOptions()
042    {
043
044        // comment only .opt file ...
045        if ((_lastComment != null) && _header)
046        {
047            setHeaderComment();
048        }
049    }
050
051    @Override public void handleComment(String comment)
052    {
053        if ((_lastComment != null) && _header)
054        {
055            setHeaderComment();
056            _header = false;
057        }
058
059        _lastComment = comment;
060    }
061
062    @Override public void handleOption(String name, String value)
063    {
064        if (getConfig().isMultiOption())
065        {
066            _options.add(name, value);
067        }
068        else
069        {
070            _options.put(name, value);
071        }
072
073        if (_lastComment != null)
074        {
075            if (_header)
076            {
077                setHeaderComment();
078            }
079            else
080            {
081                putComment(name);
082            }
083
084            _lastComment = null;
085        }
086
087        _header = false;
088    }
089
090    @Override public void startOptions()
091    {
092        if (getConfig().isHeaderComment())
093        {
094            _header = true;
095        }
096    }
097
098    protected static OptionsBuilder newInstance()
099    {
100        return ServiceFinder.findService(OptionsBuilder.class);
101    }
102
103    private Config getConfig()
104    {
105        return _options.getConfig();
106    }
107
108    private void setHeaderComment()
109    {
110        if (getConfig().isComment())
111        {
112            _options.setComment(_lastComment);
113        }
114    }
115
116    private void putComment(String key)
117    {
118        if (getConfig().isComment())
119        {
120            _options.putComment(key, _lastComment);
121        }
122    }
123}