Better XSD/WSDL generation : "import" nodes are correctly generated.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2983 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
inoussa
2014-05-01 16:58:45 +00:00
parent 0e9f4b37c4
commit 4edd9df7fa
5 changed files with 77 additions and 8 deletions

View File

@ -30,6 +30,9 @@ const
sARRAY_STYLE_EMBEDDED = 'ARRAY_STYLE_EMBEDDED'; sARRAY_STYLE_EMBEDDED = 'ARRAY_STYLE_EMBEDDED';
sARRAY_IS_COLLECTION = 'ARRAY_COLLECTION'; sARRAY_IS_COLLECTION = 'ARRAY_COLLECTION';
sWST_PROP_STORE_PREFIX = 'wstHas_'; sWST_PROP_STORE_PREFIX = 'wstHas_';
sFILE_NAME = 'FileName';
sNS_COUNT = 'NS_Count';
sNS_ITEM = 'NS_';
sXSD_NS = 'http://www.w3.org/2001/XMLSchema'; sXSD_NS = 'http://www.w3.org/2001/XMLSchema';

View File

@ -43,7 +43,7 @@ type
FTypesNode : TDOMElement; FTypesNode : TDOMElement;
FDefinitionsNode : TDOMElement; FDefinitionsNode : TDOMElement;
private private
procedure GenerateTypes(ASymTable : TwstPasTreeContainer); procedure GenerateTypes(ASymTable : TwstPasTreeContainer; AModule : TPasModule);
procedure GenerateServiceMessages( procedure GenerateServiceMessages(
ASymTable : TwstPasTreeContainer; ASymTable : TwstPasTreeContainer;
AModule : TPasModule; AModule : TPasModule;
@ -143,21 +143,43 @@ end;
{ TWsdlGenerator } { TWsdlGenerator }
procedure TWsdlGenerator.GenerateTypes(ASymTable : TwstPasTreeContainer); procedure TWsdlGenerator.GenerateTypes(
ASymTable : TwstPasTreeContainer;
AModule : TPasModule
);
var var
i : PtrInt; i, c : PtrInt;
mdl : TPasModule; mdl : TPasModule;
mdlLs : TList2; mdlLs : TList2;
g : IGenerator; g : IGenerator;
nsList : TStringList;
s : string;
begin begin
mdlLs := ASymTable.Package.Modules; mdlLs := ASymTable.Package.Modules;
if ( mdlLs.Count > 0 ) then begin if ( mdlLs.Count > 0 ) then begin
g := TWsdlTypechemaGenerator.Create(Document) as IGenerator; nsList := TStringList.Create();
for i := 0 to Pred(mdlLs.Count) do begin try
mdl := TPasModule(mdlLs[i]); c := StrToIntDef(ASymTable.Properties.GetValue(AModule,sNS_COUNT),0);
if not mdl.InheritsFrom(TPasNativeModule) then begin if (c > 0) then begin
for i := 1 to c do begin
s := ASymTable.Properties.GetValue(AModule,sNS_ITEM+IntToStr(i));
nsList.Add(s);
end;
end;
g := TWsdlTypechemaGenerator.Create(Document) as IGenerator;
for i := 0 to Pred(mdlLs.Count) do begin
if (mdl <> AModule) then begin
mdl := TPasModule(mdlLs[i]);
if mdl.InheritsFrom(TPasNativeModule) then
Continue;
s := ASymTable.GetExternalName(mdl);
if (nsList.IndexOf(s) = -1) then
Continue;
end;
g.Execute(ASymTable,mdl.Name); g.Execute(ASymTable,mdl.Name);
end; end;
finally
nsList.Free();
end; end;
end; end;
end; end;
@ -463,7 +485,7 @@ begin
raise EWsdlGeneratorException.Create('Invalid symbol table.'); raise EWsdlGeneratorException.Create('Invalid symbol table.');
Prepare(ASymTable,locMainModule); Prepare(ASymTable,locMainModule);
GenerateTypes(ASymTable); GenerateTypes(ASymTable,locMainModule);
decList := locMainModule.InterfaceSection.Declarations; decList := locMainModule.InterfaceSection.Declarations;
c := decList.Count; c := decList.Count;

View File

@ -438,8 +438,10 @@ procedure TWsdlParser.Execute(const AMode: TParserMode; const AModuleName: strin
locModule : TPasModule; locModule : TPasModule;
locIntfUsesList : TList2; locIntfUsesList : TList2;
begin begin
FSymbols.Properties.SetValue(FModule,sNS_COUNT,IntToStr(FXsdParsers.Count));
locIntfUsesList := FModule.InterfaceSection.UsesList; locIntfUsesList := FModule.InterfaceSection.UsesList;
for k := 0 to Pred(FXsdParsers.Count) do begin for k := 0 to Pred(FXsdParsers.Count) do begin
FSymbols.Properties.SetValue(FModule,(sNS_ITEM+IntToStr(k+1)),FXsdParsers[k]);
locPrs := (FXsdParsers.Objects[k] as TIntfObjectRef).Intf as IParserContext; locPrs := (FXsdParsers.Objects[k] as TIntfObjectRef).Intf as IParserContext;
locModule := locPrs.GetTargetModule(); locModule := locPrs.GetTargetModule();
if (locModule <> nil) and (locModule <> FModule) and if (locModule <> nil) and (locModule <> FModule) and

View File

@ -88,6 +88,11 @@ type
FDocument : TDOMDocument; FDocument : TDOMDocument;
FOptions: TGeneratorOptions; FOptions: TGeneratorOptions;
FShortNames : TStrings; FShortNames : TStrings;
protected
procedure GenerateImports(
ASymTable : TwstPasTreeContainer;
AModule : TPasModule
);
protected protected
function GetSchemaNode(ADocument : TDOMDocument) : TDOMNode;virtual;abstract; function GetSchemaNode(ADocument : TDOMDocument) : TDOMNode;virtual;abstract;
procedure SetPreferedShortNames(const ALongName, AShortName : string); procedure SetPreferedShortNames(const ALongName, AShortName : string);
@ -1335,6 +1340,7 @@ begin
if ( mdl = nil ) then if ( mdl = nil ) then
raise EXsdGeneratorException.CreateFmt('Unable to find module : "%s".',[AModuleName]); raise EXsdGeneratorException.CreateFmt('Unable to find module : "%s".',[AModuleName]);
Prepare(ASymTable,mdl); Prepare(ASymTable,mdl);
GenerateImports(ASymTable,mdl);
gr := GetXsdTypeHandlerRegistry(); gr := GetXsdTypeHandlerRegistry();
typeList := mdl.InterfaceSection.Declarations; typeList := mdl.InterfaceSection.Declarations;
k := typeList.Count; k := typeList.Count;
@ -1397,6 +1403,40 @@ begin
SetPreferedShortNames(s_wsdl,'wsdl'); SetPreferedShortNames(s_wsdl,'wsdl');
end; end;
procedure TCustomXsdGenerator.GenerateImports(
ASymTable : TwstPasTreeContainer;
AModule : TPasModule
);
var
locUsesList : TList2;
locModule : TPasElement;
i : Integer;
locNS, locFileName, s : string;
locSchemaNode, resNode : TDOMElement;
locCurrentNS : string;
begin
locUsesList := AModule.InterfaceSection.UsesList;
if (locUsesList.Count > 0) then begin
locCurrentNS := ASymTable.GetExternalName(AModule);
for i := 0 to Pred(locUsesList.Count) do begin
locModule := TPasElement(locUsesList[i]);
locNS := ASymTable.GetExternalName(locModule);
if SameText(locCurrentNS,locNS) then
Continue;
if locModule.InheritsFrom(TPasNativeModule) then
Continue;
locFileName := ASymTable.Properties.GetValue(locModule,sFILE_NAME);
if IsStrEmpty(locFileName) then
Continue;
locSchemaNode := GetSchemaNode(FDocument) as TDOMElement;
s := Format('%s:%s',[s_xs_short,s_import]);
resNode := CreateElement(s,locSchemaNode,FDocument);
resNode.SetAttribute(s_namespace,locNS);
resNode.SetAttribute(s_schemaLocation,locFileName);
end;
end;
end;
procedure TCustomXsdGenerator.SetPreferedShortNames(const ALongName, AShortName: string); procedure TCustomXsdGenerator.SetPreferedShortNames(const ALongName, AShortName: string);
begin begin
FShortNames.Values[ALongName] := AShortName; FShortNames.Values[ALongName] := AShortName;

View File

@ -994,6 +994,8 @@ begin
locModule.Name := locName; locModule.Name := locName;
locModule.AddRef(); locModule.AddRef();
locUsesList.Add(locModule); locUsesList.Add(locModule);
if (FSymbols.Properties.GetValue(locModule,sFILE_NAME) = '') then
FSymbols.Properties.SetValue(locModule,sFILE_NAME,locFileName);
end; end;
end; end;
end else begin end else begin