OSDN Git Service

SaveToXMLFile
authornaru <bottle@mikage.to>
Wed, 4 Dec 2002 13:26:49 +0000 (13:26 +0000)
committernaru <bottle@mikage.to>
Wed, 4 Dec 2002 13:26:49 +0000 (13:26 +0000)
bottleclient/Logs.pas

index 59968bd..91ac989 100755 (executable)
@@ -48,6 +48,8 @@ type
   TBottleLogSortType = (stLogTime, stChannel, stScript, stVote, stAgree);
   TBottleLogLoadFailureEvent = procedure(Sender: TObject; const Message: String) of object;
 
+  EXMLFileOpenException = class(Exception);
+
   TBottleLogList = class(TObjectList)
   private
     FOnLoaded: TNotifyEvent;
@@ -83,7 +85,8 @@ type
     procedure SaveToText(const FileName: String);
     procedure SaveToSstpLog(const FileName: String;
       const WithChannel: boolean = false);
-    procedure SaveToXmlFile(const FileName: String; XmlDocument: TXmlDocument);
+    procedure SaveToXMLFile(const FileName: String; XMLDocument: TXMLDocument);
+    procedure LoadFromXMLFile(const FileName: String; XMLDocument: TXMLDocument);
   end;
 
 var
@@ -246,8 +249,8 @@ procedure TBottleLogList.HttpSuccess(Sender: TObject);
 var Stream: TStringStream;
     StrList: TStringList;
 begin
-  Stream := nil;
   StrList := nil;
+  Stream := nil;
   try
     Stream := TStringStream.Create(FHttpThread.RecvString);
     StrList := TStringList.Create;
@@ -347,6 +350,52 @@ begin
   FHttpThread.Resume;
 end;
 
+procedure TBottleLogList.LoadFromXMLFile(const FileName: String;
+  XMLDocument: TXMLDocument);
+var i: integer;
+    Time: TDateTime;
+    ANode: IXMLNode;
+    Item: TLogItem;
+begin
+  Self.Clear;
+  XMLDocument.XML.LoadFromFile(FileName);
+  with XMLDocument do begin
+    try
+      Active := true;
+    except
+      raise EXMLFileOpenException.Create('\97L\8cø\82ÈXML\8c`\8e®\82Å\82Í\82 \82è\82Ü\82¹\82ñ');
+    end;
+    if DocumentElement = nil then
+      raise EXMLFileOpenException.Create('\97L\8cø\82È\8c`\8e®\82Å\82Í\82 \82è\82Ü\82¹\82ñ\81B\83\8b\81[\83g\83^\83O\82ª\82 \82è\82Ü\82¹\82ñ');
+    if DocumentElement.NodeName <> 'bottlelog' then
+      raise EXMLFileOpenException.Create('\97L\8cø\82È\8c`\8e®\82Å\82Í\82 \82è\82Ü\82¹\82ñ\81Bbottlelog\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ');
+    if DocumentElement.Attributes['version'] <> '1.0' then
+      raise EXMLFileOpenException.Create('\97L\8cø\82È\8c`\8e®\82Å\82Í\82 \82è\82Ü\82¹\82ñ\81B\82±\82Ì\83\8d\83O\83t\83@\83C\83\8b\82Ì\83o\81[\83W\83\87\83\93\82Í\93Ç\82Ý\8d\9e\82ß\82Ü\82¹\82ñ');
+    for i := 0 to DocumentElement.ChildNodes.Count-1 do begin
+      ANode := DocumentElement.ChildNodes[i];
+      Item := nil;
+      try
+        if TryStrToDateTime(ANode.ChildValues['date'], Time) then begin
+          Item := TLogItem.Create(
+            ltBottle,
+            ANode.Attributes['mid'],
+            ANode.ChildNodes['channel'].Text,
+            ANode.ChildNodes['script'].Text,
+            ANode.ChildNodes['ghost'].Text,
+            Time,
+          );
+          Item.Votes  := StrToIntDef(ANode.ChildNodes['votes'].Text, 0);
+          Item.Agrees := StrToIntDef(ANode.ChildNodes['agrees'].Text, 0);
+          Item.State := lsOpened;
+          Self.Add(Item);
+        end;
+      except
+        Item.Free;
+      end;
+    end;
+  end;
+end;
+
 procedure TBottleLogList.SaveToSstpLog(const FileName: String;
   const WithChannel: boolean = false);
 var i: integer;
@@ -393,7 +442,7 @@ begin
   end;
 end;
 
-procedure TBottleLogList.SaveToXmlFile(const FileName: String;
+procedure TBottleLogList.SaveToXMLFile(const FileName: String;
   XMLDocument: TXMLDocument);
 var i: integer;
     ANode, BNode: IXMLNode;