Coverage Report - org.ini4j.spi.AbstractProfileBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractProfileBuilder
100%
40/40
96%
25/26
2.182
 
 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.CommentedMap;
 19  
 import org.ini4j.Config;
 20  
 import org.ini4j.Ini;
 21  
 import org.ini4j.Profile;
 22  
 
 23  83
 abstract class AbstractProfileBuilder implements IniHandler
 24  
 {
 25  
     private Profile.Section _currentSection;
 26  
     private boolean _header;
 27  
     private String _lastComment;
 28  
 
 29  
     @Override public void endIni()
 30  
     {
 31  
 
 32  
         // comment only .ini files....
 33  78
         if ((_lastComment != null) && _header)
 34  
         {
 35  1
             setHeaderComment();
 36  
         }
 37  78
     }
 38  
 
 39  
     @Override public void endSection()
 40  
     {
 41  538
         _currentSection = null;
 42  538
     }
 43  
 
 44  
     @Override public void handleComment(String comment)
 45  
     {
 46  569
         if ((_lastComment != null) && _header)
 47  
         {
 48  44
             _header = false;
 49  44
             setHeaderComment();
 50  
         }
 51  
 
 52  569
         _lastComment = comment;
 53  569
     }
 54  
 
 55  
     @Override public void handleOption(String name, String value)
 56  
     {
 57  2602
         _header = false;
 58  2602
         if (getConfig().isMultiOption())
 59  
         {
 60  2020
             _currentSection.add(name, value);
 61  
         }
 62  
         else
 63  
         {
 64  582
             _currentSection.put(name, value);
 65  
         }
 66  
 
 67  2602
         if (_lastComment != null)
 68  
         {
 69  1
             putComment(_currentSection, name);
 70  1
             _lastComment = null;
 71  
         }
 72  2602
     }
 73  
 
 74  
     @Override public void startIni()
 75  
     {
 76  83
         if (getConfig().isHeaderComment())
 77  
         {
 78  78
             _header = true;
 79  
         }
 80  83
     }
 81  
 
 82  
     @Override public void startSection(String sectionName)
 83  
     {
 84  538
         if (getConfig().isMultiSection())
 85  
         {
 86  6
             _currentSection = getProfile().add(sectionName);
 87  
         }
 88  
         else
 89  
         {
 90  532
             Ini.Section s = getProfile().get(sectionName);
 91  
 
 92  532
             _currentSection = (s == null) ? getProfile().add(sectionName) : s;
 93  
         }
 94  
 
 95  538
         if (_lastComment != null)
 96  
         {
 97  430
             if (_header)
 98  
             {
 99  10
                 setHeaderComment();
 100  
             }
 101  
             else
 102  
             {
 103  420
                 putComment(getProfile(), sectionName);
 104  
             }
 105  
 
 106  430
             _lastComment = null;
 107  
         }
 108  
 
 109  538
         _header = false;
 110  538
     }
 111  
 
 112  
     abstract Config getConfig();
 113  
 
 114  
     abstract Profile getProfile();
 115  
 
 116  
     Profile.Section getCurrentSection()
 117  
     {
 118  175
         return _currentSection;
 119  
     }
 120  
 
 121  
     private void setHeaderComment()
 122  
     {
 123  55
         if (getConfig().isComment())
 124  
         {
 125  55
             getProfile().setComment(_lastComment);
 126  
         }
 127  55
     }
 128  
 
 129  
     private void putComment(CommentedMap<String, ?> map, String key)
 130  
     {
 131  421
         if (getConfig().isComment())
 132  
         {
 133  413
             map.putComment(key, _lastComment);
 134  
         }
 135  421
     }
 136  
 }