View Javadoc

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.tutorial;
17  
18  import org.ini4j.Ini;
19  import org.ini4j.IniPreferences;
20  
21  import org.ini4j.test.DwarfsData;
22  
23  import static org.junit.Assert.*;
24  
25  import java.io.File;
26  import java.io.IOException;
27  
28  import java.util.prefs.Preferences;
29  
30  //<editor-fold defaultstate="collapsed" desc="apt documentation">
31  //|
32  //|                -------------
33  //|                Preferences Tutorial
34  //|
35  //|Preferences Tutorial
36  //|
37  //| The purpose of this document is to familiarize the reader with the usage of
38  //| the [ini4j] library's Preferences interface. Each chapter contains all the
39  //| necessary code portions and explanation for a given function.
40  //|
41  //| Code sniplets in this tutorial tested with the following .ini file:
42  //| {{{../sample/dwarfs.ini.html}dwarfs.ini}}
43  //|
44  //| As soon as the Preferences object is created it functions as a standard Preferences node, and should be
45  //| used as such. Implicitly only new nodes can be created in the root node (these will be the sections).
46  //| In the first level nodes (sections) only values can be created (these will be the options).
47  //|
48  //| In the case of an invalid operation an <<<UnsupportedOperationException>>> type runtime exception is generated.
49  //| This happens if we try to set a value at the root node or to create a node on the second level,
50  //| since these operations cannot be interpreted on the whole .ini file under Preferences.
51  //|
52  //</editor-fold>
53  public class PrefsTutorial extends AbstractTutorial
54  {
55      public static void main(String[] args) throws Exception
56      {
57          new PrefsTutorial().run(filearg(args));
58      }
59  
60      protected void run(File arg) throws Exception
61      {
62          Ini ini = new Ini(arg.toURI().toURL());
63  
64          sample01(ini);
65      }
66  
67  //|
68  //|* Reading and writing values
69  //|
70  //| Values can read and write like any other Preferences node, there is no
71  //| differences.
72  //{
73      void sample01(Ini ini) throws IOException
74      {
75          Preferences prefs = new IniPreferences(ini);
76          Preferences bashful = prefs.node("bashful");
77          String home = bashful.get("homeDir", "/home");
78          int age = bashful.getInt("age", -1);
79  
80          bashful.putDouble("weight", 55.6);
81  
82  //}
83          assertEquals(DwarfsData.bashful.homeDir, bashful.get("homeDir", null));
84          assertEquals(DwarfsData.bashful.age, bashful.getInt("age", -1));
85          assertEquals(55.6, bashful.getDouble("weight", -1), 0.001);
86      }
87  }