start implementing wobjects
authorkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Tue, 17 Feb 2009 16:53:04 +0000 (16:53 +0000)
committerkonrad <konrad@6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33>
Tue, 17 Feb 2009 16:53:04 +0000 (16:53 +0000)
git-svn-id: https://silmor.de/svn/softmagic/smoke/trunk@269 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33

woc/phpout.cpp
woc/processor.cpp
woc/processor.h
www/inc/wbase/autoload.php
www/inc/wbase/object.php [new file with mode: 0644]

index a9662cb..19a1e14 100644 (file)
@@ -227,18 +227,48 @@ void WocPHPServerOut::newClass(const WocClass&cls)
                return;
        }
        tf.write(PHPSTART);
+       
+       ////
        //generate code
-       QString code="class "+cna+" extends "+cls.baseClass()+"{\n";
+       QString code="class "+cna+" extends "+cls.baseClass()+"{\n\n";
+       
        //implement enums
+       QStringList k=cls.enumTypes();
+       for(int i=0;i<k.size();i++){
+               QList<QPair<QString,int> >ev=cls.enumValues(k[i]);
+               for(int j=0;j<ev.size();j++)
+                       code+="const "+ev[j].first+"="+QString::number(ev[j].second)+";\n";
+       }
+       
        //implement properties
        
        //implement mappings
        
        //implement serializers
+       k=cls.serializers();
+       for(int i=0;i<k.size();i++){
+               code+="\nfunction toString"+k[i]+"(){\n\t$xml=new DomDocument;\n";
+               code+="\t$xml->appendChild($this->toXml"+k[i]+"($xml));\n\treturn $xml->saveXml();\n}\n";
+               code+="function toXml"+k[i]+"($xml){\n\t$root=$xml->createElement(\""+cls.name()+k[i]+"\");\n";
+               //TODO: add properties
+               code+="\treturn $root;\n}\n";
+       }
        
        //implement de-serializer
+       code+="\nstatic function fromString($txt){\n\t$xml=new DomDocument;\n";
+       code+="\t$xml->loadXml(trim($txt));\n\treturn self::fromXml($xml,$xml->documentElement);\n}\n";
+       code+="static function fromXml($xml,$elem){\n\t$data=array();\n";
+       k=cls.propertyNames();
+       for(int i=0;i<k.size();i++){
+               //TODO: scan properties
+               code+="\t??\n";
+       }
+       code+="\treturn $ret;\n}\n";
+       
+       //end of class
+       code+="\n//end of class\n};\n";
+       
        
-       code+="};\n";
        //write & close
        tf.write(code.toAscii());
        tf.write(PHPEND);
index 17832ee..94e7c84 100644 (file)
@@ -314,6 +314,16 @@ WocClass::WocClass(const QDomElement&cls)
                QDomElement el=nl.at(i).toElement();
                if(el.isNull())continue;
                QString nm=el.attribute("name");
+               if(!symok.exactMatch(nm)){
+                       qDebug("Error: Illegal enum type %s in class %s.",nm.toAscii().data(),m_name.toAscii().data());
+                       m_valid=false;
+                       return;
+               }
+               if(hasEnumType(nm)){
+                       qDebug("Error: Double definition of enum type %s in class %s.",nm.toAscii().data(),m_name.toAscii().data());
+                       m_valid=false;
+                       return;
+               }
                QList<QPair<QString,int> >ev;
                //check whether there is a reference
                if(el.hasAttribute("refColumn")){
@@ -350,6 +360,7 @@ WocClass::WocClass(const QDomElement&cls)
                        nxval=el2.attribute("value",QString::number(nxval)).toInt(0,0);
                        ev.append(QPair<QString,int>(n,nxval));
                        nxval++;
+                       //TODO: check that value name does not exist yet
                }
                m_enumvals.insert(nm,ev);
        }
@@ -358,7 +369,17 @@ WocClass::WocClass(const QDomElement&cls)
        for(int i=0;i<nl.size();i++){
                QDomElement el=nl.at(i).toElement();
                if(el.isNull())continue;
-               QString name=el.attribute("name");
+               QString name=el.attribute("table");
+               if(!symok.exactMatch(name)){
+                       qDebug("Error: Illegal mapping %s in class %s.",name.toAscii().data(),m_name.toAscii().data());
+                       m_valid=false;
+                       return;
+               }
+               if(hasMapping(name)){
+                       qDebug("Error: Double definition of mapping %s in class %s.",name.toAscii().data(),m_name.toAscii().data());
+                       m_valid=false;
+                       return;
+               }
                QList<QPair<QString,QString> >map;
                QDomNodeList nl2=el.elementsByTagName("Map");
                for(int j=0;j<nl2.size();j++){
@@ -376,6 +397,26 @@ WocClass::WocClass(const QDomElement&cls)
                }
                m_maps.insert(name,map);
        }
+       //scan serializers
+       nl=cls.elementsByTagName("ToXml");
+       for(int i=0;i<nl.size();i++){
+               QDomElement el=nl.at(i).toElement();
+               if(el.isNull())continue;
+               QString name=el.attribute("name");
+               if(!symok.exactMatch(name)){
+                       qDebug("Error: Illegal serializer %s in class %s.",name.toAscii().data(),m_name.toAscii().data());
+                       m_valid=false;
+                       return;
+               }
+               if(hasSerializer(name)){
+                       qDebug("Error: Double definition of serializer %s in class %s.",name.toAscii().data(),m_name.toAscii().data());
+                       m_valid=false;
+                       return;
+               }
+               QStringList prop=el.text().split(" ",QString::SkipEmptyParts);
+               //TODO: check that properties exist
+               m_serial.insert(name,prop);
+       }
        //check abstraction
        if(!m_abstract && isAbstract()){
                qDebug("Warning: class %s should be declared abstract.",m_name.toAscii().data());
@@ -396,6 +437,38 @@ bool WocClass::isAbstract()const
        return m_abstract;
 }
 
+QStringList WocClass::propertyNames()const
+{
+       QStringList r;
+       for(int i=0;i<m_props.size();i++)
+               r<<m_props[i].name;
+       return r;
+}
+
+QString WocClass::propertyType(QString p)const
+{
+       for(int i=0;i<m_props.size();i++)
+               if(p==m_props[i].name)
+                       return m_props[i].type;
+       return "";
+}
+
+bool WocClass::propertyIsId(QString p)const
+{
+       for(int i=0;i<m_props.size();i++)
+               if(p==m_props[i].name)
+                       return m_props[i].isid;
+       return false;
+}
+
+bool WocClass::propertyIsAbstract(QString p)const
+{
+       for(int i=0;i<m_props.size();i++)
+               if(p==m_props[i].name)
+                       return m_props[i].isabstract;
+       return false;
+}
+
 
 /******************************************************************************
  * WocTable
index fc673b1..8b47760 100644 (file)
@@ -17,6 +17,7 @@
 #include <QMap>
 #include <QObject>
 #include <QPair>
+#include <QStringList>
 
 class QDomElement;
 
@@ -31,9 +32,23 @@ class WocClass
                QString baseClass()const{return m_base;}
                
                bool hasProperty(QString)const;
+               QStringList propertyNames()const;
+               QString propertyType(QString)const;
+               bool propertyIsId(QString)const;
+               bool propertyIsAbstract(QString)const;
                
                bool isAbstract()const;
                
+               QStringList enumTypes()const{return m_enumvals.keys();}
+               bool hasEnumType(QString t)const{return m_enumvals.contains(t);}
+               QList<QPair<QString,int> > enumValues(QString t)const{return m_enumvals[t];}
+               
+               QStringList serializers()const{return m_serial.keys();}
+               bool hasSerializer(QString s)const{return m_serial.contains(s);}
+               QStringList serializerProperties(QString s)const{return m_serial[s];}
+               
+               bool hasMapping(QString m)const{return m_maps.contains(m);}
+               
        private:
                bool m_valid,m_abstract;
                QString m_name,m_base;
@@ -44,6 +59,7 @@ class WocClass
                QList<s_prop> m_props;
                QMap<QString,QList<QPair<QString,QString> > >m_maps;
                QMap<QString,QList<QPair<QString,int> > >m_enumvals;
+               QMap<QString,QStringList>m_serial;
 };
 
 class WocTable
index 7b3d4f6..3c446b9 100644 (file)
@@ -11,6 +11,7 @@
 
 $AUTOCLASS["WobTable"]="inc/wbase/table.php";
 $AUTOCLASS["WobSchemaBase"]="inc/wbase/schema.php";
+$AUTOCLASS["WObject"]="inc/wbase/object.php";
 
 function __autoload($cname)
 {
diff --git a/www/inc/wbase/object.php b/www/inc/wbase/object.php
new file mode 100644 (file)
index 0000000..154b388
--- /dev/null
@@ -0,0 +1,19 @@
+<?php
+// +----------------------------------------------------------------------
+// | PHP Source                                                           
+// +----------------------------------------------------------------------
+// | Copyright (C) 2009 by Konrad Rosenbaum <konrad@silmor.de>
+// +----------------------------------------------------------------------
+// |
+// | Copyright: See COPYING file that comes with this distribution
+// +----------------------------------------------------------------------
+//
+
+class WObject {
+       protected $data;
+       
+       protected __construct($d){$this->data=$d;}
+       
+};
+
+?>