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
20 import org.ini4j.sample.Dwarfs;
21
22 import org.ini4j.test.DwarfsData;
23
24 import static org.junit.Assert.*;
25
26 import java.io.File;
27
28 import java.util.Set;
29
30 //<editor-fold defaultstate="collapsed" desc="apt documentation">
31 //|
32 //| -------------
33 //| OptionMap Tutorial
34 //|
35 //|OptionMap Tutorial - more than just String,String map
36 //|
37 //| Option is a name/value pair stored in OptionMap. But OptionMap adds a lot of
38 //| usefull data access methods than simple get. OptionMap is base interface for
39 //| both Ini.Section, Registry.Key and Options classes, so this tutorial will
40 //| usefull for all of these.
41 //|
42 //| So in samples bellow you can use either Ini.Section, Registry.Key or Options
43 //| classes instead of OptionMap interface, because these classes implements
44 //| OptionMap.
45 //|
46 //| Code sniplets in this tutorial tested with the following files:
47 //| {{{../sample/dwarfs.ini.html}dwarfs.ini}}
48 //| {{{../sample/dwarfs.opt.html}dwarfs.opt}}
49 //|
50 //</editor-fold>
51 public class OptionMapTutorial extends AbstractTutorial
52 {
53 public static void main(String[] args) throws Exception
54 {
55 new OptionMapTutorial().run(filearg(args));
56 }
57
58 @Override protected void run(File arg) throws Exception
59 {
60 Ini ini = new Ini(arg.toURI().toURL());
61
62 sample01(ini.get(Dwarfs.PROP_HAPPY));
63 sample03(ini);
64 sample04(ini);
65 }
66
67 //|* Data model
68 //|
69 //| OptionMap implements Map\<String,String\>, so you can access options using
70 //| standard collection api.
71 //{
72 void sample01(Ini.Section section)
73 {
74
75 //
76 // read some values
77 //
78 String age = section.get("age");
79 String weight = section.get("weight");
80 String homeDir = section.get("homeDir");
81
82 // get all option names
83 Set<String> optionNames = section.keySet();
84
85 //}
86 assertEquals(String.valueOf(DwarfsData.happy.age), age);
87 assertEquals(String.valueOf(DwarfsData.happy.weight), weight);
88 assertEquals(String.valueOf(DwarfsData.happy.homeDir), homeDir);
89 }
90
91 //|
92 //|* Macro/variable substitution
93 //|
94 //| To get a value, besides <<<get()>>> you can also
95 //| use <<<fetch()>>> which resolves any occurrent $\{section/option\} format
96 //| variable references in the needed value.
97 //|
98 //{
99 void sample03(Ini ini)
100 {
101 Ini.Section dopey = ini.get("dopey");
102
103 // get method doesn't resolve variable references
104 String weightRaw = dopey.get("weight"); // = ${bashful/weight}
105 String heightRaw = dopey.get("height"); // = ${doc/height}
106
107 // to resolve references, you should use fetch method
108 String weight = dopey.fetch("weight"); // = 45.7
109 String height = dopey.fetch("height"); // = 87.7
110
111 //}
112 //| Assuming we have an .ini file with the following sections:
113 //|
114 //|+--------------+
115 //| [dopey]
116 //| weight = ${bashful/weight}
117 //| height = ${doc/height}
118 //|
119 //|[bashful]
120 //| weight = 45.7
121 //| height = 98.8
122 //|
123 //| [doc]
124 //| weight = 49.5
125 //| height = 87.7
126 //|+--------------+
127 //|
128 assertEquals(DwarfsData.INI_DOPEY_WEIGHT, weightRaw);
129 assertEquals(DwarfsData.INI_DOPEY_HEIGHT, heightRaw);
130 assertEquals(String.valueOf(DwarfsData.dopey.weight), weight);
131 assertEquals(String.valueOf(DwarfsData.dopey.height), height);
132 }
133
134 //|
135 //|* Multi values
136 //|
137 //| \[ini4j\] library introduces MultiMap interface, which is extends normal
138 //| Map, but allows multiply values per keys. You can simply index values for
139 //| a given key, similar to indexed properties in JavaBeans api.
140 //|
141 //{
142 void sample04(Ini ini)
143 {
144 Ini.Section sneezy = ini.get("sneezy");
145 String n1 = sneezy.get("fortuneNumber", 0); // = 11
146 String n2 = sneezy.get("fortuneNumber", 1); // = 22
147 String n3 = sneezy.get("fortuneNumber", 2); // = 33
148 String n4 = sneezy.get("fortuneNumber", 3); // = 44
149
150 // ok, lets do in it easier...
151 int[] n = sneezy.get("fortuneNumber", int[].class);
152 //}
153 }
154 }