1
0
mirror of synced 2024-11-16 20:13:50 +00:00
x2xmldatabinding/Forms/MainFrm.pas

652 lines
18 KiB
ObjectPascal
Raw Normal View History

2008-02-20 06:52:00 +00:00
unit MainFrm;
{$WARN UNIT_PLATFORM OFF}
2008-02-20 06:52:00 +00:00
interface
uses
Classes,
ComCtrls,
2008-02-20 06:52:00 +00:00
Controls,
Dialogs,
2008-02-20 06:52:00 +00:00
Forms,
Mask,
StdCtrls,
XMLDOM,
XMLIntf,
2008-02-20 06:52:00 +00:00
cxButtonEdit,
cxContainer,
cxControls,
cxEdit,
cxLookAndFeels,
cxMaskEdit,
cxTextEdit,
DataBindingHintsXML,
XMLDataBindingGenerator, cxGraphics, cxLookAndFeelPainters, cxClasses;
2008-02-20 06:52:00 +00:00
type
TMainForm = class(TForm)
btnClose: TButton;
btnGenerate: TButton;
btnHints: TButton;
DefaultEditStyle: TcxDefaultEditStyleController;
deFolder: TcxButtonEdit;
dlgOutputFile: TSaveDialog;
dlgSchema: TOpenDialog;
edtFolderPostfix: TcxTextEdit;
edtFolderPrefix: TcxTextEdit;
feFile: TcxButtonEdit;
feSchema: TcxButtonEdit;
2008-02-20 06:52:00 +00:00
gbOutput: TGroupBox;
lblFile: TLabel;
lblFolder: TLabel;
lblFolderPostfix: TLabel;
lblFolderPrefix: TLabel;
lblSchema: TLabel;
LookAndFeel: TcxLookAndFeelController;
plOutput: TPageControl;
2008-02-20 06:52:00 +00:00
rbFile: TRadioButton;
rbFolder: TRadioButton;
spFile: TTabSheet;
spFolder: TTabSheet;
2008-02-20 06:52:00 +00:00
procedure btnCloseClick(Sender: TObject);
procedure btnGenerateClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
2008-04-14 19:20:55 +00:00
procedure OutputTypeClick(Sender: TObject);
procedure feFilePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
procedure deFolderPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
procedure feSchemaPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
procedure feSchemaPropertiesChange(Sender: TObject);
procedure btnHintsClick(Sender: TObject);
2008-04-14 19:20:55 +00:00
private
function CheckValidSchemaFile: Boolean;
function CheckReadOnly(const AFileName: String): Boolean;
2008-04-14 19:20:55 +00:00
procedure GetFileName(Sender: TObject; const SchemaName: String; var Path, FileName: String);
function GetSettingsFileName(const AFileName: String): String;
procedure LoadSettings(const AFileName: String);
procedure SaveSettings(const AFileName: String);
2008-02-20 06:52:00 +00:00
end;
implementation
uses
FileCtrl,
2008-02-20 06:52:00 +00:00
SysUtils,
Windows,
MSXMLDOM,
MSXML2_TLB,
X2UtNamedFormat,
2008-04-14 19:20:55 +00:00
X2UtTempFile,
DataBindingSettingsXML,
DelphiXMLDataBindingGenerator;
type
TProtectedXMLDataBindingItem = class(TXMLDataBindingItem);
TProtectedXMLDataBindingProperty = class(TXMLDataBindingProperty);
THintsDelphiXMLDataBindingGenerator = class(TDelphiXMLDataBindingGenerator)
private
FHints: IXMLDataBindingHints;
protected
procedure GenerateDataBinding; override;
2008-02-20 06:52:00 +00:00
procedure ProcessHints;
procedure ProcessEnumerations;
procedure ProcessDocumentElements;
procedure ProcessInterfaces;
procedure ProcessProperties;
function FindSchema(const ASchemaName: String; out ASchema: TXMLDataBindingSchema): Boolean;
function FindNode(const ASchemaName, AXPath: String; out AItem: TXMLDataBindingItem): Boolean;
public
property Hints: IXMLDataBindingHints read FHints write FHints;
end;
2008-02-20 06:52:00 +00:00
{$R *.dfm}
{ TMainForm }
procedure TMainForm.FormCreate(Sender: TObject);
2008-04-14 19:20:55 +00:00
var
schemaFile: String;
2008-02-20 06:52:00 +00:00
begin
plOutput.ActivePageIndex := 0;
2008-04-14 19:20:55 +00:00
if ParamCount > 0 then
2008-04-14 19:20:55 +00:00
begin
schemaFile := ParamStr(1);
if FileExists(schemaFile) then
begin
feFile.Text := ChangeFileExt(schemaFile, '.pas');
deFolder.Text := ExtractFilePath(schemaFile);
{ Set schema last, the Change event will attempt to load the
settings file and overwrite the file / folder. }
feSchema.Text := schemaFile;
2008-04-14 19:20:55 +00:00
end;
end;
end;
procedure TMainForm.OutputTypeClick(Sender: TObject);
begin
if Sender = rbFile then
plOutput.ActivePage := spFile
else if Sender = rbFolder then
plOutput.ActivePage := spFolder;
2008-02-20 06:52:00 +00:00
end;
procedure TMainForm.btnGenerateClick(Sender: TObject);
var
hintsFile: String;
hints: IXMLDataBindingHints;
generator: THintsDelphiXMLDataBindingGenerator;
2008-02-20 06:52:00 +00:00
begin
if not CheckValidSchemaFile then
2008-02-20 06:52:00 +00:00
Exit;
hintsFile := ChangeFileExt(feSchema.Text, '.hints.xml');
if FileExists(hintsFile) then
hints := LoadDataBindingHints(hintsFile);
2008-02-20 06:52:00 +00:00
try
generator := THintsDelphiXMLDataBindingGenerator.Create;
try
generator.Hints := hints;
if rbFile.Checked then
begin
if not CheckReadOnly(feFile.Text) then
Exit;
generator.OutputType := otSingle;
generator.OutputPath := feFile.Text;
end else if rbFolder.Checked then
begin
generator.OutputType := otMultiple;
generator.OutputPath := deFolder.Text;
end;
generator.OnGetFileName := GetFileName;
generator.Execute(feSchema.Text);
SaveSettings(feSchema.Text);
ShowMessage('The data binding has been generated.');
finally
FreeAndNil(generator);
2008-04-14 19:20:55 +00:00
end;
2008-02-20 06:52:00 +00:00
finally
hints := nil;
2008-02-20 06:52:00 +00:00
end;
end;
procedure TMainForm.btnCloseClick(Sender: TObject);
begin
Close;
2008-02-20 06:52:00 +00:00
end;
2008-04-14 19:20:55 +00:00
procedure TMainForm.GetFileName(Sender: TObject; const SchemaName: String; var Path, FileName: String);
begin
2008-04-14 19:36:55 +00:00
FileName := ChangeFileExt(edtFolderPrefix.Text + FileName,
edtFolderPostfix.Text + ExtractFileExt(FileName));
2008-04-14 19:20:55 +00:00
CheckValidFileName(FileName);
end;
procedure TMainForm.feFilePropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
if dlgOutputFile.Execute then
feFile.Text := dlgOutputFile.FileName;
end;
procedure TMainForm.deFolderPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
var
directory: String;
begin
if SelectDirectory('Select output folder', '', directory) then
deFolder.Text := directory;
end;
procedure TMainForm.feSchemaPropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
if dlgSchema.Execute then
feSchema.Text := dlgSchema.FileName;
end;
procedure TMainForm.feSchemaPropertiesChange(Sender: TObject);
begin
if FileExists(feSchema.Text) then
LoadSettings(feSchema.Text);
end;
function TMainForm.GetSettingsFileName(const AFileName: String): String;
begin
Result := ChangeFileExt(AFileName, '.settings.xml');
end;
procedure TMainForm.LoadSettings(const AFileName: String);
var
fileName: String;
settings: IXMLDataBindingSettings;
outputSingle: IXMLOutputSingle;
outputMultiple: IXMLOutputMultiple;
begin
fileName := GetSettingsFileName(AFileName);
if FileExists(fileName) then
begin
settings := LoadDataBindingSettings(fileName);
if settings.HasOutput then
begin
case settings.Output.OutputType of
OutputType_Single:
begin
outputSingle := settings.Output.OutputSingle;
rbFile.Checked := True;
feFile.Text := outputSingle.FileName;
end;
OutputType_Multiple:
begin
outputMultiple := settings.Output.OutputMultiple;
rbFolder.Checked := True;
deFolder.Text := outputMultiple.Path;
edtFolderPrefix.Text := outputMultiple.Prefix;
edtFolderPostfix.Text := outputMultiple.Postfix;
end;
end;
end;
end;
end;
procedure TMainForm.SaveSettings(const AFileName: String);
var
fileName: String;
settings: IXMLDataBindingSettings;
outputSingle: IXMLOutputSingle;
outputMultiple: IXMLOutputMultiple;
begin
fileName := GetSettingsFileName(AFileName);
if FileExists(fileName) then
settings := LoadDataBindingSettings(fileName)
else
settings := NewDataBindingSettings;
settings.Output.ChildNodes.Clear;
if rbFile.Checked then
begin
settings.Output.OutputType := OutputType_Single;
outputSingle := settings.Output.OutputSingle;
outputSingle.FileName := feFile.Text;
end else
begin
settings.Output.OutputType := OutputType_Multiple;
outputMultiple := settings.Output.OutputMultiple;
outputMultiple.Path := deFolder.Text;
outputMultiple.Prefix := edtFolderPrefix.Text;
outputMultiple.Postfix := edtFolderPostfix.Text;
end;
settings.OwnerDocument.SaveToFile(fileName);
end;
function TMainForm.CheckValidSchemaFile: Boolean;
begin
Result := FileExists(feSchema.Text);
if not Result then
begin
MessageBox(Self.Handle, 'Please specify a valid schema file.',
'Schema file does not exist', MB_OK or MB_ICONERROR);
ActiveControl := feFile;
Exit;
end;
end;
function TMainForm.CheckReadOnly(const AFileName: String): Boolean;
begin
Result := True;
if FileExists(AFileName) and FileIsReadOnly(AFileName) then
begin
if MessageBox(Self.Handle, 'The output file is read-only. Do you want to ' +
'remove the read-only attribute?',
'Read-only', MB_YESNO or MB_ICONQUESTION) = ID_YES then
begin
Result := FileSetReadOnly(AFileName, False);
end else
Result := False;
end;
end;
procedure TMainForm.btnHintsClick(Sender: TObject);
var
hintsFile: String;
hints: IXMLDataBindingHints;
begin
if CheckValidSchemaFile then
begin
hintsFile := ChangeFileExt(feSchema.Text, '.hints.xml');
if FileExists(hintsFile) then
begin
if MessageBox(Self.Handle, 'Do you want to overwrite the existing hints file?',
'Overwrite', MB_YESNO or MB_ICONQUESTION) <> ID_YES then
Exit;
end;
hints := NewDataBindingHints;
hints.OwnerDocument.SaveToFile(hintsFile);
ShowMessage('The hints file has been generated.');
end;
end;
{ THintsDelphiXMLDataBindingGenerator }
procedure THintsDelphiXMLDataBindingGenerator.GenerateDataBinding;
begin
if Assigned(Hints) then
ProcessHints;
inherited GenerateDataBinding;
end;
procedure THintsDelphiXMLDataBindingGenerator.ProcessHints;
begin
if Hints.HasEnumerations then
ProcessEnumerations;
if Hints.HasDocumentElements then
ProcessDocumentElements;
if Hints.HasInterfaces then
ProcessInterfaces;
if Hints.HasProperties then
ProcessProperties;
end;
procedure THintsDelphiXMLDataBindingGenerator.ProcessEnumerations;
var
itemIndex: Integer;
enumeration: IXMLEnumeration;
schemaItem: TXMLDataBindingItem;
enumerationItem: TXMLDataBindingEnumeration;
hintMemberIndex: Integer;
memberName: String;
memberIndex: Integer;
begin
for itemIndex := 0 to Pred(Hints.Enumerations.Count) do
begin
enumeration := Hints.Enumerations[itemIndex];
if FindNode(enumeration.Schema, enumeration.XPath, schemaItem) and
(schemaItem.ItemType = itEnumeration) then
begin
enumerationItem := TXMLDataBindingEnumeration(schemaItem);
for hintMemberIndex := 0 to Pred(enumeration.Count) do
begin
memberName := enumeration.Member[hintMemberIndex].Name;
for memberIndex := 0 to Pred(enumerationItem.MemberCount) do
begin
if enumerationItem.Members[memberIndex].Name = memberName then
begin
enumerationItem.Members[memberIndex].TranslatedName := enumeration[hintMemberIndex].Text;
Break;
end;
end;
end;
end;
end;
end;
procedure THintsDelphiXMLDataBindingGenerator.ProcessDocumentElements;
var
schemaIndex: Integer;
schema: TXMLDataBindingSchema;
itemIndex: Integer;
documentElement: IXMLDocumentElement;
schemaItem: TXMLDataBindingItem;
begin
for schemaIndex := 0 to Pred(SchemaCount) do
begin
schema := Schemas[schemaIndex];
for itemIndex := 0 to Pred(schema.ItemCount) do
schema.Items[itemIndex].DocumentElement := False;
end;
for itemIndex := 0 to Pred(Hints.DocumentElements.Count) do
begin
documentElement := Hints.DocumentElements[itemIndex];
if FindNode(documentElement.Schema, documentElement.XPath, schemaItem) then
begin
if schemaItem.ItemType = itInterface then
schemaItem.DocumentElement := True;
end;
end;
end;
procedure THintsDelphiXMLDataBindingGenerator.ProcessInterfaces;
var
itemIndex: Integer;
interfaceName: IXMLInterfaceName;
schemaItem: TXMLDataBindingItem;
2010-02-11 10:09:00 +00:00
propertyItem: TXMLDataBindingProperty;
begin
for itemIndex := 0 to Pred(Hints.Interfaces.Count) do
begin
interfaceName := Hints.Interfaces[itemIndex];
if FindNode(interfaceName.Schema, interfaceName.XPath, schemaItem) then
begin
2010-02-11 10:09:00 +00:00
case schemaItem.ItemType of
itInterface,
itEnumeration:
schemaItem.TranslatedName := interfaceName.Text;
itProperty:
begin
propertyItem := TXMLDataBindingProperty(schemaItem);
if propertyItem.PropertyType = ptItem then
TXMLDataBindingItemProperty(propertyItem).Item.TranslatedName := interfaceName.Text;
end;
end;
end;
end;
end;
procedure THintsDelphiXMLDataBindingGenerator.ProcessProperties;
var
itemIndex: Integer;
propertyName: IXMLPropertyName;
schemaItem: TXMLDataBindingItem;
begin
for itemIndex := 0 to Pred(Hints.Properties.Count) do
begin
propertyName := Hints.Properties[itemIndex];
if FindNode(propertyName.Schema, propertyName.XPath, schemaItem) then
begin
if schemaItem.ItemType = itProperty then
schemaItem.TranslatedName := propertyName.Text;
end;
end;
end;
function THintsDelphiXMLDataBindingGenerator.FindSchema(const ASchemaName: String; out ASchema: TXMLDataBindingSchema): Boolean;
var
schemaIndex: Integer;
begin
Result := False;
if SchemaCount > 0 then
begin
if Length(ASchemaName) = 0 then
begin
ASchema := Schemas[0];
Result := True;
end else
begin
for schemaIndex := 0 to Pred(SchemaCount) do
if SameText(Schemas[schemaIndex].SchemaName, ASchemaName) then
begin
ASchema := Schemas[schemaIndex];
Result := True;
Break;
end;
end;
end;
end;
function THintsDelphiXMLDataBindingGenerator.FindNode(const ASchemaName, AXPath: String; out AItem: TXMLDataBindingItem): Boolean;
function SameNode(ANode1, ANode2: IDOMNode): Boolean;
var
attributeIndex: Integer;
attribute1: IDOMNode;
attribute2: IDOMNode;
hasParent1: Boolean;
hasParent2: Boolean;
begin
{ Compare name and number of attributes }
Result := (ANode1.nodeName = ANode2.nodeName) and
(ANode1.attributes.length = ANode2.attributes.length);
if Result then
begin
{ Compare attribute content }
for attributeIndex := 0 to Pred(ANode1.attributes.length) do
begin
attribute1 := ANode1.attributes[attributeIndex];
attribute2 := ANode2.attributes[attributeIndex];
Result := (attribute1.nodeName = attribute2.nodeName) and
(attribute1.nodeValue = attribute2.nodeValue);
if not Result then
Break;
end;
if Result then
begin
{ Compare parent nodes }
hasParent1 := Assigned(ANode1.parentNode) and (ANode1.parentNode.nodeType <> NODE_DOCUMENT);
hasParent2 := Assigned(ANode2.parentNode) and (ANode2.parentNode.nodeType <> NODE_DOCUMENT);
if hasParent1 = hasParent2 then
begin
if hasParent1 then
Result := SameNode(ANode1.parentNode, ANode2.parentNode);
end else
Result := False;
end;
end;
end;
var
schema: TXMLDataBindingSchema;
schemaItem: IDOMNode;
item: TProtectedXMLDataBindingItem;
itemIndex: Integer;
domDocument: IXMLDOMDocument2;
interfaceItem: TXMLDataBindingInterface;
propertyIndex: Integer;
propertyItem: TProtectedXMLDataBindingProperty;
begin
Result := False;
if (Length(AXPath) > 0) and FindSchema(ASchemaName, schema) then
begin
domDocument := ((schema.SchemaDef.OwnerDocument.DOMDocument as IXMLDOMNodeRef).GetXMLDOMNode as IXMLDOMDocument2);
domDocument.setProperty('SelectionLanguage', 'XPath');
domDocument.setProperty('SelectionNamespaces', 'xmlns:xs="http://www.w3.org/2001/XMLSchema"');
schemaItem := (schema.SchemaDef.DOMNode as IDOMNodeSelect).selectNode(AXPath);
if Assigned(schemaItem) then
begin
for itemIndex := 0 to Pred(schema.ItemCount) do
begin
item := TProtectedXMLDataBindingItem(schema.Items[itemIndex]);
if Assigned(item.SchemaItem) and SameNode(item.SchemaItem.DOMNode, schemaItem) then
begin
AItem := schema.Items[itemIndex];
Result := True;
end else if item.ItemType = itInterface then
begin
interfaceItem := TXMLDataBindingInterface(item);
for propertyIndex := 0 to Pred(interfaceItem.PropertyCount) do
begin
propertyItem := TProtectedXMLDataBindingProperty(interfaceItem.Properties[propertyIndex]);
if Assigned(propertyItem.SchemaItem) and SameNode(propertyItem.SchemaItem.DOMNode, schemaItem) then
begin
AItem := propertyItem;
Result := True;
Break;
end;
end;
end;
if Result then
Break;
end;
end;
end;
end;
2008-02-20 06:52:00 +00:00
end.