XMLTest.java

Go to the documentation of this file.
00001 
00002 //
00003 // XML storage classes version 1.5
00004 //
00005 // XMLTest.java
00006 //
00007 // Copyright (c) 2004, 2005, 2007, 2010 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.XMLBool;
00046 import de.mfuchs.xmlstorage.XMLChildrenFilter;
00047 import de.mfuchs.xmlstorage.XMLDoc;
00048 import de.mfuchs.xmlstorage.XMLInt;
00049 import de.mfuchs.xmlstorage.XMLIntRef;
00050 import de.mfuchs.xmlstorage.XMLNode;
00051 import de.mfuchs.xmlstorage.XMLPos;
00052 import de.mfuchs.xmlstorage.stream.XMLStreamReader;
00053 import de.mfuchs.xmlstorage.stream.XMLStreamWriter;
00054 
00055 /*
00056  * Created on 31.07.2004
00057  */
00058 
00059 /**
00060  * Examples used as tests for XMLStorage.
00061  * @author Martin Fuchs
00062  */
00063 public class XMLTest
00064 {
00065     /**
00066      * Test 1 for XMLStorage: navigate in XML data.
00067      * @throws Exception
00068      */
00069     @Test public void test1() throws Exception
00070     {
00071 //      String path = "poem.xml";
00072 //      String path = "machines.xml";
00073         String path = "packets.xml";
00074 
00075         XMLDoc doc = new XMLDoc();
00076 
00077         doc.readFile(path);
00078 
00079         XMLPos pos = new XMLPos(doc);
00080         pos.goDown("get_packets");
00081 
00082         //pos.putSubattrib("options", "hide-inactive", String.valueOf(false));
00083 
00084         XMLChildrenFilter filter = new XMLChildrenFilter(pos, "packet");
00085 
00086         for(XMLNode node: filter) {
00087             XMLIntRef dbId = new XMLIntRef(node, "db_id");
00088             //db_id.assign(-999);
00089             System.out.println("db_id: " + dbId.intValue());
00090 
00091             int x = node.getInt("db_id", 12345).intValue();
00092             System.out.println("x: " + x);
00093 
00094             System.out.println(node + " \"" + node.get("name") + "\"");
00095         }
00096 
00097         pos.back();
00098 
00099         doc.writeFile("out.xml");
00100 
00101         pos = new XMLPos(doc);
00102 
00103 //      if (pos.go("get_packets/packet"))
00104 //      if (pos.go("/get_packets/packet"))
00105 //      if (pos.go("/get_packets/packet[5]"))
00106         if (pos.go("/get_packets/packet[@db_id=\"7628\"]"))
00107 //      if (pos.go("get_machines/machine[@wsid=\"client2\"]"))
00108         {
00109 
00110             pos.back();
00111         }
00112     }
00113 
00114 
00115     /**
00116      * Write an example XML file.
00117      * @throws Exception
00118      */
00119     @Test public void test2Write() throws Exception
00120     {
00121         XMLDoc doc = new XMLDoc();
00122         XMLPos pos = new XMLPos(doc);
00123 
00124          // Zusammenstellen der XML-Struktur im Dokument 'doc'
00125         pos.create("PARAMS");
00126           pos.create("TEXT");
00127             pos.setContent("Testgruppe");
00128           pos.back(); // </TEXT>
00129 
00130           pos.create("COLOR");
00131             pos.create("RED");
00132             pos.setContent("234");
00133             pos.back(); // </RED>
00134 
00135             pos.create("GREEN");
00136             pos.setContent("22");
00137             pos.back(); // </GREEN>
00138 
00139             pos.create("BLUE");
00140             pos.setContent("33");
00141             pos.back(); // </BLUE>
00142           pos.back(); // </COLOR>
00143 
00144           pos.create("FONT");
00145             pos.create("FONT_FILENAME");
00146             pos.setContent("Arial.ttf");
00147             pos.back(); // </FONT_FILENAME>
00148 
00149             pos.create("SIZE");
00150             pos.setContent("12");
00151             pos.back(); // </SIZE>
00152           pos.back(); // </FONT>
00153         pos.back(); // </PARAMS>
00154 
00155          // Schreiben der XML-Datei
00156         doc.writeFile("sandra1.xml");
00157     }
00158 
00159     /**
00160      * Read back the example file and print some of its content.
00161      * @throws Exception
00162      */
00163     @Test public void test2Read() throws Exception
00164     {
00165         XMLDoc doc = new XMLDoc();
00166 
00167          // Einlesen des XML-Files
00168         doc.readFile("sandra1.xml");
00169 
00170         XMLPos pos = new XMLPos(doc);
00171         String filename = "";
00172         int size = 0;
00173 
00174          // Abfrage eines Elementinhalts, beispielsweise von FONT_FILENAME
00175         if (pos.go("PARAMS/FONT")) {
00176             if (pos.goDown("FONT_FILENAME")) {
00177                 filename = pos.getContent();
00178                 pos.back();
00179             }
00180 
00181             if (pos.goDown("SIZE")) {
00182                 size = new XMLInt(pos.getContent()).intValue();
00183                 pos.back();
00184             }
00185 
00186             pos.back();
00187         }
00188 
00189          // Ausgabe des ermittelten Werts
00190         System.out.println("font filename: " + filename);
00191         System.out.println("font size: " + size);
00192 
00193         assertEquals("Font filename", "Arial.ttf", filename);
00194         assertEquals("Font size", 12, size);
00195     }
00196 
00197 
00198     /**
00199      * Test 3 of XMLStorage: write another example file.
00200      * @throws Exception
00201      */
00202     @Test public void test3Write() throws Exception
00203     {
00204         XMLDoc doc = new XMLDoc();
00205         XMLPos pos = new XMLPos(doc);
00206 
00207          // create the XML tree in memory
00208         pos.create("root");
00209 
00210           pos.create("foo");
00211             pos.setContent("bar");
00212           pos.back(); // </foo>
00213 
00214           pos.create("foo2");
00215             pos.put("attr1", "value1");
00216             pos.put("attr2", "value2");
00217 
00218             pos.create("foo3");
00219             pos.setContent("bar2");
00220             pos.back();
00221           pos.back();
00222 
00223         pos.back(); // </root>
00224 
00225          // write the XML file
00226         doc.writeFile("test.xml");
00227     }
00228 
00229 
00230     /**
00231      * Stream test 1 for XMLStreamReader.
00232      * @throws Exception
00233      */
00234     @Test public void testStreamingReader1() throws Exception
00235     {
00236         String path = "packets.xml";
00237 
00238         XMLStreamReader reader = new XMLStreamReader(path);
00239 
00240         reader.goDown("get_packets");
00241 
00242         if (reader.firstChild("packet")) do {
00243             XMLInt dbId = new XMLInt(reader, "db_id");
00244             System.out.println("db_id: " + dbId.intValue());
00245 
00246             int x = reader.getNode().getInt("db_id", 12345).intValue();
00247             System.out.println("x: " + x);
00248 
00249             System.out.println(reader + " \"" + reader.get("name") + "\"");
00250         } while(reader.nextSkipOthers("packet"));
00251 
00252         reader.back();
00253 
00254         reader.close();
00255     }
00256 
00257     /**
00258      * Stream test for XMLStreamWriter.
00259      * @throws Exception
00260      */
00261     @Test public void testStreamingWriter() throws Exception
00262     {
00263         XMLStreamWriter writer = new XMLStreamWriter("stream-example.xml");
00264 
00265         writer.create("PERSON");
00266 
00267           writer.create("NAME");
00268             writer.setContent("Mustername");
00269           writer.back(); // </NAME>
00270 
00271           writer.create("VORNAME");
00272             writer.setContent("Mustervorname");
00273           writer.back(); // </VORNAME>
00274 
00275           writer.create("ADRESSE");
00276             writer.put("private", new XMLBool(true));
00277             writer.setContent("Musteradresse");
00278           writer.back(); // </ADRESSE>
00279 
00280         writer.back(); // </PERSON>
00281 
00282         writer.close();
00283     }
00284 
00285     /**
00286      * Stream test 2 for XMLStreamReader.
00287      * @throws Exception
00288      */
00289     @Test public void testStreamingReader2() throws Exception
00290     {
00291         String path = "stream-example.xml";
00292 
00293         XMLStreamReader reader = new XMLStreamReader(path);
00294 
00295         if (reader.goDown("PERSON")) {
00296             if (reader.goDown("NAME")) {
00297                 String content = reader.getContent();
00298                 System.out.println("NAME content=" + content);
00299 
00300                 reader.back(); // </NAME>
00301             }
00302 
00303             if (reader.goDown("VORNAME")) {
00304                 String content = reader.getContent();
00305                 System.out.println("VORNAME content=" + content);
00306 
00307                 reader.back(); // </VORNAME>
00308             }
00309         }
00310 
00311         if (reader.goDown("ADRESSE")) {
00312             String priv = reader.get("private");
00313             String content = reader.getContent();
00314             System.out.println("ADRESSE.@private=" + priv);
00315             System.out.println("ADRESSE content=" + content);
00316 
00317             reader.back(); // </ADRESSE>
00318         }
00319 
00320         reader.back(); // </PERSON>
00321 
00322         reader.close();
00323     }
00324 
00325 
00326     /**
00327      * Main function of XMLTest.
00328      * 
00329      * @param args unused command line arguments
00330      */
00331     public static void main(String[] args)
00332     {
00333         XMLTest me = new XMLTest();
00334 
00335         try {
00336             me.test1();
00337 
00338             me.test2Write();
00339             me.test2Read();
00340 
00341             me.test3Write();
00342 
00343             me.testStreamingReader1();
00344             me.testStreamingReader2();
00345         } catch(Exception e) {
00346             e.printStackTrace();
00347         }
00348     }
00349 }