Develop and Download Open Source Software

Browse CVS Repository

Annotation of /gikonavigoeson/gikonavi/GikoXMLDoc.pas

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.9 - (hide annotations) (download) (as text)
Thu Sep 16 14:09:52 2004 UTC (19 years, 7 months ago) by yoffy
Branch: MAIN
CVS Tags: b49, v1_49_0_548, v1_49_0_545, v1_49_0_544, v1_49_0_547, v1_49_0_554, v1_49_0_551, v1_49_0_552, v1_49_0_553, v1_49_0_546, v1_49_2_569
Branch point for: Bb49
Changes since 1.8: +0 -4 lines
File MIME type: text/x-pascal
警告の排除、コード整理。

1 yoffy 1.1 unit GikoXMLDoc;
2    
3     {
4     XMLIntf, XMLDoc ?????????????若??/span>
5 yoffy 1.2 Delphi 6 Personal ??/span>
6 yoffy 1.1 }
7     interface
8    
9     //==================================================
10     uses
11     //==================================================
12    
13 yoffy 1.8 Classes, SysUtils, Windows,
14 yoffy 1.1 YofUtils;
15    
16     //==================================================
17     type
18     //==================================================
19    
20     // ??????????篏??c???????????違??????????
21     XMLDictionary = Record
22     Name : string;
23     Value : string;
24     end;
25    
26     IXMLNode = class
27     private
28     FNodeName : string;
29 yoffy 1.8 FCapacity : Integer;
30 yoffy 1.1 FCount : Integer;
31     FAttributeCount : Integer;
32     FChildNodes : IXMLNode;
33     FNodes : array of IXMLNode;
34     FAttributes : array of XMLDictionary;
35     function GetAttribute( const Name : string ) : string;
36     function GetNode( Index : Integer ) : IXMLNode;
37     public
38 yoffy 1.5 constructor Create;
39     destructor Destroy; override;
40 yoffy 1.1
41     property NodeName : string read FNodeName write FNodeName;
42     property Attributes[ const Name : string ] : string read GetAttribute;
43     property Node[ Index : Integer ] : IXMLNode read GetNode; default;
44     property ChildNodes : IXMLNode read FChildNodes write FChildNodes;
45     property Count : Integer read FCount write FCount;
46     procedure Add( node : IXMLNode );
47     procedure AddAttribute( const Name : string; const Value : string );
48     end;
49    
50     IXMLDocument = class( IXMLNode )
51     private
52     function GetDocumentElement() : IXMLNode;
53     public
54     property DocumentElement : IXMLNode read GetDocumentElement;
55     end;
56    
57     function XMLCloseCheck(
58 yoffy 1.8 var p : PChar;
59     const tail : PChar;
60 yoffy 1.1 var node : IXMLNode;
61     out tag : string;
62     out closed : boolean // ?若?喝?冴???????若???潟?? node ?????????鴻?????? true
63     ) : boolean; // ch ?????????若???潟?????????????? true
64    
65     function XMLReadNode(
66 yoffy 1.8 var p : PChar;
67     const tail : PChar;
68 yoffy 1.1 var node : IXMLNode
69     ) : string; // node 篁ュ??????若???????????????翫???????若????
70    
71 h677 1.6 procedure LoadXMLDocument(
72     const fileName : string;
73     var doc : IXMLDocument
74     );
75 yoffy 1.1
76     //==================================================
77     const
78     //==================================================
79     kXMLWhite : TSysCharSet = [#0..#$20];
80 yoffy 1.8 kXMLNodeNameStop : TSysCharSet = [#0..#$20, '/', '>'];
81     kXMLAttributeNameStop : TSysCharSet = [#0..#$20, '=', '/', '>'];
82 yoffy 1.1 kXMLDQuote : TSysCharSet = ['"'];
83     kXMLTagStart : TSysCharSet = ['<'];
84     kXMLTagEnd : TSysCharSet = ['>'];
85 yoffy 1.7 kXMLKanji : TSysCharSet = [#$81..#$9f, #$E0..#$fc];
86 yoffy 1.1
87     //==================================================
88     implementation
89     //==================================================
90    
91     // Constructor
92 yoffy 1.5 constructor IXMLNode.Create;
93 yoffy 1.1 begin
94    
95 yoffy 1.5 inherited;
96    
97 yoffy 1.8 FCapacity := 0;
98 yoffy 1.1 FCount := 0;
99 yoffy 1.5
100     end;
101    
102     // Destructor
103     destructor IXMLNode.Destroy;
104     var
105     i : Integer;
106     begin
107    
108     for i := FCount - 1 downto 0 do
109     FNodes[ i ].Free;
110     FChildNodes.Free;
111    
112     inherited;
113 yoffy 1.1
114     end;
115    
116     function IXMLNode.GetAttribute( const Name : string ) : string;
117     var
118     i : Integer;
119     begin
120    
121     i := 0;
122     while i < FAttributeCount do
123     begin
124     if Name = FAttributes[ i ].Name then
125     begin
126     Result := FAttributes[ i ].Value;
127     exit;
128     end;
129    
130     Inc( i );
131     end;
132    
133     end;
134    
135     function IXMLNode.GetNode( Index : Integer ) : IXMLNode;
136     begin
137    
138     Result := FNodes[ Index ];
139    
140     end;
141    
142     procedure IXMLNode.Add( node : IXMLNode );
143     begin
144    
145     Inc( FCount );
146 yoffy 1.8 if FCount > FCapacity then begin
147     FCapacity := FCapacity + (FCapacity shr 2) + 1;
148     SetLength( FNodes, FCapacity );
149     end;
150    
151 yoffy 1.1 FNodes[ FCount - 1 ] := node;
152    
153     end;
154    
155     procedure IXMLNode.AddAttribute(
156     const Name : string;
157     const Value : string
158     );
159     var
160     index : Integer;
161     begin
162    
163     index := FAttributeCount;
164     Inc( FAttributeCount );
165     SetLength( FAttributes, FAttributeCount );
166 yoffy 1.8
167 yoffy 1.1 FAttributes[ index ].Name := Name;
168     FAttributes[ index ].Value := Value;
169    
170     end;
171    
172     function IXMLDocument.GetDocumentElement() : IXMLNode;
173     begin
174    
175     Result := FChildNodes[ 0 ];
176    
177     end;
178    
179 yoffy 1.8 {*!
180     \brief tok ???≪??
181     \param p ?∝刈??紮?篏?臀?/span>
182     \param tail 腟?篋?篏?臀 + 1
183     \param tok ?≪?????c??????/span>
184     \return tok ??????????ゃ???c??篏?臀?/span>
185     *}
186     function AnsiStrTok(
187     p : PChar;
188     const tail : PChar;
189     const tok : TSysCharSet
190     ) : PChar;
191 yoffy 1.1 begin
192    
193 yoffy 1.8 while p < tail do
194 yoffy 1.1 begin
195 yoffy 1.8 if p^ in tok then
196 yoffy 1.1 begin
197 yoffy 1.8 Break;
198     end else if p^ in kXMLKanji then
199     p := p + 2
200     else
201     Inc( p );
202 yoffy 1.1 end;
203    
204 yoffy 1.8 Result := p;
205    
206 yoffy 1.1 end;
207    
208 yoffy 1.8 {*!
209     \brief tok ?с???<?????c?????帥???≪??
210     \param p ?∝刈??紮?篏?臀?/span>
211     \param tail 腟?篋?篏?臀 + 1
212     \param tok ?≪?????c??????/span>
213     \return tok ?с?????????c?????帥??????????ゃ???c??篏?臀?/span>
214     *}
215     function AnsiStrNonTok(
216     p : PChar;
217     const tail : PChar;
218     const tok : TSysCharSet
219     ) : PChar;
220 yoffy 1.1 begin
221    
222 yoffy 1.8 while p < tail do
223 yoffy 1.1 begin
224 yoffy 1.8 if p^ in tok then
225 yoffy 1.1 begin
226 yoffy 1.8 if p^ in kXMLKanji then
227     p := p + 2
228     else
229     Inc( p );
230 yoffy 1.1 end else begin
231 yoffy 1.8 Break;
232 yoffy 1.1 end;
233     end;
234    
235 yoffy 1.8 Result := p;
236    
237 yoffy 1.1 end;
238    
239     function XMLCloseCheck(
240 yoffy 1.8 var p : PChar;
241     const tail : PChar;
242 yoffy 1.1 var node : IXMLNode;
243     out tag : string;
244     out closed : boolean
245     ) : boolean; // ch ?????????若???潟?????????????? true
246     var
247 yoffy 1.8 found : PChar;
248 yoffy 1.1 begin
249    
250     closed := false;
251     Result := false;
252     tag := '';
253    
254 yoffy 1.8 case p^ of
255     '>':
256     begin
257     // ??紮??帥?違????緇??障?ц?????
258     Inc( p ); // '>' 蕋??違??
259     Result := true;
260     end;
261    
262     '?':
263     begin
264     // <?xml?> ?帥?????????ゃ?????c???∴?
265     p := AnsiStrTok( p, tail, kXMLTagEnd );
266     p := AnsiStrTok( p, tail, kXMLTagStart );
267     Inc( p ); // '<' 蕋??違??
268     p := AnsiStrNonTok( p, tail, kXMLWhite );
269     //closed := true;
270     Result := true;
271     end;
272    
273     '/':
274     begin
275     // ?帥?医????茯??粋昭???ц???
276     Inc( p ); // '/' 蕋??違??
277     found := AnsiStrTok( p, tail, kXMLTagEnd );
278     // tag := Copy( p, 0, found - p ); // 篏?????羶???
279     SetLength( tag, found - p );
280     CopyMemory( PChar( tag ), p, found - p );
281    
282     p := found + 1; // '>' 蕋??違??
283     closed := true;
284     Result := true;
285     end;
286 yoffy 1.1 end;
287    
288     end;
289    
290     function XMLReadNode(
291 yoffy 1.8 var p : PChar;
292     const tail : PChar;
293 yoffy 1.1 var node : IXMLNode
294     ) : string; // node 篁ュ??????若???????????????翫???????若????
295     var
296     child : IXMLNode;
297    
298 yoffy 1.8 found : PChar;
299 yoffy 1.1 tag : string;
300    
301     isClosed : boolean;
302    
303 yoffy 1.8 nodeName : string;
304 yoffy 1.1 attributeName : string;
305     attributeValue : string;
306     label
307     NextNode;
308     begin
309     try
310     // node ????粋昭??1 ???若?????ゃ?? 1 ???若??)
311     node.ChildNodes := IXMLNode.Create;
312    
313 yoffy 1.8 while p < tail do
314 yoffy 1.1 begin
315     // NodeName 茯??粋昭??/span>
316 yoffy 1.8 p := AnsiStrNonTok( p, tail, kXMLWhite );
317 yoffy 1.1
318 yoffy 1.8 while p < tail do
319 yoffy 1.1 begin
320 yoffy 1.8 if XMLCloseCheck( p, tail, node, tag, isClosed ) then
321 yoffy 1.1 begin
322     if isClosed then
323     begin
324     Result := tag;
325     exit;
326     end;
327    
328     goto NextNode;
329 yoffy 1.8 end else if p^ = '<' then
330 yoffy 1.1 begin
331     // ?域????若??
332 yoffy 1.8 Inc( p );
333 yoffy 1.1 child := IXMLNode.Create;
334 yoffy 1.8 tag := XMLReadNode( p, tail, child );
335 yoffy 1.1 node.ChildNodes.Add( child );
336    
337     // ?帥?違????????????
338     if Length( tag ) > 0 then
339     begin
340     // ???????????????с???????????????域Κ?????
341     if tag <> node.NodeName then
342     Result := tag;
343     exit;
344     end;
345    
346     goto NextNode;
347 yoffy 1.8 end else if p^ in kXMLWhite then
348 yoffy 1.1 begin
349     // NodeName 絎?篋?
350     break;
351     end else begin
352 yoffy 1.8 found := AnsiStrTok( p, tail, kXMLNodeNameStop );
353     SetLength( nodeName, found - p );
354     CopyMemory( PChar( nodeName ), p, found - p );
355     node.NodeName := nodeName;
356 yoffy 1.1
357 yoffy 1.8 p := found;
358 yoffy 1.1 end;
359     end;
360    
361     // Attribute ????粋昭??/span>
362 yoffy 1.8 while p < tail do
363 yoffy 1.1 begin
364     // Attribute ????????茯??粋昭??/span>
365     attributeName := '';
366     attributeValue := '';
367    
368 yoffy 1.8 p := AnsiStrNonTok( p, tail, kXMLWhite );
369 yoffy 1.1
370 yoffy 1.8 while p < tail do
371 yoffy 1.1 begin
372 yoffy 1.8 if XMLCloseCheck( p, tail, node, tag, isClosed ) then
373 yoffy 1.1 begin
374     if isClosed then
375     begin
376     // ?帥?違???????????????с???帥?若??/span>
377     // ??odeName ???????????????ч??筝??ч????????????????????
378     // ???c????????若????
379     exit;
380     end;
381    
382     // 罨<?????若????/span>
383     goto NextNode;
384 yoffy 1.8 end else if p^ = '=' then
385 yoffy 1.1 begin
386     // ???????????ゃ??紮??障?????у???????篋?
387 yoffy 1.8 Inc( p );
388 yoffy 1.1 break;
389 yoffy 1.8 end else if p^ in kXMLWhite then
390 yoffy 1.1 begin
391     // Value ??絖?????????(荀?弱?)???ф??????若????/span>
392     goto NextNode;
393     end else begin
394 yoffy 1.8 found := AnsiStrTok( p, tail, kXMLAttributeNameStop );
395     SetLength( attributeName, found - p );
396     CopyMemory( PChar( attributeName ), p, found - p );
397 yoffy 1.1
398 yoffy 1.8 p := found;
399 yoffy 1.1 end;
400     end;
401    
402     // Attribute ???ゃ??茯??粋昭??/span>
403 yoffy 1.8 p := AnsiStrNonTok( p, tail, kXMLWhite );
404 yoffy 1.1
405 yoffy 1.8 while p < tail do
406 yoffy 1.1 begin
407 yoffy 1.8 if XMLCloseCheck( p, tail, node, tag, isClosed ) then
408 yoffy 1.1 begin
409     if isClosed then
410     begin
411     if Length( attributeName ) > 0 then
412     // 荀?弱?????????/span>
413     node.AddAttribute( attributeName, attributeValue );
414    
415     // ?帥?違???????????????с???帥?若??/span>
416     // ??odeName ???????????????ч??筝??ч????????????????????
417     // ???c????????若????
418     exit;
419     end;
420    
421     // 罨<?????若????/span>
422     goto NextNode;
423 yoffy 1.8 end else if p^ = '"' then
424 yoffy 1.1 begin
425     // ?ゃ?? "" ?ф????????????????????????????????????????????????????
426     // ?ゃ??筝?????粋昭??/span>
427 yoffy 1.8 Inc( p );
428     found := AnsiStrTok( p, tail, kXMLDQuote );
429     // attributeValue := Copy( p, 0, found - p ); // 篏?????羶???
430     SetLength( attributeValue, found - p );
431     CopyMemory( PChar( attributeValue ), p, found - p );
432 yoffy 1.1
433 yoffy 1.4 node.AddAttribute( attributeName, HtmlDecode( attributeValue ) );
434 yoffy 1.1
435     // ?ゃ??茯??睡???c?????х?篋?
436 yoffy 1.8 p := found + 1; // '"' 蕋??違??
437 yoffy 1.1 break;
438 yoffy 1.8 end else if p^ in kXMLWhite then
439 yoffy 1.1 begin
440     // 荀?弱?????????/span>
441 yoffy 1.4 node.AddAttribute( attributeName, HtmlDecode( attributeValue ) );
442 yoffy 1.1
443     goto NextNode;
444     end else begin
445     // 荀?弱????????綽????c??????span>
446 yoffy 1.8 attributeValue := attributeValue + p^;
447 yoffy 1.1
448 yoffy 1.8 if p^ in kXMLKanji then
449 yoffy 1.1 begin
450 yoffy 1.8 attributeValue := attributeValue + (p + 1)^;
451     p := p + 2;
452     end else begin
453     Inc( p );
454 yoffy 1.1 end;
455     end;
456     end;
457     end; // Attribute ????粋昭??/span>
458    
459     NextNode:;
460     end; // // node ????粋昭??1 ???若?????ゃ?? 1 ???若??)
461     finally
462     end;
463     end;
464    
465 h677 1.6 procedure LoadXMLDocument(
466     const fileName : string;
467     var doc : IXMLDocument
468     );
469 yoffy 1.1 type
470     xmlMode = ( xmlHoge );
471     var
472 yoffy 1.8 xmlFile : TMappedFile;
473     p : PChar;
474 yoffy 1.1 begin
475 yoffy 1.8 //Result := IXMLDocument.Create;
476 h677 1.6 //doc := IXMLDocument.Create;
477 yoffy 1.1
478 yoffy 1.8 xmlFile := TMappedFile.Create( fileName );
479 yoffy 1.1
480 yoffy 1.8 try
481     p := xmlFile.Memory;
482     XMLReadNode( p, p + xmlFile.Size, IXMLNode( doc ) );
483     //XMLReadNode( xmlFile, IXMLNode( Result ) );
484     finally
485 h677 1.6 xmlFile.Free;
486 yoffy 1.8 end;
487 yoffy 1.1
488 h677 1.6 //Result := doc;
489 yoffy 1.1
490     end;
491    
492     end.

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26