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