From bb0b86e0ab38f679c77a0654e77d112c6697f5c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mikl=C3=B3s=20M=C3=A1rton?= Date: Fri, 1 May 2020 14:01:55 +0200 Subject: [PATCH] [kxml_compiler]The kxml_compiler segfaulted when generated code from an XSD where the root element was a complexType which was a sequence of elements. This was becasuse the subelements was not added by the schema/ parser class during the parse. --- kxml_compiler/creator.cpp | 3 ++- kxml_compiler/kxml_compiler.cpp | 4 +++- kxml_compiler/parsercreatordom.cpp | 3 ++- kxml_compiler/parserxsd.cpp | 8 ++++++++ kxml_compiler/schema.h | 5 +++-- schema/parser.cpp | 3 ++- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/kxml_compiler/creator.cpp b/kxml_compiler/creator.cpp index 82f05519..908abaed 100644 --- a/kxml_compiler/creator.cpp +++ b/kxml_compiler/creator.cpp @@ -260,7 +260,8 @@ ClassDescription Creator::createClassDescription(const Schema::Element &element) QString targetClassName = Namer::getClassName(targetElement.name()); - if (targetElement.text() && !targetElement.hasAttributeRelations() && !r.isList()) { + if ((targetElement.text() || targetElement.type() < Schema::Element::ComplexType) + && !targetElement.hasAttributeRelations() && !r.isList()) { if (mVerbose) { qDebug() << " FLATTEN"; } diff --git a/kxml_compiler/kxml_compiler.cpp b/kxml_compiler/kxml_compiler.cpp index 4e3c2167..044a21a5 100644 --- a/kxml_compiler/kxml_compiler.cpp +++ b/kxml_compiler/kxml_compiler.cpp @@ -292,7 +292,9 @@ int main(int argc, char **argv) qDebug() << "Create classes"; } foreach (Schema::Element e, schemaDocument.usedElements()) { - if (!e.text()) { + // only generate classes for the simple types (nodes with no childs/attributes) + if (!e.text() + && !(e.attributeRelations().count() == 0 && e.elementRelations().count() == 0)) { c.createClass(e); } } diff --git a/kxml_compiler/parsercreatordom.cpp b/kxml_compiler/parsercreatordom.cpp index 3d70d772..56ba8b29 100644 --- a/kxml_compiler/parsercreatordom.cpp +++ b/kxml_compiler/parsercreatordom.cpp @@ -96,7 +96,8 @@ void ParserCreatorDom::createElementParser(KODE::Class &c, const Schema::Element Schema::Element targetElement = creator()->document().element((*it).target()); - if (targetElement.text() && !targetElement.hasAttributeRelations() && !(*it).isList()) { + if ((targetElement.text() || targetElement.type() < Schema::Node::ComplexType) + && !targetElement.hasAttributeRelations() && !(*it).isList()) { QString data = stringToDataConverter("e.text()", targetElement.type()); code += "result.set" + className + "( " + data + " );"; } else { diff --git a/kxml_compiler/parserxsd.cpp b/kxml_compiler/parserxsd.cpp index dad7985c..ac58e043 100644 --- a/kxml_compiler/parserxsd.cpp +++ b/kxml_compiler/parserxsd.cpp @@ -109,6 +109,10 @@ Schema::Document ParserXsd::parse(const XSD::Parser &parser) e.setBaseType(Schema::Node::String); } else if (complexType.baseTypeName().qname() == "xs:boolean") { e.setBaseType(Schema::Node::Boolean); + } else if (complexType.baseTypeName().qname() == "xs:decimal") { + e.setBaseType(Schema::Node::Decimal); + } else if (complexType.baseTypeName().qname() == "xs:date") { + e.setBaseType(Schema::Node::Date); } else if (complexType.baseTypeName().qname() == "xs:normalizedString") { e.setBaseType(Schema::Node::NormalizedString); } else if (complexType.baseTypeName().qname() == "xs:token") { @@ -120,6 +124,10 @@ Schema::Document ParserXsd::parse(const XSD::Parser &parser) e.setType(Schema::Node::String); } else if (element.type().qname() == "xs:boolean") { e.setType(Schema::Node::Boolean); + } else if (element.type().qname() == "xs:decimal") { + e.setType(Schema::Node::Decimal); + } else if (element.type().qname() == "xs:date") { + e.setType(Schema::Node::Date); } else if (element.type().qname() == "xs:normalizedString") { e.setType(Schema::Node::NormalizedString); } else if (element.type().qname() == "xs:token") { diff --git a/kxml_compiler/schema.h b/kxml_compiler/schema.h index 914bb54c..b22e3d93 100644 --- a/kxml_compiler/schema.h +++ b/kxml_compiler/schema.h @@ -98,11 +98,12 @@ class KSCHEMA_EXPORT Node : public Annotatable Int, // xs:int -> signed 32-bit integer Date, Enumeration, - ComplexType, DateTime, Decimal, - Boolean + Boolean, + ComplexType // always keep this as a last }; + Node(); virtual ~Node(); diff --git a/schema/parser.cpp b/schema/parser.cpp index bba8b08b..490ee6bd 100644 --- a/schema/parser.cpp +++ b/schema/parser.cpp @@ -370,6 +370,7 @@ void Parser::parseCompositor(ParserContext *context, const QDomElement &element, foreach (Element e, newElements) { e.setCompositor(compositor); ct.addElement(e); + d->mElements.append(e); } } } @@ -734,7 +735,7 @@ void Parser::addGlobalElement(const Element &newElement) } if (!found) { - d->mElements.append(newElement); + d->mElements.prepend(newElement); } }