Bewerken

Delen via


GDL Exercise 2: Inheriting from Virtual Templates

Exercise

Define a data type that accepts a Unicode string that is encoded by using 2 bytes to represent 1 Unicode character. Then, define non-virtual templates that inherit from the previously defined virtual templates. Do not modify the schema that you created in Exercise 1. Create a sample GDL data file and verify the schema is applied correctly. Create a sample GDL data file that does not conform to the schema and verify the errors are detected.

Solution

The following two templates define the Unicode data type.

*Include: MasterTemplate.gdl
 
*Template:  XML_STRING
{
    *Type:  DATATYPE
    *DataType:   XML_TYPE
    *XMLDataType: "string"
}
*Template:  NORMAL_STRING
{
    *Type:  DATATYPE
    *DataType:   FILTER_TYPE
    *ElementType:  XML_STRING
    *FilterTypeName: "NORMAL_STRING"
}

The following template inherits from the templates that are defined in Exercise 1. There is a construct named *Command and three types of attributes: *Name (which appears at the root level), *CommandName (which can appear within a *Command construct), and *UniName (which can appear within both contexts).

*Template:  COMMAND
{
    *Name:  "*Command"
    *Inherits: CONSTRUCTS
    *Instances:  <ANY>
}
*Template:  ROOT_NAME
{
    *Name:  "*Name"
    *Inherits: ROOT_ATTRIB
    *ValueType:  NORMAL_STRING
}
*Template:  CMD_NAME
{
    *Name:  "*CommandName"
    *Inherits: CONSTRUCT_ATTRIB
    *ValueType:  NORMAL_STRING
}
*Template:  UNIVERSAL_NAME
{
    *Name:  "*UniName"
    *Inherits: FREEFLOAT
    *ValueType:  NORMAL_STRING
}

The following GDL file conforms to the given schema.

*Name: "can only appear at root level"
*UniName:  "can appear anywhere"
*Command: X
{
    *CommandName:  "May only appear within a command"
    *UniName:  "can appear anywhere, even in a command"
    *Command: nested
    {
        *CommandName:  "nested commands are ok."
        *UniName:  "template defined a recursive nesting" 100 %
    }
}

The following GDL file does not conform to the given schema.

*CommandName:  "Error! May only appear within a command"
*Command: X
{
    *Name: "Error! can only appear at root level"
}