unit DelphiXMLDataBindingResources; interface type TDelphiXMLSection = (dxsForward, dxsInterface, dxsClass, dxsImplementation); TDelphiXMLMember = (dxmPropertyGet, dxmPropertySet, dxmPropertyDeclaration); TDelphiAccessor = (daGet, daSet); TDelphiNodeType = (dntElement, dntAttribute, dntCustom); const CrLf = #13#10; UnitHeader = '{' + CrLf + ' X2Software XML Data Binding' + CrLf + '' + CrLf + ' Generated on: %:s' + CrLf + ' Generated from: %:s' + CrLf + '}' + CrLf + 'unit %:s;' + CrLf + '' + CrLf; UnitInterface = 'interface' + CrLf + 'uses' + CrLf + '%:s' + ' Classes,' + CrLf + ' XMLDoc,' + CrLf + ' XMLIntf;' + CrLf + '' + CrLf + 'type' + CrLf; UnitImplementation = 'implementation' + CrLf; UnitFooter = '' + CrLf + 'end.' + CrLf; DocumentBinding = 'GetDocBinding(''%:s'', TXML%:s, TargetNamespace) as IXML%:s'; DocumentFunctionsInterface = ' function Get%:s(ADocument: IXMLDocument): IXML%:s;' + CrLf + ' function Load%:s(const AFileName: String): IXML%:s;' + CrLf + ' function Load%:sFromStream(AStream: TStream): IXML%:s;' + CrLf + ' function New%:s: IXML%:s;' + CrLf; DocumentFunctionsImplementation = 'function Get%:s(ADocument: IXMLDocument): IXML%:s;' + CrLf + 'begin' + CrLf + ' Result := ADocument.' + DocumentBinding + CrLf + 'end;' + CrLf + '' + CrLf + 'function Load%:s(const AFileName: String): IXML%:s;' + CrLf + 'begin' + CrLf + ' Result := LoadXMLDocument(AFileName).' + DocumentBinding + CrLf + 'end;' + CrLf + '' + CrLf + 'function Load%:sFromStream(AStream: TStream): IXML%:s;' + CrLf + 'var' + CrLf + ' doc: IXMLDocument;' + CrLf + '' + CrLf + 'begin' + CrLf + ' doc := NewXMLDocument;' + CrLf + ' doc.LoadFromStream(AStream);' + CrLf + ' Result := Get%:s(doc);' + CrLf + 'end;' + CrLf + '' + CrLf + 'function New%:s: IXML%:s;' + CrLf + 'begin' + CrLf + ' Result := NewXMLDocument.' + DocumentBinding + CrLf + 'end;' + CrLf + '' + CrLf; PropertyIntfMethodGetOptional = ' function GetHas%:s: Boolean;'; PropertyIntfMethodGetNil = ' function Get%:sIsNil: Boolean;'; PropertyIntfMethodGetText = ' function Get%:sText: WideString;'; PropertyIntfMethodGet = ' function Get%:s: %:s;'; PropertyIntfMethodSetNil = ' procedure Set%:sIsNil(const Value: Boolean);'; PropertyIntfMethodSetText = ' procedure Set%:sText(const Value: WideString);'; PropertyIntfMethodSet = ' procedure Set%:s(const Value: %:s);'; PropertyInterfaceOptional = ' property Has%:s: Boolean read GetHas%:s;'; PropertyInterfaceNilReadOnly = ' property %:sIsNil: Boolean read Get%:sIsNil;'; PropertyInterfaceNil = ' property %:sIsNil: Boolean read Get%:sIsNil write Set%:sIsNil;'; PropertyInterfaceTextReadOnly = ' property %:sText: WideString read Get%:sText;'; PropertyInterfaceReadOnly = ' property %:s: %:s read Get%:s;'; PropertyInterfaceText = ' property %:sText: WideString read Get%:sText write Set%:sText;'; PropertyInterface = ' property %:s: %:s read Get%:s write Set%:s;'; PropertyImplMethodGetOptional = 'function TXML%:s.GetHas%:s: Boolean;' + CrLf + 'begin' + CrLf + ' Result := Assigned(ChildNodes.FindNode(''%:s''));' + CrLf + 'end;' + CrLf + '' + CrLf; PropertyImplMethodGetNil = 'function TXML%:s.Get%:sIsNil: Boolean;' + CrLf + 'begin' + CrLf + ' Result := GetNodeIsNil(ChildNodes[''%:s'']);' + CrLf + 'end;' + CrLf + '' + CrLf; PropertyImplMethodSetNil = 'procedure TXML%:s.Set%:sIsNil(const Value: Boolean);' + CrLf + 'begin' + CrLf + ' SetNodeIsNil(ChildNodes[''%:s''], Value);' + CrLf + 'end;' + CrLf + '' + CrLf; PropertyImplMethodGetText = 'function TXML%:s.Get%:sText: WideString;' + CrLf + 'begin' + CrLf + ' Result := ChildNodes[''%:s''].Text;' + CrLf + 'end;' + CrLf + '' + CrLf; PropertyImplMethodSetText = 'procedure TXML%:s.Set%:sText(const Value: WideString);' + CrLf + 'begin' + CrLf + ' ChildNodes[''%:s''].NodeValue := Value;' + CrLf + 'end;' + CrLf + '' + CrLf; SectionComments: array[TDelphiXMLSection] of String = ( ' { Forward declarations for %:s }', ' { Interfaces for %:s }', ' { Classes for %:s }', '{ Implementation for %:s }' ); PrefixInterface = 'IXML'; PrefixClass = 'TXML'; PrefixField = 'F'; InterfaceItemForward = ' IXML%:s = interface;'; InterfaceItemInterface = ' IXML%:s = interface(%:s)'; InterfaceItemClass = ' TXML%:s = class(%:s, IXML%:s)'; CollectionInterface = 'IXMLNodeCollection'; CollectionClass = 'TXMLNodeCollection'; ItemInterface = 'IXMLNode'; ItemClass = 'TXMLNode'; // #ToDo1 (MvR) 9-3-2008: document / node / etc // #ToDo1 (MvR) 9-3-2008: WideString etc ? ReservedWords: array[0..106] of String = ( 'absolute', 'abstract', 'and', 'array', 'as', 'asm', 'assembler', {'automated', }'begin', 'case', 'cdecl', 'class', 'const', 'constructor', {'contains', }'default', 'deprecated', 'destructor', 'dispid', 'dispinterface', 'div', 'do', 'downto', 'dynamic', 'else', 'end', 'except', 'export', 'exports', 'external', 'far', {'file', 'final', }'finalization', 'finally', 'for', 'forward', 'function', 'goto', 'if', 'implementation', 'implements', 'in', 'index', 'inherited', 'initialization', 'inline', 'interface', 'is', 'label', 'library', 'local', 'message', 'mod', {'name', }'near', 'nil', 'nodefault', 'not', 'object', 'of', 'or', 'out', 'overload', 'override', 'package', 'packed', 'pascal', 'platform', 'private', 'procedure', 'program', 'property', 'protected', 'public', 'published', 'raise', 'read', 'readonly', 'record', 'register', 'reintroduce', 'repeat', 'requires', 'resident', 'resourcestring', 'safecall', 'sealed', 'set', 'shl', 'shr', 'static', 'stdcall', 'stored', 'string', 'then', 'threadvar', 'to', 'try', 'type', 'unit', 'unsafe', 'until', 'uses', 'var', 'varargs', 'virtual', 'while', 'with', 'write', 'writeonly', 'xor' ); SafeChars = ['A'..'Z', 'a'..'z', '0'..'9', '_', '-']; type TTypeConversion = (tcNone, tcBoolean, tcFloat, tcDateTime, tcDate, tcTime, tcString, tcBase64); TTypeConversions = set of TTypeConversion; TTypeMapping = record SchemaName: String; DelphiName: String; Conversion: TTypeConversion; end; const SimpleTypeMapping: array[0..10] of TTypeMapping = ( (SchemaName: 'int'; DelphiName: 'Integer'; Conversion: tcNone), (SchemaName: 'integer'; DelphiName: 'Integer'; Conversion: tcNone), (SchemaName: 'short'; DelphiName: 'Smallint'; Conversion: tcNone), (SchemaName: 'date'; DelphiName: 'TDateTime'; Conversion: tcDate), (SchemaName: 'time'; DelphiName: 'TDateTime'; Conversion: tcTime), (SchemaName: 'dateTime'; DelphiName: 'TDateTime'; Conversion: tcDateTime), (SchemaName: 'float'; DelphiName: 'Double'; Conversion: tcFloat), (SchemaName: 'double'; DelphiName: 'Double'; Conversion: tcFloat), (SchemaName: 'boolean'; DelphiName: 'Boolean'; Conversion: tcBoolean), (SchemaName: 'string'; DelphiName: 'WideString'; Conversion: tcString), (SchemaName: 'base64Binary'; DelphiName: 'WideString'; Conversion: tcBase64) ); TypeConversionReqUtils: array[TTypeConversion] of Boolean = ( { tcNone } False, { tcBoolean } True, { tcFloat } True, { tcDateTime } True, { tcDate } True, { tcTime } True, { tcString } False, { tcBase64 } True ); TypeConversionNone: array[TDelphiAccessor, TDelphiNodeType] of String = ( { daGet } ( { dntElement } ' %:s := ChildNodes[''%:s''].NodeValue;', { dntAttribute } ' %:s := AttributeNodes[''%:s''].NodeValue;', { dntCustom } ' %:s := %:s;' ), { daSet } ( { dntElement } ' ChildNodes[''%:s''].NodeValue := %:s;', { dntAttribute } ' SetAttribute(''%:s'', %:s);', { dntCustom } ' %:s := %:s;' ) ); TypeConversion: array[TDelphiAccessor, TDelphiNodeType, TTypeConversion] of String = ( { daGet } ( { dntElement } ( { tcNone } '', { tcBoolean } '', { tcFloat } ' %:s := XMLToFloat(ChildNodes[''%:s''].NodeValue);', { tcDateTime } ' %:s := XMLToDateTime(ChildNodes[''%:s''].NodeValue, xdtDateTime);', { tcDate } ' %:s := XMLToDateTime(ChildNodes[''%:s''].NodeValue, xdtDate);', { tcTime } ' %:s := XMLToDateTime(ChildNodes[''%:s''].NodeValue, xdtTime);', { tcString } ' %:s := ChildNodes[''%:s''].Text;', { tcBas64 } ' %:s := Base64Decode(Trim(ChildNodes[''%:s''].Text));' ), { dntAttribute } ( { tcNone } '', { tcBoolean } '', { tcFloat } ' %:s := XMLToFloat(AttributeNodes[''%:s''].NodeValue);', { tcDateTime } ' %:s := XMLToDateTime(AttributeNodes[''%:s''].NodeValue, xdtDateTime);', { tcDate } ' %:s := XMLToDateTime(AttributeNodes[''%:s''].NodeValue, xdtDate);', { tcTime } ' %:s := XMLToDateTime(AttributeNodes[''%:s''].NodeValue, xdtTime);', { tcString } ' %:s := AttributeNodes[''%:s''].Text;', { tcBase64 } ' %:s := Base64Decode(Trim(AttributeNodes[''%:s''].Text));' ), { dntCustom} ( { tcNone } '', { tcBoolean } '', { tcFloat } ' %:s := XMLToFloat(%:s);', { tcDateTime } ' %:s := XMLToDateTime(%:s, xdtDateTime);', { tcDate } ' %:s := XMLToDateTime(%:s, xdtDate);', { tcTime } ' %:s := XMLToDateTime(%:s, xdtTime);', { tcString } '', { tcBase64 } ' %:s := Base64Decode(Trim(%:s));' ) ), { daSet } ( { dntElement } ( { tcNone } '', { tcBoolean } ' ChildNodes[''%:s''].NodeValue := BoolToXML(%:s);', { tcFloat } ' ChildNodes[''%:s''].NodeValue := FloatToXML(%:s);', { tcDateTime } ' ChildNodes[''%:s''].NodeValue := DateTimeToXML(%:s, xdtDateTime);', { tcDate } ' ChildNodes[''%:s''].NodeValue := DateTimeToXML(%:s, xdtDate);', { tcTime } ' ChildNodes[''%:s''].NodeValue := DateTimeToXML(%:s, xdtTime);', { tcString } '', { tcBase64 } ' ChildNodes[''%:s''].NodeValue := Base64Encode(%:s);' ), { dntAttribute } ( { tcNone } '', { tcBoolean } ' SetAttribute(''%:s'', BoolToXML(%:s));', { tcFloat } ' SetAttribute(''%:s'', FloatToXML(%:s));', { tcDateTime } ' SetAttribute(''%:s'', DateTimeToXML(%:s, xdtDateTime));', { tcDate } ' SetAttribute(''%:s'', DateTimeToXML(%:s, xdtDate));', { tcTime } ' SetAttribute(''%:s'', DateTimeToXML(%:s, xdtTime));', { tcString } '', { tcBase64 } ' SetAttribute(''%:s'', Base64Encode(%:s));' ), { dntCustom} ( { tcNone } '', { tcBoolean } ' %:s := BoolToXML(%:s);', { tcFloat } ' %:s := FloatToXML(%:s);', { tcDateTime } ' %:s := DateTimeToXML(%:s, xdtDateTime);', { tcDate } ' %:s := DateTimeToXML(%:s, xdtDate);', { tcTime } ' %:s := DateTimeToXML(%:s, xdtTime);', { tcString } '', { tcBase64 } ' %:s := Base64Encode(%:s);' ) ) ); implementation end.