Example2.java

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.2
00004 //
00005 // Example2.java
00006 //
00007 // Copyright (c) 2005 Martin Fuchs <martin-fuchs@gmx.net>
00008 //
00009 
00010 
00011 /*
00012 
00013  All rights reserved.
00014 
00015  Redistribution and use in source and binary forms, with or without
00016  modification, are permitted provided that the following conditions are met:
00017 
00018  * Redistributions of source code must retain the above copyright
00019     notice, this list of conditions and the following disclaimer.
00020  * Redistributions in binary form must reproduce the above copyright
00021     notice, this list of conditions and the following disclaimer in
00022     the documentation and/or other materials provided with the
00023     distribution.
00024 
00025  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00028  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00029  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00030  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00031  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00032  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00033  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00034  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  POSSIBILITY OF SUCH DAMAGE.
00036 
00037 */
00038 
00039 package de.mfuchs;
00040 
00041 import static org.junit.Assert.assertEquals;
00042 
00043 import org.junit.Test;
00044 
00045 import de.mfuchs.xmlstorage.XMLDoc;
00046 import de.mfuchs.xmlstorage.XMLIntRef;
00047 import de.mfuchs.xmlstorage.XMLPos;
00048 import de.mfuchs.xmlstorage.XMLStringRef;
00049 
00050 
00051 /**
00052  * Example to use XMLStorage for writing and
00053  * reading XML files containing numbers.
00054  * 
00055  * @author martin
00056  */
00057 public class Example2
00058 {
00059     /**
00060      * Write an example XML file to disk.
00061      * @throws Exception
00062      */
00063     @Test public void testWrite() throws Exception
00064     {
00065         XMLDoc doc = new XMLDoc();
00066         XMLPos pos = new XMLPos(doc);
00067 
00068          // Zusammenstellen der XML-Struktur im Dokument 'doc'
00069         pos.create("PARAMS");
00070           pos.put("text", "Testgruppe");
00071 
00072           pos.create("COLOR");
00073             new XMLIntRef(pos, "red").assign(234);
00074             new XMLIntRef(pos, "green").assign(22);
00075             new XMLIntRef(pos, "blue").assign(33);
00076           pos.back(); // </COLOR>
00077 
00078           pos.create("FONT");
00079             new XMLStringRef(pos, "font-filename").assign("Arial.ttf");
00080             new XMLIntRef(pos, "size").assign(12);
00081             pos.back(); // </SIZE>
00082           pos.back(); // </FONT>
00083         pos.back(); // </PARAMS>
00084 
00085          // Schreiben der XML-Datei
00086         doc.writeFile("example2.xml");
00087     }
00088 
00089 
00090     /**
00091      * Read the example file back again.
00092      * @throws Exception
00093      */
00094     @Test public void testRead() throws Exception
00095     {
00096          // Einlesen des XML-Files
00097         XMLDoc doc = new XMLDoc("example2.xml");
00098 
00099         XMLPos pos = new XMLPos(doc);
00100         String filename = "";
00101 
00102          // Abfrage eines Attributwerts, beispielsweise von "font-filename"
00103         if (pos.go("PARAMS/FONT")) {
00104             filename = pos.get("font-filename");
00105             pos.back();
00106         }
00107 
00108          // Ausgabe des ermittelten Werts
00109         System.out.println("font filename: " + filename);
00110 
00111         assertEquals("Arial.ttf", filename); 
00112     }
00113 
00114 
00115     /**
00116      * Main function of Example2.
00117      * @param args unused command line arguments
00118      */
00119     public static void main(String[] args)
00120     {
00121         Example2 me = new Example2();
00122 
00123         try {
00124             me.testWrite();
00125             me.testRead();
00126         } catch(Exception e) {
00127             e.printStackTrace();
00128         }
00129     }
00130 }