Coverage Report - org.ini4j.spi.AbstractParser
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractParser
100%
34/34
94%
17/18
2
 
 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  
 abstract class AbstractParser
 30  
 {
 31  
     private final String _comments;
 32  126
     private Config _config = Config.getGlobal();
 33  
     private final String _operators;
 34  
 
 35  
     protected AbstractParser(String operators, String comments)
 36  126
     {
 37  126
         _operators = operators;
 38  126
         _comments = comments;
 39  126
     }
 40  
 
 41  
     protected Config getConfig()
 42  
     {
 43  11475
         return _config;
 44  
     }
 45  
 
 46  
     protected void setConfig(Config value)
 47  
     {
 48  114
         _config = value;
 49  114
     }
 50  
 
 51  
     protected void parseError(String line, int lineNumber) throws InvalidFileFormatException
 52  
     {
 53  18
         throw new InvalidFileFormatException("parse error (at line: " + lineNumber + "): " + line);
 54  
     }
 55  
 
 56  
     IniSource newIniSource(InputStream input, HandlerBase handler)
 57  
     {
 58  10
         return new IniSource(input, handler, _comments, getConfig());
 59  
     }
 60  
 
 61  
     IniSource newIniSource(Reader input, HandlerBase handler)
 62  
     {
 63  94
         return new IniSource(input, handler, _comments, getConfig());
 64  
     }
 65  
 
 66  
     IniSource newIniSource(URL input, HandlerBase handler) throws IOException
 67  
     {
 68  25
         return new IniSource(input, handler, _comments, getConfig());
 69  
     }
 70  
 
 71  
     void parseOptionLine(String line, HandlerBase handler, int lineNumber) throws InvalidFileFormatException
 72  
     {
 73  3408
         int idx = indexOfOperator(line);
 74  3408
         String name = null;
 75  3408
         String value = null;
 76  
 
 77  3408
         if (idx < 0)
 78  
         {
 79  6
             if (getConfig().isEmptyOption())
 80  
             {
 81  3
                 name = line;
 82  
             }
 83  
             else
 84  
             {
 85  3
                 parseError(line, lineNumber);
 86  
             }
 87  
         }
 88  
         else
 89  
         {
 90  3402
             name = unescapeFilter(line.substring(0, idx)).trim();
 91  3402
             value = unescapeFilter(line.substring(idx + 1)).trim();
 92  
         }
 93  
 
 94  3405
         if (name.length() == 0)
 95  
         {
 96  5
             parseError(line, lineNumber);
 97  
         }
 98  
 
 99  3400
         if (getConfig().isLowerCaseOption())
 100  
         {
 101  491
             name = name.toLowerCase(Locale.getDefault());
 102  
         }
 103  
 
 104  3400
         handler.handleOption(name, value);
 105  3400
     }
 106  
 
 107  
     String unescapeFilter(String line)
 108  
     {
 109  7368
         return getConfig().isEscape() ? EscapeTool.getInstance().unescape(line) : line;
 110  
     }
 111  
 
 112  
     private int indexOfOperator(String line)
 113  
     {
 114  3408
         int idx = -1;
 115  
 
 116  10224
         for (char c : _operators.toCharArray())
 117  
         {
 118  6816
             int index = line.indexOf(c);
 119  
 
 120  6816
             if ((index >= 0) && ((idx == -1) || (index < idx)))
 121  
             {
 122  4225
                 idx = index;
 123  
             }
 124  
         }
 125  
 
 126  3408
         return idx;
 127  
     }
 128  
 }