Archive for February, 2008

CodeSynthesis XSD 3.1.0 released

Wednesday, February 13th, 2008

XSD 3.1.0 was released a couple of days ago. For an exhaustive list of new features see the official announcement. In this post I would like to go into more detail on a few major features, namely the file-per-type compilation mode and configurable identifier naming conventions.

File-per-type compilation mode

First, some background on the kinds of problems this feature is meant to solve. While in most cases it is natural to generate one set of source files from each schema file and map XML Schema include and import constructs to the preprocessor #include directives, XML Schema include and import mechanisms are quite a bit less strict compared to #include. For example, you can have two schemas each with a type that inherits from a base in another schema (that is, these schemas are dependent on each other and this dependency involves inheritance). Or, you can have a schema that does not include/import definitions for all the types it is referencing. Instead such a schema relies on being included or imported into another schema which provides the missing definitions (while this can also happen in C++, it is not very common). As a result, sometimes it is not possible to compile the schemas separately and/or map XML Scheme include/import to C++ #include. For such situations the file-per-type compilation mode was introduced in addition to the existing file-per-schema mode.

In the new mode (the --file-per-type command option), the XSD compiler generates a separate set of files for each type defined in XML Schema. It still generates a set of source files corresponding to the schema files which now include the header files for the types and contain parsing and serialization functions. In this compilation mode you only need to compile the root schema for your vocabulary; the code will be automatically generated for all included and imported schemas. If your vocabulary has several root schemas which in turn include or import a common subset of schemas then you will need to specify all these root schemas in a single invocation of the compiler.

One reason why the file-per-schema mode should be preferred whenever possible is the potentially large number of source files that are generated in the file-per-type mode (some of the schemas that we have tested contain 1000-1,500 types). To minimize the impact of the file-per-type mode on the C++ compilation time, it is a good idea to generate the XML Schema namespace into a separate header file (see the --generate-xml-schema and --extern-xml-schema options) and to set up a precompiled header.

To help dealing with a potentially large number of files that the new mode produces, the new --file-list option was added to the XSD compiler that allows you to write a list of generated source files into a file. The --file-list-prologue, --file-list-epilogue, and --file-list-delim options allow you to turn this file into, for example, a makefile fragment with the list of files assigned to a variable. The following GNU make fragment shows how to put all of the above information together:

XSD    := ... # path to the XSD compiler
LIBXSD := ... # path to the XSD runtime library
# Schema compilation.
xsd      := ... # list of all schema files
xsd_root := ... # root schema(s)
-include gen.make
gen.make: $(xsd)
  $(XSD) cxx-tree --file-per-type --output-dir gen 
--file-list $@ --file-list-prologue "gen := " --file-list-delim " \\n" 
--extern-xml-schema xml-schema.xsd --cxx-prologue '#include "all.hxx"' 
  $(XSD) cxx-tree --generate-xml-schema --output-dir gen xml-schema.xsd
src := driver.cxx $(filter %.cxx,$(gen))
obj := $(src:.cxx=.o)
# Precompiled header.
$(obj): gen/all.hxx.gch
gen/all.hxx.gch: gen/all.hxx gen/xml-schema.hxx
  $(CXX) -I$(LIBXSD) -o $@ $<
# Object code and driver.
driver: $(obj) -lxerces-c
  $(CXX) -o $@ $^
%.o: %.cxx
  $(CXX) -I$(LIBXSD) -c $< -o $@

The gen/all.hxx file is the precompiled header for the project and could look like this:

#ifndef GEN_ALL_HXX
#define GEN_ALL_HXX
#warning precompiled header is not used
#include "xml-schema.hxx"
#endif // GEN_ALL_HXX

Another interesting aspect of the file-per-type compilation mode is how it is implemented in XSD. A straightforward but complex approach would have been to support this mode in the code generators in addition to the file-per-schema mode. Instead, an internal schema graph transformation was implemented that transforms the semantic graph to make it appear as if each type is in a separate schema file. After this transformation the unchanged code generators are used as in the file-per-schema mode.

Configurable identifier naming conventions

One common objection to using automatic code generation is the difference between the identifier naming conventions used in a project and in the generated code. To address this concern, the XSD compiler allows you to specify a naming convention that should be used in the generated code for the C++/Tree mapping.

The two new options, --type-naming and --function-naming, allow you to select type and function naming conventions from a predefined set of widely-used styles. You can also provide regular expressions to customize or completely override one of the predefined styles.

Available type naming conventions are K&R (for example, test_type), upper-camel-case (for example, TestType), and Java (the same as upper-camel-case). Available function naming conventions are K&R (for example, test_function), lower-camel-case (for example, testFunction), and Java (for example, getTestFunction for accessors and setTestFunction for modifiers).

For more information see the NAMING CONVENTION section in the XSD Compiler Command Line Manual (man pages).