From cc62e18e4e265bb6e5853cac91b23d68f91c4c63 Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Fri, 21 Jun 2013 10:25:49 +0000 Subject: [PATCH] Support for namespaces in child nodes --- Shared/XMLDataBindingUtils.pas | 38 +++++++++++++++++++ Units/DelphiXMLDataBindingGenerator.pas | 2 +- Units/DelphiXMLDataBindingResources.pas | 50 ++++++++++++------------- Units/XMLDataBindingGenerator.pas | 10 ++++- 4 files changed, 72 insertions(+), 28 deletions(-) diff --git a/Shared/XMLDataBindingUtils.pas b/Shared/XMLDataBindingUtils.pas index 48ede1e..cd7f043 100644 --- a/Shared/XMLDataBindingUtils.pas +++ b/Shared/XMLDataBindingUtils.pas @@ -11,6 +11,8 @@ interface uses Classes, SysUtils, + XMLDoc, + xmldom, XMLIntf; @@ -28,6 +30,22 @@ type end; + TX2XMLNode = class(TXMLNode) + private + function GetChildNodesNS(const ANodeName, ANamespaceURI: DOMString): IXMLNode; + protected + property ChildNodesNS[const ANodeName, ANamespaceURI: DOMString]: IXMLNode read GetChildNodesNS; + end; + + + TX2XMLNodeCollection = class(TXMLNodeCollection) + private + function GetChildNodesNS(const ANodeName, ANamespaceURI: DOMString): IXMLNode; + protected + property ChildNodesNS[const ANodeName, ANamespaceURI: DOMString]: IXMLNode read GetChildNodesNS; + end; + + const AllTimeFragments = [Low(TXMLTimeFragment)..High(TXMLTimeFragment)]; @@ -122,6 +140,26 @@ type +{ TX2XMLNode } +function TX2XMLNode.GetChildNodesNS(const ANodeName, ANamespaceURI: DOMString): IXMLNode; +begin + Result := ChildNodes.FindNode(ANodeName, ANamespaceURI); + if (not Assigned(Result)) and (doNodeAutoCreate in OwnerDocument.Options) then + Result := AddChild(ANodeName, ANamespaceURI); +end; + + + +{ TX2XMLNodeCollection } +function TX2XMLNodeCollection.GetChildNodesNS(const ANodeName, ANamespaceURI: DOMString): IXMLNode; +begin + Result := ChildNodes.FindNode(ANodeName, ANamespaceURI); + if (not Assigned(Result)) and (doNodeAutoCreate in OwnerDocument.Options) then + Result := AddChild(ANodeName, ANamespaceURI); +end; + + + function DateTimeToXML(ADate: TDateTime; AFormat: TXMLDateTimeFormat; ATimeFragments: TXMLTimeFragments): string; var formatSettings: TFormatSettings; diff --git a/Units/DelphiXMLDataBindingGenerator.pas b/Units/DelphiXMLDataBindingGenerator.pas index 317acaf..7dd4785 100644 --- a/Units/DelphiXMLDataBindingGenerator.pas +++ b/Units/DelphiXMLDataBindingGenerator.pas @@ -1334,7 +1334,7 @@ begin sourceCode.Add('begin'); if AProperty.HasTargetNamespace then - sourceCode.Add(' Result := (ChildNodes.FindNode(''%:s'', ''%:s'') as IXML%:s);') + sourceCode.Add(' Result := (ChildNodesNS[''%:s'', ''%:s''] as IXML%:s);') else sourceCode.Add(' Result := (ChildNodes[''%:s''] as IXML%:s);'); diff --git a/Units/DelphiXMLDataBindingResources.pas b/Units/DelphiXMLDataBindingResources.pas index d082872..266bfb5 100644 --- a/Units/DelphiXMLDataBindingResources.pas +++ b/Units/DelphiXMLDataBindingResources.pas @@ -151,7 +151,7 @@ const { dntElementNS } 'function TXML%:s.Get%:sIsNil: Boolean;' + CrLf + 'begin' + CrLf + - ' Result := GetNodeIsNil(ChildNodes.FindNode(''%:s'', ''%:s''));' + CrLf + + ' Result := GetNodeIsNil(ChildNodesNS[''%:s'', ''%:s'']);' + CrLf + 'end;' + CrLf + '' + CrLf ); @@ -168,7 +168,7 @@ const { dntElementNS } 'procedure TXML%:s.Set%:sIsNil(const Value: Boolean);' + CrLf + 'begin' + CrLf + - ' SetNodeIsNil(ChildNodes.FindNode(''%:s'', ''%:s''), Value);' + CrLf + + ' SetNodeIsNil(ChildNodesNS[''%:s'', ''%:s''], Value);' + CrLf + 'end;' + CrLf + '' + CrLf ); @@ -185,14 +185,14 @@ const { dntElement } 'function TXML%:s.Get%:sText: WideString;' + CrLf + 'begin' + CrLf + - ' Result := ChildNodes.FindNode(''%:s'', ''%:s'').Text;' + CrLf + + ' Result := ChildNodesNS[''%:s'', ''%:s''].Text;' + CrLf + 'end;' + CrLf + '' + CrLf ); PropertyImplMethodGetTextAttr = 'function TXML%:s.Get%:sText: WideString;' + CrLf + 'begin' + CrLf + - ' Result := AttributeNodes[''%:s''].Text;' + CrLf + + ' Result := AttributeNodes[''%:s''].Text;' + CrLf + 'end;' + CrLf + '' + CrLf; @@ -208,7 +208,7 @@ const { dntElementNS } 'procedure TXML%:s.Set%:sText(const Value: WideString);' + CrLf + 'begin' + CrLf + - ' ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue := Value;' + CrLf + + ' ChildNodesNS[''%:s'', ''%:s''].NodeValue := Value;' + CrLf + 'end;' + CrLf + '' + CrLf ); @@ -231,7 +231,7 @@ const { dntElementNS } 'procedure TXML%:s.Save%:sToStream(AStream: TStream);' + CrLf + 'begin' + CrLf + - ' Base64DecodeToStream(Trim(ChildNodes.FindNode(''%:s'', ''%:s'').Text), AStream);' + CrLf + + ' Base64DecodeToStream(Trim(ChildNodesNS[''%:s'', ''%:s''].Text), AStream);' + CrLf + 'end;' + CrLf + '' + CrLf ); @@ -248,7 +248,7 @@ const { dntElementNS } 'procedure TXML%:s.Save%:sToFile(const AFileName: string);' + CrLf + 'begin' + CrLf + - ' Base64DecodeToFile(Trim(ChildNodes.FindNode(''%:s'', ''%:s'').Text), AFileName);' + CrLf + + ' Base64DecodeToFile(Trim(ChildNodesNS[''%:s'', ''%:s''].Text), AFileName);' + CrLf + 'end;' + CrLf + '' + CrLf ); @@ -275,10 +275,10 @@ const CollectionInterface = 'IXMLNodeCollection'; - CollectionClass = 'TXMLNodeCollection'; + CollectionClass = 'TX2XMLNodeCollection'; ItemInterface = 'IXMLNode'; - ItemClass = 'TXMLNode'; + ItemClass = 'TX2XMLNode'; @@ -369,7 +369,7 @@ const { daGet } ( { dntElement } ' %:s := ChildNodes[''%:s''].NodeValue;', - { dntElementNS } ' %:s := ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue;', + { dntElementNS } ' %:s := ChildNodesNS[''%:s'', ''%:s''].NodeValue;', { dntAttribute } ' %:s := AttributeNodes[''%:s''].NodeValue;', { dntNodeValue } ' %:s := GetNodeValue;', { dntCustom } ' %:s := %:s;' @@ -377,7 +377,7 @@ const { daSet } ( { dntElement } ' ChildNodes[''%:s''].NodeValue := %:s;', - { dntElementNS } ' ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue := %:s;', + { dntElementNS } ' ChildNodesNS[''%:s'', ''%:s''].NodeValue := %:s;', { dntAttribute } ' SetAttribute(''%:s'', %:s);', { dntNodeValue } ' SetNodeValue(%:s);', { dntCustom } ' %:s := %:s;' @@ -405,13 +405,13 @@ const ( { tcNone } '', { tcBoolean } '', - { tcFloat } ' %:s := XMLToFloat(ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue);', - { tcDateTime } ' %:s := XMLToDateTime(ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue, xdtDateTime);', - { tcDate } ' %:s := XMLToDateTime(ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue, xdtDate);', - { tcTime } ' %:s := XMLToDateTime(ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue, xdtTime);', - { tcString } ' %:s := ChildNodes.FindNode(''%:s'', ''%:s'').Text;', - { tcBase64 } ' %:s := Base64Decode(Trim(ChildNodes.FindNode(''%:s'', ''%:s'').Text));', - { tcNode } ' %:s := ChildNodes.FindNode(''%:s'', ''%:s'');' + { tcFloat } ' %:s := XMLToFloat(ChildNodesNS[''%:s'', ''%:s''].NodeValue);', + { tcDateTime } ' %:s := XMLToDateTime(ChildNodesNS[''%:s'', ''%:s''].NodeValue, xdtDateTime);', + { tcDate } ' %:s := XMLToDateTime(ChildNodesNS[''%:s'', ''%:s''].NodeValue, xdtDate);', + { tcTime } ' %:s := XMLToDateTime(ChildNodesNS[''%:s'', ''%:s''].NodeValue, xdtTime);', + { tcString } ' %:s := ChildNodesNS[''%:s'', ''%:s''].Text;', + { tcBase64 } ' %:s := Base64Decode(Trim(ChildNodesNS[''%:s'', ''%:s''].Text));', + { tcNode } ' %:s := ChildNodesNS[''%:s'', ''%:s''];' ), { dntAttribute } ( @@ -467,14 +467,14 @@ const { dntElementNS } ( { tcNone } '', - { tcBoolean } ' ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue := BoolToXML(%:s);', - { tcFloat } ' ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue := FloatToXML(%:s);', - { tcDateTime } ' ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue := DateTimeToXML(%:s, xdtDateTime);', - { tcDate } ' ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue := DateTimeToXML(%:s, xdtDate);', - { tcTime } ' ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue := DateTimeToXML(%:s, xdtTime);', + { tcBoolean } ' ChildNodesNS[''%:s'', ''%:s''].NodeValue := BoolToXML(%:s);', + { tcFloat } ' ChildNodesNS[''%:s'', ''%:s''].NodeValue := FloatToXML(%:s);', + { tcDateTime } ' ChildNodesNS[''%:s'', ''%:s''].NodeValue := DateTimeToXML(%:s, xdtDateTime);', + { tcDate } ' ChildNodesNS[''%:s'', ''%:s''].NodeValue := DateTimeToXML(%:s, xdtDate);', + { tcTime } ' ChildNodesNS[''%:s'', ''%:s''].NodeValue := DateTimeToXML(%:s, xdtTime);', { tcString } '', - { tcBase64 } ' ChildNodes.FindNode(''%:s'', ''%:s'').NodeValue := Base64Encode(%:s);', - { tcNode } ' ChildNodes.FindNode(''%:s'', ''%:s'') := %:s;' + { tcBase64 } ' ChildNodesNS[''%:s'', ''%:s''].NodeValue := Base64Encode(%:s);', + { tcNode } ' ChildNodesNS[''%:s'', ''%:s''] := %:s;' ), { dntAttribute } ( diff --git a/Units/XMLDataBindingGenerator.pas b/Units/XMLDataBindingGenerator.pas index 5dfbec0..1d91297 100644 --- a/Units/XMLDataBindingGenerator.pas +++ b/Units/XMLDataBindingGenerator.pas @@ -976,6 +976,7 @@ var propertyType: TXMLDataBindingItem; propertyItem: TXMLDataBindingProperty; namespace: string; + schemaDef: IXMLSchemaDef; begin propertyType := ProcessElement(ASchema, AElement); @@ -991,9 +992,14 @@ begin AElement.Name, AElement.DataType); - if not VarIsNull(AElement.SchemaDef.TargetNamespace) then + if Assigned(AElement.Ref) then + schemaDef := AElement.Ref.SchemaDef + else + schemaDef := AElement.SchemaDef; + + if Assigned(schemaDef) and (not VarIsNull(schemaDef.TargetNamespace)) then begin - namespace := AElement.SchemaDef.TargetNamespace; + namespace := schemaDef.TargetNamespace; if namespace <> Schemas[0].TargetNamespace then propertyItem.TargetNamespace := namespace; end;