001 /*
002 * Copyright 2005,2009 Ivan SZKIBA
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.ini4j.tutorial;
017
018 import org.ini4j.Reg;
019
020 import org.ini4j.sample.Dwarf;
021
022 import org.ini4j.test.DwarfsData;
023 import org.ini4j.test.Helper;
024
025 import static org.junit.Assert.*;
026
027 import java.io.File;
028 import java.io.IOException;
029
030 import java.net.URI;
031
032 //<editor-fold defaultstate="collapsed" desc="apt documentation">
033 //|
034 //| -------------
035 //| Reg Tutorial
036 //|
037 //|Reg Tutorial - Windows .REG file handling
038 //|
039 //| Windows regedit commands .REG file format is very close to .ini format.
040 //| \[ini4j\] provides org.ini4j.Reg class to model .REG format. This tutorial
041 //| show the differences between Ini and Reg classes.
042 //|
043 //| Code sniplets in this tutorial tested with the following .reg file:
044 //| {{{../sample/dwarfs.reg.html}dwarfs.reg}}
045 //|
046 //</editor-fold>
047 public class RegTutorial extends AbstractTutorial
048 {
049 public static final String FILENAME = "../sample/dwarfs.reg";
050
051 public static void main(String[] args) throws Exception
052 {
053 new RegTutorial().run(filearg(args));
054 }
055
056 @Override protected void run(File arg) throws Exception
057 {
058 Reg reg = new Reg(arg.toURI().toURL());
059
060 sample01(arg);
061 sample02();
062 }
063
064 //|
065 //|* Loading and storing
066 //|
067 //| There is nothing special with loading and storing data, it works exactly same
068 //| as in Ini class. But while loading data, Reg class will strip .REG special
069 //| values (double qoute around strings, type data from option, etc). So after
070 //| loading a .REG file, you can use it exactly same way as Ini class. Ofcource
071 //| if you store Reg class, it will put all above meta information int file, so
072 //| the result will be a valid .REG file. You don't need to worry about file
073 //| encoding, version in first line, etc,etc.
074 //|
075 //| Assume you have a .REG file, with the following section/key:
076 //|
077 //|+---+
078 //|[HKEY_CURRENT_USER\Software\ini4j-test\dwarfs\bashful]
079 //|@="bashful"
080 //|"weight"=hex(2):34,00,35,00,2e,00,37,00,00,00
081 //|"height"="98.8"
082 //|"age"=dword:00000043
083 //|"homePage"="http://snowwhite.tale/~bashful"
084 //|"homeDir"="/home/bashful"
085 //|+---+
086 //|
087 //| As you see, "weight" and "age" is not simlpe strings. The "height" is a REG_DWORD
088 //| type while "weight" is REG_EXPAND_SZ. Don't worry, Reg class take care about
089 //| type conversion, you will access these as with regular .ini files:
090 //{
091 void sample01(File file) throws IOException
092 {
093 Reg reg = new Reg(file);
094 Reg.Key hive = reg.get(Reg.Hive.HKEY_CURRENT_USER.toString());
095 Reg.Key bashful;
096
097 bashful = hive.lookup("Software", "ini4j-test", "dwarfs", "bashful");
098
099 // 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 }