Skip to content

Parsing and Generating code

Lars-Erik Aabech edited this page Oct 20, 2013 · 5 revisions

Note

I assume you'll browse through the unit-tests and code to do stuff like this, so this will be all the docs you'll get about it. ;) (for now, at least)

Intermediate DTOs

XML and code is parsed into DTOs which serve as an intermediate level between parsing code then serializing XML, and deserializing XML then generating code.

Controlling code generation

Code generation is done by composite CodeGeneratorBase implementations.

The default factory looks like this:

private CodeGeneratorBase CreateGenerators(ContentTypeConfiguration c, IEnumerable<DataTypeDefinition> dataTypes)
{
    return new NamespaceGenerator(c,
        new ImportsGenerator(c),
        new ClassGenerator(c,
            new EntityDescriptionGenerator(c),
            new CtorGenerator(c),
            new DocumentTypeInfoGenerator(c),
            new StructureGenerator(c),
            new PropertiesGenerator(c,
                new PropertyDeclarationGenerator(c, dataTypes.ToList(), new EntityDescriptionGenerator(c)),
                new PropertyBodyGenerator(c)
                )
            )
        );
}

This makes it easy to change for instance how and which attributes are generated for the classes.

Controlling parsing (code-first)

Parsing code is done by composite ContentTypeCodeParserBase implementations.

For some amazing reason, it's a lot easier than generating code. :)

    protected List<ContentTypeCodeParserBase> CreateDocumentTypeParser()
    {
        return new List<ContentTypeCodeParserBase>
        {
            new DocumentTypeInfoParser(Configuration),
            new StructureParser(Configuration),
            new PropertiesParser(Configuration,
                new PropertyParser(Configuration, DataTypes)
            ),
            new TabsParser(Configuration)
        };
    }

XML

XML is just serialized and deserialized from DTOs. Done by manually building an XDocument.

Clone this wiki locally