|
3 | 3 | // Portions based on .NET runtime API (MIT License, Copyright .NET Foundation and Contributors) |
4 | 4 | #pragma once |
5 | 5 | #include <memory> |
| 6 | +#include <optional> |
6 | 7 | #include <string> |
7 | 8 | #include <vector> |
8 | 9 | #include <utility> |
9 | 10 |
|
| 11 | +#include "SharpRuntime/SharpRuntimeHelper.hpp" |
10 | 12 | #include "System/Xml/ReadState.hpp" |
11 | 13 | #include "System/Xml/XmlNodeType.hpp" |
12 | 14 |
|
13 | 15 | namespace System::Xml { |
14 | 16 |
|
15 | 17 | struct XmlReaderState; ///< Opaque tinyxml2 state; defined in XmlReader.cpp. |
| 18 | + class XmlReaderSettings; |
16 | 19 |
|
17 | 20 | /** |
18 | 21 | * @brief Represents a reader that provides fast, non-cached, forward-only access to XML data. |
@@ -43,6 +46,25 @@ namespace System::Xml { |
43 | 46 | * is on no node (including after @c Close()). */ |
44 | 47 | [[nodiscard]] std::string getNameProperty() const; |
45 | 48 |
|
| 49 | + /** @brief Returns the local part of the current node's name — the text after the |
| 50 | + * namespace prefix's colon, or the whole name when it has no prefix. */ |
| 51 | + [[nodiscard]] std::string getLocalNameProperty() const; |
| 52 | + |
| 53 | + /** @brief Returns the namespace prefix of the current node's name, or @c "" when the |
| 54 | + * name has none. */ |
| 55 | + [[nodiscard]] std::string getPrefixProperty() const; |
| 56 | + |
| 57 | + /** @brief Returns the depth of the current node: 0 for the document's top-level nodes, |
| 58 | + * one more for each enclosing element; an attribute is one deeper than its element. */ |
| 59 | + [[nodiscard]] SharpRuntime::intcs getDepthProperty() const; |
| 60 | + |
| 61 | + /** @brief Returns @c true when the current element has at least one attribute. */ |
| 62 | + [[nodiscard]] bool getHasAttributesProperty() const; |
| 63 | + |
| 64 | + /** @brief Returns the number of attributes on the current element, or 0 on any other |
| 65 | + * node. */ |
| 66 | + [[nodiscard]] SharpRuntime::intcs getAttributeCountProperty() const; |
| 67 | + |
46 | 68 | /** @brief Returns the text value of the current node (Text/CDATA/Comment), or @c "" |
47 | 69 | * when the reader is on no node (including after @c Close()). */ |
48 | 70 | [[nodiscard]] std::string getValueProperty() const; |
@@ -73,6 +95,67 @@ namespace System::Xml { |
73 | 95 | */ |
74 | 96 | bool MoveToElement(); |
75 | 97 |
|
| 98 | + /** |
| 99 | + * @brief Moves the cursor to the first attribute of the current element. |
| 100 | + * |
| 101 | + * @return @c true if the element has an attribute; @c false on any other node. |
| 102 | + */ |
| 103 | + bool MoveToFirstAttribute(); |
| 104 | + |
| 105 | + /** |
| 106 | + * @brief Skips comments, processing instructions, the XML declaration, document type |
| 107 | + * nodes and whitespace until the reader is on a content node (element, |
| 108 | + * end element, text or CDATA) or at end of file; an attribute cursor is moved |
| 109 | + * back to its element first. |
| 110 | + * |
| 111 | + * @return The node type the reader stopped on; @c XmlNodeType::None at end of file. |
| 112 | + */ |
| 113 | + XmlNodeType MoveToContent(); |
| 114 | + |
| 115 | + /** |
| 116 | + * @brief Calls @c MoveToContent() and tells whether it stopped on a start element. |
| 117 | + * |
| 118 | + * @return @c true when the current content node is an element. |
| 119 | + */ |
| 120 | + bool IsStartElement(); |
| 121 | + |
| 122 | + /** |
| 123 | + * @brief Calls @c MoveToContent() and tells whether it stopped on a start element |
| 124 | + * with the given qualified name. |
| 125 | + * |
| 126 | + * @param name The qualified name to match. |
| 127 | + * @return @c true when the current content node is an element named @p name. |
| 128 | + */ |
| 129 | + bool IsStartElement(const std::string& name); |
| 130 | + |
| 131 | + /** |
| 132 | + * @brief Skips the current node and, for a non-empty element, all of its children, |
| 133 | + * leaving the reader on the node that follows; on an attribute the element |
| 134 | + * is skipped. Does nothing when the reader is on no node. |
| 135 | + */ |
| 136 | + void Skip(); |
| 137 | + |
| 138 | + /** |
| 139 | + * @brief Resolves a namespace prefix in the scope of the current node, exactly as the |
| 140 | + * @c xmlns declarations on it and its ancestors define it. |
| 141 | + * |
| 142 | + * @param prefix The prefix to resolve; @c "" asks for the default namespace. |
| 143 | + * @return The namespace URI, or @c std::nullopt when the prefix is not declared in |
| 144 | + * scope. The @c xml and @c xmlns prefixes resolve to their fixed URIs. |
| 145 | + */ |
| 146 | + [[nodiscard]] std::optional<std::string> LookupNamespace(const std::string& prefix) const; |
| 147 | + |
| 148 | + /** @brief Always @c true: the parser records the line every node starts on. These three |
| 149 | + * members are the @c IXmlLineInfo contract, offered directly because this reader keeps |
| 150 | + * no vtable (the class is pinned to a single owning pointer). */ |
| 151 | + [[nodiscard]] bool HasLineInfo() const; |
| 152 | + |
| 153 | + /** @brief Returns the 1-based line the current node starts on, or 0 on no node. */ |
| 154 | + [[nodiscard]] SharpRuntime::intcs getLineNumberProperty() const; |
| 155 | + |
| 156 | + /** @brief Returns 0: the parser does not record the column a node starts in. */ |
| 157 | + [[nodiscard]] SharpRuntime::intcs getLinePositionProperty() const; |
| 158 | + |
76 | 159 | /** |
77 | 160 | * @brief Moves to the next attribute of the current element. |
78 | 161 | * |
@@ -105,6 +188,16 @@ namespace System::Xml { |
105 | 188 | */ |
106 | 189 | void ReadStartElement(); |
107 | 190 |
|
| 191 | + /** |
| 192 | + * @brief Checks, after @c MoveToContent(), that the current node is a start element |
| 193 | + * with the given qualified name and advances past it. |
| 194 | + * |
| 195 | + * @param name The qualified name the element must have. |
| 196 | + * @throws XmlException when the current content node is not that element, with the |
| 197 | + * message @c "Element 'name' was not found. Line L, position P." |
| 198 | + */ |
| 199 | + void ReadStartElement(const std::string& name); |
| 200 | + |
108 | 201 | /** |
109 | 202 | * @brief Verifies that the current node is an end-element and advances the reader. |
110 | 203 | * |
@@ -138,6 +231,20 @@ namespace System::Xml { |
138 | 231 | */ |
139 | 232 | static XmlReader* Create(const std::string& inputUri); |
140 | 233 |
|
| 234 | + /** |
| 235 | + * @brief Creates an XmlReader as the one-argument overload does, applying @p settings: |
| 236 | + * @c DtdProcessing::Prohibit rejects a document that carries a DOCTYPE, |
| 237 | + * @c DtdProcessing::Ignore drops the node, and @c IgnoreComments, |
| 238 | + * @c IgnoreProcessingInstructions and @c IgnoreWhitespace drop those nodes. |
| 239 | + * |
| 240 | + * @param inputUri File path or raw XML text. |
| 241 | + * @param settings The reader settings to apply. |
| 242 | + * @return Heap-allocated XmlReader; caller owns the pointer. |
| 243 | + * @throws XmlException on parse error, or on a DOCTYPE when DTD processing is |
| 244 | + * prohibited ("For security reasons DTD is prohibited in this XML document. …"). |
| 245 | + */ |
| 246 | + static XmlReader* Create(const std::string& inputUri, const XmlReaderSettings& settings); |
| 247 | + |
141 | 248 | /** |
142 | 249 | * @brief Creates an XmlReader that parses @p xmlContent as raw XML. |
143 | 250 | * |
|
0 commit comments