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.Reg;
19  
20  import org.ini4j.sample.Dwarf;
21  
22  import org.ini4j.test.DwarfsData;
23  import org.ini4j.test.Helper;
24  
25  import static org.junit.Assert.*;
26  
27  import java.io.File;
28  import java.io.IOException;
29  
30  import java.net.URI;
31  
32  //<editor-fold defaultstate="collapsed" desc="apt documentation">
33  //|
34  //|                -------------
35  //|                Reg Tutorial
36  //|
37  //|Reg Tutorial - Windows .REG file handling
38  //|
39  //| Windows regedit commands .REG file format is very close to .ini format.
40  //| \[ini4j\] provides org.ini4j.Reg class to model .REG format. This tutorial
41  //| show the differences between Ini and Reg classes.
42  //|
43  //| Code sniplets in this tutorial tested with the following .reg file:
44  //| {{{../sample/dwarfs.reg.html}dwarfs.reg}}
45  //|
46  //</editor-fold>
47  public class RegTutorial extends AbstractTutorial
48  {
49      public static final String FILENAME = "../sample/dwarfs.reg";
50  
51      public static void main(String[] args) throws Exception
52      {
53          new RegTutorial().run(filearg(args));
54      }
55  
56      @Override protected void run(File arg) throws Exception
57      {
58          Reg reg = new Reg(arg.toURI().toURL());
59  
60          sample01(arg);
61          sample02();
62      }
63  
64  //|
65  //|* Loading and storing
66  //|
67  //| There is nothing special with loading and storing data, it works exactly same
68  //| as in Ini class. But while loading data, Reg class will strip .REG special
69  //| values (double qoute around strings, type data from option, etc). So after
70  //| loading a .REG file, you can use it exactly same way as Ini class. Ofcource
71  //| if you store Reg class, it will put all above meta information int file, so
72  //| the result will be a valid .REG file. You don't need to worry about file
73  //| encoding, version in first line, etc,etc.
74  //|
75  //| Assume you have a .REG file, with the following section/key:
76  //|
77  //|+---+
78  //|[HKEY_CURRENT_USER\Software\ini4j-test\dwarfs\bashful]
79  //|@="bashful"
80  //|"weight"=hex(2):34,00,35,00,2e,00,37,00,00,00
81  //|"height"="98.8"
82  //|"age"=dword:00000043
83  //|"homePage"="http://snowwhite.tale/~bashful"
84  //|"homeDir"="/home/bashful"
85  //|+---+
86  //|
87  //| As you see, "weight" and "age" is not simlpe strings. The "height" is a REG_DWORD
88  //| type while "weight" is REG_EXPAND_SZ. Don't worry, Reg class take care about
89  //| type conversion, you will access these as with regular .ini files:
90  //{
91      void sample01(File file) throws IOException
92      {
93          Reg reg = new Reg(file);
94          Reg.Key hive = reg.get(Reg.Hive.HKEY_CURRENT_USER.toString());
95          Reg.Key bashful;
96  
97          bashful = hive.lookup("Software", "ini4j-test", "dwarfs", "bashful");
98  
99          // or ...
100         bashful = hive.lookup("Software\\ini4j-test\\dwarfs\\bashful");
101 
102         // or even...
103         bashful = reg.get("HKEY_CURRENT_USER\\Software\\ini4j-test\\dwarfs\\bashful");
104 
105         // read some data
106         double weight = bashful.get("weight", double.class);  // = 45.7
107         double height = bashful.get("height", double.class);  // = 98.8
108         int age = bashful.get("age", int.class);  // = 67
109         URI homePage = bashful.get("homePage", URI.class);  // = new URI("http://snowwhite.tale/~bashful");
110         String homeDir = bashful.get("homeDir");  // = "/home/bashful"
111 
112 //}
113         assertNotNull(reg.get(Helper.DWARFS_REG_PATH + "\\dwarfs"));
114         Helper.assertEquals(DwarfsData.bashful, bashful.as(Dwarf.class));
115     }
116 
117 //|
118 //|* Types
119 //|
120 //| When you load data into Reg class, it will preserve meta informations, such as
121 //| type informations. If you create new values, by default these will have
122 //| tpye REG_SZ. Ofcource you may specify type information for values.
123 //{
124     void sample02()
125     {
126         Reg reg = new Reg();
127         Reg.Key key = reg.add("HKEY_CURRENT_USER\\Software\\ini4j-test\\dwarfs\\sleepy");
128 
129         key.put("fortuneNumber", 99);
130         key.putType("fortuneNumber", Reg.Type.REG_MULTI_SZ);
131 
132 //}
133 //|
134 //| If you store reg object above, it will contains a section similar to this:
135 //|
136 //|+---+
137 //|[HKEY_CURRENT_USER\Software\ini4j-test\dwarfs\sleepy]
138 //|"fortuneNumber"=hex(7):39,00,39,00,00,00,00,00
139 //|+---+
140 //|
141     }
142 }