Microsoft Information Protection (MIP) SDK for C++: Reference 1.15
Doxygen-generated documentation for MIP SDK written in C++
Loading...
Searching...
No Matches
xml_node.h
Go to the documentation of this file.
1/*
2 *
3 * Copyright (c) Microsoft Corporation.
4 * All rights reserved.
5 *
6 * This code is licensed under the MIT License.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files(the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions :
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 *
26 */
27 /**
28 * @brief Contains XmlNode interface definition used to interact with xml parsers
29 *
30 * @file xml_node.h
31 */
32
33#ifndef API_MIP_XML_NODE_H_
34#define API_MIP_XML_NODE_H_
35
36#include <memory>
37#include <string>
38#include <vector>
39
40#include "mip/mip_namespace.h"
41
42MIP_NAMESPACE_BEGIN
43
44namespace xml {
45
47 std::string prefix;
48 std::string uri;
49};
50
51/**
52 * @brief Constants for the different types of xml elements nodes.
53 */
54enum class XmlNodeType : int {
55 UNKNOWN = -1,
56 NONE = 0,
57 ELEMENT = 1,
58 ATTRIBUTE = 2,
59 TEXT = 3,
60 CDATA = 4,
62 ENTITY = 6,
64 COMMENT = 8,
65 DOCUMENT = 9,
66 DOCUMENT_TYPE = 10,
68 NOTATION = 12,
69 WHITESPACE = 13,
71 END_ELEMENT = 15,
72 END_ENTITY = 16,
74};
75
76/**
77* brief defines abstraction over XML node.
78* @warning XmlNodes will be treated as memory managed by XmlDocument except for
79* RemoveNodeFromDocument() which should handle its own memory clean up
80*/
81/* Example XML for methods
82(1) <bk:book xmlns:bk='testbook' bk:ISBN='1-861001-57-5'>
83(2) <title>
84(3) Pride And Prejudice
85(4) </title>
86(5) <author>Jane Austen</author>
87(6) </bk:book>
88*/
89class XmlNode {
90public:
91 /**
92 * @brief Get the value of an attribute of this node
93 *
94 * @param attributeName The attribute name to search for
95 *
96 * @return The value of that attribute or an empty string if not found
97 *
98 * @example (1)->GetAttributeValue("ISBN") == "1-861001-57-5"
99 */
100 virtual std::string GetAttributeValue(const std::string& attributeName) const = 0;
101 /**
102 * @brief Get all attributes of this node
103 *
104 * @return A vector of pairs representing each attribute name and value
105 *
106 * @example (1)->GetAttributes() == {("ISBN", "1-861001-57-5")}
107 */
108 virtual std::vector<std::pair<std::string, std::string>> GetAttributes() const = 0;
109 /**
110 * @brief Gets the next node on the same level of the heirarchy as the current node
111 *
112 * @return The next XmlNode on the same level as this one or a null XmlNode if there are none
113 *
114 * @example (2)->GetNextNode() == (5)
115 */
116 virtual std::shared_ptr<XmlNode> GetNextNode() const = 0;
117 /**
118 * @brief Get the first child of the node
119 *
120 * @return A shared pointer to the first child of the object node, or a null XmlNode if no children
121 *
122 * @example (2)->GetFirstChild() == (3)
123 */
124 virtual std::shared_ptr<XmlNode> GetFirstChild() const = 0;
125 /**
126 * @brief Get the name of the current node
127 *
128 * @return A string representing the name of the node
129 *
130 * @example (2)->GetName == "title"
131 */
132 virtual std::string GetName() const = 0;
133 /**
134 * @brief Get the content of a text node
135 *
136 * @return A string matching the value of a text node. Empty if not a text node.
137 *
138 * @example (3)->GetContent == "Pride And Prejudice"
139 */
140 virtual std::string GetContent() const = 0;
141 /**
142 * @brief Get the inner text of a element node with a text node as the first child
143 *
144 * @return A string matching the internal text of the child of the node
145 *
146 * @example (2)->GetInnerText == "Pride And Prejudice"
147 */
148 virtual std::string GetInnerText() const = 0;
149 /**
150 * @brief Get the uri and prefix of node
151 *
152 * @return An XmlNamespace obect with the strings of uri and prefix filled to match any namespace
153 * empty strings if no namespace
154 *
155 * @example (1)->GetNamespace == XmlNamespace(uri="testbook", prefix="bk")
156 */
157 virtual XmlNamespace GetNamespace() const = 0;
158 /**
159 * @brief Get the type of node this xml is represented as
160 *
161 * @return A member of the enum describing the node type
162 */
163 virtual XmlNodeType GetNodeType() const = 0;
164 /**
165 * @brief See if the object has been initialized with an underlying xml node
166 *
167 * @return True if underlying node. False if not.
168 */
169 virtual bool IsNull() const = 0;
170
171 /**
172 * @brief Add a property to an existing node
173 *
174 * @param attributeName Name of the attribute to add
175 * @param attributeValue Value of the attribute to add
176 */
177 virtual void AddAttribute(const std::string& attributeName, const std::string& attributeValue) = 0;
178
179 /**
180 * @brief Removes a node property by name
181 *
182 * @param attributeName The property name to remove
183 *
184 * @return 0 if the property was found and successfully removed and -1 otherwise
185 */
186 virtual int RemoveAttribute(const std::string& attributeName) = 0;
187 /**
188 * @brief Add a child to this XmlNode. New child is inserted after any existing children
189 *
190 * @param name The name of the new node
191 *
192 * @return A pointer to the node that was added or a nullptr if the operation failed.
193 */
194 virtual std::shared_ptr<XmlNode> AddNewChild(const std::string& name) = 0;
195 /**
196 * @brief Add a child to this XmlNode. New child is inserted after any existing children
197 *
198 * @param name The name of the new node
199 * @param namespaceName The namespace prefix to insert the new node under
200 *
201 * @return A pointer to the node that was added or a nullptr if the operation failed.
202 */
203 virtual std::shared_ptr<XmlNode> AddNewChild(const std::string& name, const std::string& namespaceName) = 0;
204 /**
205 * @brief Add inner text to this xml node
206 *
207 * @param content The string to add as inner text
208 *
209 * @return true if content was added or false if the operation failed.
210 */
211 virtual bool AddContent(const std::string& content) = 0;
212 /**
213 * @brief Remove this node and children from the xml document
214 *
215 * @return true if node was successfully deleted or false if failed.
216 */
217 virtual bool RemoveNodeFromDocument() = 0;
218
219 /** @cond DOXYGEN_HIDE */
220 virtual ~XmlNode() {}
221 /** @endcond */
222};
223
224} // xml
225
226MIP_NAMESPACE_END
227
228#endif // API_MIP_XML_NODE_H_
brief defines abstraction over XML node.
Definition xml_node.h:89
virtual std::shared_ptr< XmlNode > AddNewChild(const std::string &name)=0
Add a child to this XmlNode.
virtual std::string GetContent() const =0
virtual std::shared_ptr< XmlNode > GetFirstChild() const =0
virtual std::string GetAttributeValue(const std::string &attributeName) const =0
virtual std::vector< std::pair< std::string, std::string > > GetAttributes() const =0
virtual int RemoveAttribute(const std::string &attributeName)=0
Removes a node property by name.
virtual void AddAttribute(const std::string &attributeName, const std::string &attributeValue)=0
Add a property to an existing node.
virtual std::string GetName() const =0
virtual std::shared_ptr< XmlNode > GetNextNode() const =0
virtual bool RemoveNodeFromDocument()=0
Remove this node and children from the xml document.
virtual std::shared_ptr< XmlNode > AddNewChild(const std::string &name, const std::string &namespaceName)=0
Add a child to this XmlNode.
virtual XmlNodeType GetNodeType() const =0
Get the type of node this xml is represented as.
virtual std::string GetInnerText() const =0
virtual bool IsNull() const =0
See if the object has been initialized with an underlying xml node.
virtual XmlNamespace GetNamespace() const =0
virtual bool AddContent(const std::string &content)=0
Add inner text to this xml node.
MIP namespace macros.
XmlNodeType
Constants for the different types of xml elements nodes.
Definition xml_node.h:54
std::string prefix
Definition xml_node.h:47
std::string uri
Definition xml_node.h:48