start of schema file generator
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 31 Jul 2010 17:34:28 +0000 (17:34 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Sat, 31 Jul 2010 17:34:28 +0000 (17:34 +0000)
git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@581 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

woc/mfile.h
woc/php/phpstrans.cpp
woc/proc/processor.cpp
woc/proc/proctrans.h
woc/soap/schemaout.cpp [new file with mode: 0644]
woc/soap/schemaout.h [new file with mode: 0644]
woc/soap/soap.pri
woc/soap/soap12.xsd

index 44a3d38..07b298d 100644 (file)
 #include <QStringList>
 
 /**overwrites QFile to only generate a file if its new version differs from an existing one, it creates a temporary file with ",new" added to the file name; additionally it records all file names that it touches;
-this is not a complete implementation - just enough for woc*/
+this is not a complete implementation - just enough for woc; it assumes that files are opened to be (re)created*/
 class MFile:public QFile
 {
        public:
+               /**creates a new file (tentatively) with the given name (temporarily with ",new" appended)*/
                MFile(QString name,QObject*parent=0);
+               /**creates a file object*/
                MFile(QObject *parent=0);
+               /**implicitly closes the file*/
                ~MFile();
                
+               /**compares the old (if it exists) and new version of the file and overwrites the old one if there are differences*/
                virtual void close();
+               /**opens the file, WriteOnly is assumed as mode*/
                virtual bool open(QIODevice::OpenMode m);
                
+               /**returns the name of the file (without ",new")*/
                QString fileName()const{return name;}
+               /**sets the file name for the file to be written; must be called before it is opened*/
                void setFileName(QString n);
                
+               /**returns true if that file name has been touched yet*/
                static bool touchedFile(QString);
        private:
                //flag to show whether file is open
index 721ad75..4c4bd05 100644 (file)
@@ -131,8 +131,8 @@ QString WocPHPServerTransaction::trnOutput(const WocTransaction&trn)
 {
        QStringList sl=trn.outputNames();
        QString code="\t/*start of output encoding*/\n";
-       code+="\ttry{\n\t\t$xml=$this->xmlCreate(\"WobResponse-"+trn.name()+"\");\n";
-       code+="\t\t$root=$xml['root'];\n";
+       code+="\ttry{\n\t\t$xmla=$this->xmlCreate(\"WobResponse-"+trn.name()+"\");\n";
+       code+="\t\t$xml=$xmla['doc'];\n\t\t$root=$xmla['root'];\n";
        for(int i=0;i<sl.size();i++){
                QString t=trn.outputType(sl[i]);
                if(trn.isAttributeType(t)){
@@ -164,7 +164,7 @@ QString WocPHPServerTransaction::trnOutput(const WocTransaction&trn)
                }
        }
        code+="\t\theader(\"X-WobResponse-Status: Ok\");\n";
-       code+="\t\tprint($this->xmlToString($xml));\n";
+       code+="\t\tprint($this->xmlToString($xmla));\n";
        code+="\t}catch(Exception $e){$this->handleException($e);}\n";
        code+="\t/*end of output*/\n";
        return code;
index 51136af..3903f8b 100644 (file)
@@ -15,6 +15,7 @@
 #include "phpout.h"
 #include "qtout.h"
 #include "htmlout.h"
+#include "schemaout.h"
 
 #include "domquery.h"
 
@@ -173,6 +174,10 @@ bool WocProcessor::processFile(QString fn)
                        new WocHtmlOut(el);
                        if(m_error)return false;
                }else
+               if(tn=="SchemaOutput"){
+                       new WocSchemaOut(el.attribute("filename"));
+                       if(m_error)return false;
+               }else
                if(tn=="Version"){
                        if(el.hasAttribute("comm"))
                                m_verComm=el.attribute("comm");
index 219e6df..eeb6035 100644 (file)
@@ -62,9 +62,9 @@ class WocTransaction
                AuthMode authMode()const{return m_mode;}
                
                /**returns true if the type given is a list*/
-               bool isListType(QString t)const{return t.startsWith("List:");}
+               static bool isListType(QString t){return t.startsWith("List:");}
                /**returns the type without list or xml qualifiers*/
-               QString plainType(QString t)const{
+               static QString plainType(QString t){
                        if(t.startsWith("List:"))t=t.mid(5);
                        QStringList l=t.split("/",QString::SkipEmptyParts);
                        if(l.size()>0)t=l[0];else t="unknown";
@@ -77,19 +77,19 @@ class WocTransaction
                        else return "";
                }
                /**returns true if the type is integer*/
-               bool isIntType(QString t)const{QString pt=plainType(t);return pt=="int" || pt=="int32" || pt=="int64";}
+               static bool isIntType(QString t){QString pt=plainType(t);return pt=="int" || pt=="int32" || pt=="int64";}
                /**returns true if the type is boolean*/
-               bool isBoolType(QString t)const{return plainType(t)=="bool";}
+               static bool isBoolType(QString t){return plainType(t)=="bool";}
                /**returns true if the type is a string*/
-               bool isStringType(QString t)const{QString p=plainType(t);return p=="astring"||p=="string";}
+               static bool isStringType(QString t){QString p=plainType(t);return p=="astring"||p=="string";}
                /**returns true if the type is a blob*/
-               bool isBlobType(QString t)const{QString p=plainType(t);return p=="blob";}
+               static bool isBlobType(QString t){QString p=plainType(t);return p=="blob";}
                /**returns true if the type is to be encoded as attribute*/
-               bool isAttributeType(QString t)const{return t=="astring"||t=="int"||t=="int32"||t=="int64"||t=="bool";}
+               static bool isAttributeType(QString t){return t=="astring"||t=="int"||t=="int32"||t=="int64"||t=="bool";}
                /**returns true if the type is to be encoded as element*/
-               bool isElementType(QString t)const{return !isAttributeType(t);}
+               static bool isElementType(QString t){return !isAttributeType(t);}
                /**return true if the type is an object type*/
-               bool isObjectType(QString t)const{QString p=plainType(t);return p!="astring"&&p!="string"&&p!="int"&&p!="int32"&&p!="int64"&&p!="bool"&&p!="blob";}
+               static bool isObjectType(QString t){QString p=plainType(t);return p!="astring"&&p!="string"&&p!="int"&&p!="int32"&&p!="int64"&&p!="bool"&&p!="blob";}
                
                /**return the documentation of the transaction*/
                QStringList docStrings()const{return m_docstrings;}
diff --git a/woc/soap/schemaout.cpp b/woc/soap/schemaout.cpp
new file mode 100644 (file)
index 0000000..8273cff
--- /dev/null
@@ -0,0 +1,131 @@
+//
+// C++ Interface: schemaout
+//
+// Description: XML Schema file generator
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2009
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#include "schemaout.h"
+
+#include "mfile.h"
+
+#include <QDebug>
+
+WocSchemaOut::WocSchemaOut(QString fname): WocOutput()
+{
+       m_name=fname;
+       WocProcessor*woc=WocProcessor::instance();
+       m_doc.appendChild(m_doc.createComment("Automatically generated Schema file for project "+woc->projectName()));
+       m_root=m_doc.createElementNS(woc->xmlSchemaNamespace(),"xs:schema");
+       m_root.setAttribute("targetNamespace",woc->xmlProjectNamespace());
+       m_root.setAttribute("xmlns",woc->xmlProjectNamespace());
+}
+
+void WocSchemaOut::finalize()
+{
+       //finish off
+       m_doc.appendChild(m_root);
+       //write
+       MFile fd(WocProcessor::instance()->baseDir()+"/"+m_name);
+       fd.open(QIODevice::WriteOnly);
+       fd.write(m_doc.toByteArray());
+}
+
+void WocSchemaOut::newClass(const WocClass& cls)
+{
+       m_root.appendChild(m_doc.createComment("Class "+cls.name()));
+       //create type
+       QDomElement cel=m_doc.createElement("xs:complexType");
+       cel.setAttribute("name","class-"+cls.name());
+       //get properties
+       //create elements
+       //create attributes
+       //add to root
+       m_root.appendChild(cel);
+}
+
+void WocSchemaOut::newTransaction(const WocTransaction& trn)
+{
+       m_root.appendChild(m_doc.createComment("Transaction "+trn.name()));
+       //create elements
+       QDomElement tel=m_doc.createElement("xs:element");
+       tel.setAttribute("name",trn.name()+"-Request");
+       tel.setAttribute("type",trn.name()+"-Request");
+       m_root.appendChild(tel);
+       tel=m_doc.createElement("xs:element");
+       tel.setAttribute("name",trn.name()+"-Response");
+       tel.setAttribute("type",trn.name()+"-Response");
+       m_root.appendChild(tel);
+       
+       //create type: req
+       tel=m_doc.createElement("xs:complexType");
+       tel.setAttribute("name",trn.name()+"-Request");
+       QDomElement wel=m_doc.createElement("xs:sequence");
+       QStringList vars=trn.inputNames();
+       for(int i=0;i<vars.size();i++){
+               QString tp=trn.inputType(vars[i]);
+               if(trn.isAttributeType(tp)){
+                       QDomElement at=m_doc.createElement("xs:attribute");
+                       at.setAttribute("name",vars[i]);
+                       at.setAttribute("type",schemaType(tp));
+                       at.setAttribute("use","optional");
+                       tel.appendChild(at);
+               }else{
+                       QDomElement el=m_doc.createElement("xs:element");
+                       el.setAttribute("name",vars[i]);
+                       el.setAttribute("type",schemaType(tp));
+                       el.setAttribute("minOccurs","0");
+                       if(trn.isListType(tp))
+                               el.setAttribute("maxOccurs","unbounded");
+                       wel.appendChild(el);
+               }
+       }
+       tel.appendChild(wel);
+       m_root.appendChild(tel);
+       //create type: rsp
+       tel=m_doc.createElement("xs:complexType");
+       tel.setAttribute("name",trn.name()+"-Response");
+       wel=m_doc.createElement("xs:sequence");
+       vars=trn.outputNames();
+       for(int i=0;i<vars.size();i++){
+               QString tp=trn.outputType(vars[i]);
+               if(trn.isAttributeType(tp)){
+                       QDomElement at=m_doc.createElement("xs:attribute");
+                       at.setAttribute("name",vars[i]);
+                       at.setAttribute("type",schemaType(tp));
+                       at.setAttribute("use","optional");
+                       tel.appendChild(at);
+               }else{
+                       QDomElement el=m_doc.createElement("xs:element");
+                       el.setAttribute("name",vars[i]);
+                       el.setAttribute("type",schemaType(tp));
+                       el.setAttribute("minOccurs","0");
+                       if(trn.isListType(tp))
+                               el.setAttribute("maxOccurs","unbounded");
+                       wel.appendChild(el);
+               }
+       }
+       tel.appendChild(wel);
+       m_root.appendChild(tel);
+}
+
+void WocSchemaOut::newTable(const WocTable& )
+{}
+
+QString WocSchemaOut::schemaType(QString tp)
+{
+       tp=WocTransaction::plainType(tp);
+       if(WocTransaction::isObjectType(tp))return "class-"+tp;
+       if(WocTransaction::isStringType(tp))return "xs:string";
+       if(WocTransaction::isBlobType(tp))return "xs:base64binary";
+       if(WocTransaction::isBoolType(tp))return "xs:boolean";
+       if(WocTransaction::isIntType(tp))return "xs:integer";
+       //fallback, not pretty
+       qDebug()<<"Warning: schema generator found unknown type"<<tp;
+       return "xs:anysimpleType";
+}
diff --git a/woc/soap/schemaout.h b/woc/soap/schemaout.h
new file mode 100644 (file)
index 0000000..0b55f93
--- /dev/null
@@ -0,0 +1,45 @@
+//
+// C++ Interface: schemaout
+//
+// Description: XML Schema file generator
+//
+//
+// Author: Konrad Rosenbaum <konrad@silmor.de>, (C) 2009
+//
+// Copyright: See README/COPYING files that come with this distribution
+//
+//
+
+#ifndef WOC_SCHEMAOUT_H
+#define WOC_SCHEMAOUT_H
+
+#include "processor.h"
+
+#include <QDomDocument>
+#include <QDomElement>
+
+/**generates output for a schema file*/
+class WocSchemaOut:public WocOutput
+{
+       public:
+               /**initializes the output object with the given file name that will receive the schema for this project*/
+               WocSchemaOut(QString);
+       protected:
+               /**writes any last words after parsing finished*/
+               virtual void finalize();
+               /**creates a class*/
+               virtual void newClass(const WocClass&);
+               /**creates a table*/
+               virtual void newTable(const WocTable&);
+               /**creates a transaction*/
+               virtual void newTransaction(const WocTransaction&);
+       private:
+               QString m_name;
+               QDomDocument m_doc;
+               QDomElement m_root;
+               
+               /**helper: returns the corresponding schema type for a WOB type*/
+               QString schemaType(QString);
+};
+
+#endif
index 09ec496..379ddb6 100644 (file)
@@ -1 +1,9 @@
-RESOURCES += soap/schemafiles.qrc
\ No newline at end of file
+RESOURCES += soap/schemafiles.qrc
+
+SOURCES += \
+       soap/schemaout.cpp
+
+HEADERS += \
+       soap/schemaout.h
+
+INCLUDEPATH += soap
\ No newline at end of file
index 85a6d8e..c8e82f9 100644 (file)
@@ -21,7 +21,8 @@
                   elementFormDefault="qualified" >
 
   <xs:import namespace="http://www.w3.org/XML/1998/namespace" 
-             schemaLocation="http://www.w3.org/2001/xml.xsd"/>
+             schemaLocation="xml.xsd"/>
+  <!--           schemaLocation="http://www.w3.org/2001/xml.xsd"/> -->
 
   <!-- Envelope, header and body -->
   <xs:element name="Envelope" type="tns:Envelope" />