1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QDOM_H
5#define QDOM_H
6
7#include <QtXml/qtxmlglobal.h>
8#include <QtCore/qstring.h>
9
10QT_BEGIN_NAMESPACE
11
12
13#ifndef QT_NO_DOM
14
15class QIODevice;
16class QTextStream;
17
18class QDomDocumentPrivate;
19class QDomDocumentTypePrivate;
20class QDomDocumentFragmentPrivate;
21class QDomNodePrivate;
22class QDomNodeListPrivate;
23class QDomImplementationPrivate;
24class QDomElementPrivate;
25class QDomNotationPrivate;
26class QDomEntityPrivate;
27class QDomEntityReferencePrivate;
28class QDomProcessingInstructionPrivate;
29class QDomAttrPrivate;
30class QDomCharacterDataPrivate;
31class QDomTextPrivate;
32class QDomCommentPrivate;
33class QDomCDATASectionPrivate;
34class QDomNamedNodeMapPrivate;
35class QDomImplementationPrivate;
36
37class QDomNodeList;
38class QDomElement;
39class QDomText;
40class QDomComment;
41class QDomCDATASection;
42class QDomProcessingInstruction;
43class QDomAttr;
44class QDomEntityReference;
45class QDomDocument;
46class QDomNamedNodeMap;
47class QDomDocument;
48class QDomDocumentFragment;
49class QDomDocumentType;
50class QDomImplementation;
51class QDomNode;
52class QDomEntity;
53class QDomNotation;
54class QDomCharacterData;
55class QXmlStreamReader;
56
57class Q_XML_EXPORT QDomImplementation
58{
59public:
60 QDomImplementation();
61 QDomImplementation(const QDomImplementation&);
62 ~QDomImplementation();
63 QDomImplementation& operator= (const QDomImplementation&);
64 bool operator== (const QDomImplementation&) const;
65 bool operator!= (const QDomImplementation&) const;
66
67 // functions
68 bool hasFeature(const QString& feature, const QString& version) const;
69 QDomDocumentType createDocumentType(const QString& qName, const QString& publicId, const QString& systemId);
70 QDomDocument createDocument(const QString& nsURI, const QString& qName, const QDomDocumentType& doctype);
71
72 enum InvalidDataPolicy { AcceptInvalidChars = 0, DropInvalidChars, ReturnNullNode };
73 static InvalidDataPolicy invalidDataPolicy();
74 static void setInvalidDataPolicy(InvalidDataPolicy policy);
75
76 // Qt extension
77 bool isNull();
78
79private:
80 QDomImplementationPrivate* impl;
81 QDomImplementation(QDomImplementationPrivate*);
82
83 friend class QDomDocument;
84};
85
86class Q_XML_EXPORT QDomNode
87{
88public:
89 enum NodeType {
90 ElementNode = 1,
91 AttributeNode = 2,
92 TextNode = 3,
93 CDATASectionNode = 4,
94 EntityReferenceNode = 5,
95 EntityNode = 6,
96 ProcessingInstructionNode = 7,
97 CommentNode = 8,
98 DocumentNode = 9,
99 DocumentTypeNode = 10,
100 DocumentFragmentNode = 11,
101 NotationNode = 12,
102 BaseNode = 21,// this is not in the standard
103 CharacterDataNode = 22 // this is not in the standard
104 };
105
106 enum EncodingPolicy
107 {
108 EncodingFromDocument = 1,
109 EncodingFromTextStream = 2
110 };
111
112 QDomNode();
113 QDomNode(const QDomNode&);
114 QDomNode& operator= (const QDomNode&);
115 bool operator== (const QDomNode&) const;
116 bool operator!= (const QDomNode&) const;
117 ~QDomNode();
118
119 // DOM functions
120 QDomNode insertBefore(const QDomNode& newChild, const QDomNode& refChild);
121 QDomNode insertAfter(const QDomNode& newChild, const QDomNode& refChild);
122 QDomNode replaceChild(const QDomNode& newChild, const QDomNode& oldChild);
123 QDomNode removeChild(const QDomNode& oldChild);
124 QDomNode appendChild(const QDomNode& newChild);
125 bool hasChildNodes() const;
126 QDomNode cloneNode(bool deep = true) const;
127 void normalize();
128 bool isSupported(const QString& feature, const QString& version) const;
129
130 // DOM read-only attributes
131 QString nodeName() const;
132 NodeType nodeType() const;
133 QDomNode parentNode() const;
134 QDomNodeList childNodes() const;
135 QDomNode firstChild() const;
136 QDomNode lastChild() const;
137 QDomNode previousSibling() const;
138 QDomNode nextSibling() const;
139 QDomNamedNodeMap attributes() const;
140 QDomDocument ownerDocument() const;
141 QString namespaceURI() const;
142 QString localName() const;
143 bool hasAttributes() const;
144
145 // DOM attributes
146 QString nodeValue() const;
147 void setNodeValue(const QString&);
148 QString prefix() const;
149 void setPrefix(const QString& pre);
150
151 // Qt extensions
152 bool isAttr() const;
153 bool isCDATASection() const;
154 bool isDocumentFragment() const;
155 bool isDocument() const;
156 bool isDocumentType() const;
157 bool isElement() const;
158 bool isEntityReference() const;
159 bool isText() const;
160 bool isEntity() const;
161 bool isNotation() const;
162 bool isProcessingInstruction() const;
163 bool isCharacterData() const;
164 bool isComment() const;
165
166 /**
167 * Shortcut to avoid dealing with QDomNodeList
168 * all the time.
169 */
170 QDomNode namedItem(const QString& name) const;
171
172 bool isNull() const;
173 void clear();
174
175 QDomAttr toAttr() const;
176 QDomCDATASection toCDATASection() const;
177 QDomDocumentFragment toDocumentFragment() const;
178 QDomDocument toDocument() const;
179 QDomDocumentType toDocumentType() const;
180 QDomElement toElement() const;
181 QDomEntityReference toEntityReference() const;
182 QDomText toText() const;
183 QDomEntity toEntity() const;
184 QDomNotation toNotation() const;
185 QDomProcessingInstruction toProcessingInstruction() const;
186 QDomCharacterData toCharacterData() const;
187 QDomComment toComment() const;
188
189 void save(QTextStream&, int, EncodingPolicy=QDomNode::EncodingFromDocument) const;
190
191 QDomElement firstChildElement(const QString &tagName = QString(), const QString &namespaceURI = QString()) const;
192 QDomElement lastChildElement(const QString &tagName = QString(), const QString &namespaceURI = QString()) const;
193 QDomElement previousSiblingElement(const QString &tagName = QString(), const QString &namespaceURI = QString()) const;
194 QDomElement nextSiblingElement(const QString &taName = QString(), const QString &namespaceURI = QString()) const;
195
196 int lineNumber() const;
197 int columnNumber() const;
198
199protected:
200 QDomNodePrivate* impl;
201 QDomNode(QDomNodePrivate*);
202
203private:
204 friend class QDomDocument;
205 friend class QDomDocumentType;
206 friend class QDomNodeList;
207 friend class QDomNamedNodeMap;
208};
209
210class Q_XML_EXPORT QDomNodeList
211{
212public:
213 QDomNodeList();
214 QDomNodeList(const QDomNodeList&);
215 QDomNodeList& operator= (const QDomNodeList&);
216 bool operator== (const QDomNodeList&) const;
217 bool operator!= (const QDomNodeList&) const;
218 ~QDomNodeList();
219
220 // DOM functions
221 QDomNode item(int index) const;
222 inline QDomNode at(int index) const { return item(index); } // Qt API consistency
223
224 // DOM read only attributes
225 int length() const;
226 inline int count() const { return length(); } // Qt API consitancy
227 inline int size() const { return length(); } // Qt API consistency
228 inline bool isEmpty() const { return length() == 0; } // Qt API consistency
229
230private:
231 QDomNodeListPrivate* impl;
232 QDomNodeList(QDomNodeListPrivate*);
233
234 friend class QDomNode;
235 friend class QDomElement;
236 friend class QDomDocument;
237};
238
239class Q_XML_EXPORT QDomDocumentType : public QDomNode
240{
241public:
242 QDomDocumentType();
243 QDomDocumentType(const QDomDocumentType& x);
244 QDomDocumentType& operator= (const QDomDocumentType&);
245
246 // DOM read only attributes
247 QString name() const;
248 QDomNamedNodeMap entities() const;
249 QDomNamedNodeMap notations() const;
250 QString publicId() const;
251 QString systemId() const;
252 QString internalSubset() const;
253
254 // Overridden from QDomNode
255 inline QDomNode::NodeType nodeType() const { return DocumentTypeNode; }
256
257private:
258 QDomDocumentType(QDomDocumentTypePrivate*);
259
260 friend class QDomImplementation;
261 friend class QDomDocument;
262 friend class QDomNode;
263};
264
265class Q_XML_EXPORT QDomDocument : public QDomNode
266{
267public:
268 enum class ParseOption {
269 Default = 0x00,
270 UseNamespaceProcessing = 0x01,
271 PreserveSpacingOnlyNodes = 0x02,
272 };
273 Q_DECLARE_FLAGS(ParseOptions, ParseOption)
274
275 struct ParseResult
276 {
277 QString errorMessage;
278 qsizetype errorLine = 0;
279 qsizetype errorColumn = 0;
280
281 explicit operator bool() const noexcept { return errorMessage.isEmpty(); }
282 };
283
284 QDomDocument();
285 explicit QDomDocument(const QString& name);
286 explicit QDomDocument(const QDomDocumentType& doctype);
287 QDomDocument(const QDomDocument& x);
288 QDomDocument& operator= (const QDomDocument&);
289 ~QDomDocument();
290
291 // DOM functions
292 QDomElement createElement(const QString& tagName);
293 QDomDocumentFragment createDocumentFragment();
294 QDomText createTextNode(const QString& data);
295 QDomComment createComment(const QString& data);
296 QDomCDATASection createCDATASection(const QString& data);
297 QDomProcessingInstruction createProcessingInstruction(const QString& target, const QString& data);
298 QDomAttr createAttribute(const QString& name);
299 QDomEntityReference createEntityReference(const QString& name);
300 QDomNodeList elementsByTagName(const QString& tagname) const;
301 QDomNode importNode(const QDomNode& importedNode, bool deep);
302 QDomElement createElementNS(const QString& nsURI, const QString& qName);
303 QDomAttr createAttributeNS(const QString& nsURI, const QString& qName);
304 QDomNodeList elementsByTagNameNS(const QString& nsURI, const QString& localName);
305 QDomElement elementById(const QString& elementId);
306
307 // DOM read only attributes
308 QDomDocumentType doctype() const;
309 QDomImplementation implementation() const;
310 QDomElement documentElement() const;
311
312 // Overridden from QDomNode
313 inline QDomNode::NodeType nodeType() const { return DocumentNode; }
314
315 // Qt extensions
316#if QT_DEPRECATED_SINCE(6, 8)
317 QT_DEPRECATED_VERSION_X_6_8("Use the overload taking ParseOptions instead.")
318 bool setContent(const QByteArray &text, bool namespaceProcessing, QString *errorMsg = nullptr, int *errorLine = nullptr, int *errorColumn = nullptr);
319 QT_DEPRECATED_VERSION_X_6_8("Use the overload taking ParseOptions instead.")
320 bool setContent(const QString &text, bool namespaceProcessing, QString *errorMsg = nullptr, int *errorLine = nullptr, int *errorColumn = nullptr);
321 QT_DEPRECATED_VERSION_X_6_8("Use the overload taking ParseOptions instead.")
322 bool setContent(QIODevice *dev, bool namespaceProcessing, QString *errorMsg = nullptr, int *errorLine = nullptr, int *errorColumn = nullptr);
323 QT_DEPRECATED_VERSION_X_6_8("Use the overload returning ParseResult instead.")
324 bool setContent(const QByteArray &text, QString *errorMsg, int *errorLine = nullptr, int *errorColumn = nullptr);
325 QT_DEPRECATED_VERSION_X_6_8("Use the overload returning ParseResult instead.")
326 bool setContent(const QString &text, QString *errorMsg, int *errorLine = nullptr, int *errorColumn = nullptr);
327 QT_DEPRECATED_VERSION_X_6_8("Use the overload returning ParseResult instead.")
328 bool setContent(QIODevice *dev, QString *errorMsg, int *errorLine = nullptr, int *errorColumn = nullptr);
329 QT_DEPRECATED_VERSION_X_6_8("Use the overload taking ParseOptions instead.")
330 bool setContent(QXmlStreamReader *reader, bool namespaceProcessing, QString *errorMsg = nullptr,
331 int *errorLine = nullptr, int *errorColumn = nullptr);
332#endif // QT_DEPRECATED_SINCE(6, 8)
333
334 Q_WEAK_OVERLOAD
335 ParseResult setContent(const QByteArray &data, ParseOptions options = ParseOption::Default)
336 { return setContentImpl(data, options); }
337 ParseResult setContent(QAnyStringView data, ParseOptions options = ParseOption::Default);
338 ParseResult setContent(QIODevice *device, ParseOptions options = ParseOption::Default);
339 ParseResult setContent(QXmlStreamReader *reader, ParseOptions options = ParseOption::Default);
340
341 // Qt extensions
342 QString toString(int = 1) const;
343 QByteArray toByteArray(int = 1) const;
344
345private:
346 ParseResult setContentImpl(const QByteArray &data, ParseOptions options);
347
348 QDomDocument(QDomDocumentPrivate*);
349
350 friend class QDomNode;
351};
352
353class Q_XML_EXPORT QDomNamedNodeMap
354{
355public:
356 QDomNamedNodeMap();
357 QDomNamedNodeMap(const QDomNamedNodeMap&);
358 QDomNamedNodeMap& operator= (const QDomNamedNodeMap&);
359 bool operator== (const QDomNamedNodeMap&) const;
360 bool operator!= (const QDomNamedNodeMap&) const;
361 ~QDomNamedNodeMap();
362
363 // DOM functions
364 QDomNode namedItem(const QString& name) const;
365 QDomNode setNamedItem(const QDomNode& newNode);
366 QDomNode removeNamedItem(const QString& name);
367 QDomNode item(int index) const;
368 QDomNode namedItemNS(const QString& nsURI, const QString& localName) const;
369 QDomNode setNamedItemNS(const QDomNode& newNode);
370 QDomNode removeNamedItemNS(const QString& nsURI, const QString& localName);
371
372 // DOM read only attributes
373 int length() const;
374 int count() const { return length(); } // Qt API consitancy
375 inline int size() const { return length(); } // Qt API consistency
376 inline bool isEmpty() const { return length() == 0; } // Qt API consistency
377
378 // Qt extension
379 bool contains(const QString& name) const;
380
381private:
382 QDomNamedNodeMapPrivate* impl;
383 QDomNamedNodeMap(QDomNamedNodeMapPrivate*);
384
385 friend class QDomNode;
386 friend class QDomDocumentType;
387 friend class QDomElement;
388};
389
390class Q_XML_EXPORT QDomDocumentFragment : public QDomNode
391{
392public:
393 QDomDocumentFragment();
394 QDomDocumentFragment(const QDomDocumentFragment& x);
395 QDomDocumentFragment& operator= (const QDomDocumentFragment&);
396
397 // Overridden from QDomNode
398 inline QDomNode::NodeType nodeType() const { return DocumentFragmentNode; }
399
400private:
401 QDomDocumentFragment(QDomDocumentFragmentPrivate*);
402
403 friend class QDomDocument;
404 friend class QDomNode;
405};
406
407class Q_XML_EXPORT QDomCharacterData : public QDomNode
408{
409public:
410 QDomCharacterData();
411 QDomCharacterData(const QDomCharacterData& x);
412 QDomCharacterData& operator= (const QDomCharacterData&);
413
414 // DOM functions
415 QString substringData(unsigned long offset, unsigned long count);
416 void appendData(const QString& arg);
417 void insertData(unsigned long offset, const QString& arg);
418 void deleteData(unsigned long offset, unsigned long count);
419 void replaceData(unsigned long offset, unsigned long count, const QString& arg);
420
421 // DOM read only attributes
422 int length() const;
423
424 // DOM attributes
425 QString data() const;
426 void setData(const QString&);
427
428 // Overridden from QDomNode
429 QDomNode::NodeType nodeType() const;
430
431private:
432 QDomCharacterData(QDomCharacterDataPrivate*);
433
434 friend class QDomDocument;
435 friend class QDomText;
436 friend class QDomComment;
437 friend class QDomNode;
438};
439
440class Q_XML_EXPORT QDomAttr : public QDomNode
441{
442public:
443 QDomAttr();
444 QDomAttr(const QDomAttr& x);
445 QDomAttr& operator= (const QDomAttr&);
446
447 // DOM read only attributes
448 QString name() const;
449 bool specified() const;
450 QDomElement ownerElement() const;
451
452 // DOM attributes
453 QString value() const;
454 void setValue(const QString&);
455
456 // Overridden from QDomNode
457 inline QDomNode::NodeType nodeType() const { return AttributeNode; }
458
459private:
460 QDomAttr(QDomAttrPrivate*);
461
462 friend class QDomDocument;
463 friend class QDomElement;
464 friend class QDomNode;
465};
466
467class Q_XML_EXPORT QDomElement : public QDomNode
468{
469public:
470 QDomElement();
471 QDomElement(const QDomElement& x);
472 QDomElement& operator= (const QDomElement&);
473
474 // DOM functions
475 QString attribute(const QString& name, const QString& defValue = QString() ) const;
476 void setAttribute(const QString& name, const QString& value);
477 void setAttribute(const QString& name, qlonglong value);
478 void setAttribute(const QString& name, qulonglong value);
479 inline void setAttribute(const QString& name, int value)
480 { setAttribute(name, value: qlonglong(value)); }
481 inline void setAttribute(const QString& name, uint value)
482 { setAttribute(name, value: qulonglong(value)); }
483 void setAttribute(const QString& name, float value);
484 void setAttribute(const QString& name, double value);
485 void removeAttribute(const QString& name);
486 QDomAttr attributeNode(const QString& name);
487 QDomAttr setAttributeNode(const QDomAttr& newAttr);
488 QDomAttr removeAttributeNode(const QDomAttr& oldAttr);
489 QDomNodeList elementsByTagName(const QString& tagname) const;
490 bool hasAttribute(const QString& name) const;
491
492 QString attributeNS(const QString& nsURI, const QString& localName, const QString& defValue = QString()) const;
493 void setAttributeNS(const QString& nsURI, const QString& qName, const QString& value);
494 inline void setAttributeNS(const QString& nsURI, const QString& qName, int value)
495 { setAttributeNS(nsURI, qName, value: qlonglong(value)); }
496 inline void setAttributeNS(const QString& nsURI, const QString& qName, uint value)
497 { setAttributeNS(nsURI, qName, value: qulonglong(value)); }
498 void setAttributeNS(const QString& nsURI, const QString& qName, qlonglong value);
499 void setAttributeNS(const QString& nsURI, const QString& qName, qulonglong value);
500 void setAttributeNS(const QString& nsURI, const QString& qName, double value);
501 void removeAttributeNS(const QString& nsURI, const QString& localName);
502 QDomAttr attributeNodeNS(const QString& nsURI, const QString& localName);
503 QDomAttr setAttributeNodeNS(const QDomAttr& newAttr);
504 QDomNodeList elementsByTagNameNS(const QString& nsURI, const QString& localName) const;
505 bool hasAttributeNS(const QString& nsURI, const QString& localName) const;
506
507 // DOM read only attributes
508 QString tagName() const;
509 void setTagName(const QString& name); // Qt extension
510
511 // Overridden from QDomNode
512 QDomNamedNodeMap attributes() const;
513 inline QDomNode::NodeType nodeType() const { return ElementNode; }
514
515 QString text() const;
516
517private:
518 QDomElement(QDomElementPrivate*);
519
520 friend class QDomDocument;
521 friend class QDomNode;
522 friend class QDomAttr;
523};
524
525class Q_XML_EXPORT QDomText : public QDomCharacterData
526{
527public:
528 QDomText();
529 QDomText(const QDomText& x);
530 QDomText& operator= (const QDomText&);
531
532 // DOM functions
533 QDomText splitText(int offset);
534
535 // Overridden from QDomCharacterData
536 inline QDomNode::NodeType nodeType() const { return TextNode; }
537
538private:
539 QDomText(QDomTextPrivate*);
540
541 friend class QDomCDATASection;
542 friend class QDomDocument;
543 friend class QDomNode;
544};
545
546class Q_XML_EXPORT QDomComment : public QDomCharacterData
547{
548public:
549 QDomComment();
550 QDomComment(const QDomComment& x);
551 QDomComment& operator= (const QDomComment&);
552
553 // Overridden from QDomCharacterData
554 inline QDomNode::NodeType nodeType() const { return CommentNode; }
555
556private:
557 QDomComment(QDomCommentPrivate*);
558
559 friend class QDomDocument;
560 friend class QDomNode;
561};
562
563class Q_XML_EXPORT QDomCDATASection : public QDomText
564{
565public:
566 QDomCDATASection();
567 QDomCDATASection(const QDomCDATASection& x);
568 QDomCDATASection& operator= (const QDomCDATASection&);
569
570 // Overridden from QDomText
571 inline QDomNode::NodeType nodeType() const { return CDATASectionNode; }
572
573private:
574 QDomCDATASection(QDomCDATASectionPrivate*);
575
576 friend class QDomDocument;
577 friend class QDomNode;
578};
579
580class Q_XML_EXPORT QDomNotation : public QDomNode
581{
582public:
583 QDomNotation();
584 QDomNotation(const QDomNotation& x);
585 QDomNotation& operator= (const QDomNotation&);
586
587 // DOM read only attributes
588 QString publicId() const;
589 QString systemId() const;
590
591 // Overridden from QDomNode
592 inline QDomNode::NodeType nodeType() const { return NotationNode; }
593
594private:
595 QDomNotation(QDomNotationPrivate*);
596
597 friend class QDomDocument;
598 friend class QDomNode;
599};
600
601class Q_XML_EXPORT QDomEntity : public QDomNode
602{
603public:
604 QDomEntity();
605 QDomEntity(const QDomEntity& x);
606 QDomEntity& operator= (const QDomEntity&);
607
608 // DOM read only attributes
609 QString publicId() const;
610 QString systemId() const;
611 QString notationName() const;
612
613 // Overridden from QDomNode
614 inline QDomNode::NodeType nodeType() const { return EntityNode; }
615
616private:
617 QDomEntity(QDomEntityPrivate*);
618
619 friend class QDomNode;
620};
621
622class Q_XML_EXPORT QDomEntityReference : public QDomNode
623{
624public:
625 QDomEntityReference();
626 QDomEntityReference(const QDomEntityReference& x);
627 QDomEntityReference& operator= (const QDomEntityReference&);
628
629 // Overridden from QDomNode
630 inline QDomNode::NodeType nodeType() const { return EntityReferenceNode; }
631
632private:
633 QDomEntityReference(QDomEntityReferencePrivate*);
634
635 friend class QDomDocument;
636 friend class QDomNode;
637};
638
639class Q_XML_EXPORT QDomProcessingInstruction : public QDomNode
640{
641public:
642 QDomProcessingInstruction();
643 QDomProcessingInstruction(const QDomProcessingInstruction& x);
644 QDomProcessingInstruction& operator= (const QDomProcessingInstruction&);
645
646 // DOM read only attributes
647 QString target() const;
648
649 // DOM attributes
650 QString data() const;
651 void setData(const QString& d);
652
653 // Overridden from QDomNode
654 inline QDomNode::NodeType nodeType() const { return ProcessingInstructionNode; }
655
656private:
657 QDomProcessingInstruction(QDomProcessingInstructionPrivate*);
658
659 friend class QDomDocument;
660 friend class QDomNode;
661};
662
663
664Q_XML_EXPORT QTextStream& operator<<(QTextStream&, const QDomNode&);
665
666#endif // QT_NO_DOM
667
668QT_END_NAMESPACE
669
670#endif // QDOM_H
671

source code of qtbase/src/xml/dom/qdom.h