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 */
016package org.ini4j.tutorial;
017
018import org.ini4j.Ini;
019
020import org.ini4j.sample.Dwarfs;
021
022import org.ini4j.test.DwarfsData;
023
024import static org.junit.Assert.*;
025
026import java.io.File;
027
028import java.util.Set;
029
030//<editor-fold defaultstate="collapsed" desc="apt documentation">
031//|
032//|                -------------
033//|                OptionMap Tutorial
034//|
035//|OptionMap Tutorial - more than just String,String map
036//|
037//| Option is a name/value pair stored in OptionMap. But OptionMap adds a lot of
038//| usefull data access methods than simple get. OptionMap is base interface for
039//| both Ini.Section, Registry.Key and Options classes, so this tutorial will
040//| usefull for all of these.
041//|
042//| So in samples bellow you can use either Ini.Section, Registry.Key or Options
043//| classes instead of OptionMap interface, because these classes implements
044//| OptionMap.
045//|
046//| Code sniplets in this tutorial tested with the following files:
047//| {{{../sample/dwarfs.ini.html}dwarfs.ini}}
048//| {{{../sample/dwarfs.opt.html}dwarfs.opt}}
049//|
050//</editor-fold>
051public class OptionMapTutorial extends AbstractTutorial
052{
053    public static void main(String[] args) throws Exception
054    {
055        new OptionMapTutorial().run(filearg(args));
056    }
057
058    @Override protected void run(File arg) throws Exception
059    {
060        Ini ini = new Ini(arg.toURI().toURL());
061
062        sample01(ini.get(Dwarfs.PROP_HAPPY));
063        sample03(ini);
064        sample04(ini);
065    }
066
067//|* Data model
068//|
069//| OptionMap implements Map\<String,String\>, so you can access options using
070//| standard collection api.
071//{
072    void sample01(Ini.Section section)
073    {
074
075        //
076        // read some values
077        //
078        String age = section.get("age");
079        String weight = section.get("weight");
080        String homeDir = section.get("homeDir");
081
082        // get all option names
083        Set<String> optionNames = section.keySet();
084
085//}
086        assertEquals(String.valueOf(DwarfsData.happy.age), age);
087        assertEquals(String.valueOf(DwarfsData.happy.weight), weight);
088        assertEquals(String.valueOf(DwarfsData.happy.homeDir), homeDir);
089    }
090
091//|
092//|* Macro/variable substitution
093//|
094//| To get a value, besides <<<get()>>> you can also
095//| use <<<fetch()>>> which resolves any occurrent $\{section/option\} format
096//| variable references in the needed value.
097//|
098//{
099    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}