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.Ini; 019 import org.ini4j.IniPreferences; 020 021 import org.ini4j.test.DwarfsData; 022 023 import static org.junit.Assert.*; 024 025 import java.io.File; 026 import java.io.IOException; 027 028 import java.util.prefs.Preferences; 029 030 //<editor-fold defaultstate="collapsed" desc="apt documentation"> 031 //| 032 //| ------------- 033 //| Preferences Tutorial 034 //| 035 //|Preferences Tutorial 036 //| 037 //| The purpose of this document is to familiarize the reader with the usage of 038 //| the [ini4j] library's Preferences interface. Each chapter contains all the 039 //| necessary code portions and explanation for a given function. 040 //| 041 //| Code sniplets in this tutorial tested with the following .ini file: 042 //| {{{../sample/dwarfs.ini.html}dwarfs.ini}} 043 //| 044 //| As soon as the Preferences object is created it functions as a standard Preferences node, and should be 045 //| used as such. Implicitly only new nodes can be created in the root node (these will be the sections). 046 //| In the first level nodes (sections) only values can be created (these will be the options). 047 //| 048 //| In the case of an invalid operation an <<<UnsupportedOperationException>>> type runtime exception is generated. 049 //| This happens if we try to set a value at the root node or to create a node on the second level, 050 //| since these operations cannot be interpreted on the whole .ini file under Preferences. 051 //| 052 //</editor-fold> 053 public class PrefsTutorial extends AbstractTutorial 054 { 055 public static void main(String[] args) throws Exception 056 { 057 new PrefsTutorial().run(filearg(args)); 058 } 059 060 protected void run(File arg) throws Exception 061 { 062 Ini ini = new Ini(arg.toURI().toURL()); 063 064 sample01(ini); 065 } 066 067 //| 068 //|* Reading and writing values 069 //| 070 //| Values can read and write like any other Preferences node, there is no 071 //| differences. 072 //{ 073 void sample01(Ini ini) throws IOException 074 { 075 Preferences prefs = new IniPreferences(ini); 076 Preferences bashful = prefs.node("bashful"); 077 String home = bashful.get("homeDir", "/home"); 078 int age = bashful.getInt("age", -1); 079 080 bashful.putDouble("weight", 55.6); 081 082 //} 083 assertEquals(DwarfsData.bashful.homeDir, bashful.get("homeDir", null)); 084 assertEquals(DwarfsData.bashful.age, bashful.getInt("age", -1)); 085 assertEquals(55.6, bashful.getDouble("weight", -1), 0.001); 086 } 087 }