{
    This file is part of the Web Service Toolkit
    Copyright (c) 2006 by Inoussa OUEDRAOGO

    This file is provide under modified LGPL licence
    ( the files COPYING.modifiedLGPL and COPYING.LGPL).
    

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
unit server_binary_formatter;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, TypInfo,
  base_service_intf, server_service_intf,
  base_binary_formatter;

const
  sBINARY_CONTENT_TYPE = 'binary';

  procedure Server_service_RegisterBinaryFormat();
  
implementation

Type

  { TBinaryFormatter }

  TBinaryFormatter = class(TBaseBinaryFormatter,IFormatterBase,IFormatterResponse)
  Private
    FCallProcedureName : string;
    FCallTarget : string;
  Protected
    procedure BeginCallResponse(Const AProcName,ATarget:string);
    procedure EndCallResponse();
    procedure BeginCallRead(ACallContext : ICallContext);
    function GetCallProcedureName():String;
    function GetCallTarget():String;
    procedure BeginExceptionList(
      const AErrorCode : string;
      const AErrorMsg  : string
    );
    procedure EndExceptionList();
  End;

  { TBinaryFormatterFactory }

  TBinaryFormatterFactory = class(TInterfacedObject,IItemFactory)
  protected
    function CreateInstance():IInterface;
  End;

{ TBinaryFormatterFactory }

function TBinaryFormatterFactory.CreateInstance(): IInterface;
begin
  Result := TBinaryFormatter.Create() as IFormatterResponse;
end;


{ TBinaryFormatter }

procedure TBinaryFormatter.BeginCallResponse(const AProcName, ATarget: string);
begin
  BeginObject('Body',Nil);
    BeginObject(ATarget,Nil);
      BeginObject(AProcName + 'Response',Nil);
end;

procedure Print(const AMsg:string);
begin
  WriteLn(AMsg);
End;

procedure TBinaryFormatter.EndCallResponse();
begin
      EndScope();
    EndScope();
  EndScope();
  //PrintObj(GetRootData(),0,@Print);
end;

procedure TBinaryFormatter.BeginCallRead(ACallContext : ICallContext);
Var
  s : string;
begin
  ClearStack();
  PushStack(GetRootData(),stObject);
  s := 'Body';
  BeginScopeRead(s,nil);
    FCallTarget := StackTop().GetByIndex(0)^.Name;
    BeginScopeRead(FCallTarget,nil);
      FCallProcedureName := StackTop().GetByIndex(0)^.Name;
      BeginScopeRead(FCallProcedureName,nil);
end;

function TBinaryFormatter.GetCallProcedureName(): String;
begin
  Result := FCallProcedureName;
end;

function TBinaryFormatter.GetCallTarget(): String;
begin
  Result := FCallTarget;
end;

procedure TBinaryFormatter.BeginExceptionList(
  const AErrorCode: string;
  const AErrorMsg: string
);
begin
  BeginObject('Body',Nil);
    BeginObject('Fault',Nil);
      Put('faultcode',TypeInfo(string),AErrorCode);
      Put('faultstring',TypeInfo(string),AErrorMsg);
end;

procedure TBinaryFormatter.EndExceptionList();
begin
    EndScope();
  EndScope();
end;

procedure Server_service_RegisterBinaryFormat();
begin
  GetFormatterRegistry().Register(sBINARY_CONTENT_TYPE,TBinaryFormatterFactory.Create() as IItemFactory);
end;

Initialization

end.
