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.InvalidFileFormatException;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.Reader;
024
025import java.net.URL;
026
027public class OptionsParser extends AbstractParser
028{
029    private static final String COMMENTS = "!#";
030    private static final String OPERATORS = ":=";
031
032    public OptionsParser()
033    {
034        super(OPERATORS, COMMENTS);
035    }
036
037    public static OptionsParser newInstance()
038    {
039        return ServiceFinder.findService(OptionsParser.class);
040    }
041
042    public static OptionsParser newInstance(Config config)
043    {
044        OptionsParser instance = newInstance();
045
046        instance.setConfig(config);
047
048        return instance;
049    }
050
051    public void parse(InputStream input, OptionsHandler handler) throws IOException, InvalidFileFormatException
052    {
053        parse(newIniSource(input, handler), handler);
054    }
055
056    public void parse(Reader input, OptionsHandler handler) throws IOException, InvalidFileFormatException
057    {
058        parse(newIniSource(input, handler), handler);
059    }
060
061    public void parse(URL input, OptionsHandler handler) throws IOException, InvalidFileFormatException
062    {
063        parse(newIniSource(input, handler), handler);
064    }
065
066    private void parse(IniSource source, OptionsHandler handler) throws IOException, InvalidFileFormatException
067    {
068        handler.startOptions();
069        for (String line = source.readLine(); line != null; line = source.readLine())
070        {
071            parseOptionLine(line, handler, source.getLineNumber());
072        }
073
074        handler.endOptions();
075    }
076}