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