Coverage Report - org.ini4j.Options
 
Classes in this File Line Coverage Branch Coverage Complexity
Options
100%
65/65
90%
9/10
1.292
 
 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;
 17  
 
 18  
 import org.ini4j.spi.OptionsBuilder;
 19  
 import org.ini4j.spi.OptionsFormatter;
 20  
 import org.ini4j.spi.OptionsHandler;
 21  
 import org.ini4j.spi.OptionsParser;
 22  
 
 23  
 import java.io.File;
 24  
 import java.io.FileNotFoundException;
 25  
 import java.io.FileOutputStream;
 26  
 import java.io.IOException;
 27  
 import java.io.InputStream;
 28  
 import java.io.InputStreamReader;
 29  
 import java.io.OutputStream;
 30  
 import java.io.OutputStreamWriter;
 31  
 import java.io.Reader;
 32  
 import java.io.Writer;
 33  
 
 34  
 import java.net.URL;
 35  
 
 36  
 public class Options extends BasicOptionMap implements Persistable, Configurable
 37  
 {
 38  
     private static final long serialVersionUID = -1119753444859181822L;
 39  
     private String _comment;
 40  
     private Config _config;
 41  
     private File _file;
 42  
 
 43  
     public Options()
 44  32
     {
 45  32
         _config = Config.getGlobal().clone();
 46  32
         _config.setEmptyOption(true);
 47  32
     }
 48  
 
 49  
     public Options(Reader input) throws IOException, InvalidFileFormatException
 50  
     {
 51  5
         this();
 52  5
         load(input);
 53  5
     }
 54  
 
 55  
     public Options(InputStream input) throws IOException, InvalidFileFormatException
 56  
     {
 57  5
         this();
 58  5
         load(input);
 59  3
     }
 60  
 
 61  
     public Options(URL input) throws IOException, InvalidFileFormatException
 62  
     {
 63  4
         this();
 64  4
         load(input);
 65  4
     }
 66  
 
 67  
     public Options(File input) throws IOException, InvalidFileFormatException
 68  
     {
 69  1
         this();
 70  1
         _file = input;
 71  1
         load();
 72  1
     }
 73  
 
 74  
     public String getComment()
 75  
     {
 76  5
         return _comment;
 77  
     }
 78  
 
 79  
     public void setComment(String value)
 80  
     {
 81  14
         _comment = value;
 82  14
     }
 83  
 
 84  
     @Override public Config getConfig()
 85  
     {
 86  1029
         return _config;
 87  
     }
 88  
 
 89  
     @Override public void setConfig(Config value)
 90  
     {
 91  8
         _config = value;
 92  8
     }
 93  
 
 94  
     @Override public File getFile()
 95  
     {
 96  1
         return _file;
 97  
     }
 98  
 
 99  
     @Override public void setFile(File value)
 100  
     {
 101  3
         _file = value;
 102  3
     }
 103  
 
 104  
     @Override public void load() throws IOException, InvalidFileFormatException
 105  
     {
 106  4
         if (_file == null)
 107  
         {
 108  1
             throw new FileNotFoundException();
 109  
         }
 110  
 
 111  3
         load(_file);
 112  3
     }
 113  
 
 114  
     @Override public void load(InputStream input) throws IOException, InvalidFileFormatException
 115  
     {
 116  8
         load(new InputStreamReader(input, getConfig().getFileEncoding()));
 117  6
     }
 118  
 
 119  
     @Override public void load(Reader input) throws IOException, InvalidFileFormatException
 120  
     {
 121  17
         OptionsParser.newInstance(getConfig()).parse(input, newBuilder());
 122  14
     }
 123  
 
 124  
     @Override public void load(URL input) throws IOException, InvalidFileFormatException
 125  
     {
 126  7
         OptionsParser.newInstance(getConfig()).parse(input, newBuilder());
 127  7
     }
 128  
 
 129  
     @Override public void load(File input) throws IOException, InvalidFileFormatException
 130  
     {
 131  3
         load(input.toURI().toURL());
 132  3
     }
 133  
 
 134  
     @Override public void store() throws IOException
 135  
     {
 136  2
         if (_file == null)
 137  
         {
 138  1
             throw new FileNotFoundException();
 139  
         }
 140  
 
 141  1
         store(_file);
 142  1
     }
 143  
 
 144  
     @Override public void store(OutputStream output) throws IOException
 145  
     {
 146  2
         store(new OutputStreamWriter(output, getConfig().getFileEncoding()));
 147  2
     }
 148  
 
 149  
     @Override public void store(Writer output) throws IOException
 150  
     {
 151  6
         store(OptionsFormatter.newInstance(output, getConfig()));
 152  6
     }
 153  
 
 154  
     @Override public void store(File output) throws IOException
 155  
     {
 156  1
         OutputStream stream = new FileOutputStream(output);
 157  
 
 158  1
         store(stream);
 159  1
         stream.close();
 160  1
     }
 161  
 
 162  
     protected OptionsHandler newBuilder()
 163  
     {
 164  24
         return OptionsBuilder.newInstance(this);
 165  
     }
 166  
 
 167  
     protected void store(OptionsHandler formatter) throws IOException
 168  
     {
 169  6
         formatter.startOptions();
 170  6
         storeComment(formatter, _comment);
 171  6
         for (String name : keySet())
 172  
         {
 173  63
             storeComment(formatter, getComment(name));
 174  63
             int n = getConfig().isMultiOption() ? length(name) : 1;
 175  
 
 176  133
             for (int i = 0; i < n; i++)
 177  
             {
 178  70
                 String value = get(name, i);
 179  
 
 180  70
                 formatter.handleOption(name, value);
 181  
             }
 182  63
         }
 183  
 
 184  6
         formatter.endOptions();
 185  6
     }
 186  
 
 187  
     @Override boolean isPropertyFirstUpper()
 188  
     {
 189  128
         return getConfig().isPropertyFirstUpper();
 190  
     }
 191  
 
 192  
     private void storeComment(OptionsHandler formatter, String comment)
 193  
     {
 194  69
         formatter.handleComment(comment);
 195  69
     }
 196  
 }