Coverage Report - org.ini4j.spi.OptionsBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
OptionsBuilder
100%
35/35
95%
19/20
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.Options;
 20  
 
 21  24
 public class OptionsBuilder implements OptionsHandler
 22  
 {
 23  
     private boolean _header;
 24  
     private String _lastComment;
 25  
     private Options _options;
 26  
 
 27  
     public static OptionsBuilder newInstance(Options opts)
 28  
     {
 29  24
         OptionsBuilder instance = newInstance();
 30  
 
 31  24
         instance.setOptions(opts);
 32  
 
 33  24
         return instance;
 34  
     }
 35  
 
 36  
     public void setOptions(Options value)
 37  
     {
 38  24
         _options = value;
 39  24
     }
 40  
 
 41  
     @Override public void endOptions()
 42  
     {
 43  
 
 44  
         // comment only .opt file ...
 45  21
         if ((_lastComment != null) && _header)
 46  
         {
 47  1
             setHeaderComment();
 48  
         }
 49  21
     }
 50  
 
 51  
     @Override public void handleComment(String comment)
 52  
     {
 53  158
         if ((_lastComment != null) && _header)
 54  
         {
 55  10
             setHeaderComment();
 56  10
             _header = false;
 57  
         }
 58  
 
 59  158
         _lastComment = comment;
 60  158
     }
 61  
 
 62  
     @Override public void handleOption(String name, String value)
 63  
     {
 64  652
         if (getConfig().isMultiOption())
 65  
         {
 66  647
             _options.add(name, value);
 67  
         }
 68  
         else
 69  
         {
 70  5
             _options.put(name, value);
 71  
         }
 72  
 
 73  652
         if (_lastComment != null)
 74  
         {
 75  109
             if (_header)
 76  
             {
 77  1
                 setHeaderComment();
 78  
             }
 79  
             else
 80  
             {
 81  108
                 putComment(name);
 82  
             }
 83  
 
 84  109
             _lastComment = null;
 85  
         }
 86  
 
 87  652
         _header = false;
 88  652
     }
 89  
 
 90  
     @Override public void startOptions()
 91  
     {
 92  24
         if (getConfig().isHeaderComment())
 93  
         {
 94  18
             _header = true;
 95  
         }
 96  24
     }
 97  
 
 98  
     protected static OptionsBuilder newInstance()
 99  
     {
 100  24
         return ServiceFinder.findService(OptionsBuilder.class);
 101  
     }
 102  
 
 103  
     private Config getConfig()
 104  
     {
 105  796
         return _options.getConfig();
 106  
     }
 107  
 
 108  
     private void setHeaderComment()
 109  
     {
 110  12
         if (getConfig().isComment())
 111  
         {
 112  12
             _options.setComment(_lastComment);
 113  
         }
 114  12
     }
 115  
 
 116  
     private void putComment(String key)
 117  
     {
 118  108
         if (getConfig().isComment())
 119  
         {
 120  99
             _options.putComment(key, _lastComment);
 121  
         }
 122  108
     }
 123  
 }