1
0
mirror of synced 2024-11-14 19:13:50 +00:00
x2xmldatabinding/Units/DelphiXMLDataBindingGenerator.pas

1040 lines
34 KiB
ObjectPascal
Raw Normal View History

2008-02-20 06:52:00 +00:00
unit DelphiXMLDataBindingGenerator;
interface
uses
Classes,
XMLSchema,
2008-03-09 20:36:27 +00:00
X2UtHashes,
DelphiXMLDataBindingResources,
2008-02-20 06:52:00 +00:00
XMLDataBindingGenerator,
XMLDataBindingHelpers;
2008-02-20 06:52:00 +00:00
type
TGetFileNameEvent = procedure(Sender: TObject; const SchemaName: String; var Result: String) of object;
2008-02-20 06:52:00 +00:00
TDelphiXMLDataBindingGenerator = class(TXMLDataBindingGenerator)
private
FOnGetFileName: TGetFileNameEvent;
2008-03-09 20:36:27 +00:00
FProcessedItems: TX2OIHash;
2008-02-20 06:52:00 +00:00
protected
procedure GenerateDataBinding(); override;
procedure GenerateSingleDataBinding();
procedure GenerateMultipleDataBinding();
2008-03-09 20:36:27 +00:00
function DelphiSafeName(const AName: String): String;
function TranslateItemName(AItem: TXMLDataBindingItem): String; override;
2008-02-20 06:52:00 +00:00
function DoGetFileName(const ASchemaName: String): String;
2008-03-09 20:36:27 +00:00
function GetDataTypeMapping(ADataType: IXMLTypeDef; out ATypeMapping: TTypeMapping): Boolean;
2008-02-26 21:53:11 +00:00
function TranslateDataType(ADataType: IXMLTypeDef): String;
function CreateNewGUID(): String;
2008-02-20 06:52:00 +00:00
procedure WriteUnitHeader(AStream: TStreamHelper; const AFileName: String);
2008-03-09 20:36:27 +00:00
procedure WriteSection(AStream: TStreamHelper; ASection: TDelphiXMLSection);
procedure WriteDocumentFunctions(AStream: TStreamHelper; ASection: TDelphiXMLSection);
procedure WriteEnumerationConstants(AStream: TStreamHelper);
procedure WriteEnumerationConversions(AStream: TStreamHelper);
procedure WriteDocumentation(AStream: TStreamHelper; AItem: TXMLDataBindingItem);
2008-03-09 20:36:27 +00:00
procedure WriteSchemaItem(AStream: TStreamHelper; AItem: TXMLDataBindingItem; ASection: TDelphiXMLSection);
procedure WriteSchemaInterface(AStream: TStreamHelper; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
procedure WriteSchemaInterfaceProperties(AStream: TStreamHelper; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
procedure WriteSchemaCollection(AStream: TStreamHelper; AItem: TXMLDataBindingCollection; ASection: TDelphiXMLSection);
procedure WriteSchemaCollectionProperties(AStream: TStreamHelper; AItem: TXMLDataBindingCollection; ASection: TDelphiXMLSection);
procedure WriteSchemaEnumeration(AStream: TStreamHelper; AItem: TXMLDataBindingEnumeration; ASection: TDelphiXMLSection);
procedure WriteSchemaEnumerationArray(AStream: TStreamHelper; AItem: TXMLDataBindingEnumeration);
function DataTypeConversion(const ADestination, ASource: string; ADataType: IXMLTypeDef; AToNative: Boolean; const ALinesBefore: string = ''): string;
function XMLToNativeDataType(const ADestination, ASource: string; ADataType: IXMLTypeDef; const ALinesBefore: string = ''): string;
function NativeDataTypeToXML(const ADestination, ASource: string; ADataType: IXMLTypeDef; const ALinesBefore: string = ''): string;
2008-03-09 20:36:27 +00:00
property ProcessedItems: TX2OIHash read FProcessedItems;
2008-02-20 06:52:00 +00:00
public
property OnGetFileName: TGetFileNameEvent read FOnGetFileName write FOnGetFileName;
end;
2008-02-20 06:52:00 +00:00
implementation
uses
Contnrs,
SysUtils,
X2UtNamedFormat;
2008-02-20 06:52:00 +00:00
2008-03-09 20:36:27 +00:00
2008-02-20 06:52:00 +00:00
{ TDelphiXMLDataBindingGenerator }
procedure TDelphiXMLDataBindingGenerator.GenerateDataBinding();
begin
case OutputType of
otSingle: GenerateSingleDataBinding();
otMultiple: GenerateMultipleDataBinding();
end;
end;
procedure TDelphiXMLDataBindingGenerator.GenerateSingleDataBinding();
var
unitName: String;
unitStream: TStreamHelper;
begin
2008-03-09 20:36:27 +00:00
unitName := DoGetFileName(Schemas[0].SchemaName);
2008-02-20 06:52:00 +00:00
unitStream := TStreamHelper.Create(TFileStream.Create(unitName, fmCreate), soOwned);
try
WriteUnitHeader(unitStream, unitName);
unitStream.Write(UnitInterface);
2008-03-09 20:36:27 +00:00
WriteSection(unitStream, dxsForward);
FProcessedItems := TX2OIHash.Create();
try
FProcessedItems.Clear();
WriteSection(unitStream, dxsInterface);
2008-03-09 20:36:27 +00:00
FProcessedItems.Clear();
WriteSection(unitStream, dxsClass);
finally
FreeAndNil(FProcessedItems);
2008-02-20 06:52:00 +00:00
end;
2008-03-09 20:36:27 +00:00
WriteDocumentFunctions(unitStream, dxsInterface);
WriteEnumerationConstants(unitStream);
2008-02-26 21:53:11 +00:00
unitStream.Write(UnitImplementation);
2008-03-09 20:36:27 +00:00
WriteDocumentFunctions(unitStream, dxsImplementation);
WriteEnumerationConversions(unitStream);
// #ToDo1 (MvR) 20-3-2008: write conversion methods
2008-03-09 20:36:27 +00:00
WriteSection(unitStream, dxsImplementation);
2008-02-20 06:52:00 +00:00
unitStream.Write(unitFooter);
2008-02-20 06:52:00 +00:00
finally
FreeAndNil(unitStream);
end;
end;
procedure TDelphiXMLDataBindingGenerator.GenerateMultipleDataBinding();
begin
end;
function TDelphiXMLDataBindingGenerator.GetDataTypeMapping(ADataType: IXMLTypeDef; out ATypeMapping: TTypeMapping): Boolean;
2008-03-09 20:36:27 +00:00
var
mappingIndex: Integer;
dataTypeName: string;
2008-02-26 21:53:11 +00:00
begin
2008-03-09 20:36:27 +00:00
Assert(not ADataType.IsComplex, 'Complex DataTypes not supported');
Assert(ADataType.Enumerations.Count = 0, 'Enumerations not supported');
Result := False;
2008-02-26 21:53:11 +00:00
2008-03-09 20:36:27 +00:00
if (ADataType.NamespaceURI = SXMLSchemaURI_1999) or
(ADataType.NamespaceURI = SXMLSchemaURI_2000_10) or
(ADataType.NamespaceURI = SXMLSchemaURI_2001) then
2008-02-26 21:53:11 +00:00
begin
2008-03-09 20:36:27 +00:00
dataTypeName := ADataType.Name;
for mappingIndex := Low(SimpleTypeMapping) to High(SimpleTypeMapping) do
if SimpleTypeMapping[mappingIndex].SchemaName = dataTypeName then
2008-03-09 20:36:27 +00:00
begin
ATypeMapping := SimpleTypeMapping[mappingIndex];
Result := True;
2008-03-09 20:36:27 +00:00
Break;
end;
end;
end;
function TDelphiXMLDataBindingGenerator.TranslateDataType(ADataType: IXMLTypeDef): String;
var
typeMapping: TTypeMapping;
begin
Result := 'Variant';
if GetDataTypeMapping(ADataType, typeMapping) then
Result := typeMapping.DelphiName;
2008-03-09 20:36:27 +00:00
end;
function TDelphiXMLDataBindingGenerator.DelphiSafeName(const AName: String): String;
var
wordIndex: Integer;
begin
Result := AName;
for wordIndex := Low(ReservedWords) to High(ReservedWords) do
2008-02-26 21:53:11 +00:00
begin
2008-03-09 20:36:27 +00:00
if Result = ReservedWords[wordIndex] then
2008-02-26 21:53:11 +00:00
begin
2008-03-09 20:36:27 +00:00
Result := '_' + Result;
Break;
2008-02-26 21:53:11 +00:00
end;
end;
end;
2008-03-09 20:36:27 +00:00
function TDelphiXMLDataBindingGenerator.TranslateItemName(AItem: TXMLDataBindingItem): String;
begin
Result := DelphiSafeName(inherited TranslateItemName(AItem));
case AItem.ItemType of
itEnumerationMember:
Result := TXMLDataBindingEnumerationMember(AItem).Enumeration.TranslatedName + '_' + Result;
end;
end;
2008-02-20 06:52:00 +00:00
procedure TDelphiXMLDataBindingGenerator.WriteUnitHeader(AStream: TStreamHelper; const AFileName: String);
begin
2008-03-09 20:36:27 +00:00
// #ToDo3 (MvR) 14-4-2007: if outputtype = multiple, use include files
AStream.WriteNamedFmt(UnitHeader,
['SourceFileName', SourceFileName,
'UnitName', ChangeFileExt(ExtractFileName(AFileName), '')]);
2008-03-09 20:36:27 +00:00
end;
procedure TDelphiXMLDataBindingGenerator.WriteSection(AStream: TStreamHelper; ASection: TDelphiXMLSection);
var
schemaIndex: Integer;
schema: TXMLDataBindingSchema;
itemIndex: Integer;
begin
for schemaIndex := 0 to Pred(SchemaCount) do
begin
schema := Schemas[schemaIndex];
AStream.WriteLnNamedFmt(SectionComments[ASection],
['SchemaName', schema.SchemaName]);
2008-03-09 20:36:27 +00:00
for itemIndex := 0 to Pred(schema.ItemCount) do
WriteSchemaItem(AStream, schema.Items[itemIndex], ASection);
AStream.WriteLn;
end;
end;
procedure TDelphiXMLDataBindingGenerator.WriteDocumentFunctions(AStream: TStreamHelper; ASection: TDelphiXMLSection);
var
schemaIndex: Integer;
schema: TXMLDataBindingSchema;
itemIndex: Integer;
item: TXMLDataBindingItem;
interfaceItem: TXMLDataBindingInterface;
hasItem: Boolean;
begin
hasItem := False;
for schemaIndex := 0 to Pred(SchemaCount) do
2008-02-20 06:52:00 +00:00
begin
2008-03-09 20:36:27 +00:00
schema := Schemas[schemaIndex];
for itemIndex := 0 to Pred(schema.ItemCount) do
2008-02-20 06:52:00 +00:00
begin
2008-03-09 20:36:27 +00:00
item := schema.Items[itemIndex];
2008-02-20 06:52:00 +00:00
2008-03-09 20:36:27 +00:00
if item.ItemType = itInterface then
2008-02-20 06:52:00 +00:00
begin
2008-03-09 20:36:27 +00:00
interfaceItem := TXMLDataBindingInterface(item);
if item.DocumentElement then
2008-02-26 21:53:11 +00:00
begin
2008-03-09 20:36:27 +00:00
if not hasItem then
begin
if ASection = dxsInterface then
AStream.Write(' ');
2008-03-17 12:17:55 +00:00
2008-03-09 20:36:27 +00:00
AStream.WriteLn('{ Document functions }');
hasItem := True;
end;
2008-03-17 12:17:55 +00:00
with TNamedFormatStringList.Create() do
try
case ASection of
dxsInterface: Add(DocumentFunctionsInterface);
dxsImplementation: Add(DocumentFunctionsImplementation);
2008-03-17 12:17:55 +00:00
end;
2008-03-09 20:36:27 +00:00
AStream.Write(Format(['SourceName', interfaceItem.Name,
'Name', interfaceItem.TranslatedName]));
2008-03-17 12:17:55 +00:00
finally
Free();
end;
2008-03-09 20:36:27 +00:00
AStream.WriteLn();
2008-02-26 21:53:11 +00:00
end;
2008-03-09 20:36:27 +00:00
end;
2008-02-20 06:52:00 +00:00
end;
end;
2008-03-09 20:36:27 +00:00
if hasItem and (ASection = dxsInterface) then
begin
// #ToDo3 (MvR) 9-3-2008: namespace support?
AStream.WriteLn('const');
AStream.WriteLn(' TargetNamespace = '''';');
AStream.WriteLn();
end;
end;
procedure TDelphiXMLDataBindingGenerator.WriteEnumerationConstants(AStream: TStreamHelper);
2008-02-20 06:52:00 +00:00
var
2008-03-09 20:36:27 +00:00
item: TXMLDataBindingItem;
itemIndex: Integer;
schema: TXMLDataBindingSchema;
schemaIndex: Integer;
enumerations: TObjectList;
2008-02-20 06:52:00 +00:00
begin
2008-03-09 20:36:27 +00:00
{ Write array constants for enumerations }
enumerations := TObjectList.Create(False);
try
for schemaIndex := 0 to Pred(SchemaCount) do
2008-02-20 06:52:00 +00:00
begin
schema := Schemas[schemaIndex];
2008-03-09 20:36:27 +00:00
for itemIndex := 0 to Pred(schema.ItemCount) do
2008-02-26 21:53:11 +00:00
begin
item := schema.Items[itemIndex];
2008-03-09 20:36:27 +00:00
if item.ItemType = itEnumeration then
enumerations.Add(item);
2008-02-26 21:53:11 +00:00
end;
2008-02-20 06:52:00 +00:00
end;
if enumerations.Count > 0 then
begin
AStream.WriteLn('const');
for itemIndex := 0 to Pred(enumerations.Count) do
WriteSchemaEnumerationArray(AStream, TXMLDataBindingEnumeration(enumerations[itemIndex]));
end;
finally
FreeAndNil(enumerations);
2008-02-20 06:52:00 +00:00
end;
2008-03-09 20:36:27 +00:00
end;
2008-02-20 06:52:00 +00:00
procedure TDelphiXMLDataBindingGenerator.WriteEnumerationConversions(AStream: TStreamHelper);
begin
//
end;
2008-03-09 20:36:27 +00:00
procedure TDelphiXMLDataBindingGenerator.WriteDocumentation(AStream: TStreamHelper; AItem: TXMLDataBindingItem);
var
lines: TStringList;
lineIndex: Integer;
begin
// #ToDo2 (MvR) 9-3-2008: check for Delphi comment-ending sequences
if not AItem.HasDocumentation then
exit;
lines := TStringList.Create();
try
lines.Text := WrapText(AItem.Documentation, 76);
AStream.WriteLn(' {');
for lineIndex := 0 to Pred(lines.Count) do
AStream.WriteLn(' ' + lines[lineIndex]);
AStream.WriteLn(' }');
finally
FreeAndNil(lines);
end;
end;
2008-02-26 21:53:11 +00:00
2008-02-20 06:52:00 +00:00
2008-03-09 20:36:27 +00:00
procedure TDelphiXMLDataBindingGenerator.WriteSchemaItem(AStream: TStreamHelper; AItem: TXMLDataBindingItem; ASection: TDelphiXMLSection);
begin
case AItem.ItemType of
itInterface: WriteSchemaInterface(AStream, TXMLDataBindingInterface(AItem), ASection);
itCollection: WriteSchemaCollection(AStream, TXMLDataBindingCollection(AItem), ASection);
itEnumeration: WriteSchemaEnumeration(AStream, TXMLDataBindingEnumeration(AItem), ASection);
2008-02-20 06:52:00 +00:00
end;
end;
2008-03-09 20:36:27 +00:00
procedure TDelphiXMLDataBindingGenerator.WriteSchemaInterface(AStream: TStreamHelper; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
var
parent: String;
2008-02-20 06:52:00 +00:00
begin
2008-03-09 20:36:27 +00:00
if ASection in [dxsInterface, dxsClass] then
begin
{ Ensure the base item is completely defined first, Delphi doesn't allow
inheritance with just a forward declaration. }
if ProcessedItems.Exists(AItem) then
exit;
if Assigned(AItem.BaseItem) then
WriteSchemaInterface(AStream, AItem.BaseItem, ASection);
ProcessedItems[AItem] := 1;
end;
2008-02-26 21:53:11 +00:00
case ASection of
dxsForward:
AStream.WriteLnNamedFmt(InterfaceItemForward,
['Name', AItem.TranslatedName]);
2008-02-26 21:53:11 +00:00
dxsInterface:
begin
2008-03-09 20:36:27 +00:00
if Assigned(AItem.BaseItem) then
2008-03-17 12:17:55 +00:00
parent := PrefixInterface + AItem.BaseItem.TranslatedName
2008-03-09 20:36:27 +00:00
else
parent := ItemInterface;
WriteDocumentation(AStream, AItem);
AStream.WriteLnNamedFmt(InterfaceItemInterface,
['Name', AItem.TranslatedName,
'ParentName', parent]);
AStream.WriteLn(' ' + CreateNewGUID());
2008-03-09 20:36:27 +00:00
WriteSchemaInterfaceProperties(AStream, AItem, ASection);
2008-02-26 21:53:11 +00:00
AStream.WriteLn(' end;');
AStream.WriteLn();
end;
dxsClass:
begin
2008-03-09 20:36:27 +00:00
if Assigned(AItem.BaseItem) then
2008-03-17 12:17:55 +00:00
parent := PrefixClass + AItem.BaseItem.TranslatedName
2008-03-09 20:36:27 +00:00
else
parent := ItemClass;
AStream.WriteLnNamedFmt(InterfaceItemClass,
['Name', AItem.TranslatedName,
'ParentName', parent]);
2008-03-09 20:36:27 +00:00
WriteSchemaInterfaceProperties(AStream, AItem, ASection);
2008-02-26 21:53:11 +00:00
AStream.WriteLn(' end;');
AStream.WriteLn();
end;
dxsImplementation:
begin
2008-03-09 20:36:27 +00:00
WriteSchemaInterfaceProperties(AStream, AItem, ASection);
2008-02-26 21:53:11 +00:00
end;
end;
end;
2008-03-09 20:36:27 +00:00
procedure TDelphiXMLDataBindingGenerator.WriteSchemaInterfaceProperties(AStream: TStreamHelper; AItem: TXMLDataBindingInterface; ASection: TDelphiXMLSection);
procedure WriteAfterConstruction;
var
propertyIndex: Integer;
propertyItem: TXMLDataBindingProperty;
itemProperty: TXMLDataBindingItemProperty;
hasInterface: Boolean;
begin
hasInterface := False;
for propertyIndex := 0 to Pred(AItem.PropertyCount) do
begin
propertyItem := AItem.Properties[propertyIndex];
if propertyItem.PropertyType = ptItem then
begin
itemProperty := TXMLDataBindingItemProperty(propertyItem);
if Assigned(itemProperty.Item) and
(itemProperty.Item.ItemType <> itEnumeration) then
begin
case ASection of
dxsClass:
begin
AStream.WriteLn(' public');
AStream.WriteLn(' procedure AfterConstruction; override;');
break;
end;
dxsImplementation:
begin
if not hasInterface then
begin
AStream.WriteLnFmt('procedure TXML%s.AfterConstruction;', [AItem.TranslatedName]);
2008-03-09 20:36:27 +00:00
AStream.WriteLn('begin');
hasInterface := True;
end;
AStream.WriteLnNamedFmt(' RegisterChildNode(''%<SourceName>:s'', TXML%<Name>:s);',
['SourceName', itemProperty.Item.Name,
'Name', itemProperty.Item.TranslatedName]);
2008-03-09 20:36:27 +00:00
end;
end;
end;
end;
end;
if (ASection = dxsImplementation) and hasInterface then
begin
AStream.WriteLn('end;');
AStream.WriteLn();
end;
end;
2008-02-26 21:53:11 +00:00
var
2008-03-09 20:36:27 +00:00
propertyIndex: Integer;
itemProperty: TXMLDataBindingProperty;
propertyItem: TXMLDataBindingItem;
dataTypeName: String;
writeOptional: Boolean;
writeTextProp: Boolean;
hasMembers: Boolean;
member: TDelphiXMLMember;
value: String;
2008-03-17 12:17:55 +00:00
sourceCode: TNamedFormatStringList;
propertyItemName: String;
2008-02-26 21:53:11 +00:00
begin
2008-03-09 20:36:27 +00:00
// #ToDo1 (MvR) 9-3-2008: refactor WriteSchemaInterfaceProperties
if ASection = dxsForward then
Exit;
2008-02-26 21:53:11 +00:00
2008-03-09 20:36:27 +00:00
if ASection = dxsImplementation then
WriteAfterConstruction();
2008-02-26 21:53:11 +00:00
if ASection = dxsClass then
AStream.WriteLn(' protected');
2008-03-09 20:36:27 +00:00
hasMembers := False;
2008-03-09 20:36:27 +00:00
for member := Low(TDelphiXMLMember) to High(TDelphiXMLMember) do
2008-02-20 06:52:00 +00:00
begin
if hasMembers then
AStream.WriteLn;
hasMembers := False;
2008-03-09 20:36:27 +00:00
for propertyIndex := 0 to Pred(AItem.PropertyCount) do
begin
itemProperty := AItem.Properties[propertyIndex];
2008-03-17 12:17:55 +00:00
propertyItem := nil;
2008-03-09 20:36:27 +00:00
dataTypeName := '';
writeTextProp := False;
2008-03-12 06:31:09 +00:00
writeOptional := True;
2008-03-09 20:36:27 +00:00
{ Get data type }
case itemProperty.PropertyType of
ptSimple:
dataTypeName := TranslateDataType(TXMLDataBindingSimpleProperty(itemProperty).DataType);
ptItem:
begin
propertyItem := TXMLDataBindingItemProperty(itemProperty).Item;
if Assigned(propertyItem) then
begin
if propertyItem.ItemType = itEnumeration then
begin
2008-03-17 12:17:55 +00:00
dataTypeName := PrefixClass;
2008-03-09 20:36:27 +00:00
writeTextProp := True;
end else
2008-03-17 12:17:55 +00:00
dataTypeName := PrefixInterface;
2008-03-09 20:36:27 +00:00
2008-03-12 06:31:09 +00:00
{ Collections have a Count property, no need to write a
2008-03-17 12:17:55 +00:00
HasX property as well. }
2008-03-12 06:31:09 +00:00
writeOptional := (propertyItem.ItemType <> itCollection);
2008-03-09 20:36:27 +00:00
dataTypeName := dataTypeName + propertyItem.TranslatedName;
end;
end;
end;
if Length(dataTypeName) > 0 then
begin
2008-03-17 12:17:55 +00:00
writeOptional := writeOptional and
itemProperty.IsOptional and
(member in [dxmPropertyGet, dxmPropertyDeclaration]);
2008-03-09 20:36:27 +00:00
2008-03-17 12:17:55 +00:00
sourceCode := TNamedFormatStringList.Create();
try
case ASection of
dxsInterface,
dxsClass:
2008-03-09 20:36:27 +00:00
begin
2008-03-17 12:17:55 +00:00
{ Interface declaration }
case member of
dxmPropertyGet:
begin
if writeOptional then
sourceCode.Add(PropertyIntfMethodGetOptional);
2008-03-09 20:36:27 +00:00
2008-03-17 12:17:55 +00:00
if writeTextProp then
sourceCode.Add(PropertyIntfMethodGetText);
2008-03-09 20:36:27 +00:00
sourceCode.Add(PropertyIntfMethodGet);
hasMembers := True;
2008-03-09 20:36:27 +00:00
end;
2008-03-17 12:17:55 +00:00
dxmPropertySet:
if not itemProperty.IsReadOnly then
begin
if writeTextProp then
sourceCode.Add(PropertyIntfMethodSetText);
2008-03-09 20:36:27 +00:00
sourceCode.Add(PropertyIntfMethodSet);
hasMembers := True;
2008-03-17 12:17:55 +00:00
end;
dxmPropertyDeclaration:
2008-03-09 20:36:27 +00:00
begin
2008-03-17 12:17:55 +00:00
if writeOptional then
sourceCode.Add(PropertyInterfaceOptional);
2008-03-17 12:17:55 +00:00
if itemProperty.IsReadOnly then
begin
if writeTextProp then
sourceCode.Add(PropertyInterfaceTextReadOnly);
2008-03-09 20:36:27 +00:00
sourceCode.Add(PropertyInterfaceReadOnly);
end else
begin
if writeTextProp then
sourceCode.Add(PropertyInterfaceText);
2008-03-09 20:36:27 +00:00
sourceCode.Add(PropertyInterface);
end;
hasMembers := True;
end;
end;
2008-03-17 12:17:55 +00:00
end;
dxsImplementation:
begin
{ Implementation }
case member of
dxmPropertyGet:
begin
if writeOptional then
sourceCode.Add(PropertyImplMethodGetOptional);
2008-03-17 12:17:55 +00:00
if writeTextProp then
sourceCode.Add(PropertyImplMethodGetText);
2008-03-17 12:17:55 +00:00
sourceCode.Add('function TXML%<Name>:s.Get%<PropertyName>:s: %<DataType>:s;');
2008-03-17 12:17:55 +00:00
case itemProperty.PropertyType of
ptSimple:
sourceCode.Add(XMLToNativeDataType('Result',
'ChildNodes[''%<PropertySourceName>:s''].NodeValue',
TXMLDataBindingSimpleProperty(itemProperty).DataType));
2008-03-09 20:36:27 +00:00
2008-03-17 12:17:55 +00:00
ptItem:
begin
if Assigned(propertyItem) then
begin
case propertyItem.ItemType of
itInterface,
itCollection:
begin
sourceCode.Add('begin');
sourceCode.Add(' Result := (ChildNodes[''%<Name>:s''] as IXML%<PropertyItemName>:s);');
2008-03-17 12:17:55 +00:00
sourceCode.Add('end;');
end;
itEnumeration:
begin
sourceCode.Add('var');
sourceCode.Add(' nodeValue: WideString;');
sourceCode.Add(' enumValue: %<DataType>:s;');
2008-03-17 12:17:55 +00:00
sourceCode.AddLn;
sourceCode.Add('begin');
sourceCode.Add(' Result := %<DataType>:s(-1);');
sourceCode.Add(' nodeValue := Get%<PropertyName>:sText;');
sourceCode.Add(' for enumValue := Low(%<DataType>:s) to High(%<DataType>:s) do');
sourceCode.Add(' if %<PropertyName>:sValues[enumValue] = nodeValue then');
2008-03-17 12:17:55 +00:00
sourceCode.Add(' begin');
sourceCode.Add(' Result := enumValue;');
sourceCode.Add(' break;');
sourceCode.Add(' end;');
sourceCode.Add('end;');
2008-03-17 12:17:55 +00:00
end;
2008-03-09 20:36:27 +00:00
end;
2008-03-17 12:17:55 +00:00
end;
2008-03-09 20:36:27 +00:00
end;
2008-03-17 12:17:55 +00:00
end;
2008-03-09 20:36:27 +00:00
2008-03-17 12:17:55 +00:00
sourceCode.AddLn;
end;
dxmPropertySet:
if not itemProperty.IsReadOnly then
2008-03-09 20:36:27 +00:00
begin
2008-03-17 12:17:55 +00:00
if writeTextProp then
sourceCode.Add(PropertyImplMethodSetText);
sourceCode.Add('procedure TXML%<Name>:s.Set%<PropertyName>:s(const Value: %<DataType>:s);');
value := 'ChildNodes[''%<PropertySourceName>:s''].NodeValue';
if Assigned(propertyItem) and (propertyItem.ItemType = itEnumeration) then
2008-03-17 12:17:55 +00:00
begin
sourceCode.Add('begin');
sourceCode.Add(' ' + value + ' := %<PropertyItemName>:sValues[Value]');
2008-03-17 12:17:55 +00:00
sourceCode.Add('end;');
sourceCode.AddLn;
end else
begin
if itemProperty.PropertyType <> ptSimple then
raise Exception.Create('Setter must be a simple type');
2008-03-17 12:17:55 +00:00
sourceCode.Add(NativeDataTypeToXML(value, 'Value',
TXMLDataBindingSimpleProperty(itemProperty).DataType));
end;
2008-03-09 20:36:27 +00:00
end;
2008-03-17 12:17:55 +00:00
end;
2008-03-09 20:36:27 +00:00
end;
end;
2008-03-17 12:17:55 +00:00
propertyItemName := '';
if Assigned(propertyItem) then
propertyItemName := propertyItem.TranslatedName;
AStream.Write(sourceCode.Format(['Name', AItem.TranslatedName,
'PropertySourceName', itemProperty.Name,
'PropertyName', itemProperty.TranslatedName,
'PropertyItemName', propertyItemName,
'DataType', dataTypeName]));
finally
FreeAndNil(sourceCode);
2008-03-09 20:36:27 +00:00
end;
end;
end;
end;
if ASection = dxsClass then
WriteAfterConstruction();
end;
procedure TDelphiXMLDataBindingGenerator.WriteSchemaCollection(AStream: TStreamHelper; AItem: TXMLDataBindingCollection; ASection: TDelphiXMLSection);
begin
2008-03-17 12:17:55 +00:00
if not Assigned(AItem.CollectionItem) then
Exit;
2008-03-09 20:36:27 +00:00
case ASection of
dxsForward:
2008-03-17 12:17:55 +00:00
AStream.WriteLnNamedFmt(InterfaceItemForward,
['Name',
AItem.TranslatedName]);
2008-03-09 20:36:27 +00:00
dxsInterface:
begin
2008-03-17 12:17:55 +00:00
AStream.WriteLnNamedFmt(InterfaceItemInterface,
['Name', AItem.TranslatedName,
'ParentName', CollectionInterface]);
AStream.WriteLn(' ' + CreateNewGUID());
2008-03-09 20:36:27 +00:00
WriteSchemaCollectionProperties(AStream, AItem, ASection);
AStream.WriteLn(' end;');
AStream.WriteLn();
end;
dxsClass:
begin
2008-03-17 12:17:55 +00:00
AStream.WriteLnNamedFmt(InterfaceItemClass,
['Name', AItem.TranslatedName,
'ParentName', CollectionClass]);
2008-03-09 20:36:27 +00:00
WriteSchemaCollectionProperties(AStream, AItem, ASection);
AStream.WriteLn(' end;');
AStream.WriteLn();
end;
dxsImplementation:
begin
WriteSchemaCollectionProperties(AStream, AItem, ASection);
end;
2008-02-26 21:53:11 +00:00
end;
2008-03-09 20:36:27 +00:00
end;
2008-02-26 21:53:11 +00:00
2008-03-09 20:36:27 +00:00
procedure TDelphiXMLDataBindingGenerator.WriteSchemaCollectionProperties(AStream: TStreamHelper; AItem: TXMLDataBindingCollection; ASection: TDelphiXMLSection);
var
2008-03-17 12:17:55 +00:00
dataIntfName: string;
dataTypeName: string;
dataClassName: string;
2008-03-17 12:17:55 +00:00
sourceCode: TNamedFormatStringList;
typeDef: IXMLTypeDef;
2008-03-09 20:36:27 +00:00
begin
if ASection = dxsClass then
AStream.WriteLn(' protected');
// #ToDo1 (MvR) 17-3-2008: DataType for enumerations
case AItem.CollectionItem.PropertyType of
ptSimple:
begin
dataTypeName := TranslateDataType(TXMLDataBindingSimpleProperty(AItem.CollectionItem).DataType);
dataClassName := 'TXMLNode';
dataIntfName := 'IXMLNode';
end;
ptItem:
begin
dataTypeName := PrefixInterface + AItem.CollectionItemTranslatedName;
dataClassName := PrefixClass + AItem.CollectionItemTranslatedName;
dataIntfName := dataTypeName;
end;
end;
2008-03-09 20:36:27 +00:00
2008-03-17 12:17:55 +00:00
sourceCode := TNamedFormatStringList.Create();
try
case ASection of
dxsInterface,
dxsClass:
begin
sourceCode.Add(' function Get_%<ItemName>:s(Index: Integer): %<DataType>:s;');
case AItem.CollectionItem.PropertyType of
ptSimple:
begin
sourceCode.Add(' function Add(%<ItemName>:s: %<DataType>:s): %<DataInterface>:s;');
sourceCode.Add(' function Insert(Index: Integer; %<ItemName>:s: %<DataType>:s): %<DataInterface>:s;');
end;
ptItem:
begin
sourceCode.Add(' function Add: %<DataType>:s;');
sourceCode.Add(' function Insert(Index: Integer): %<DataType>:s;');
end;
end;
2008-03-17 12:17:55 +00:00
end;
dxsImplementation:
begin
sourceCode.Add('procedure TXML%<Name>:s.AfterConstruction;');
2008-03-17 12:17:55 +00:00
sourceCode.Add('begin');
sourceCode.Add(' RegisterChildNode(''%<ItemSourceName>:s'', %<DataClass>:s);');
2008-03-17 12:17:55 +00:00
sourceCode.AddLn;
sourceCode.Add(' ItemTag := ''%<ItemSourceName>:s'';');
sourceCode.Add(' ItemInterface := %<DataInterface>:s;');
2008-03-17 12:17:55 +00:00
sourceCode.AddLn;
sourceCode.Add(' inherited;');
sourceCode.Add('end;');
sourceCode.AddLn;
case AItem.CollectionItem.PropertyType of
ptSimple:
begin
typeDef := TXMLDataBindingSimpleProperty(AItem.CollectionItem).DataType;
// #ToDo1 (MvR) 19-3-2008: .Text for strings ?
sourceCode.Add('function TXML%<Name>:s.Get_%<ItemName>:s(Index: Integer): %<DataType>:s;');
sourceCode.Add(XMLToNativeDataType('Result', 'List[Index].NodeValue', typeDef));
sourceCode.AddLn;
sourceCode.Add('function TXML%<Name>:s.Add(%<ItemName>:s: %<DataType>:s): %<DataInterface>:s;');
sourceCode.Add(NativeDataTypeToXML('Result.NodeValue', '%<ItemName>:s', typeDef,
' Result := AddItem(-1);'));
sourceCode.AddLn;
sourceCode.Add('function TXML%<Name>:s.Insert(Index: Integer; %<ItemName>:s: %<DataType>:s): %<DataInterface>:s;');
sourceCode.Add(NativeDataTypeToXML('Result.NodeValue', '%<ItemName>:s', typeDef,
' Result := AddItem(Index);'));
sourceCode.AddLn;
end;
2008-03-17 12:17:55 +00:00
ptItem:
begin
sourceCode.Add('function TXML%<Name>:s.Get_%<ItemName>:s(Index: Integer): %<DataType>:s;');
sourceCode.Add('begin');
sourceCode.Add(' Result := (List[Index] as %<DataType>:s);');
sourceCode.Add('end;');
sourceCode.AddLn;
sourceCode.Add('function TXML%<Name>:s.Add: %<DataType>:s;');
sourceCode.Add('begin');
sourceCode.Add(' Result := (AddItem(-1) as %<DataType>:s);');
sourceCode.Add('end;');
sourceCode.AddLn;
sourceCode.Add('function TXML%<Name>:s.Insert(Index: Integer): %<DataType>:s;');
sourceCode.Add('begin');
sourceCode.Add(' Result := (AddItem(Index) as %<DataType>:s);');
sourceCode.Add('end;');
sourceCode.AddLn;
end;
end;
2008-03-17 12:17:55 +00:00
end;
end;
2008-03-09 20:36:27 +00:00
2008-03-17 12:17:55 +00:00
case ASection of
dxsInterface:
begin
sourceCode.AddLn;
sourceCode.Add(' property %<ItemName>:s[Index: Integer]: %<DataType>:s read Get_%<ItemName>:s; default;');
2008-03-17 12:17:55 +00:00
end;
2008-03-09 20:36:27 +00:00
2008-03-17 12:17:55 +00:00
dxsClass:
begin
sourceCode.Add(' public');
sourceCode.Add(' procedure AfterConstruction; override;');
end;
end;
2008-03-09 20:36:27 +00:00
2008-03-17 12:17:55 +00:00
AStream.Write(sourceCode.Format(['Name', AItem.TranslatedName,
'ItemName', AItem.CollectionItemTranslatedName,
2008-03-17 12:17:55 +00:00
'ItemSourceName', AItem.CollectionItem.Name,
'DataType', dataTypeName,
'DataClass', dataClassName,
2008-03-17 12:17:55 +00:00
'DataInterface', dataIntfName]));
finally
FreeAndNil(sourceCode);
2008-03-09 20:36:27 +00:00
end;
2008-02-26 21:53:11 +00:00
end;
2008-03-09 20:36:27 +00:00
procedure TDelphiXMLDataBindingGenerator.WriteSchemaEnumeration(AStream: TStreamHelper; AItem: TXMLDataBindingEnumeration; ASection: TDelphiXMLSection);
2008-02-26 21:53:11 +00:00
var
2008-03-09 20:36:27 +00:00
memberIndex: Integer;
2008-02-26 21:53:11 +00:00
enumStart: String;
lineIndent: String;
begin
2008-03-09 20:36:27 +00:00
if (ASection <> dxsForward) or (AItem.MemberCount = 0) then
2008-02-26 21:53:11 +00:00
exit;
enumStart := NamedFormat(' TXML%<Name>:s = (',
['Name', AItem.TranslatedName]);
2008-02-26 21:53:11 +00:00
AStream.Write(enumStart);
lineIndent := StringOfChar(' ', Length(enumStart));
2008-03-09 20:36:27 +00:00
for memberIndex := 0 to Pred(AItem.MemberCount) do
2008-02-26 21:53:11 +00:00
begin
2008-03-09 20:36:27 +00:00
if memberIndex > 0 then
2008-02-26 21:53:11 +00:00
AStream.Write(lineIndent);
2008-03-09 20:36:27 +00:00
AStream.Write(AItem.Members[memberIndex].TranslatedName);
2008-02-26 21:53:11 +00:00
2008-03-09 20:36:27 +00:00
if memberIndex < Pred(AItem.MemberCount) then
2008-02-26 21:53:11 +00:00
AStream.WriteLn(',')
else
AStream.WriteLn(');');
end;
end;
2008-03-09 20:36:27 +00:00
procedure TDelphiXMLDataBindingGenerator.WriteSchemaEnumerationArray(AStream: TStreamHelper; AItem: TXMLDataBindingEnumeration);
2008-02-26 21:53:11 +00:00
var
2008-03-09 20:36:27 +00:00
memberIndex: Integer;
enumStart: String;
lineIndent: String;
2008-02-26 21:53:11 +00:00
begin
2008-03-09 20:36:27 +00:00
if (AItem.MemberCount = 0) then
exit;
2008-02-26 21:53:11 +00:00
enumStart := NamedFormat(' %<Name>:sValues: ', ['Name', AItem.TranslatedName]);
AStream.WriteLn(enumStart + NamedFormat('array[TXML%<Name>:s] of WideString =',
['Name', AItem.TranslatedName]));
2008-03-09 20:36:27 +00:00
lineIndent := StringOfChar(' ', Length(enumStart));
2008-02-26 21:53:11 +00:00
2008-03-09 20:36:27 +00:00
AStream.WriteLn(lineIndent + '(');
2008-02-26 21:53:11 +00:00
2008-03-09 20:36:27 +00:00
for memberIndex := 0 to Pred(AItem.MemberCount) do
2008-02-26 21:53:11 +00:00
begin
AStream.Write(NamedFormat('%<Indent>:s ''%<Name>:s''',
['Indent', lineIndent,
'Name', AItem.Members[memberIndex].Name]));
2008-02-26 21:53:11 +00:00
2008-03-09 20:36:27 +00:00
if memberIndex < Pred(AItem.MemberCount) then
AStream.WriteLn(',')
else
2008-02-26 21:53:11 +00:00
AStream.WriteLn();
2008-02-20 06:52:00 +00:00
end;
2008-03-09 20:36:27 +00:00
AStream.WriteLn(lineIndent + ');');
2008-02-20 06:52:00 +00:00
AStream.WriteLn();
end;
function TDelphiXMLDataBindingGenerator.DataTypeConversion(const ADestination, ASource: string; ADataType: IXMLTypeDef; AToNative: Boolean; const ALinesBefore: string): string;
var
typeMapping: TTypeMapping;
begin
with TNamedFormatStringList.Create() do
try
if not GetDataTypeMapping(ADataType, typeMapping) then
typeMapping.Conversion := tcNone;
if Length(TypeConversionVariables[typeMapping.Conversion]) > 0 then
begin
Add('var');
Add(TypeConversionVariables[typeMapping.Conversion]);
end;
Add('begin');
if Length(ALinesBefore) > 0 then
Add(ALinesBefore);
if AToNative then
Add(TypeConversionToNative[typeMapping.Conversion])
else
Add(TypeConversionToXML[typeMapping.Conversion]);
Add('end;');
Result := Format(['Destination', ADestination,
'Source', ASource]);
finally
Free();
end;
end;
function TDelphiXMLDataBindingGenerator.XMLToNativeDataType(const ADestination, ASource: string; ADataType: IXMLTypeDef; const ALinesBefore: string): string;
begin
Result := DataTypeConversion(ADestination, ASource, ADataType, True, ALinesBefore);
end;
function TDelphiXMLDataBindingGenerator.NativeDataTypeToXML(const ADestination, ASource: string; ADataType: IXMLTypeDef; const ALinesBefore: string): string;
begin
Result := DataTypeConversion(ADestination, ASource, ADataType, False, ALinesBefore);
end;
2008-02-26 21:53:11 +00:00
function TDelphiXMLDataBindingGenerator.CreateNewGUID(): String;
var
guid: TGUID;
begin
Result := '{ GUID generation failed }';
if CreateGUID(guid) = S_OK then
Result := '[''' + GUIDToString(guid) + ''']';
end;
2008-02-20 06:52:00 +00:00
function TDelphiXMLDataBindingGenerator.DoGetFileName(const ASchemaName: String): String;
begin
Result := OutputPath;
if OutputType = otMultiple then
begin
Result := IncludeTrailingPathDelimiter(Result) + ASchemaName + '.pas';
if Assigned(FOnGetFileName) then
FOnGetFileName(Self, ASchemaName, Result);
end;
end;
end.
2008-03-09 20:36:27 +00:00