X2C = XML Schema to C++ classes
It is a very common situation: you have some xml file and you need to read it and process. Generaly, there are three different approches: 1/ SAX Read it as any other file: line by line and it is up to you to parse it. It is useful if such xml file is too big to keep it in memory or you want only small parts of it. 2/ DOM Uses its parser and creates a new universal object for each element and attribute it finds. These objects doesn't represent structure of xml file enought to provide static type control. 3/ Real objects Assignes each element and attribute to its own type with structure providing exactly those methods defined by XML Schema. Usually this approche requires two phases: 1) read schema, build data model, generate data structures, save them to files 2) read file valid to schema, assign each element to class, attribute to fields and manage subelements
I chose the last one because it provides the most human interface (if you think everything is object)
You run the first program with arguments: schema file which describes whole structure, there are some limitations, but most of them will work user namespace - namespace of all structures in schema you define - can be determined from header but it is to complicated schema namespace - namespace which holds definition of schema itself output directory - where will be generated files placed It does some magic and suddenly files in output directory appears.
These files contains C++ classes which can be instantited simply by providing xml file to the root class factory method. Classes themselves does whole job to read xml file, parse it and save all information to their fileds. Usage is the simpleast possible, one command: RootElement::fromFile(file);
This application doesn't try to provide whole Object XML Mapping, but it should be sufficient for simple cases like parsing configuration or maybe some lite implementations of REST or RPC client. I tried to make its usage the simplest possible and in comparison with some draft, this final version doesn't generate any inheritance or polymorphism code which makes it easy to patch if necessary.
First it uses DOM parser from Qt xml module and loads whole schema to memory. There is nothing to comment, those 10 lines were taken from Qt Example.
Then it does some normalization:
1/ removes content of
Next data model is created, it contains most information from schema and makes following steps independent on schema file. 1/ Elements and Attributes empty objects are created, because they will be referenced from types. References on this level are pointer based, not id based as in schema file. They just have their name and nothing else, type will be assigned later. 2/ Types are created, they have their unique name. Iterating through their subtree elements and attribute refs are added to type model. More about types in special chapter. In fact type doesn't have elements and attributes but references to them. This provides possibility to override some properties: Element can have default value and Element reference has another default value which overrides the element's one. 3/ Types are assigned to elements and attributes. This completes data model.
The last piece generates files which of c++ classes. There are many ways how such classes could look like, after considering them, I chose 1 class = 1 element. An attribute is field member of class and text value in element acts acts like attribute with name "value". Others are discussed later. This narrows the way how it is implemented and provide information enough to generate class. Because minOccures, maxOccures attributes are not supported, each element appear acts as if: minOccures=0, maxOccures=unbounded. So elements of the same type are stored in one QList, and that way is determined the way how they are used. Each attribute and content of element (which is converted to attribute) may appear at most once. So attributes are simple string fields and converted to proper data type fields and also have flag if they are present. Finally there are some factory methods which cretates a new object from file (user use) or from DOM element (recurse).
Handling subelements is easy: no data type conversion is necessary and error checking has been done by schema. Attributes are rather complicated: there must be some rules, how string value is converted to destinantion data type. I tried to provide very wide range of data types extended by non standard values ("yes" is a valid boolean). If simple types are complicated, list types are pure magic: they require usage of simple types code which they wrap into their own code. Unfortunately, calling methods in this situation is impossible because I could not know their generic name. There might be a way how to solve this clearly: each element would have methods for each data type: bool to${dataType}(QString input, void *output); But it is too late now and it would require to rewrite most of generation part and introduce some inheritance in generated code. Unions are not implemented, they would require to generate some extra classes for each union type and some mechanism to determine which data type the item is. It would make whole application bigger and buggier; and one more reason: those xml files breakes object oriented design.
Concept of types differs from that used in xml.
Here, simple type means a type which holds value.
Complex type holds subelements and no value, mixed content is not supported at all.
Both kinds of types can have attributes;
Inner model of subelements is implemented in the simpest way; it can hold list of each of subelements.
It doesn't respect minOccures, maxOccures -- it ignores them because it would be very hard to determine actual edges in cases which involves
During designing this program I went throught many schemas of generated code. Here I will try to comment some and describe why it was not chosen. 1/ Classes will copy structure of data model There will be super class Type which will provide abstract type-relative methods and its descendants SimpleType, ComplexType. Another super classes would be Element and Attribute, target elements would inherit from element or attribute and one type. Types would make their own hierarchy according to extensions and restrictions. This might look nice, but its maintaince would be a nightmare, it would produce twice more classes and patching them to specific task would be impossible. 2/ Similar to the current one with convertors There would be only one supertype, which would provide convertors from string to all basic simple types. Checking and maybe general tasks like loading xml file would be implemented there as well. Each particular element would be descendant of this class and would use it methods to do most of work. Also patching would be easy, just reimplement virtual method in particular element, this seems to be quite clean. As I said a few paragraphs higher, I discovered this option when I was to finish the current one.
You can run tests.sh which tests all *.xsd tests, test marked with 0 is OK. Some tests are disabled either with .blocked = use another namespace schema, or .group = use groups.