Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

xml.d

Go to the documentation of this file.
00001 module mango.test.xml;
00002 
00003 import  mango.io.Uri,
00004         mango.io.Stdout,
00005         mango.io.Token,
00006         mango.io.Reader,
00007         mango.io.Writer,
00008         mango.io.Buffer,
00009         mango.io.Socket,
00010         mango.io.Conduit,
00011         mango.io.FilePath,
00012         mango.io.FileStyle,
00013         mango.io.FileProxy,
00014         mango.io.Tokenizer,
00015         mango.io.TextReader,
00016         mango.io.TextWriter,
00017         mango.io.FileBucket,
00018         mango.io.FileConduit,
00019         mango.io.TextLayout,
00020         mango.io.TextFormat,
00021         mango.io.ColumnWriter,
00022         mango.io.DisplayWriter,
00023         mango.io.PickleReader,
00024         mango.io.PickleWriter,
00025         mango.io.SocketConduit,
00026         mango.io.SocketListener,
00027         mango.io.ArrayAllocator,
00028         mango.io.PickleRegistry,
00029         mango.io.DatagramSocket,
00030         mango.io.MulticastSocket;
00031 
00032         // need these for reading & writing classes
00033 import  mango.io.model.IReader,
00034         mango.io.model.IWriter,
00035         mango.io.model.IPickle;
00036 
00037 import mango.log.Logger;
00038 import mango.log.ConsoleAppender;
00039 
00040 import  mango.icu.UMango,
00041         mango.icu.UString,
00042         mango.icu.UDomainName,
00043         mango.icu.UStringPrep,
00044         mango.icu.UConverter,
00045         mango.icu.USet,
00046         mango.icu.UCollator,
00047         mango.icu.UTimeZone,
00048         mango.icu.UEnumeration,
00049         mango.icu.UMessageFormat;
00050 
00051 import  mango.xml.dom.all,
00052   mango.xml.dom.parser.teqXML,
00053   mango.xml.rpc.client,
00054   mango.xml.rpc.objects,
00055   mango.xml.rpc.server;
00056 
00057 import mango.servlet.ServletProvider;
00058 import mango.http.server.HttpServer;
00059 
00060 
00061 /*******************************************************************************
00062 
00063 
00064 *******************************************************************************/
00065 
00066 
00067 void testRPCClient ()
00068 {
00069   XmlRpcClient client = new XmlRpcClient("http://scripts.incutio.com/xmlrpc/simpleserver.php");
00070   MethodCall mc = new MethodCall(new UString("test.getTime"));
00071   MethodResponse mr = client.sendMethodCall(mc);
00072   Stdout.put("Time: ").put(mr.getParams()[0].getString()).cr();
00073 }
00074 
00075 
00076 /*******************************************************************************
00077 
00078 
00079 *******************************************************************************/
00080 
00081 
00082 void testRPCServer ()
00083 {       
00084   MethodResponse testMethod(MethodCall mc) {
00085     MethodResponse mr = new MethodResponse();
00086     Value val = new Value(new UString("means nothing"));
00087     mr.appendParam(val);
00088     return mr;
00089   }
00090   Logger logger = Logger.getLogger ("neuralNexus");
00091   logger.setLevel(Logger.Level.Trace);
00092   logger.addAppender(new ConsoleAppender());
00093   
00094   //The local port to bind to
00095   InternetAddress bindTo = new InternetAddress(8080);
00096   
00097   //The ServletProvider
00098   ServletProvider sp = new ServletProvider();
00099   
00100   //This is the nnServer in servlet form
00101   XmlRpcServlet s = new XmlRpcServlet(logger);
00102   s.addCallHandler(new UString("test.getTime"), &testMethod);
00103   IRegisteredServlet irs = sp.addServlet (s, "XmlRpc");
00104   sp.addMapping ("/RPC2", irs);
00105   
00106   //The HTTP Server
00107   HttpServer httpServer = new HttpServer(sp, bindTo, 1, logger);
00108   
00109   //Go!
00110   httpServer.start();
00111 
00112   XmlRpcClient client = new XmlRpcClient("http://localhost:8080/RPC2");
00113   MethodCall mc = new MethodCall(new UString("test.getTime"));
00114   MethodResponse mr = client.sendMethodCall(mc);
00115   Stdout.put("Time: ").put(mr.getParams()[0].getString()).cr();
00116 
00117   delete httpServer;
00118 }
00119 
00120 
00121 /*******************************************************************************
00122 
00123 
00124 *******************************************************************************/
00125 
00126 void testDOM ()
00127 {
00128         Document doc = new DocumentImpl(null);
00129         Element root = doc.createElement(new UString("root"));
00130         Element text = doc.createElement(new UString("text"));
00131         root.appendChild(text);
00132         CDATASection data = doc.createCDATASection(new UString("this is my text"));
00133         text.appendChild(data);
00134         Element secText = doc.createElement(new UString("text"));
00135         assert (secText !== null);
00136         root.appendChild(secText);
00137         assert (root.childNodes.length == 2);
00138         assert (root.childNodes.item(0) === text);
00139         assert (root.childNodes.item(1) === secText);
00140         assert (cast(Element)secText.parentNode === root);
00141         assert (cast(Element)secText.previousSibling !== null);
00142         assert (cast(Element)text.nextSibling === secText);
00143 
00144         Element top = doc.createElement(new UString("top"));
00145         root.insertBefore(top, text);
00146         assert (root.childNodes.length == 3);
00147         assert (root.childNodes.item(0) === top);
00148         assert (root.childNodes.item(1) === text);
00149         assert (root.childNodes.item(2) === secText);
00150         assert (text.previousSibling === top);
00151 }
00152 
00153 /*******************************************************************************
00154 
00155 
00156 *******************************************************************************/
00157 
00158 char[] xmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
00159 <!--A collection of recipies-->
00160 <collection>
00161   <description>
00162      Some recipes used for the XML tutorial.
00163   </description>
00164   <recipe>
00165     <![CDATA[<title>Beef Parmesan with Garlic Angel Hair Pasta</title>]]>
00166     <ingredient name=\"beef cube steak\" amount=\"1.5\" unit=\"pound\"/>
00167     <preparation>
00168       <step>
00169         Preheat oven to 350 degrees F (175 degrees C).
00170       </step>
00171     </preparation>
00172     <comment>
00173       Make the meat ahead of &quot;time&quot;, &amp; refrigerate over night, the acid in the
00174       tomato sauce will tenderize the meat even more. If you do this, save the
00175       mozzarella till the last minute.
00176     </comment>
00177     <nutrition calories='11\"67' fat=\"23\" carbohydrates=\"45\" protein=\"32\"/>
00178   </recipe>
00179 </collection>";
00180 
00181 void testTeqParser (char[] file=null, char[] outfile=null)
00182 {       
00183   IBuffer buff;
00184   if (file is null) {
00185         buff = new GrowableBuffer(xmlString.length);
00186         buff.setContent(xmlString, xmlString.length);
00187   } else {
00188     FileConduit fc = new FileConduit(file);
00189     buff = fc.createBuffer();
00190   }
00191   Document doc = TeqXML.Parse(buff);
00192   doc.dump(Stdout, 0);
00193   Stdout.cr();
00194   Stdout.setEncoder (new StringEncoder16 ("utf8"));
00195   Stdout.setEncoder (new StringEncoder32 ("utf8"));
00196   if (outfile) {
00197     FileConduit fc = new FileConduit(outfile, FileStyle.ReadWriteCreate);
00198     TeqXML.Write(doc, fc.createBuffer(), "utf8");
00199     TeqXML.Write(doc, Stdout.getBuffer(), "utf8");
00200   } else {
00201     TeqXML.Write(doc, Stdout.getBuffer(), "utf8");
00202   }
00203   Stdout.cr().flush();
00204 }
00205 
00206 void testEnc() {
00207   FileConduit fc = new FileConduit("test.out", FileStyle.ReadWriteCreate);
00208   IWriter w = new Writer(fc); 
00209   w.setEncoder (new StringEncoder8 ("utf8"));
00210   w.setEncoder (new StringEncoder16 ("utf8"));
00211   w.setEncoder (new StringEncoder32 ("utf8"));
00212   w.put("<?xml version='1.0'?>");
00213   w.put("<element").put(' ').put("/>");
00214   w.cr().flush();
00215 }
00216 
00217 void main(char[][] args) {
00218   try {
00219     testRPCServer();
00220     testRPCClient();
00221     testDOM();
00222     //testEnc();
00223     if (args.length == 3)
00224       testTeqParser(args[1], args[2]);
00225     else if (args.length == 2)
00226       testTeqParser(args[1]);
00227     else
00228       testTeqParser();
00229   } catch (Object o) {
00230     printf(o.toString() ~ "\n\0");
00231   }
00232 }

Generated on Sat Apr 9 20:11:30 2005 for Mango by doxygen 1.3.6