Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: base/xml_utils.h

Issue 624713003: Keep only base/extractor.[cc|h]. (Closed) Base URL: https://chromium.googlesource.com/external/omaha.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/wtl_atlapp_wrapper.h ('k') | base/xml_utils.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2005-2009 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 // ========================================================================
15 //
16 // xml_utils.h
17 //
18 // Utilities for working with XML files via MSXML.
19
20 #ifndef OMAHA_BASE_XML_UTILS_H_
21 #define OMAHA_BASE_XML_UTILS_H_
22
23 #include <windows.h>
24 #include <objbase.h>
25 #include <msxml.h>
26 #include <atlstr.h>
27 #include <utility>
28 #include <vector>
29
30 namespace omaha {
31
32 // Creates a DOMDocument that disallows external definitions to be included and
33 // resolved as part of the XML document stream at parse time.
34 HRESULT CoCreateSafeDOMDocument(IXMLDOMDocument** my_xmldoc);
35
36 // xmlfile can be any specified encoding.
37 HRESULT LoadXMLFromFile(const TCHAR* xmlfile,
38 bool preserve_whitespace,
39 IXMLDOMDocument** xmldoc);
40
41 // xmlstring must be UTF-16 or UCS-2.
42 HRESULT LoadXMLFromMemory(const TCHAR* xmlstring,
43 bool preserve_whitespace,
44 IXMLDOMDocument** xmldoc);
45
46 // xmldata can be any raw data supported by xml parser
47 HRESULT LoadXMLFromRawData(const std::vector<byte>& xmldata,
48 bool preserve_whitespace,
49 IXMLDOMDocument** xmldoc);
50
51 // xmlfile is in encoding specified in the XML document.
52 HRESULT SaveXMLToFile(IXMLDOMDocument* xmldoc, const TCHAR * xmlfile);
53
54 // xmlstring is in UCS-2
55 HRESULT SaveXMLToMemory(IXMLDOMDocument* xmldoc, CString* xmlstring);
56
57 // buffer is in the encoding specified in the XML document.
58 HRESULT SaveXMLToRawData(IXMLDOMDocument* xmldoc, std::vector<byte>* buffer);
59
60 // Canonicalizes the XML string so you can compute a signature on it.
61 // This is not the official canonicalization but a cheaper scheme which
62 // depends on the whitespace stripping capability of MSXML.
63 //
64 // xmlstring is in UTF-16 or UCS-2
65 HRESULT CanonicalizeXML(const TCHAR* xmlstring, CString* canonical_xmlstring);
66
67
68 // Dealing with element/attribute names: the combination of a base name
69 // and a namespace URI is a fully-qualified XML name, or: XMLFQName.
70
71 // We can't just typedef a std::pair because we need proper comparison operators
72 // in case we want to stick a XMLFQName into a standard collection.
73 struct XMLFQName {
74 XMLFQName();
75 XMLFQName(const TCHAR* u, const TCHAR* b);
76 ~XMLFQName();
77
78 CString uri;
79 CString base;
80 };
81
82 bool operator==(const XMLFQName& u, const XMLFQName& v);
83 bool operator!=(const XMLFQName& u, const XMLFQName& v);
84 bool operator< (const XMLFQName& u, const XMLFQName& v);
85 bool operator> (const XMLFQName& u, const XMLFQName& v);
86 bool operator<=(const XMLFQName& u, const XMLFQName& v);
87 bool operator>=(const XMLFQName& u, const XMLFQName& v);
88
89 bool EqualXMLName(const XMLFQName& u, const XMLFQName& v);
90 bool EqualXMLName(IXMLDOMNode* pnode, const XMLFQName& u);
91 bool EqualXMLName(const XMLFQName& u, IXMLDOMNode* pnode);
92
93 // Returns the FQ name from the node.
94 HRESULT GetXMLFQName(IXMLDOMNode* node, XMLFQName* name);
95
96 // Returns a string version of an XMLFQName suitable for debugging use.
97 CString XMLFQNameToString(const XMLFQName& fqname);
98
99 // Returns a string version of a node's name suitable for debugging use.
100 CString NodeToString(IXMLDOMNode* pnode);
101
102 //
103 // Routines for dealing with fragments of DOM trees.
104 //
105 // Creates an XMLDOMNode of the given type with a given name and optional text.
106 HRESULT CreateXMLNode(IXMLDOMDocument* xmldoc,
107 int node_type,
108 const TCHAR* node_name,
109 const TCHAR* namespace_uri,
110 const TCHAR* text,
111 IXMLDOMNode** node_out);
112
113 // Adds newchild as a child node of xmlnode after all existing children.
114 HRESULT AppendXMLNode(IXMLDOMNode* xmlnode, IXMLDOMNode* new_child);
115
116 // Adds text as a child node of xmlnode after all existing children.
117 HRESULT AppendXMLNode(IXMLDOMNode* xmlnode, const TCHAR* text);
118
119 // Adds newchild as an attribute node of xmlnode replacing existing
120 // attribute with same name.
121 HRESULT AddXMLAttributeNode(IXMLDOMNode* xmlnode, IXMLDOMAttribute* new_child);
122
123 // Adds name/value pair as an attribute node of xmlnode replacing
124 // existing attribute with same name.
125 HRESULT AddXMLAttributeNode(IXMLDOMElement* xmlelement,
126 const TCHAR* attribute_name,
127 const TCHAR* attribute_value);
128
129 // Adds name/value pair as an attribute node of xmlnode replacing
130 // existing attribute with same name.
131 // Can add attributes to nodes other than IXMLDOMElement.
132 // Can add attributes with non-null namespaces.
133 HRESULT AddXMLAttributeNode(IXMLDOMNode* xmlnode,
134 const TCHAR* attribute_namespace,
135 const TCHAR* attribute_name,
136 const TCHAR* attribute_value);
137
138 // Removes all children of the given node that have the specified name.
139 HRESULT RemoveXMLChildrenByName(IXMLDOMNode* xmlnode, const XMLFQName& name);
140
141 // Gets a child of a given node by name
142 HRESULT GetXMLChildByName(IXMLDOMElement* xmlnode,
143 const TCHAR* child_name,
144 IXMLDOMNode** xmlchild);
145
146 // Adds newchild as a child node of xmlnode, before the exiting
147 // child item_number.
148 HRESULT InsertXMLBeforeItem(IXMLDOMNode* xmlnode,
149 IXMLDOMNode* new_child,
150 size_t item_number);
151
152 // Gets parse error information after a failed load.
153 HRESULT GetXMLParseError(IXMLDOMDocument* xmldoc,
154 IXMLDOMParseError** parse_error);
155
156 // Interprets parse error.
157 HRESULT InterpretXMLParseError(IXMLDOMParseError* parse_error,
158 HRESULT* error_code,
159 CString* message);
160
161 // Gets the number of children of this node.
162 HRESULT GetNumChildren(IXMLDOMNode* pnode, int* num_children);
163
164 // Gets the number of attributes of this node.
165 int GetNumAttributes(IXMLDOMNode* pnode);
166
167 // Returns true if the specified attribute is in this node.
168 bool HasAttribute(IXMLDOMNode* node, const TCHAR* attr_name);
169
170 // Reads and parses attributes of nodes.
171 HRESULT ReadBooleanAttribute(IXMLDOMNode* node,
172 const TCHAR* attr_name,
173 bool* value);
174 HRESULT ReadIntAttribute(IXMLDOMNode* node,
175 const TCHAR* attr_name,
176 int* value);
177 HRESULT ReadGuidAttribute(IXMLDOMNode* node,
178 const TCHAR* attr_name,
179 GUID* value);
180 HRESULT ReadStringAttribute(IXMLDOMNode* node,
181 const TCHAR* attr_name,
182 CString* value);
183
184 // Reads an attribute as a BSTR, given the node and the name of the attribute.
185 // This is a helper for the other ReadXXXAttribute methods.
186 HRESULT ReadAttribute(IXMLDOMNode* node,
187 const TCHAR* attr_name,
188 BSTR* value);
189
190 // Reads the string value of a node element, either TEXT or CDATA.
191 HRESULT ReadStringValue(IXMLDOMNode* node, CString* value);
192
193 // Maps over a list of XML DOM nodes of some kind, executing a function or
194 // a member function against each node in the list.
195 // Passes a cookie along to each function call useful for accumulating results.
196 // Template class List is usually a IXMLDOMNodeList or a IXMLDOMNamedNodeMap.
197 template <class List, class Cookie>
198 HRESULT ForEachNodeInList(List list,
199 HRESULT (*fun)(IXMLDOMNode*, Cookie),
200 Cookie cookie) {
201 ASSERT1(list); // List assumed to be a pointer type or smart pointer type
202 ASSERT1(fun);
203
204 long len = 0; // NOLINT
205 RET_IF_FAILED(list->get_length(&len));
206 for (long i = 0; i != len; ++i) { // NOLINT
207 CComPtr<IXMLDOMNode> node;
208 RET_IF_FAILED(list->get_item(i, &node));
209 ASSERT1(node);
210 RET_IF_FAILED(fun(node, cookie));
211 }
212 return S_OK;
213 }
214
215 // Same as ForEachNodeInList but it calls a member function of an object.
216 template <class List, class Object, class Cookie>
217 HRESULT ForEachNodeInListObj(List list,
218 Object* object,
219 HRESULT (Object::*mem_fun)(IXMLDOMNode*, Cookie),
220 Cookie cookie) {
221 ASSERT1(list);
222 ASSERT1(object);
223 ASSERT1(fun);
224
225 long len = 0; // NOLINT
226 RET_IF_FAILED(list->get_length(&len));
227 for (long i = 0; i != len; ++i) { // NOLINT
228 CComPtr<IXMLDOMNode> node;
229 RET_IF_FAILED(list->get_item(i, &node));
230 ASSERT1(node);
231 RET_IF_FAILED((object->*fun)(node, cookie));
232 }
233 return S_OK;
234 }
235
236 // Maps over the attributes of a node, executing a function against each
237 // attribute. Passes a cookie along to each function call.
238 template <typename Cookie>
239 HRESULT ForEachAttribute(IXMLDOMNode* node,
240 HRESULT (*fun)(IXMLDOMNode*, Cookie),
241 Cookie cookie) {
242 ASSERT1(node);
243 ASSERT1(fun);
244
245 CComPtr<IXMLDOMNamedNodeMap> attr_list;
246 RET_IF_FAILED(node->get_attributes(&attr_list));
247 ASSERT1(attr_list);
248 RET_IF_FAILED(ForEachNodeInList(attr_list, fun, cookie));
249 return S_OK;
250 }
251
252 // Maps over the children nodes of a node, executing a function against
253 // each child node. Passes a cookie along to each function call.
254 template <typename Cookie>
255 HRESULT ForEachChildNode(IXMLDOMNode* node,
256 HRESULT (*fun)(IXMLDOMNode*, Cookie),
257 Cookie cookie) {
258 ASSERT1(node);
259 ASSERT1(fun);
260
261 CComPtr<IXMLDOMNodeList> child_list;
262 RET_IF_FAILED(node->get_childNodes(&child_list));
263 ASSERT1(child_list);
264 RET_IF_FAILED(ForEachNodeInList(child_list, fun, cookie));
265 return S_OK;
266 }
267
268 // Same as ForEachChildNode but it calls a member function of an object.
269 template <typename Object, typename Cookie>
270 HRESULT ForEachChildNodeObj(IXMLDOMNode* node,
271 Object* object,
272 HRESULT (Object::*mem_fun)(IXMLDOMNode*, Cookie),
273 Cookie cookie) {
274 ASSERT1(node);
275 ASSERT1(object);
276 ASSERT1(fun);
277
278 CComPtr<IXMLDOMNodeList> child_list;
279 RET_IF_FAILED(node->get_childNodes(&child_list));
280 ASSERT1(child_list);
281 RET_IF_FAILED(ForEachNodeInListObj(child_list, object, mem_fun, cookie));
282 return S_OK;
283 }
284
285 } // namespace omaha
286
287 #endif // OMAHA_BASE_XML_UTILS_H_
288
OLDNEW
« no previous file with comments | « base/wtl_atlapp_wrapper.h ('k') | base/xml_utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698