Coverage Report - org.ini4j.spi.IniParser
 
Classes in this File Line Coverage Branch Coverage Complexity
IniParser
97%
37/38
100%
20/20
2.25
 
 1  
 /*
 2  
  * Copyright 2005,2009 Ivan SZKIBA
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *      http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.ini4j.spi;
 17  
 
 18  
 import org.ini4j.Config;
 19  
 import org.ini4j.InvalidFileFormatException;
 20  
 
 21  
 import java.io.IOException;
 22  
 import java.io.InputStream;
 23  
 import java.io.Reader;
 24  
 
 25  
 import java.net.URL;
 26  
 
 27  
 import java.util.Locale;
 28  
 
 29  
 public class IniParser extends AbstractParser
 30  
 {
 31  
     private static final String COMMENTS = ";#";
 32  
     private static final String OPERATORS = ":=";
 33  
     static final char SECTION_BEGIN = '[';
 34  
     static final char SECTION_END = ']';
 35  
 
 36  
     public IniParser()
 37  
     {
 38  96
         super(OPERATORS, COMMENTS);
 39  96
     }
 40  
 
 41  
     public static IniParser newInstance()
 42  
     {
 43  85
         return ServiceFinder.findService(IniParser.class);
 44  
     }
 45  
 
 46  
     public static IniParser newInstance(Config config)
 47  
     {
 48  84
         IniParser instance = newInstance();
 49  
 
 50  84
         instance.setConfig(config);
 51  
 
 52  84
         return instance;
 53  
     }
 54  
 
 55  
     public void parse(InputStream input, IniHandler handler) throws IOException, InvalidFileFormatException
 56  
     {
 57  9
         parse(newIniSource(input, handler), handler);
 58  0
     }
 59  
 
 60  
     public void parse(Reader input, IniHandler handler) throws IOException, InvalidFileFormatException
 61  
     {
 62  75
         parse(newIniSource(input, handler), handler);
 63  71
     }
 64  
 
 65  
     public void parse(URL input, IniHandler handler) throws IOException, InvalidFileFormatException
 66  
     {
 67  17
         parse(newIniSource(input, handler), handler);
 68  16
     }
 69  
 
 70  
     private void parse(IniSource source, IniHandler handler) throws IOException, InvalidFileFormatException
 71  
     {
 72  101
         handler.startIni();
 73  101
         String sectionName = null;
 74  
 
 75  3307
         for (String line = source.readLine(); line != null; line = source.readLine())
 76  
         {
 77  3220
             if (line.charAt(0) == SECTION_BEGIN)
 78  
             {
 79  569
                 if (sectionName != null)
 80  
                 {
 81  474
                     handler.endSection();
 82  
                 }
 83  
 
 84  569
                 sectionName = parseSectionLine(line, source, handler);
 85  
             }
 86  
             else
 87  
             {
 88  2651
                 if (sectionName == null)
 89  
                 {
 90  4
                     if (getConfig().isGlobalSection())
 91  
                     {
 92  1
                         sectionName = getConfig().getGlobalSectionName();
 93  1
                         handler.startSection(sectionName);
 94  
                     }
 95  
                     else
 96  
                     {
 97  3
                         parseError(line, source.getLineNumber());
 98  
                     }
 99  
                 }
 100  
 
 101  2648
                 parseOptionLine(line, handler, source.getLineNumber());
 102  
             }
 103  
         }
 104  
 
 105  87
         if (sectionName != null)
 106  
         {
 107  85
             handler.endSection();
 108  
         }
 109  
 
 110  87
         handler.endIni();
 111  87
     }
 112  
 
 113  
     private String parseSectionLine(String line, IniSource source, IniHandler handler) throws InvalidFileFormatException
 114  
     {
 115  
         String sectionName;
 116  
 
 117  569
         if (line.charAt(line.length() - 1) != SECTION_END)
 118  
         {
 119  5
             parseError(line, source.getLineNumber());
 120  
         }
 121  
 
 122  564
         sectionName = unescapeFilter(line.substring(1, line.length() - 1).trim());
 123  564
         if ((sectionName.length() == 0) && !getConfig().isUnnamedSection())
 124  
         {
 125  2
             parseError(line, source.getLineNumber());
 126  
         }
 127  
 
 128  562
         if (getConfig().isLowerCaseSection())
 129  
         {
 130  106
             sectionName = sectionName.toLowerCase(Locale.getDefault());
 131  
         }
 132  
 
 133  562
         handler.startSection(sectionName);
 134  
 
 135  562
         return sectionName;
 136  
     }
 137  
 }