From: konrad Date: Sat, 31 Jul 2010 21:52:54 +0000 (+0000) Subject: schema validates X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=f455546627d13eeb70f30c8340e4219007b460b3;p=web%2Fkonrad%2Fpack.git schema validates changed bools to match XMLSchema xs:boolean type git-svn-id: https://silmor.de/svn/softmagic/pack/trunk@582 6e3c4bff-ac9f-4ac1-96c5-d2ea494d3e33 --- diff --git a/phpbase/object.php b/phpbase/object.php index 28e350c..ea4a937 100644 --- a/phpbase/object.php +++ b/phpbase/object.php @@ -31,6 +31,23 @@ class WObject { if(!method_exists($obj,"propertyArray"))return null; return $obj->propertyArray(); } + + /**helper function for XML'ized objects that transforms XML boolean to PHP boolean*/ + public static function fromXmlBoolean($str) + { + $s=strtolower(trim($str)); + if(is_numeric($s))return ($s+0)!=0; + if($s=="true" || $s=="yes")return true; + else false; + } + /**helper function for XML'ized objects: it returns whether the string is a valid boolean*/ + public static function isXmlBoolean($str) + { + $s=strtolower(trim($str)); + if(is_numeric($s))return true; + if($s=="true" || $s=="yes" || $s=="false" || $s=="no")return true; + else false; + } }; ?> diff --git a/qtbase/WHelper.cpp b/qtbase/WHelper.cpp index 84ec965..cc7441f 100644 --- a/qtbase/WHelper.cpp +++ b/qtbase/WHelper.cpp @@ -27,3 +27,13 @@ QListWHelper::elementsByTagName(const QDomElement&root,QString tag) } return ret; } + +bool WHelper::str2bool(QString s) +{ + s=s.trimmed().toLower(); + bool ok; + int i=s.toInt(&ok); + if(ok)return i!=0; + if(s=="true" || s=="yes")return true; + else return false; +} diff --git a/qtbase/WHelper.h b/qtbase/WHelper.h index f9e0f22..acd94a4 100644 --- a/qtbase/WHelper.h +++ b/qtbase/WHelper.h @@ -24,6 +24,9 @@ class WHelper:public QObject protected: /**helper for de-serializers: returns direct child elements with given tag name (necessary because QDomElement::elementsByTagName traverses all children)*/ static QListelementsByTagName(const QDomElement&,QString); + + /**helper for XML decoding: transforms string to boolean*/ + bool str2bool(QString s); }; #endif diff --git a/qtbase/WObject.cpp b/qtbase/WObject.cpp index 85ad4d2..10a7629 100644 --- a/qtbase/WObject.cpp +++ b/qtbase/WObject.cpp @@ -10,4 +10,4 @@ // // -#include "WObject.h" \ No newline at end of file +#include "WObject.h" diff --git a/woc/php/phpclass.cpp b/woc/php/phpclass.cpp index d7bb590..cc77fd4 100644 --- a/woc/php/phpclass.cpp +++ b/woc/php/phpclass.cpp @@ -221,7 +221,7 @@ QString WocPHPClass::classPropertyListGetters(const WocClass&cls,QString prop) }else if(cls.propertyIsBool(prop)){ code+="public function getstrlist_"+prop+"(){\n"; - code+="\t$ret=array();\n\tforeach($this->prop_"+prop+" as $p)$ret[]=$p?\"yes\":\"no\";\n"; + code+="\t$ret=array();\n\tforeach($this->prop_"+prop+" as $p)$ret[]=$p?\"true\":\"false\";\n"; code+="\treturn $ret;\n}\n"; }else if(cls.propertyIsEnum(prop)){ @@ -283,8 +283,8 @@ QString WocPHPClass::classPropertyListSetters(const WocClass&cls,QString prop) if(cls.propertyIsBool(prop)){ code+="\t$prop=array();\n\tforeach($values as $value)\n"; code+="\t\tif(is_bool($value))$prop[]=$value!=false;else\n"; - code+="\t\tif($value==\"yes\")$prop[]=true;else\n"; - code+="\t\tif($value==\"no\")$prop[]=false;else return false;\n"; + code+="\t\tif(WObject::isXmlBoolean($value))$prop[]=WObject::fromXmlBoolean($value);else\n"; + code+="\t\treturn false;\n"; code+="\t$this->prop_"+prop+"=$prop;\n\treturn true;\n"; acode+="\tif(is_bool($value)){\n"; acode+="\t\t$this->prop_"+prop+"=$value!=false;\n\t\treturn true;\n\t}else return false;\n"; @@ -329,7 +329,7 @@ QString WocPHPClass::classPropertyScalarGetters(const WocClass&cls,QString prop) code+="public function getstr_"+prop+"(){return \"\".$this->prop_"+prop+";}\n"; else if(cls.propertyIsBool(prop)) - code+="public function getstr_"+prop+"(){return $this->prop_"+prop+"?\"yes\":\"no\";}\n"; + code+="public function getstr_"+prop+"(){return $this->prop_"+prop+"?\"true\":\"false\";}\n"; else if(cls.propertyIsEnum(prop)){ code+="public function getstr_"+prop+"(){\n\tswitch($this->prop_"+prop+"){\n"; @@ -368,8 +368,8 @@ QString WocPHPClass::classPropertyScalarSetters(const WocClass&cls,QString prop) else if(cls.propertyIsBool(prop)){ code+="\tif(is_bool($value))$this->prop_"+prop+"=$value!=false;else\n"; - code+="\tif($value==\"yes\")$this->prop_"+prop+"=true;else\n"; - code+="\tif($value==\"no\")$this->prop_"+prop+"=false;\n\telse return false;\n"; + code+="\tif(WObject::isXmlBoolean($value))$this->prop_"+ prop+"=WObject::fromXmlBoolean($value);\n"; + code+="\telse return false;\n"; code+="\treturn true;\n"; }else if(cls.propertyIsString(prop)||cls.propertyIsBlob(prop)) @@ -520,7 +520,7 @@ QString WocPHPClass::propertyToXml(const WocClass&cls,QString sl) if(cls.propertyIsAttribute(prop)) return code+"\tif($p!==null)$root->setAttribute(\""+prop+"\",$this->getstr_"+prop+"());\n"; //is it an element? - if(cls.propertyIsElement(prop)) + if(cls.propertyIsSimpleElement(prop)) return code+"\tif($p!==null)$root->appendChild($xml->createElement(\""+prop+"\",xq($this->getstr_"+prop+"())));\n"; //is it a class? if(cls.propertyIsObject(prop)) diff --git a/woc/php/phpstrans.cpp b/woc/php/phpstrans.cpp index 4c4bd05..2a396b3 100644 --- a/woc/php/phpstrans.cpp +++ b/woc/php/phpstrans.cpp @@ -88,9 +88,11 @@ QString WocPHPServerTransaction::trnInput(const WocTransaction&trn) for(int i=0;igetAttribute(\""+sl[i]+"\")"; + code+="\t\t$this->ainput[\""+sl[i]+"\"]="; + if(trn.isBoolType(t))code+="WObject::fromXmlBoolean("; + code+="$root->getAttribute(\""+sl[i]+"\")"; if(trn.isIntType(t))code+="+0"; - if(trn.isBoolType(t))code+="==\"yes\""; + if(trn.isBoolType(t))code+=")"; code+=";\n"; }else{ if(trn.isListType(t)){ @@ -101,9 +103,11 @@ QString WocPHPServerTransaction::trnInput(const WocTransaction&trn) }else if(trn.isBlobType(t)){ code+="\t\t\t$this->ainput[\""+sl[i]+"\"][]=base64_decode($el->textContent);\n"; }else{ - code+="\t\t\t$this->ainput[\""+sl[i]+"\"][]=$el->textContent"; + code+="\t\t\t$this->ainput[\""+sl[i]+"\"][]="; + if(trn.isBoolType(t))code+="WObject::fromXmlBoolean("; + code+="$el->textContent"; if(trn.isIntType(t))code+="+0"; - if(trn.isBoolType(t))code+="==\"yes\""; + if(trn.isBoolType(t))code+=")"; code+=";\n"; } code+="\t\t}\n"; diff --git a/woc/proc/procclass.cpp b/woc/proc/procclass.cpp index f656fef..b50edc7 100644 --- a/woc/proc/procclass.cpp +++ b/woc/proc/procclass.cpp @@ -256,6 +256,7 @@ QString WocClass::propertyPlainType(QString p)const const QStringList WocClass::attrtypes=QStringList()<<"astring"<<"int"<<"int32"<<"int64"<<"bool"; bool WocClass::propertyIsAttribute(QString p)const { + if(p.startsWith("List:"))return false; QString t=propertyPlainType(p); if(attrtypes.contains(t))return true; if(hasEnumType(t))return true; @@ -263,12 +264,15 @@ bool WocClass::propertyIsAttribute(QString p)const } const QStringList WocClass::elemtypes=QStringList()<<"string"<<"blob"; -bool WocClass::propertyIsElement(QString p)const +bool WocClass::propertyIsSimpleElement(QString p) const { QString t=propertyPlainType(p); - if(elemtypes.contains(t))return true; - if(hasEnumType(t))return true; - return false; + return elemtypes.contains(t); +} + +bool WocClass::propertyIsElement(QString p)const +{ + return !propertyIsAttribute(p); } bool WocClass::propertyIsObject(QString p)const diff --git a/woc/proc/procclass.h b/woc/proc/procclass.h index 3bad610..6d7b063 100644 --- a/woc/proc/procclass.h +++ b/woc/proc/procclass.h @@ -60,6 +60,8 @@ class WocClass bool propertyIsAttribute(QString)const; /**returns whether this property is serialized as XML element*/ bool propertyIsElement(QString)const; + /**returns whether this property is serialized as XML element, but not a class*/ + bool propertyIsSimpleElement(QString)const; /**returns whether this property has an enum type*/ bool propertyIsEnum(QString)const; /**returns whether the property is an object*/ diff --git a/woc/qt/qtclass.cpp b/woc/qt/qtclass.cpp index 0ff8f0c..7395609 100644 --- a/woc/qt/qtclass.cpp +++ b/woc/qt/qtclass.cpp @@ -198,7 +198,7 @@ void WocQtClass::classDeserializer(const WocClass&cls,MFile&hdr,MFile&src,QStrin scd+="\t\telse throw WDeserializerException(QCoreApplication::translate(\"WobTransaction\",\"Class '%1' property '%2' is integer list, but non-integer was found.\").arg(\""+cn+"\").arg(\""+k[i]+"\"));\n"; }else if(cls.propertyIsBool(k[i])){ - scd+="\t\tadd"+k[i]+"(el.text()==\"yes\");\n"; + scd+="\t\tadd"+k[i]+"(str2bool(el.text()));\n"; }else if(cls.propertyIsString(k[i])){ scd+="\t\tadd"+k[i]+"(el.text());\n"; @@ -233,7 +233,7 @@ void WocQtClass::classDeserializer(const WocClass&cls,MFile&hdr,MFile&src,QStrin scd+="\t\telse throw WDeserializerException(QCoreApplication::translate(\"WobTransaction\",\"Class '%1' property '%2' is integer, but non-integer was found.\").arg(\""+cn+"\").arg(\""+k[i]+"\"));\n"; }else if(cls.propertyIsBool(k[i])){ - scd+="\t\tset"+k[i]+"(root.attribute(\""+k[i]+"\")==\"yes\");\n"; + scd+="\t\tset"+k[i]+"(str2bool(root.attribute(\""+k[i]+"\")));\n"; }else if(cls.propertyIsString(k[i])){ scd+="\t\tset"+k[i]+"(root.attribute(\""+k[i]+"\"));\n"; @@ -259,10 +259,10 @@ void WocQtClass::classDeserializer(const WocClass&cls,MFile&hdr,MFile&src,QStrin scd+="\t\tset"+k[i]+"(nl.at(0).toElement().text());\n"; }else if(cls.propertyIsBlob(k[i])){ - scd+="\t\tset"+k[i]+"(QByteArray::fromBase64(nl.at(0).toElement().text().toAscii()));\n"; + scd+="\t\tset"+k[i]+ "(QByteArray::fromBase64(nl.at(0).toElement().text().toAscii()));\n"; }else if(cls.propertyIsObject(k[i])){ - scd+="\t\tset"+k[i]+"("+m_prefix+"O"+cls.propertyPlainType(k[i])+"(nl.at(0).toElement()));\n"; + scd+="\t\tset"+k[i]+"("+ m_prefix+"O"+cls.propertyPlainType(k[i])+ "(nl.at(0).toElement()));\n"; }else{ scd+="#error \"Internal Generator error.\"\n"; qDebug("Error: unable to generate code for property %s of type %s.",k[i].toAscii().data(),cls.propertyType(k[i]).toAscii().data()); @@ -320,7 +320,7 @@ void WocQtClass::classSerializers(const WocClass&cls,MFile&hdr,MFile&src,QString }else if(cls.propertyIsBool(prop)){ scd+="\t\tQDomElement el=doc.createElement(\""+prop+"\");\n"; - scd+="\t\tel.appendChild(doc.createTextNode(mp_"+prop+"[i]?\"yes\":\"no\"));\n"; + scd+="\t\tel.appendChild(doc.createTextNode(mp_"+prop+"[i]?\"true\":\"false\"));\n"; scd+="\t\tr.appendChild(el);\n"; }else if(cls.propertyIsBlob(prop)){ @@ -343,7 +343,7 @@ void WocQtClass::classSerializers(const WocClass&cls,MFile&hdr,MFile&src,QString scd+="\tif(!mp_"+prop+".isNull()){\n"; if(cls.propertyIsAttribute(prop)){ if(cls.propertyIsBool(prop)) - scd+="\t\tr.setAttribute(\""+prop+"\",mp_"+prop+".value()?\"yes\":\"no\");\n"; + scd+="\t\tr.setAttribute(\""+prop+"\",mp_"+prop+".value()?\"true\":\"false\");\n"; else scd+="\t\tr.setAttribute(\""+prop+"\",mp_"+prop+".value());\n"; }else{ diff --git a/woc/qt/qtctrans.cpp b/woc/qt/qtctrans.cpp index e38b36e..9af83b7 100644 --- a/woc/qt/qtctrans.cpp +++ b/woc/qt/qtctrans.cpp @@ -168,7 +168,7 @@ QString WocQtClientTransaction::trnInput(const WocTransaction&trn) if(trn.isAttributeType(t)){ code+="\troot.setAttribute(\""+sl[i]+"\",in_"+sl[i]; if(trn.isBoolType(t)) - code+="?\"yes\":\"no\""; + code+="?\"true\":\"false\""; code+=");\n"; }else{ if(trn.isListType(t)){ @@ -183,7 +183,7 @@ QString WocQtClientTransaction::trnInput(const WocTransaction&trn) code+="QString::number(in_"+sl[i]+"[i])"; else if(trn.isBoolType(t)) - code+="in_"+sl[i]+"[i]?\"yes\":\"no\""; + code+="in_"+sl[i]+"[i]?\"true\":\"false\""; else if(trn.isBlobType(t)) code+="in_"+sl[i]+".toBase64()"; @@ -237,9 +237,11 @@ QString WocQtClientTransaction::trnOutput(const WocTransaction&trn) for(int i=0;i +#include +#include WocSchemaOut::WocSchemaOut(QString fname): WocOutput() { @@ -24,6 +26,11 @@ WocSchemaOut::WocSchemaOut(QString fname): WocOutput() m_root=m_doc.createElementNS(woc->xmlSchemaNamespace(),"xs:schema"); m_root.setAttribute("targetNamespace",woc->xmlProjectNamespace()); m_root.setAttribute("xmlns",woc->xmlProjectNamespace()); + m_root.setAttribute("elementFormDefault","qualified"); + QDomElement el=m_doc.createElement("xs:import"); + el.setAttribute("namespace",woc->xmlPackNamespace()); + el.setAttribute("schemaLocation","wob-base.xsd"); + m_root.appendChild(el); } void WocSchemaOut::finalize() @@ -34,17 +41,62 @@ void WocSchemaOut::finalize() MFile fd(WocProcessor::instance()->baseDir()+"/"+m_name); fd.open(QIODevice::WriteOnly); fd.write(m_doc.toByteArray()); + fd.close(); + //copy + QString s=QFileInfo(WocProcessor::instance()->baseDir()+"/"+m_name).dir().path(); + MFile wfd(s+"/wob-base.xsd"); + QFile bfd(":/wob-base.xsd"); + wfd.open(QIODevice::WriteOnly); + bfd.open(QIODevice::ReadOnly); + wfd.write(bfd.readAll()); } void WocSchemaOut::newClass(const WocClass& cls) { m_root.appendChild(m_doc.createComment("Class "+cls.name())); + //create enums + QStringList enl=cls.enumTypes(); + for(int i=0;i vll=cls.enumValues(enl[i]); + for(int j=0;j - + - + + + - - - - - - - - - - + \ No newline at end of file