Debian Codegraph @

src/compatibility/delphi-only/dom.pas

raw
1 {
2 Copyright 2019-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Classes to access XML from Delphi.
17
18 The API of this unit is deliberately compatible with (a subset of) FPC DOM
19 unit, so you can just do "uses DOM" in both FPC and Delphi code
20 and operate on XML with the same API.
21 Underneath we use Delphi XML / DOM API based on interfaces
22 ( https://docwiki.embarcadero.com/RADStudio/Sydney/en/Using_the_Document_Object_Model ). }
23 unit DOM;
24
25 {$I castleconf.inc}
26
27 { Delphi XML can be supported by various vendors.
28 See https://docwiki.embarcadero.com/RADStudio/Sydney/en/Using_the_Document_Object_Model .
29
30 The default:
31 - on Windows is MSXML
32 (in some older versions you had to download it from
33 https://www.microsoft.com/en-us/download/details.aspx?id=3988 ).
34 - On other platforms, despite the Delphi docs (which say that XML will just
35 not work without selecting other vendor) it seems some other vendor is
36 automatically picked.
37 TODO: which one? Omni?
38
39 Define this symbol to explicitly select OmniXML vendor on all platform.
40 OmniXML is:
41 - cross-platform
42 - open-source
43 https://code.google.com/archive/p/omnixml/
44 https://github.com/mremec/omnixml/
45 (though it is at this point just included in Delphi, so you don't need to care)
46 - doesn't cause a crash when CastleConfig is finalized from C++ Builder
47 (testcase: examples/delphi/cpp_builder/window/)
48 }
49 {$define CASTLE_XML_OMNI}
50
51 interface
52
53 uses XMLDoc, XMLIntf, Contnrs, Classes, SysUtils;
54
55 const
56
57
58 // DOM Level 1 exception codes:
59
60 INDEX_SIZE_ERR = 1; // index or size is negative, or greater than the allowed value
61 DOMSTRING_SIZE_ERR = 2; // Specified range of text does not fit into a DOMString
62 HIERARCHY_REQUEST_ERR = 3; // node is inserted somewhere it does not belong
63 WRONG_DOCUMENT_ERR = 4; // node is used in a different document than the one that created it (that does not support it)
64 INVALID_CHARACTER_ERR = 5; // invalid or illegal character is specified, such as in a name
65 NO_DATA_ALLOWED_ERR = 6; // data is specified for a node which does not support data
66 NO_MODIFICATION_ALLOWED_ERR = 7; // an attempt is made to modify an object where modifications are not allowed
67 NOT_FOUND_ERR = 8; // an attempt is made to reference a node in a context where it does not exist
68 NOT_SUPPORTED_ERR = 9; // implementation does not support the type of object requested
69 INUSE_ATTRIBUTE_ERR = 10; // an attempt is made to add an attribute that is already in use elsewhere
70
71 // DOM Level 2 exception codes:
72
73 INVALID_STATE_ERR = 11; // an attempt is made to use an object that is not, or is no longer, usable
74 SYNTAX_ERR = 12; // invalid or illegal string specified
75 INVALID_MODIFICATION_ERR = 13; // an attempt is made to modify the type of the underlying object
76 NAMESPACE_ERR = 14; // an attempt is made to create or change an object in a way which is incorrect with regard to namespaces
77 INVALID_ACCESS_ERR = 15; // parameter or operation is not supported by the underlying object
78
79
80 type
81 DOMString = string;
82 TDOMNode = class;
83 TDOMAttr = class;
84 TDOMNodeList = class;
85 TDOMDocument = class;
86
87 { Node type.
88 This type is not present in FPC DOM unit, instead ELEMENT_NODE
89 and so on are numerical constants. }
90 TXMLNodeType = (
91 INTERNAL_RESERVED,
92 ELEMENT_NODE,
93 ATTRIBUTE_NODE,
94 TEXT_NODE,
95 CDATA_SECTION_NODE,
96 ENTITY_REFERENCE_NODE,
97 ENTITY_NODE,
98 PROCESSING_INSTRUCTION_NODE,
99 COMMENT_NODE,
100 DOCUMENT_NODE,
101 DOCUMENT_TYPE_NODE,
102 DOCUMENT_FRAGMENT_NODE,
103 NOTATION_NODE
104 );
105
106
107 EDOMError = class(Exception)
108 public
109 Code: Integer;
110 constructor Create(ACode: Integer; const ASituation: String);
111 end;
112
113 EDOMHierarchyRequest = class(EDOMError)
114 public
115 constructor Create(const ASituation: String);
116 end;
117
118 EDOMNotFound = class(EDOMError)
119 public
120 constructor Create(const ASituation: String);
121 end;
122
123 TDOMNamedNodeMap = class
124 strict private
125 FOwnerDocument: TDOMDocument;
126 FOwnerNode: TDOMNode;
127 Nodes: TObjectList;
128 InternalList: IXMLNodeList;
129
130 function GetItem(const I: Cardinal): TDOMNode;
131 function GetLength: Cardinal;
132 private
133 function InternalRemove(const Name: String): TDOMNode;
134 public
135 constructor Create(OwnerNode: TDOMNode);
136 destructor Destroy;override;
137 function GetNamedItem(Name: String): TDOMNode;
138
139 property Item[const Index: Cardinal]: TDOMNode read GetItem; default;
140 property Length: Cardinal read GetLength;
141 procedure InvalidateMap;
142 end;
143
144 { Node is a general concept in XML, it can be an element, attribute etc.
145
146 We use TComponent to represent both TDOMNode and TXMLDocument,
147 and all nodes are always owned by the document.
148 Create nodes only by TXMLDocument methods like CreateElement, CreateComment. }
149 TDOMNode = class(TComponent)
150 private
151 FOwnerDocument: TDOMDocument;
152 InternalNode: IXMLNode;
153 FParentNode: TDOMNode;
154 strict private
155 FChildNodes: TDOMNodeList;
156 function GetNodeValue: String;
157 procedure SetNodeValue(const Value: String);
158 function GetFirstChild: TDOMNode; virtual;
159 protected
160 function GetNodeName: String;
161 procedure InvalidateParent;
162 function GetAttributes: TDOMNamedNodeMap; virtual;
163 function GetParentNode: TDOMNode; virtual;
164 function GetTextContent: String; virtual;
165 procedure SetTextContent(const Value: String);virtual;
166 public
167 constructor Create(const AOwnerDocument: TDOMDocument; const AInternalNode: IXMLNode); reintroduce;
168 destructor Destroy; override;
169 property NodeName: String read GetNodeName;
170 property NodeValue: String read GetNodeValue write SetNodeValue;
171 property OwnerDocument: TDOMDocument read FOwnerDocument;
172 function ChildNodes: TDOMNodeList;
173 function NodeType: TXMLNodeType;
174
175 function InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode; virtual;
176 function DetachChild(Child: TDOMNode): TDOMNode;
177 function RemoveChild(Child: TDOMNode): TDOMNode;
178 procedure AppendChild(const Child: TDOMNode); virtual;
179 function FindNode(const NodeName: String): TDOMNode;
180
181 property Attributes: TDOMNamedNodeMap read GetAttributes;
182 property ParentNode: TDOMNode read GetParentNode;
183 property FirstChild: TDOMNode read GetFirstChild;
184
185 property TextContent: String read GetTextContent write SetTextContent;
186 end;
187
188 TDOMNodeList = class
189 strict private
190 FOwnerDocument: TDOMDocument;
191 InternalList: IXMLNodeList;
192 Nodes: TObjectList;
193 function GetCount: Cardinal;
194 function GetItem(const I: Cardinal): TDOMNode;
195 private
196 procedure RemoveNode(const Child: TDOMNode);
197 public
198 constructor Create(const ANode: TDOMNode);
199 destructor Destroy; override;
200 property Item[const I: Cardinal]: TDOMNode read GetItem; default;
201 property Count: Cardinal read GetCount;
202 property Length: Cardinal read GetCount;
203 end;
204
205 TDOMElement = class(TDOMNode)
206 strict private
207 FAttributes: TDOMNamedNodeMap;
208 protected
209 function GetAttributes: TDOMNamedNodeMap; override;
210
211 public
212 destructor Destroy; override;
213
214 { Read from Element attribute value and returns @true,
215 or (if there is no such attribute) returns @false
216 and does not modify Value. Value is a "var", not "out" param,
217 because in the latter case it's guaranteed that the old Value
218 will not be cleared.
219
220 Note that the returned Value may be empty, even when this returns @true,
221 if the value is explicitly set to empty in XML (by @code(xxx="") in XML). }
222 function AttributeString(const Name: String; var Value: String): Boolean; overload;
223
224 function GetAttribute(const name: String): String;
225 procedure SetAttribute(const Name, Value: String);
226 function GetAttributeNode(const Name: String): TDOMAttr;
227 procedure RemoveAttribute(const Name: String);
228 function HasAttribute(const Name: String): Boolean;
229
230 function TagName: String;
231
232 procedure AppendChild(const Child: TDOMNode); override;
233
234 property AttribStrings[const Name: String]: String
235 read GetAttribute write SetAttribute; default;
236 end;
237
238 TDOMCharacterData = class(TDOMNode)
239 function Data: String;
240 end;
241
242 TDOMAttr = class (TDOMNode)
243 public
244 property Name: String read GetNodeName;
245 end;
246
247 TDOMText = class(TDOMCharacterData)
248 end;
249
250 TDOMComment = class(TDOMCharacterData)
251 end;
252
253 TDOMCDATASection = class(TDOMText)
254 end;
255
256 TDOMDocument = class(TComponent)
257 strict private
258 FDocumentElement: TDOMElement;
259 function GetDocumentElement: TDOMElement;
260 public
261 InternalDocument: IXMLDocument;
262 constructor Create; reintroduce;
263 destructor Destroy; override;
264
265 procedure LoadFromStream(Stream: TStream);
266
267 property DocumentElement: TDOMElement read GetDocumentElement;
268 function CreateElement(const Name: String): TDOMElement;
269 function ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode;
270 function CreateComment(const CommentContents: String): TDOMComment;
271 procedure AppendChild(const Child: TDOMNode);
272 end;
273
274 TXMLDocument = class(TDOMDocument)
275
276 end;
277
278 implementation
279
280 {$ifdef CASTLE_XML_OMNI}
281 uses Xml.omnixmldom, Xml.xmldom;
282 {$else}
283 // otherwise we use default MSXML on Windows, which requires some adjustments
284 {$ifdef MSWINDOWS}
285 uses ComObj, Xml.Win.msxmldom;
286 {$endif}
287 {$endif}
288
289 { TDOMNodeList --------------------------------------------------------------- }
290
291 constructor TDOMNodeList.Create(const ANode: TDOMNode);
292 begin
293 inherited Create;
294 FOwnerDocument := ANode.OwnerDocument;
295 InternalList := ANode.InternalNode.ChildNodes;
296
297 { We pass OwnsChildren=false parameter to TObjectList.Create,
298 because we use TComponent ownership mechanism to free TXMLNode instances. }
299 Nodes := TObjectList.Create(false);
300 end;
301
302 destructor TDOMNodeList.Destroy;
303 begin
304 FreeAndNil(Nodes);
305 inherited;
306 end;
307
308 function TDOMNodeList.GetCount: Cardinal;
309 begin
310 Result := InternalList.Count;
311 end;
312
313 function TDOMNodeList.GetItem(const I: Cardinal): TDOMNode;
314 var
315 NewNode: TDOMNode;
316 begin
317 // create on-demand TDOMNode wrapping InternalList[I]
318 if Nodes.Count < Integer(I + 1) then
319 Nodes.Count := I + 1;
320 if Nodes[I] = nil then
321 begin
322 case InternalList[I].NodeType of
323 ntElement: NewNode := TDOMElement.Create(FOwnerDocument, InternalList[I]);
324 ntText : NewNode := TDOMText.Create(FOwnerDocument, InternalList[I]);
325 ntComment: NewNode := TDOMComment.Create(FOwnerDocument, InternalList[I]);
326 ntCData : NewNode := TDOMCDATASection.Create(FOwnerDocument, InternalList[I]);
327 else NewNode := TDOMNode.Create(FOwnerDocument, InternalList[I]);
328 end;
329 Nodes[I] := NewNode;
330 end;
331 // Temporary workaroud for Delphi 12. This will change to access using Integer anyway in delphi-linux branch.
332 Result := Nodes[Integer(I)] as TDOMNode;
333 end;
334
335 procedure TDOMNodeList.RemoveNode(const Child: TDOMNode);
336 begin
337 Nodes.Remove(Child);
338 end;
339
340 { TDOMNode ------------------------------------------------------------------- }
341
342 procedure TDOMNode.AppendChild(const Child: TDOMNode);
343 begin
344 raise EDOMHierarchyRequest.Create('Node.AppendChild');
345 end;
346
347 function TDOMNode.ChildNodes: TDOMNodeList;
348 begin
349 if FChildNodes = nil then
350 FChildNodes := TDOMNodeList.Create(Self);
351 Result := FChildNodes;
352 end;
353
354 constructor TDOMNode.Create(const AOwnerDocument: TDOMDocument; const AInternalNode: IXMLNode);
355 begin
356 inherited Create(AOwnerDocument);
357 FOwnerDocument := AOwnerDocument;
358 FParentNode := nil;
359 InternalNode := AInternalNode;
360 end;
361
362 destructor TDOMNode.Destroy;
363 begin
364 FreeAndNil(FChildNodes);
365 inherited;
366 end;
367
368 function TDOMNode.DetachChild(Child: TDOMNode): TDOMNode;
369 begin
370 if Child.ParentNode <> Self then
371 raise EDOMNotFound.Create('Node.RemoveChild');
372
373 Child.InvalidateParent;
374
375 InternalNode.ChildNodes.Remove(Child.InternalNode);
376 ChildNodes.RemoveNode(Child);
377 Result := Child;
378 end;
379
380 function TDOMNode.FindNode(const NodeName: String): TDOMNode;
381 var
382 I: Integer;
383 begin
384 for I := 0 to Integer(ChildNodes.Count) - 1 do
385 begin
386 if ChildNodes[I].NodeName = NodeName then
387 Exit(ChildNodes[I]);
388 end;
389 Result := nil;
390 end;
391
392 function TDOMNode.GetAttributes: TDOMNamedNodeMap;
393 begin
394 Result := nil;
395 end;
396
397 function TDOMNode.GetFirstChild: TDOMNode;
398 begin
399 if FChildNodes = nil then
400 Exit(nil);
401
402 if FChildNodes.Count = 0 then
403 Exit(nil);
404
405 Result := FChildNodes.Item[0];
406 end;
407
408 function TDOMNode.GetNodeName: String;
409 begin
410 Result := InternalNode.NodeName;
411 end;
412
413 function TDOMNode.GetNodeValue: String;
414 begin
415 Result := InternalNode.NodeValue;
416 end;
417
418 function TDOMNode.GetParentNode: TDOMNode;
419 var
420 InternalParentNode: IXMLNode;
421 I: Integer;
422 NodeToTest: TDOMNode;
423 begin
424 InternalParentNode := InternalNode.ParentNode;
425
426 if InternalParentNode = nil then
427 Exit(nil);
428
429 if FParentNode <> nil then
430 Exit(FParentNode);
431
432 for I := 0 to FOwnerDocument.ComponentCount -1 do
433 begin
434 if not (FOwnerDocument.Components[I] is TDOMNode) then
435 continue;
436
437 NodeToTest := TDOMNode(FOwnerDocument.Components[I]);
438
439 if NodeToTest.InternalNode = InternalParentNode then
440 begin
441 FParentNode := NodeToTest;
442 break;
443 end;
444 end;
445
446 Result := FParentNode;
447 end;
448
449 function TDOMNode.GetTextContent: String;
450 begin
451 Result := NodeValue;
452 end;
453
454 function TDOMNode.InsertBefore(NewChild, RefChild: TDOMNode): TDOMNode;
455 begin
456 raise EDOMHierarchyRequest.Create('Node.InsertBefore');
457 end;
458
459 procedure TDOMNode.InvalidateParent;
460 begin
461 FParentNode := nil;
462 end;
463
464 function TDOMNode.NodeType: TXMLNodeType;
465 const
466 NodeTypeMap: array [XMLIntf.TNodeType] of TXMLNodeType = (
467 INTERNAL_RESERVED,
468 ELEMENT_NODE,
469 ATTRIBUTE_NODE,
470 TEXT_NODE,
471 CDATA_SECTION_NODE,
472 ENTITY_REFERENCE_NODE,
473 ENTITY_NODE,
474 PROCESSING_INSTRUCTION_NODE,
475 COMMENT_NODE,
476 DOCUMENT_NODE,
477 DOCUMENT_TYPE_NODE,
478 DOCUMENT_FRAGMENT_NODE,
479 NOTATION_NODE
480 );
481 begin
482 Result := NodeTypeMap[InternalNode.NodeType];
483 end;
484
485 function TDOMNode.RemoveChild(Child: TDOMNode): TDOMNode;
486 begin
487 Result := DetachChild(Child);
488 end;
489
490 procedure TDOMNode.SetNodeValue(const Value: String);
491 begin
492 InternalNode.NodeValue := Value;
493 end;
494
495 procedure TDOMNode.SetTextContent(const Value: String);
496 begin
497 SetNodeValue(Value);
498 end;
499
500 { TDOMElement ---------------------------------------------------------------- }
501
502 destructor TDOMElement.Destroy;
503 begin
504 FreeAndNil(FAttributes);
505 inherited;
506 end;
507
508 procedure TDOMElement.AppendChild(const Child: TDOMNode);
509 begin
510 InternalNode.ChildNodes.Add(Child.InternalNode);
511 end;
512
513 function TDOMElement.AttributeString(const Name: String; var Value: String): Boolean;
514 var
515 ANodes: IXMLNodeList;
516 Index: Integer;
517 begin
518 ANodes := InternalNode.AttributeNodes;
519 Index := ANodes.IndexOf(Name);
520 Result := Index <> -1;
521 if Result then
522 Value := ANodes[Index].NodeValue;
523 end;
524
525 function TDOMElement.GetAttribute(const name: String): String;
526 begin
527 Result := InternalNode.Attributes[Name];
528 end;
529
530 function TDOMElement.GetAttributeNode(const Name: String): TDOMAttr;
531 begin
532 Result := GetAttributes.GetNamedItem(Name) as TDOMAttr
533 end;
534
535 function TDOMElement.GetAttributes: TDOMNamedNodeMap;
536 begin
537 if FAttributes = nil then
538 FAttributes := TDOMNamedNodeMap.Create(Self);
539
540 Result := FAttributes;
541 end;
542
543 function TDOMElement.HasAttribute(const Name: String): Boolean;
544 begin
545 Result := InternalNode.HasAttribute(Name);
546 end;
547
548 procedure TDOMElement.RemoveAttribute(const Name: String);
549 var
550 AttributeNode: IXMLNode;
551 begin
552 AttributeNode := InternalNode.AttributeNodes.FindNode(Name);
553 if Assigned(AttributeNode) then
554 begin
555 if Assigned(FAttributes) then
556 begin
557 FAttributes.InternalRemove(Name);
558 end;
559
560 InternalNode.AttributeNodes.Remove(AttributeNode);
561 end;
562 end;
563
564 procedure TDOMElement.SetAttribute(const Name, Value: String);
565 begin
566 InternalNode.Attributes[Name] := Value;
567 if FAttributes <> nil then
568 FAttributes.InvalidateMap;
569 end;
570
571 function TDOMElement.TagName: String;
572 begin
573 Result := NodeName;
574 end;
575
576 { TDOMCharacterData --------------------------------------------------------- }
577
578 function TDOMCharacterData.Data: String;
579 begin
580 Result := NodeValue;
581 end;
582
583 { TDOMDocument --------------------------------------------------------------- }
584
585 procedure TDOMDocument.AppendChild(const Child: TDOMNode);
586 begin
587 { We support only one root element now. To change that we should
588 use InternalDocument.DomDocument API and IDOMNode interface not
589 IXMLNode. }
590
591 if InternalDocument.DocumentElement = nil then
592 begin
593 if Child is TDOMElement then
594 begin
595 InternalDocument.DocumentElement := Child.InternalNode;
596 FDocumentElement := TDOMElement(Child);
597 end else
598 if Child <> nil then
599 raise EDOMHierarchyRequest.CreateFmt('TDOMDocument.AppendChild accepts only TDOMElement, but given %s', [
600 Child.ClassName
601 ]);
602 end else
603 raise Exception.Create('Delphi limitation: There can be only one root element!');
604 end;
605
606 constructor TDOMDocument.Create;
607 var
608 InternalDocumentNonRef: XMLDoc.TXMLDocument;
609 begin
610 inherited Create(nil);
611
612 { MSXML special tweak:
613 This needs to be called before using COM interfaces with MSXML.
614 It seems other XML vendors like OmniXML do not require it. }
615 {$ifndef CASTLE_XML_OMNI}
616 {$ifdef MSWINDOWS}
617 { This needs to be called before using COM interfaces with MSXML. }
618 CoInitializeEx(nil, 0);
619 {$endif}
620 {$endif}
621
622 InternalDocumentNonRef := XMLDoc.TXMLDocument.Create(Self);
623 {$ifdef CASTLE_XML_OMNI}
624 InternalDocumentNonRef.DOMVendor := GetDOMVendor(sOmniXmlVendor);
625 {$endif}
626 InternalDocumentNonRef.Active := true;
627 InternalDocument := InternalDocumentNonRef; // assign to interface, it is ref-counted from now on
628
629 FDocumentElement := nil;
630 end;
631
632 function TDOMDocument.CreateComment(const CommentContents: String): TDOMComment;
633 begin
634
635 Result := TDOMComment.Create(Self, InternalDocument.CreateNode(CommentContents, ntComment));
636 end;
637
638 function TDOMDocument.CreateElement(const Name: String): TDOMElement;
639 begin
640 Result := TDOMElement.Create(Self, InternalDocument.CreateElement(Name, ''));
641 end;
642
643 destructor TDOMDocument.Destroy;
644 begin
645 { Note that we do nothing with InternalDocument,
646 it's a COM interface and should be ref-counted.
647 No need for "InternalDocument := nil;". }
648 inherited;
649 end;
650
651 function TDOMDocument.GetDocumentElement: TDOMElement;
652 begin
653 Result := FDocumentElement;
654 end;
655
656 procedure TDOMDocument.LoadFromStream(Stream: TStream);
657 begin
658 FreeAndNil(FDocumentElement);
659
660 InternalDocument.LoadFromStream(Stream);
661
662 if InternalDocument.DocumentElement <> nil then
663 begin
664 FDocumentElement := TDOMElement.Create(Self, InternalDocument.DocumentElement);
665 end;
666 end;
667
668 function TDOMDocument.ReplaceChild(NewChild, OldChild: TDOMNode): TDOMNode;
669 begin
670 if (OldChild = FDocumentElement) and (NewChild is TDOMElement) then
671 begin
672 FDocumentElement := TDOMElement(NewChild);
673 InternalDocument.DocumentElement := NewChild.InternalNode;
674
675 Result := OldChild;
676 end;
677
678 Result := nil;
679 // TODO: Exception? nil?
680 end;
681
682 { TDOMNamedNodeMap ----------------------------------------------------------- }
683
684 constructor TDOMNamedNodeMap.Create(OwnerNode: TDOMNode);
685 begin
686 inherited Create;
687 FOwnerNode := OwnerNode;
688 FOwnerDocument := FOwnerNode.FOwnerDocument;
689 InternalList := FOwnerNode.InternalNode.AttributeNodes;
690
691 { We pass OwnsChildren=false parameter to TObjectList.Create,
692 because we use TComponent ownership mechanism to free TXMLNode instances. }
693 Nodes := TObjectList.Create(false);
694 end;
695
696 destructor TDOMNamedNodeMap.Destroy;
697 begin
698 FreeAndNil(Nodes);
699 inherited;
700 end;
701
702 function TDOMNamedNodeMap.GetItem(const I: Cardinal): TDOMNode;
703 var
704 NewNode: TDOMNode;
705 begin
706 // create on-demand TDOMNode wrapping InternalList[I]
707 if Nodes.Count < Integer(I + 1) then
708 Nodes.Count := I + 1;
709 if Nodes[I] = nil then
710 begin
711 case InternalList[I].NodeType of
712 ntElement : NewNode := TDOMElement.Create(FOwnerDocument, InternalList[I]);
713 ntText : NewNode := TDOMText.Create(FOwnerDocument, InternalList[I]);
714 ntComment : NewNode := TDOMComment.Create(FOwnerDocument, InternalList[I]);
715 ntCData : NewNode := TDOMCDATASection.Create(FOwnerDocument, InternalList[I]);
716 ntAttribute: NewNode := TDOMAttr.Create(FOwnerDocument, InternalList[I]);
717 else NewNode := TDOMNode.Create(FOwnerDocument, InternalList[I]);
718 end;
719 Nodes[I] := NewNode;
720 NewNode.FParentNode := FOwnerNode;
721 end;
722 // Temporary workaroud for Delphi 12. This will change to access using Integer anyway in delphi-linux branch.
723 Result := Nodes[Integer(I)] as TDOMNode;
724 end;
725
726 function TDOMNamedNodeMap.GetLength: Cardinal;
727 begin
728 Result := InternalList.Count;
729 end;
730
731 function TDOMNamedNodeMap.GetNamedItem(Name: String): TDOMNode;
732 var
733 I: Integer;
734 begin
735 I := InternalList.IndexOf(Name);
736 if I = -1 then
737 Exit(nil);
738 Result := GetItem(I);
739 end;
740
741 function TDOMNamedNodeMap.InternalRemove(const Name: String): TDOMNode;
742 var
743 I: Integer;
744 begin
745 I := InternalList.IndexOf(Name);
746 if I = -1 then
747 Exit(nil);
748
749 Result := Nodes[I] as TDOMNode;
750 Nodes.Delete(I);
751 end;
752
753 procedure TDOMNamedNodeMap.InvalidateMap;
754 begin
755 // after adding a new attribute InternalNode.AttributeNodes returns
756 // new IXMLNodeList with NEW nodes so we need change InternalList pointer
757 // this solution is not perfect, previously retrieved attributes will
758 // consume memory until the document is released
759
760 InternalList := FOwnerNode.InternalNode.AttributeNodes;
761 Nodes.Clear;
762 end;
763
764 { EDOMError ------------------------------------------------------------------ }
765
766 constructor EDOMError.Create(ACode: Integer; const ASituation: String);
767 begin
768 Code := ACode;
769 inherited Create(Self.ClassName + ' in ' + ASituation);
770 end;
771
772 { EDOMHierarchyRequest ------------------------------------------------------- }
773
774 constructor EDOMHierarchyRequest.Create(const ASituation: String);
775 begin
776 inherited Create(HIERARCHY_REQUEST_ERR, ASituation);
777 end;
778
779 { EDOMNotFound --------------------------------------------------------------- }
780
781 constructor EDOMNotFound.Create(const ASituation: String);
782 begin
783 inherited Create(NOT_FOUND_ERR, ASituation);
784 end;
785
786 initialization
787 {$ifdef CASTLE_XML_OMNI}
788 (*In the past we changed DefaultDOMVendor, but this was more invasive,
789 as it affects XML implementation used by Delphi IDE (since this unit
790 is also in a design-time package).
791
792 It caused occasional issues when trying to save / open a project in Delphi IDE,
793 about "document encoding",
794 with Delphi IDE stacktrace like this:
795
796 [6FBD3F97]{xmlrtl290.bpl} Xml.XMLDoc.TXMLDocument.LoadData (Line 2557, "Xml.XMLDoc.pas" + 11) + $26
797 [7168A1C4]{rtl290.bpl } System.@CheckAutoResult (Line 40283, "System.pas" + 4) + $6
798 [6FBD3F97]{xmlrtl290.bpl} Xml.XMLDoc.TXMLDocument.LoadData (Line 2557, "Xml.XMLDoc.pas" + 11) + $26
799 [6FBD3E53]{xmlrtl290.bpl} Xml.XMLDoc.TXMLDocument.SetActive (Line 2523, "Xml.XMLDoc.pas" + 13) + $7
800 [67E70559]{profiledeployide290.bpl} DeploymentImpl.TDeploymentStorageManager.ReadDefaultDeployment (Line 1802, "DeploymentImpl.pas" + 47) + $7
801 [67E6C40D]{profiledeployide290.bpl} DeploymentImpl.TDeploymentModuleHandler.EnsureDefaultDeployment (Line 651, "DeploymentImpl.pas" + 13) + $1A
802 [67E6D5C5]{profiledeployide290.bpl} DeploymentImpl.TDeploymentModuleHandler.GetFiles (Line 909, "DeploymentImpl.pas" + 1) + $2
803 [67E6D420]{profiledeployide290.bpl} DeploymentImpl.TDeploymentModuleHandler.GetSortedFiles (Line 865, "DeploymentImpl.pas" + 1) + $2
804 [67E71758]{profiledeployide290.bpl} DeploymentImpl.TDeploymentStorageManager.Write (Line 1962, "DeploymentImpl.pas" + 9) + $8
805 [67E72B37]{profiledeployide290.bpl} DeploymentImpl.TDeploymentStorageManager.ProjectSaving (Line 2153, "DeploymentImpl.pas" + 3) + $11
806 [6F162850]{coreide290.bpl} ProjectFileUtils.CallProjectFileStorage (Line 562, "ProjectFileUtils.pas" + 14) + $B
807 [6F163D55]{coreide290.bpl} ProjectFileUtils.NotifyProjectStorageSaving (Line 844, "ProjectFileUtils.pas" + 4) + $67
808 [6F0C67F5]{coreide290.bpl} ProjectModule.TBaseProject.NotifyProjectStorageSaving (Line 2028, "ProjectModule.pas" + 3) + $23
809 [6F0C6B5A]{coreide290.bpl} ProjectModule.TCustomProject.Save (Line 2108, "ProjectModule.pas" + 20) + $3
810 [6F029C25]{coreide290.bpl} ProjectGroup.SaveProjects (Line 2022, "ProjectGroup.pas" + 68) + $1E
811 [6F029EB4]{coreide290.bpl} ProjectGroup.TProjectGroup.Save (Line 2058, "ProjectGroup.pas" + 2) + $1
812 [6F02DBF9]{coreide290.bpl} ProjectGroup.TProjectGroupWrapper.Save (Line 3357, "ProjectGroup.pas" + 2) + $7
813 [00C28263]{bds.exe } AppMain.TAppBuilder.CanCloseProjectGroup + $4F
814 ....
815
816 The testcase was not 100% confirmed, but it seems that changing package settings
817 from AllProjects group, then trying to open a different project (like fps_game),
818 saying "Yes" to save the AllProjects changes, was causing errors.
819
820 Now we do a safer approach: never change global DefaultDOMVendor,
821 instead we'll set TXMLDocument.DOMVendor after TXMLDocument creation.
822 *)
823 //DefaultDOMVendor := sOmniXmlVendor;
824
825 {$else}
826 {$ifdef MSWINDOWS}
827 { Reading X3D XML fails without this.
828 See https://bobsotherblog.wordpress.com/2013/09/19/fixing-dtd-is-prohibited-error-in-delphi/
829 https://docwiki.embarcadero.com/Libraries/Sydney/en/Xml.Win.msxmldom.MSXML6_ProhibitDTD }
830 Xml.Win.msxmldom.MSXMLDOMDocumentFactory.AddDOMProperty('ProhibitDTD', False);
831 {$endif}
832 {$endif}
833 end.
834