Coverage Report - org.ini4j.IniPreferences
 
Classes in this File Line Coverage Branch Coverage Complexity
IniPreferences
100%
32/32
100%
8/8
1.308
IniPreferences$SectionPreferences
100%
26/26
100%
4/4
1.308
 
 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 java.io.IOException;
 19  
 import java.io.InputStream;
 20  
 import java.io.Reader;
 21  
 
 22  
 import java.net.URL;
 23  
 
 24  
 import java.util.ArrayList;
 25  
 import java.util.List;
 26  
 import java.util.prefs.AbstractPreferences;
 27  
 import java.util.prefs.BackingStoreException;
 28  
 
 29  18
 public class IniPreferences extends AbstractPreferences
 30  
 {
 31  
 
 32  
     /** frequently used empty String array */
 33  1
     private static final String[] EMPTY = {};
 34  
 
 35  
     /** underlaying <code>Ini</code> implementation */
 36  
     private final Ini _ini;
 37  
 
 38  
     /**
 39  
      * Constructs a new preferences node on top of <code>Ini</code> instance.
 40  
      *
 41  
      * @param ini underlaying <code>Ini</code> instance
 42  
      */
 43  
     public IniPreferences(Ini ini)
 44  
     {
 45  10
         super(null, "");
 46  10
         _ini = ini;
 47  10
     }
 48  
 
 49  
     /**
 50  
      * Constructs a new preferences node based on newly loaded <code>Ini</code> instance.
 51  
      *
 52  
      * This is just a helper constructor, to make simpler constructing <code>IniPreferences</code>
 53  
      * directly from <code>Reader</code>.
 54  
      *
 55  
      * @param input the <code>Reader</code> containing <code>Ini</code> data
 56  
      * @throws IOException if an I/O error occured
 57  
      * @throws InvalidFileFormatException if <code>Ini</code> parsing error occured
 58  
      */
 59  
     public IniPreferences(Reader input) throws IOException, InvalidFileFormatException
 60  
     {
 61  1
         super(null, "");
 62  1
         _ini = new Ini(input);
 63  1
     }
 64  
 
 65  
     /**
 66  
      * Constructs a new preferences node based on newly loaded <code>Ini</code> instance.
 67  
      *
 68  
      * This is just a helper constructor, to make simpler constructing <code>IniPreferences</code>
 69  
      * directly from <code>InputStream</code>.
 70  
      *
 71  
      * @param input the <code>InputStream</code> containing <code>Ini</code> data
 72  
      * @throws IOException if an I/O error occured
 73  
      * @throws InvalidFileFormatException if <code>Ini</code> parsing error occured
 74  
      */
 75  
     public IniPreferences(InputStream input) throws IOException, InvalidFileFormatException
 76  
     {
 77  3
         super(null, "");
 78  3
         _ini = new Ini(input);
 79  3
     }
 80  
 
 81  
     /**
 82  
      * Constructs a new preferences node based on newly loaded <code>Ini</code> instance.
 83  
      *
 84  
      * This is just a helper constructor, to make simpler constructing <code>IniPreferences</code>
 85  
      * directly from <code>URL</code>.
 86  
      *
 87  
      * @param input the <code>URL</code> containing <code>Ini</code> data
 88  
      * @throws IOException if an I/O error occured
 89  
      * @throws InvalidFileFormatException if <code>Ini</code> parsing error occured
 90  
      */
 91  
     public IniPreferences(URL input) throws IOException, InvalidFileFormatException
 92  
     {
 93  1
         super(null, "");
 94  1
         _ini = new Ini(input);
 95  1
     }
 96  
 
 97  
     /**
 98  
      * Provide access to underlaying {@link org.ini4j.Ini} implementation.
 99  
      *
 100  
      * @return <code>Ini</code> implementation
 101  
      */
 102  
     protected Ini getIni()
 103  
     {
 104  1
         return _ini;
 105  
     }
 106  
 
 107  
     /**
 108  
      * Implements the <CODE>getSpi</CODE> method as per the specification in
 109  
      * {@link java.util.prefs.AbstractPreferences#getSpi(String)}.
 110  
      *
 111  
      * This implementation doesn't support this operation, so allways throws UnsupportedOperationException.
 112  
      *
 113  
      * @return if the value associated with the specified key at this preference node, or null if there is no association for this key, or the association cannot be determined at this time.
 114  
      * @param key key to getvalue for
 115  
      * @throws UnsupportedOperationException this implementation allways throws this exception
 116  
      */
 117  
     @Override protected String getSpi(String key) throws UnsupportedOperationException
 118  
     {
 119  1
         throw new UnsupportedOperationException();
 120  
     }
 121  
 
 122  
     /**
 123  
      * Implements the <CODE>childrenNamesSpi</CODE> method as per the specification in
 124  
      * {@link java.util.prefs.AbstractPreferences#childrenNamesSpi()}.
 125  
      * @return an array containing the names of the children of this preference node.
 126  
      * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 127  
      */
 128  
     @Override protected String[] childrenNamesSpi() throws BackingStoreException
 129  
     {
 130  5
         List<String> names = new ArrayList<String>();
 131  
 
 132  5
         for (String name : _ini.keySet())
 133  
         {
 134  13
             if (name.indexOf(_ini.getPathSeparator()) < 0)
 135  
             {
 136  5
                 names.add(name);
 137  
             }
 138  
         }
 139  
 
 140  5
         return names.toArray(EMPTY);
 141  
     }
 142  
 
 143  
     /**
 144  
      * Implements the <CODE>childSpi</CODE> method as per the specification in
 145  
      * {@link java.util.prefs.AbstractPreferences#childSpi(String)}.
 146  
      * @param name child name
 147  
      * @return child node
 148  
      */
 149  
     @Override protected SectionPreferences childSpi(String name)
 150  
     {
 151  13
         Ini.Section sec = _ini.get(name);
 152  13
         boolean isNew = sec == null;
 153  
 
 154  13
         if (isNew)
 155  
         {
 156  3
             sec = _ini.add(name);
 157  
         }
 158  
 
 159  13
         return new SectionPreferences(this, sec, isNew);
 160  
     }
 161  
 
 162  
     /**
 163  
      * Implements the <CODE>flushSpi</CODE> method as per the specification in
 164  
      * {@link java.util.prefs.AbstractPreferences#flushSpi()}.
 165  
      *
 166  
      * This implementation does nothing.
 167  
      *
 168  
      * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 169  
      */
 170  
     @Override protected void flushSpi() throws BackingStoreException
 171  
     {
 172  
         assert true;
 173  5
     }
 174  
 
 175  
     /**
 176  
      * Implements the <CODE>keysSpi</CODE> method as per the specification in
 177  
      * {@link java.util.prefs.AbstractPreferences#keysSpi()}.
 178  
      *
 179  
      * This implementation allways return an empty array.
 180  
      *
 181  
      * @return an empty array.
 182  
      * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 183  
      */
 184  
     @Override protected String[] keysSpi() throws BackingStoreException
 185  
     {
 186  1
         return EMPTY;
 187  
     }
 188  
 
 189  
     /**
 190  
      * Implements the <CODE>putSpi</CODE> method as per the specification in
 191  
      * {@link java.util.prefs.AbstractPreferences#putSpi(String,String)}.
 192  
      *
 193  
      * This implementation doesn;t support this operation, so allways throws UnsupportedOperationException.
 194  
      *
 195  
      * @param key key to set value for
 196  
      * @param value new value for key
 197  
      * @throws UnsupportedOperationException this implementation allways throws this exception
 198  
      */
 199  
     @Override protected void putSpi(String key, String value) throws UnsupportedOperationException
 200  
     {
 201  1
         throw new UnsupportedOperationException();
 202  
     }
 203  
 
 204  
     /**
 205  
      * Implements the <CODE>removeNodeSpi</CODE> method as per the specification in
 206  
      * {@link java.util.prefs.AbstractPreferences#removeNodeSpi()}.
 207  
      *
 208  
      * This implementation doesn;t support this operation, so allways throws UnsupportedOperationException.
 209  
      * @throws UnsupportedOperationException this implementation allways throws this exception
 210  
      * @throws BackingStoreException this implementation never throws this exception
 211  
      */
 212  
     @Override protected void removeNodeSpi() throws BackingStoreException, UnsupportedOperationException
 213  
     {
 214  1
         throw new UnsupportedOperationException();
 215  
     }
 216  
 
 217  
     /**
 218  
      * Implements the <CODE>removeSpi</CODE> method as per the specification in
 219  
      * {@link java.util.prefs.AbstractPreferences#removeSpi(String)}.
 220  
      * @param key key to remove
 221  
      * @throws UnsupportedOperationException this implementation allways throws this exception
 222  
      */
 223  
     @Override protected void removeSpi(String key) throws UnsupportedOperationException
 224  
     {
 225  1
         throw new UnsupportedOperationException();
 226  
     }
 227  
 
 228  
     /**
 229  
      * Implements the <CODE>syncSpi</CODE> method as per the specification in
 230  
      * {@link java.util.prefs.AbstractPreferences#syncSpi()}.
 231  
      *
 232  
      * This implementation does nothing.
 233  
      *
 234  
      * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 235  
      */
 236  
     @Override protected void syncSpi() throws BackingStoreException
 237  
     {
 238  
         assert true;
 239  2
     }
 240  
 
 241  2
     protected class SectionPreferences extends AbstractPreferences
 242  
     {
 243  
 
 244  
         /** underlaying <code>Section</code> implementation */
 245  
         private final Ini.Section _section;
 246  
 
 247  
         /**
 248  
          * Constructs a new SectionPreferences instance on top of Ini.Section instance.
 249  
          *
 250  
          * @param parent parent preferences node
 251  
          * @parem section underlaying Ini.Section instance
 252  
          * @param isNew indicate is this a new node or already existing one
 253  
          */
 254  
         SectionPreferences(AbstractPreferences parent, Ini.Section section, boolean isNew)
 255  15
         {
 256  15
             super(parent, section.getSimpleName());
 257  15
             _section = section;
 258  15
             newNode = isNew;
 259  15
         }
 260  
 
 261  
         /**
 262  
          * Implements the <CODE>flush</CODE> method as per the specification in
 263  
          * {@link java.util.prefs.Preferences#flush()}.
 264  
          *
 265  
          * This implementation just call parent's <code>flush()</code> method.
 266  
          *
 267  
          * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 268  
          */
 269  
         @Override public void flush() throws BackingStoreException
 270  
         {
 271  3
             parent().flush();
 272  3
         }
 273  
 
 274  
         /**
 275  
          * Implements the <CODE>sync</CODE> method as per the specification in
 276  
          * {@link java.util.prefs.Preferences#sync()}.
 277  
          *
 278  
          * This implementation just call parent's <code>sync()</code> method.
 279  
          *
 280  
          * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 281  
          */
 282  
         @Override public void sync() throws BackingStoreException
 283  
         {
 284  1
             parent().sync();
 285  1
         }
 286  
 
 287  
         /**
 288  
          * Implements the <CODE>getSpi</CODE> method as per the specification in
 289  
          * {@link java.util.prefs.AbstractPreferences#getSpi(String)}.
 290  
          * @return if the value associated with the specified key at this preference node, or null if there is no association for this key, or the association cannot be determined at this time.
 291  
          * @param key key to getvalue for
 292  
          */
 293  
         @Override protected String getSpi(String key)
 294  
         {
 295  52
             return _section.fetch(key);
 296  
         }
 297  
 
 298  
         /**
 299  
          * Implements the <CODE>childrenNamesSpi</CODE> method as per the specification in
 300  
          * {@link java.util.prefs.AbstractPreferences#childrenNamesSpi()}.
 301  
          *
 302  
          * This implementation allways returns an empty array.
 303  
          *
 304  
          * @return an emty array.
 305  
          * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 306  
          */
 307  
         @Override protected String[] childrenNamesSpi() throws BackingStoreException
 308  
         {
 309  3
             return _section.childrenNames();
 310  
         }
 311  
 
 312  
         /**
 313  
          * Implements the <CODE>childSpi</CODE> method as per the specification in
 314  
          * {@link java.util.prefs.AbstractPreferences#childSpi(String)}.
 315  
          *
 316  
          * This implementation doesn't support this operation.
 317  
          *
 318  
          * @throws UnsupportedOperationException this implementation allways throws this exception
 319  
          * @param name child name
 320  
          * @return child node
 321  
          */
 322  
         @Override protected SectionPreferences childSpi(String name) throws UnsupportedOperationException
 323  
         {
 324  2
             Ini.Section child = _section.getChild(name);
 325  2
             boolean isNew = child == null;
 326  
 
 327  2
             if (isNew)
 328  
             {
 329  1
                 child = _section.addChild(name);
 330  
             }
 331  
 
 332  2
             return new SectionPreferences(this, child, isNew);
 333  
         }
 334  
 
 335  
         /**
 336  
          * Implements the <CODE>flushSpi</CODE> method as per the specification in
 337  
          * {@link java.util.prefs.AbstractPreferences#flushSpi()}.
 338  
          *
 339  
          * This implementation does nothing.
 340  
          *
 341  
          * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 342  
          */
 343  
         @Override protected void flushSpi() throws BackingStoreException
 344  
         {
 345  
             assert true;
 346  8
         }
 347  
 
 348  
         /**
 349  
          * Implements the <CODE>keysSpi</CODE> method as per the specification in
 350  
          * {@link java.util.prefs.AbstractPreferences#keysSpi()}.
 351  
          *
 352  
          * @return an array of the keys that have an associated value in this preference node.
 353  
          * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 354  
          */
 355  
         @Override protected String[] keysSpi() throws BackingStoreException
 356  
         {
 357  4
             return _section.keySet().toArray(EMPTY);
 358  
         }
 359  
 
 360  
         /**
 361  
          * Implements the <CODE>putSpi</CODE> method as per the specification in
 362  
          * {@link java.util.prefs.AbstractPreferences#putSpi(String,String)}.
 363  
          *
 364  
          * @param key key to set value for
 365  
          * @param value new value of key
 366  
          */
 367  
         @Override protected void putSpi(String key, String value)
 368  
         {
 369  5
             _section.put(key, value);
 370  5
         }
 371  
 
 372  
         /**
 373  
          * Implements the <CODE>removeNodeSpi</CODE> method as per the specification in
 374  
          * {@link java.util.prefs.AbstractPreferences#removeNodeSpi()}.
 375  
          *
 376  
          * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 377  
          */
 378  
         @Override protected void removeNodeSpi() throws BackingStoreException
 379  
         {
 380  1
             _ini.remove(_section);
 381  1
         }
 382  
 
 383  
         /**
 384  
          * Implements the <CODE>removeSpi</CODE> method as per the specification in
 385  
          * {@link java.util.prefs.AbstractPreferences#removeSpi(String)}.
 386  
          * @param key key to remove
 387  
          */
 388  
         @Override protected void removeSpi(String key)
 389  
         {
 390  1
             _section.remove(key);
 391  1
         }
 392  
 
 393  
         /**
 394  
          * Implements the <CODE>syncSpi</CODE> method as per the specification in
 395  
          * {@link java.util.prefs.AbstractPreferences#syncSpi()}.
 396  
          *
 397  
          * This implementation does nothing.
 398  
          *
 399  
          * @throws BackingStoreException if this operation cannot be completed due to a failure in the backing store, or inability to communicate with it.
 400  
          */
 401  
         @Override protected void syncSpi() throws BackingStoreException
 402  
         {
 403  
             assert true;
 404  3
         }
 405  
     }
 406  
 }