Back to index

scribus-ng  1.3.4.dfsg+svn20071115
desaxe_test.cpp
Go to the documentation of this file.
00001 /*
00002  *  desaxe_test.h
00003  *  
00004  *
00005  *  Created by Andreas Vox on 02.06.06.
00006  *  This example file is in the public domain.
00007  *
00008  */
00009 
00010 #include <iostream.h>
00011 #include <qfile.h>
00012 #include <qfileinfo.h>
00013 #include <qdom.h>
00014 
00015 #include "digester.h"
00016 #include "actions.h"
00017 #include "simple_actions.h"
00018 
00019 class ELEM {
00020 public:
00021        ELEM(const Xml_string& n) : name_(n), attributes(), children(), text() {}
00022        
00023        void setName(const Xml_string& n) 
00024        { 
00025               name_ = n; 
00026        }
00027        
00028        const Xml_string& name() 
00029        { 
00030               return name_; 
00031        }
00032        
00033        void setAttribute(const Xml_string& key, const Xml_string& val)
00034        {
00035               attributes[key] = val;
00036        }
00037        
00038        void addElem(ELEM* child)
00039        {
00040 //            cerr << "adding child: " << child << "\n";
00041               children.push_back(child);
00042        }
00043               
00044        void addText(const Xml_string& txt)
00045        {
00046               text += txt;
00047        }
00048 
00049        void print()
00050        {
00051               std::cout << "<" << name_ << " ";
00052               Xml_attr::iterator it;
00053               for (it=attributes.begin(); it != attributes.end(); ++it)
00054                      std::cout << Xml_key(it) << "=\"" << Xml_data(it) << "\" ";
00055               std::cout << ">\n" << text << "\n";
00056               
00057               for (uint i=0; i < children.size(); ++i)
00058                      children[i]->print();
00059               std::cout << "</" << name_ << ">\n";
00060        }
00061 
00062 private:
00063        Xml_string name_;
00064        Xml_attr attributes;
00065        std::vector<ELEM*> children;
00066        Xml_string text;
00067 };
00068 
00069 
00070 void dummyDoc(desaxe::Digester& dig)
00071 {
00072        dig.reset();
00073        
00074        Xml_attr attr;
00075        dig.begin("doc", attr);
00076        attr["level"] = "1";
00077        dig.chars("aha.");
00078        dig.begin("elem", attr);
00079        attr["level"] = "2";
00080        dig.chars("soso.");
00081        dig.begin("elem", attr);
00082        attr["level"] = "3";
00083        dig.chars("alpha ");
00084        dig.end("elem");
00085        dig.chars("bravo ");
00086        dig.end("elem");
00087        dig.chars("charly ");
00088        attr["level"] = "2";
00089        attr["test"] = "bingo";
00090        dig.begin("elem", attr);
00091        dig.chars("delta ");
00092                       attr["level"] = "3";
00093                 dig.begin("elem", attr);
00094                    dig.chars("echo ");
00095                             dig.end("elem");
00096                             dig.chars("foxtrott ");     
00097                             dig.end("elem");
00098                             attr["test"] = "naja";
00099                             dig.chars("golf ");
00100                             attr["level"] = "2";
00101                             dig.begin("elem", attr);
00102                             dig.chars("hotel ");
00103                             attr["level"] = "3";
00104                             dig.begin("elem", attr);
00105                             dig.chars("india ");
00106                             dig.end("elem");
00107                             dig.chars("juliet ");
00108                             dig.end("elem");
00109                             dig.chars("kilo ");
00110                             dig.end("doc");
00111 }                           
00112 
00113 bool loadRawText(const QString & filename, QCString & buf)
00114 {
00115        bool ret = false;
00116        QFile f(filename);
00117        QFileInfo fi(f);
00118        if (fi.exists())
00119        {
00120               QCString tempBuf(f.size() + 1);
00121               if (f.open(IO_ReadOnly))
00122               {
00123                      unsigned int bytesRead = f.readBlock(tempBuf.data(), f.size());
00124                      tempBuf[bytesRead] = '\0';
00125                      ret = bytesRead == f.size();
00126                      if (ret)
00127                             buf = tempBuf; // sharing makes this efficient
00128               }
00129        }
00130        if (f.isOpen())
00131               f.close();
00132        return ret;
00133 }
00134 
00135 
00136 int main(int argc, char** argv)
00137 {
00138        using namespace desaxe;
00139        
00140        Digester dig;
00141        Action fac = FactoryWithName<ELEM>();
00142        Action s_attr = SetAttributes<ELEM>( & ELEM::setAttribute );
00143        Action res = Result<ELEM>();
00144        Action set  = SetterP<ELEM, ELEM>( & ELEM::addElem );
00145        Action txt = AddText<ELEM>( & ELEM::addText );
00146        
00147        dig.addRule("*",fac);
00148        dig.addRule("*",s_attr);
00149        dig.addRule("*",txt);
00150        dig.addRule("/*",res);
00151        dig.addRule("*/*", set);
00152        
00153        if (argc < 2) {
00154               dummyDoc(dig);
00155               dig.result<ELEM>()->print();
00156        }
00157        else {
00158               const char* file = argc == 2? "Dokument-1.xml" : argv[2];
00159               switch (QString(argv[1]).toInt()) {
00160                      case 1:
00161                             dig.parseFile(file);
00162                             break;
00163                      case 2:
00164                      {
00165                             QCString docBytes("");
00166                             loadRawText(file, docBytes);
00167                             QDomDocument docu("scridoc");
00168                             return docu.setContent(QString(docBytes));
00169                      }
00170                      default:
00171                             dig.parseFile(file);
00172                             dig.result<ELEM>()->print();
00173               }
00174        }      
00175 }