转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
Delphi 6 SOAP 源码中的BUG修正         ★★★★

Delphi 6 SOAP 源码中的BUG修正

作者:闵涛 文章来源:闵涛的学习笔记 点击数:2253 更新时间:2009/4/23 18:41:03

Delphi 6 SOAP 源码中的BUG修正

最近我正在用delphi 6 做一个关于SOAP的项目,调试程序的时候跟踪源码发现了delphi 6的源码中的一些bug :(

在 delphi/source/soap 目录下面的XSBuiltIns.pas 文件中,第438行如下:

procedure TXSDate.XSToNative(Value: WideString);
var
  TempDate: TDateTime;
begin
  FAdditionalYearDigits := Pos(XMLDateSeparator,Value) - (1 + XMLDefaultYearDigits);
  TempDate := StrToDate(XMLDateToStr(Value, FAdditionalYearDigits));  // 注意这行代码
  DecodeDate(TempDate, FYear, FMonth, FDay);
end;

其中调用了一个XMLDateToStr函数,下面是该函数的代码(在XSBuiltIns.pas 的241行):

function XMLDateToStr(ADate: WideString; AddDigits: Word = 0): WideString; 
begin
  Result := Copy(ADate, XMLMonthPos + AddDigits, 2) + DateSeparator +
            Copy(ADate, XMLDayPos + AddDigits, 2 ) +
            DateSeparator + Copy(ADate, XMLYearPos, XMLDefaultYearDigits + AddDigits); 
end;

注意, XMLDateToStr返回的日期格式是 MM-DD-YYYY。例如如果传过去的参数Adate = ''''2001-12-08'''',则XMLDateToStr(''''2001-12-08'''', 0)的结果是''''12-08-2001''''。XMLDateToStr的第二个参数AddDigits是多余的年代的位数,估计是用来解决千年虫问题的,对于标准的10位日期格式AddDigits始终是0。

现在问题来了,回到XSToNative的代码中,这行代码:

TempDate := StrToDate(XMLDateToStr(Value, FAdditionalYearDigits));

以函数XMLDateToStr的返回值作为参数调用了StrToDate这个函数, 函数StrToDate的作用是将字符串转化为日期,这个函数的参数应该是一个表示日期的字符串,但是日期的字符串格式必须符合当前平台的区域设置,比如我们常用的中文平台的短日期格式一般是 YYYY-MM-DD,而英文平台一般是 MM-DD-YYYY,所以如果在中文的平台上调用strToDate()来格式化一个格式为"MM-DD-YYYY"的字符串就会产生一个日期格式错误异常!!

下面是delphi的帮助中对StrToDate函数的解释的内容:

function StrToDate(const S: string): TDateTime; 
Description 
Call StrToDate to parse a string that specifies a date. If S does not contain a valid date, 
StrToDate raises an EConvertError exception.S must consist of two or three numbers, separated by the character
defined by the DateSeparator global variable. 
The order for month, day, and year is determined by the ShortDateFormat global variable--possible 
combinations are m/d/y, d/m/y, and y/m/d.

过程DecodeDate(TempDate, FYear, FMonth, FDay);的最后一行代码

DecodeDate(TempDate, FYear, FMonth, FDay);

作用是将TempDate重新解析为FYear, FMonth, FDay三个域,其实可以直接从原来的字符串表示的日期中解析出这三个域,所以稍微修改一下就可以解决这个bug。

具体做法是:
将delphi/source/soap 目录下面的XSBuiltIns.pas 文件中的第433行开始的函数改为

procedure TXSDate.XSToNative(Value: WideString);
begin
  FAdditionalYearDigits := Pos(XMLDateSeparator,Value) - (1 + XMLDefaultYearDigits);
  try
    FYear := StrToInt(Copy(Value, XMLYearPos, XMLDefaultYearDigits + FAdditionalYearDigits));
    FMonth := StrToInt(Copy(Value, XMLDayPos + FAdditionalYearDigits, 2 ));
    FDay := StrToInt(Copy(Value, XMLMonthPos + FAdditionalYearDigits, 2));
  except
    raise EConvertError.CreateResFmt(@SInvalidDate, [Value]);
  end;
end;

在文件XSBuiltIns.pas的开头要加上

interface
uses SysUtils, InvokeRegistry, SysConst;  // 加上了 SysConst, 因为资源字符串SInvalidDate定义在SysConst.pas中

另外,还是在delphi/source/soap 目录下面的XSBuiltIns.pas 文件中,下面这个函数:

// get Small Int Using Digits in value, positive or negative. 
function IntFromValue(Value: WideString; Digits: Integer): SmallInt; begin
  Result := 0;
  if Value[1] = ''''-'''' then
    Result := StrToInt(Value)
  else if Value <> '''''''' then
    Result := StrToInt(Copy(Value, 1, Digits));
end;

很明显的bug呀!假如参数Value = '''''''' 那么执行到 if Value[1]=''''-'''' then .. 就出错了:( 事实上我的程序运行过程中就遇到这个情况,当SOAP通过XML传递过来的日期时间中没有包括时间的毫秒域的时候,就会遇到用一个''''''''调用IntFromValue的情况。

修改以后的该函数应该如下所示:

// modified by starfish
function IntFromValue(Value: WideString; Digits: Integer): SmallInt;
begin
  if Value = '''''''' then
    Result := 0
  else if Value[1] = ''''-'''' then
    Result := StrToInt(Value)
  else 
    Result := StrToInt(Copy(Value, 1, Digits));
end;

另外还有

function TXSTime.GetAsTime: TDateTime;
function TXSDate.GetAsDate: TDateTime;
function TXSCustomDateTime.GetAsDateTime: TDateTime;

也都有我前面说到的那个日期时间的bug,写这段代码的程序员忘记了日期时间格式会随着不同的系统而不同。下面是我彻底修改过后的XSBuiltIns.pas文件,所有被修改的地方都加了注释。

注意:我不能保证我修改后的代码一定完全正确,但是我可以肯定它原来的代码中有很多bug!

下面修改后的XSBuiltIns.pas文件,将其放入 delpi/source/soap/ 目录下覆盖原来的文件并重新编译所有的SOAP项目即可。

 
{*******************************************************}
{                                                       }
{ Borland Delphi Visual Component Library               }
{                SOAP Support                           }
{                                                       }
{ Copyright (c) 2001 Borland Software Corporation       }
{                                                       }
{*******************************************************}
unit XSBuiltIns;
interface
// SysConst, DateUtils is added by starfish
uses SysUtils, InvokeRegistry, SysConst, DateUtils;
const
  SoapTimePrefix = ''''T'''';
  XMLDateSeparator = ''''-'''';
  XMLHourOffsetMinusMarker = ''''-'''';
  XMLHourOffsetPlusMarker = ''''+'''';
  XMLTimeSeparator = '''':'''';
  XMLMonthPos = 6;
  XMLDayPos = 9;
  XMLYearPos = 1;
  XMLMilSecPos = 10;
  XMLDefaultYearDigits = 4;
  XMLDurationStart = ''''P'''';
  XMLDurationYear = ''''Y'''';
  XMLDurationMonth = ''''M'''';
  XMLDurationDay = ''''D'''';
  XMLDurationHour = ''''H'''';
  XMLDurationMinute = ''''M'''';
  XMLDurationSecond = ''''S'''';
resourcestring
  SInvalidHour = ''''Invalid hour: %d'''';
  SInvalidMinute = ''''Invalid minute: %d'''';
  SInvalidSecond = ''''Invalid second: %d'''';
  SInvalidFractionSecond = ''''Invalid second: %f'''';
  SInvalidMillisecond = ''''Invalid millisecond: %d'''';
  SInvalidHourOffset = ''''Invalid hour offset: %d'''';
  SInvalidDay = ''''Invalid day: %d'''';
  SInvalidMonth = ''''Invalid month: %d'''';
  SInvalidDuration = ''''Invalid Duration String: %s'''';
type
{ forward declarations }
TXSDuration = class;
TXSTime = class;
TXSDate = class;
TXSDateTime = class;
{ TXSTime }
TXSTime = class(TRemotableXS)
  private
    FHour: Word;
    FMinute: Word;
    FSecond: Word;
    FMillisecond: Word;
    FHourOffset: SmallInt;
    FMinuteOffset: SmallInt;
    function BuildHourOffset: WideString;
  protected
    function GetAsTime: TDateTime;
    procedure SetAsTime(Value: TDateTime);
    procedure SetHour(const Value: Word);
    procedure SetMinute(const Value: Word);
    procedure SetSecond(const Value: Word);
    procedure SetMillisecond(const Value: Word);
    procedure SetHourOffset(const Value: SmallInt);
    procedure SetMinuteOffset(const Value: SmallInt);
  public
    function Clone: TXSTime;
    property Hour: Word read FHour write SetHour default 0;
    property Minute: Word read FMinute write SetMinute default 0;
    property Second: Word read FSecond write SetSecond default 0;
    property Millisecond: Word read FMillisecond write SetMillisecond default 0;
    property HourOffset: SmallInt read FHourOffset write SetHourOffset default 0;
    property MinuteOffset: SmallInt read FMinuteOffset write SetMinuteOffset;
    procedure XSToNative(Value: WideString); override;
    function NativeToXS: WideString; override;
    property AsTime: TDateTime read GetAsTime write SetAsTime;
end;
{ TXSDate }
TXSDate = class(TRemotableXS)
  private
    FAdditionalYearDigits: Word;
    FMonth: Word;
    FDay: Word;
    FYear: Word;
    FMaxDay: Word;
    FMaxMonth: Word;
    FMinDay: Word;
    FMinMonth: Word;
  protected
    function GetAsDate: TDateTime;
    procedure SetAsDate(Value: TDateTime);
    procedure SetMonth(const Value: Word);
    procedure SetDay(const Value: Word);
    procedure SetYear(const Value: Word);
    property MaxDay: Word read FMaxDay write FMaxDay;
    property MaxMonth: Word read FMaxMonth write FMaxMonth;
    property MinDay: Word read FMinDay write FMinDay;
    property MinMonth: Word read FMinMonth write FMinMonth;
  public
    constructor Create; override;
    property Month: Word read FMonth write SetMonth default 0;
    property Day: Word read FDay write SetDay default 0;
    property Year: Word read FYear write SetYear default 0;
    function Clone: TXSDate;
    procedure XSToNative(Value: WideString); override;
    function NativeToXS: WideString; override;
    property AsDate: TDateTime read GetAsDate write SetAsDate;
end;
{ TXSCustomDateTime }
TXSCustomDateTime = class(TRemotableXS)
  private
    FDateParam: TXSDate;
    FTimeParam: TXSTime;
  protected
    function GetAsDateTime: TDateTime;
    function GetHour: Word;
    function GetMinute: Word;
    function GetSecond: Word;
    function GetMonth: Word;
    function GetDay: Word;
    function GetYear: Word;
    function GetMillisecond: Word;
    function GetHourOffset: SmallInt;
    function GetMinuteOffset: SmallInt;
    procedure SetAsDateTime(Value: TDateTime);
    procedure SetHour(const Value: Word); virtual;
    procedure SetMinute(const Value: Word); virtual;
    procedure SetSecond(const Value: Word); virtual;
    procedure SetMillisecond(const Value: Word); virtual;
    procedure SetHourOffset(const Value: SmallInt); virtual;
    procedure SetMinuteOffset(const Value: SmallInt); virtual;
    procedure SetMonth(const Value: Word); virtual;
    procedure SetDay(const Value: Word); virtual;
    procedure SetYear(const Value: Word); virtual;
  public
    constructor Create;  override;
    destructor Destroy; override;
    property AsDateTime: TDateTime read GetAsDateTime write SetAsDateTime;
    property Hour: Word read GetHour write SetHour default 0;
    property Minute: Word read GetMinute write SetMinute default 0;
    property Second: Word read GetSecond write SetSecond default 0;
    // the following line is added by starfish
    property Millisecond: Word read GetMillisecond write SetMillisecond default 0;
    property Month: Word read GetMonth write SetMonth default 0;
    property Day: Word read GetDay write SetDay default 0;
    property Year: Word read GetYear write SetYear default 0;
end;
{ TXSDateTime }
TXSDateTime = class(TXSCustomDateTime)
  private
    function ValidValue(Value, Subtract, Min, Max: Integer; var Remainder: Integer): Integer;
  public  
    function CompareDateTimeParam(const Value1, Value2: TXSDateTime): TXSDuration;
  public
    function Clone: TXSDateTime;
    property Millisecond: Word read GetMillisecond write SetMillisecond default 0;
    property HourOffset: SmallInt read GetHourOffset write SetHourOffset default 0;
    property MinuteOffset: SmallInt read GetMinuteOffset write SetMinuteOffset default 0;
    procedure XSToNative(Value: WideString); override;
    function NativeToXS: WideString; override;
end;
TXSDu

[1] [2] [3]  下一页


[系统软件]InstallShield Express for delphi制作安装程序定…  [系统软件]BCB6 下devexpress 安装手记
[常用软件]InstallShield Express制作Delphi数据库安装程序  [常用软件]Internet Explorer 6 Public Preview 最新出击!!
[常用软件]painter 6 手绘实例《油彩篇》  [常用软件]painter 6 手绘实例《粉彩篇》
[常用软件]Painter 6 手绘实例《胶彩篇》  [VB.NET程序]VB.NET实现DirectSound9 (6) 声音特效
[VB.NET程序]Visual Basic 6 逆向工程与反逆向工程 (2)  [VB.NET程序]Visual Basic 6 逆向工程与反逆向工程 (1)
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台