Fixed loading of plug ins when multiple plug in class sets used. Especially H.323...
[pwlib.git] / samples / pxml / main.cxx
blob799406c21a9bc5546701baa3a4a128c32e7d02b9
1 /*
2 * main.cxx
4 * PWLib application source file for PxmlTest
6 * Main program entry point.
8 * Copyright 2002 David Iodice.
10 * $Log$
11 * Revision 1.1 2002/03/07 01:56:56 robertj
12 * Added XML sample/test program.
16 #include <ptlib.h>
17 #include "main.h"
20 PCREATE_PROCESS(PxmlTest);
24 PxmlTest::PxmlTest()
25 : PProcess("XML Testing Guru", "PxmlTest", 1, 0, AlphaCode, 1)
29 static const PString XML_ID("<?xml version=\"1.0\"?>");
30 static const PString LEThdr("<s:Envelope xmlns:s=\"http://schemas.letter.org/letter/envelope/\" s:encodingStyle=\"http://schemas.letter.org/letter/encoding/\"><s:Body>");
31 static const PString LETtrl("</s:Body></s:Envelope>");
32 static const PString SetNumHdr("<u:SetNum xmlns:u=\"urn:schemas-dustbowl:1\"><Num>");
33 static const PString SetNumTrl("</Num></u:SetNum>");
35 void printElement(int i,PXMLObject * el)
37 if (el->IsElement())
39 cout << "("<<i++<<") "<< ((const PXMLElement *)el)->GetName() << endl;
40 if (((const PXMLElement *)el)->HasAttributes())
42 for (PINDEX j=0; j< ((const PXMLElement *)el)->GetNumAttributes(); ++j)
44 cout << " " << ((const PXMLElement *)el)->GetKeyAttribute(j) << "=" << ((const PXMLElement *)el)->GetDataAttribute(j);
47 cout << " " << ((const PXMLElement *)el)->GetData() << endl;
48 if (((const PXMLElement *)el)->HasSubObjects())
50 PXMLObjectArray obs = ((const PXMLElement *)el)->GetSubObjects();
51 for (PINDEX o=0;o<obs.GetSize();++o) printElement(i,&obs[o]);
56 void PxmlTest::Main()
58 PString t("Color");
59 int num=5;
60 PString COLORVAL("<Color>5</Color>");
61 PString COLORT1("<Color>");
62 PString COLORT2("</Color>");
63 PString COLORT3("<Color/>");
65 PString ns(PString::Signed,num);
66 PAssert((COLORT1 == PXML::CreateStartTag(t)),"PXML::CreateStartTag(t) failed");
67 PAssert((COLORT2 == PXML::CreateEndTag(t)),"PXML::CreateEndTag(t) failed");
68 PAssert((COLORT3 == PXML::CreateTagNoData(t)),"PXML::CreateTagNoData(t) failed");
69 PAssert((COLORVAL == PXML::CreateTag(t,ns)),"PXML::CreateTag(t,ns) failed");
71 PString ch = LEThdr + SetNumHdr + "23" + SetNumTrl + LETtrl;
73 PXML xml(ch,PXML::Indent |PXML::NewLineAfterElement+PXML::NoIgnoreWhiteSpace);
74 PStringStream s;
75 s << xml;
77 //can we recreate the XML?
79 PXML revamp;
80 PString ROOTELE("s:Envelope");
81 PString ROOTATKEY1("xmlns:s");
82 PString ROOTATDAT1("http://schemas.letter.org/letter/envelope/");
83 PString ROOTATKEY2("s:encodingStyle");
84 PString ROOTATDAT2("http://schemas.letter.org/letter/encoding/");
85 PAssert((ROOTELE == xml.GetRootElement()->GetName()),"Root element name not as expected");
86 if (xml.GetRootElement()->HasAttributes()) {
87 for (PINDEX a=0; a<xml.GetRootElement()->GetNumAttributes(); ++a) {
88 PAssert((ROOTATKEY1 == xml.GetRootElement()->GetKeyAttribute(a) ||
89 ROOTATKEY2 == xml.GetRootElement()->GetKeyAttribute(a)),"Root element attribute key not as expected");
90 PAssert((ROOTATDAT1 == xml.GetRootElement()->GetDataAttribute(a) ||
91 ROOTATDAT2 == xml.GetRootElement()->GetDataAttribute(a)),"Root element attribute data not as expected");
94 PAssert((PString() == xml.GetRootElement()->GetData()),"Root element data not as expected");
96 #if 0
97 //===
98 // An example of printing elements -- see the function defined above
99 //===
100 for (PINDEX i=0; i<xml.GetNumElements(); ++i)
102 printElement(i,xml.GetElement(i));
106 //===
107 // Just checking reloading the same data in a different format
108 //===
109 PStringStream rs; xml.GetRootElement()->PrintOn(rs, -1, 0);
110 cout << rs << endl;
111 revamp.Load(rs);;
112 cout << "Revamped XML" << revamp << endl;
113 #endif
115 //===
116 // Extract a subset of the XML
117 // This can be used when the "data" of an XML element
118 // is XML and you don't need that parsed yet :-)
119 //===
120 PStringStream ss;
121 xml.GetElement(0)->PrintOn(ss, -1, 0);
123 PString EXCERPT("<s:Body>" + SetNumHdr + "23" + SetNumTrl + "</s:Body>");
124 PAssert((EXCERPT == ss),"XML subset data not as expected");
127 PXMLElement el(NULL, t);
128 PXMLData * d = new PXMLData(NULL,ns);
129 el.AddSubObject(d);
130 PAssert((PString("Color") == el.GetName()),"Element name not as expected");
131 PAssert((PString("5") == el.GetData()),"Element data not as expected");
133 #if 0
134 //===
135 // Checkout these examples of the PXMLSettings class
137 // Constructor with data and options
138 //===
139 PXMLSettings dgi(ch,PXML::Indent | PXML::NewLineAfterElement | PXML::NoIgnoreWhiteSpace);
140 cout << "PXMLSettings: " << dgi << endl;
141 // Default Constructor
142 PXMLSettings opts;
143 PString optsect("Options");
144 PString comType("Comm Type");
145 PString val("FTP");
146 opts.SetAttribute(optsect,comType,val);
147 cout << "attributes==>" << opts.GetAttribute(optsect,comType) << "<==" << endl;
148 cout << "PXMLSettings: " << opts;
149 cout << "done" << endl;
150 #endif
152 // Constructor with PConfig and String
153 PFilePath fp("cfg.txt");
154 PConfig cfg(fp, "Options");
155 PXMLSettings xmlcfg(cfg);
156 PStringStream xc;
157 xc << xmlcfg;
158 PFilePath fp2("cfgOut.txt");
159 PConfig * cfg2; cfg2 = new PConfig(fp2, "Root");
160 xmlcfg.ToConfig(*cfg2);
161 delete cfg2;
162 PTextFile tfp(fp2);
163 PString fxc;
164 char xcc;
165 while(tfp.Read((void *)&xcc,1)) fxc += xcc;
166 PXMLSettings xmlcfg2(cfg);
167 PStringStream xc2;
168 xc2 << xmlcfg2;
169 PAssert((xc2 == xc),"Config not as expected");
171 cout << "*** Test Passed ***" << endl;
174 // End of File ///////////////////////////////////////////////////////////////