Example1.java

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.2
00004 //
00005 // Example1.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.XMLPos;
00047 
00048 /**
00049  * Example for writing and reading a XML file.
00050  * 
00051  * @author martin
00052  */
00053 public class Example1
00054 {
00055     /**
00056      * Write exampe XML file.
00057      * @throws Exception
00058      */
00059     @Test public void testWrite() throws Exception
00060     {
00061         XMLDoc doc = new XMLDoc();
00062         XMLPos pos = new XMLPos(doc);
00063 
00064          // Zusammenstellen der XML-Struktur im Dokument 'doc'
00065         pos.create("PARAMS");
00066           pos.create("TEXT");
00067             pos.setContent("Testgruppe\nZeile 2", true);
00068           pos.back(); // </TEXT>
00069 
00070           pos.create("COLOR");
00071             pos.create("RED");
00072             pos.setContent("234");
00073             pos.back(); // </RED>
00074 
00075             pos.create("GREEN");
00076             pos.setContent("22");
00077             pos.back(); // </GREEN>
00078 
00079             pos.create("BLUE");
00080             pos.setContent("33");
00081             pos.back(); // </BLUE>
00082           pos.back(); // </COLOR>
00083 
00084           pos.create("FONT");
00085             pos.create("FONT_FILENAME");
00086             pos.setContent("Arial.ttf");
00087             pos.back(); // </FONT_FILENAME>
00088 
00089             pos.create("SIZE");
00090             pos.setContent("12");
00091             pos.back(); // </SIZE>
00092           pos.back(); // </FONT>
00093         pos.back(); // </PARAMS>
00094 
00095          // Schreiben der XML-Datei
00096         doc.writeFile("example1.xml");
00097     }
00098 
00099 
00100     /**
00101      * Read the example file back again.
00102      * @throws Exception
00103      */
00104     @Test public void testRead() throws Exception
00105     {
00106         XMLDoc doc = new XMLDoc();
00107 
00108          // Einlesen des XML-Files
00109         doc.readFile("example1.xml");
00110 
00111         String txt = "";
00112         String filename = "";
00113 
00114         XMLPos pos = new XMLPos(doc);
00115 
00116         if (pos.goDown("PARAMS")) {
00117             if (pos.goDown("TEXT")) {
00118                 txt = pos.getContent();
00119                 pos.back();
00120             }
00121             pos.back();
00122         }
00123 
00124          // Abfrage eines Elementinhalts, beispielsweise von FONT_FILENAME
00125         if (pos.go("PARAMS/FONT/FONT_FILENAME")) {
00126             filename = pos.getContent();
00127             pos.back();
00128         }
00129 
00130          // Ausgabe der ermittelten Werte
00131         System.out.println("TEXT: " + txt);
00132         System.out.println("font filename: " + filename);
00133 
00134         assertEquals("Arial.ttf", filename); 
00135     }
00136 
00137 
00138     /**
00139      * Main function of Example1.
00140      * @param args unused command line arguments
00141      */
00142     public static void main(String[] args)
00143     {
00144         Example1 me = new Example1();
00145 
00146         try {
00147             me.testWrite();
00148             me.testRead();
00149         } catch(Exception e) {
00150             e.printStackTrace();
00151         }
00152     }
00153 }