libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
XML_Stuff impl.hpp
Go to the documentation of this file.
1 // TITLE:
2 //
3 // AUTHOR:
4 // Created by J.M.McGuiness, E-mail: coder@hussar.me.uk
5 //
6 // DESCRIPTION:
7 // Note: You need to put the line "#import <msxml4.dll> implementation_only" in the "stdafx.cpp" or you'll get link errors with the MSXML stuff....
8 // (I need to do this as the more simple route of omitting the "no_implementation" in this file leads to those link errors anyway, if the "#import <msxml4.dll>" is put in "stdafx.h".
9 //
10 // LEGALITIES:
11 // Copyright © 2004 by J.M.McGuiness, all rights reserved.
12 //
13 // This library is free software; you can redistribute it and/or
14 // modify it under the terms of the GNU Lesser General Public
15 // License as published by the Free Software Foundation; either
16 // version 2.1 of the License, or (at your option) any later version.
17 //
18 // This library is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 // Lesser General Public License for more details.
22 //
23 // You should have received a copy of the GNU Lesser General Public
24 // License along with this library; if not, write to the Free Software
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 //
27 /////////////////////////////////////////////////////////////////////////////
28 
29 namespace jmmcg { namespace LIBJMMCG_VER_NAMESPACE { namespace NTUtils { namespace XML {
30 
31  inline MSXML2::IXMLDOMNodePtr __fastcall
33  static const tchar * const node_types_names[]={
34  _T("attribute"),
35  _T("document"),
36  _T("element"),
37  _T("text")
38  };
39  return doc->createNode(node_types_names[type],node_name,xml_ns);
40  }
41 
42  inline MSXML2::IXMLDOMNodePtr __fastcall
44  return node->appendChild(CreateNode(type,node_name,doc,xml_ns));
45  }
46 
47  inline MSXML2::IXMLDOMNodePtr __fastcall
49  const MSXML2::IXMLDOMNodePtr comment_node(CreateNode(element,node_name,doc,xml_ns,node));
50  const MSXML2::IXMLDOMTextPtr text(CreateNode(text,node_name,doc,xml_ns,comment_node));
51  text->nodeValue=comment;
52  return text;
53  }
54 
55  inline tstring __fastcall
57  return tstring(static_cast<const tchar *>(doc->parseError->reason));
58  }
59 
60  /////////////////////////////////////////////////////////////////////////////
61 
62  inline void __fastcall
63  ToXML(const MSXML2::IXMLDOMDocumentPtr &doc,const MSXML2::IXMLDOMNodePtr &n,const POINT &pt,const tchar *xml_ns) {
64  try {
65  const MSXML2::IXMLDOMElementPtr node(CreateNode(element,_T("point"),doc,xml_ns,n));
66  node->setAttribute(_T("x"),pt.x);
67  node->setAttribute(_T("y"),pt.y);
68  } catch (const _com_error &err) {
69  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMDocumentPtr &"),tostring(doc)));
70  info.add_arg(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n));
71  info.add_arg(_T("const POINT *"),tostring(&pt));
72  info.add_arg(_T("const tchar *"),xml_ns);
73  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
74  }
75  }
76 
77  inline void __fastcall
78  FromXML(const MSXML2::IXMLDOMNodePtr &n,POINT &pt) {
79  try {
80  const MSXML2::IXMLDOMNamedNodeMapPtr attribs(n->selectSingleNode(_T("point"))->attributes);
81  fromstring(static_cast<const tchar *>(attribs->getNamedItem(_T("x"))->text),pt.x);
82  fromstring(static_cast<const tchar *>(attribs->getNamedItem(_T("y"))->text),pt.y);
83  } catch (const _com_error &err) {
84  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n)));
85  info.add_arg(_T("const POINT *"),tostring(&pt));
86  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
87  }
88  }
89 
90  inline void __fastcall
91  ToXML(const MSXML2::IXMLDOMDocumentPtr &doc,const MSXML2::IXMLDOMNodePtr &n,const RECT &rect,const tchar *xml_ns) {
92  try {
93  const MSXML2::IXMLDOMElementPtr node(CreateNode(element,_T("rectangle"),doc,xml_ns,n));
94  node->setAttribute(_T("top"),rect.top);
95  node->setAttribute(_T("left"),rect.left);
96  node->setAttribute(_T("bottom"),rect.bottom);
97  node->setAttribute(_T("right"),rect.right);
98  } catch (const _com_error &err) {
99  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMDocumentPtr &"),tostring(doc)));
100  info.add_arg(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n));
101  info.add_arg(_T("const RECT *"),tostring(&rect));
102  info.add_arg(_T("const tchar *"),xml_ns);
103  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
104  }
105  }
106 
107  inline void __fastcall
108  FromXML(const MSXML2::IXMLDOMNodePtr &n,RECT &rect) {
109  try {
110  const MSXML2::IXMLDOMNamedNodeMapPtr attribs(n->selectSingleNode(_T("rectangle"))->attributes);
111  fromstring(static_cast<const tchar *>(attribs->getNamedItem(_T("top"))->text),rect.top);
112  fromstring(static_cast<const tchar *>(attribs->getNamedItem(_T("left"))->text),rect.left);
113  fromstring(static_cast<const tchar *>(attribs->getNamedItem(_T("bottom"))->text),rect.bottom);
114  fromstring(static_cast<const tchar *>(attribs->getNamedItem(_T("right"))->text),rect.right);
115  } catch (const _com_error &err) {
116  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n)));
117  info.add_arg(_T("const RECT *"),tostring(&rect));
118  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
119  }
120  }
121 
122  template<typename Value> inline void __fastcall
123  ToXML(const MSXML2::IXMLDOMDocumentPtr &doc,const MSXML2::IXMLDOMNodePtr &n,const tchar *colln_name,const std::vector<Value> &colln,const tchar *xml_ns) {
124  try {
125  const MSXML2::IXMLDOMNodePtr colln_node(CreateNode(element,colln_name,doc,xml_ns,n));
126  for (std::vector<Value>::const_iterator i(colln.begin());i!=colln.end();++i) {
127  const MSXML2::IXMLDOMNodePtr node(CreateNode(element,_T("node"),doc,xml_ns,colln_node));
128  i->ToXML(doc,node);
129  }
130  } catch (const _com_error &err) {
131  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMDocumentPtr &"),tostring(doc)));
132  info.add_arg(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n));
133  info.add_arg(_T("const tchar *"),colln_name);
134  info.add_arg(_T("const std::vector<Value> *"),tostring(&colln));
135  info.add_arg(_T("const tchar *"),xml_ns);
136  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
137  }
138  }
139 
140  template<> inline void __fastcall
141  ToXML(const MSXML2::IXMLDOMDocumentPtr &doc,const MSXML2::IXMLDOMNodePtr &n,const tchar *colln_name,const std::vector<double> &colln,const tchar *xml_ns) {
142  try {
143  const MSXML2::IXMLDOMNodePtr colln_node(CreateNode(element,colln_name,doc,xml_ns,n));
144  for (std::vector<double>::const_iterator i(colln.begin());i!=colln.end();++i) {
145  const MSXML2::IXMLDOMElementPtr node(CreateNode(element,_T("node"),doc,xml_ns,colln_node));
146  node->setAttribute(_T("value"),*i);
147  }
148  } catch (const _com_error &err) {
149  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMDocumentPtr &"),tostring(doc)));
150  info.add_arg(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n));
151  info.add_arg(_T("const tchar *"),colln_name);
152  info.add_arg(_T("const std::vector<double> *"),tostring(&colln));
153  info.add_arg(_T("const tchar *"),xml_ns);
154  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
155  }
156  }
157 
158  template<> inline void __fastcall
159  ToXML(const MSXML2::IXMLDOMDocumentPtr &doc,const MSXML2::IXMLDOMNodePtr &n,const tchar *colln_name,const std::vector<unsigned long> &colln,const tchar *xml_ns) {
160  try {
161  const MSXML2::IXMLDOMNodePtr colln_node(CreateNode(element,colln_name,doc,xml_ns,n));
162  for (std::vector<unsigned long>::const_iterator i(colln.begin());i!=colln.end();++i) {
163  const MSXML2::IXMLDOMElementPtr node(CreateNode(element,_T("node"),doc,xml_ns,colln_node));
164  node->setAttribute(_T("value"),static_cast<long>(*i));
165  }
166  } catch (const _com_error &err) {
167  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMDocumentPtr &"),tostring(doc)));
168  info.add_arg(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n));
169  info.add_arg(_T("const tchar *"),colln_name);
170  info.add_arg(_T("const std::vector<unsigned long> *"),tostring(&colln));
171  info.add_arg(_T("const tchar *"),xml_ns);
172  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
173  }
174  }
175 
176  template<> inline void __fastcall
177  ToXML(const MSXML2::IXMLDOMDocumentPtr &doc,const MSXML2::IXMLDOMNodePtr &n,const tchar *colln_name,const std::vector<CPoint> &colln,const tchar *xml_ns) {
178  try {
179  const MSXML2::IXMLDOMNodePtr colln_node(CreateNode(element,colln_name,doc,xml_ns,n));
180  for (std::vector<CPoint>::const_iterator i(colln.begin());i!=colln.end();++i) {
181  const MSXML2::IXMLDOMNodePtr node(CreateNode(element,_T("node"),doc,xml_ns,colln_node));
182  ToXML(doc,node,*i,xml_ns);
183  }
184  } catch (const _com_error &err) {
185  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMDocumentPtr &"),tostring(doc)));
186  info.add_arg(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n));
187  info.add_arg(_T("const tchar *"),colln_name);
188  info.add_arg(_T("const std::vector<CPoint> *"),tostring(&colln));
189  info.add_arg(_T("const tchar *"),xml_ns);
190  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
191  }
192  }
193 
194  template<typename Value> inline void __fastcall
195  FromXML(const MSXML2::IXMLDOMNodePtr &n,const tstring &colln_name,std::vector<Value> &colln) {
196  try {
197  assert(colln.empty());
198  const MSXML2::IXMLDOMNodeListPtr nodes(n->selectNodes((colln_name+_T("/*")).c_str()));
199  for (long i=0;i<nodes->length;++i) {
200  colln.push_back(Value::FromXML(nodes->item[i]));
201  }
202  } catch (const _com_error &err) {
203  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n)));
204  info.add_arg(_T("const tchar *"),colln_name);
205  info.add_arg(_T("const std::vector<Value> *"),tostring(&colln));
206  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
207  }
208  }
209 
210  template<> inline void __fastcall
211  FromXML(const MSXML2::IXMLDOMNodePtr &n,const tstring &colln_name,std::vector<double> &colln) {
212  try {
213  assert(colln.empty());
214  const MSXML2::IXMLDOMNodeListPtr nodes(n->selectNodes((colln_name+_T("/node/@value")).c_str()));
215  for (long i=0;i<nodes->length;++i) {
216  double tmp;
217  fromstring(static_cast<const tchar *>(nodes->item[i]->text),tmp);
218  colln.push_back(tmp);
219  }
220  } catch (const _com_error &err) {
221  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n)));
222  info.add_arg(_T("const tchar *"),colln_name);
223  info.add_arg(_T("const std::vector<double> *"),tostring(&colln));
224  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
225  }
226  }
227 
228  template<> inline void __fastcall
229  FromXML(const MSXML2::IXMLDOMNodePtr &n,const tstring &colln_name,std::vector<unsigned long> &colln) {
230  try {
231  assert(colln.empty());
232  const MSXML2::IXMLDOMNodeListPtr nodes(n->selectNodes((colln_name+_T("/node/@value")).c_str()));
233  for (long i=0;i<nodes->length;++i) {
234  unsigned long tmp;
235  fromstring(static_cast<const tchar *>(nodes->item[i]->text),tmp);
236  colln.push_back(tmp);
237  }
238  } catch (const _com_error &err) {
239  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n)));
240  info.add_arg(_T("const tchar *"),colln_name);
241  info.add_arg(_T("const std::vector<unsigned long> *"),tostring(&colln));
242  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
243  }
244  }
245 
246  template<> inline void __fastcall
247  FromXML(const MSXML2::IXMLDOMNodePtr &n,const tstring &colln_name,std::vector<CPoint> &colln) {
248  try {
249  assert(colln.empty());
250  const MSXML2::IXMLDOMNodeListPtr nodes(n->selectNodes((colln_name+_T("/*")).c_str()));
251  for (long i=0;i<nodes->length;++i) {
252  CPoint val;
253  FromXML(nodes->item[i],val);
254  colln.push_back(val);
255  }
256  } catch (const _com_error &err) {
257  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(void),info::function::argument(_T("const MSXML2::IXMLDOMNodePtr &"),tostring(n)));
258  info.add_arg(_T("const tchar *"),colln_name);
259  info.add_arg(_T("const std::vector<CPoint> *"),tostring(&colln));
260  throw exception(err.ErrorMessage(),info,JMMCG_REVISION_HDR(_T(LIBJMMCG_VERSION_NUMBER)));
261  }
262  }
263 
264  /////////////////////////////////////////////////////////////////////////////
265 
266  inline __stdcall
268  : doc(MakeDoc()) {
269  doc->appendChild(doc->createProcessingInstruction(_T("xml"),_T("version='1.0'")));
270  doc->async=false;
271  }
272 
273  inline __stdcall
275  : doc(d.Doc()) {
276  doc->appendChild(doc->createProcessingInstruction(_T("xml"),_T("version='1.0'")));
277  doc->async=false;
278  }
279 
280  inline __stdcall
282  }
283 
284  inline const MSXML2::IXMLDOMDocumentPtr & __fastcall
285  DocWrapper::Doc(void) const noexcept(true) {
286  return doc;
287  }
288 
289  inline void __fastcall
291  if (doc->loadXML(xml)==VARIANT_FALSE) {
292  throw exception(
294  info::function(__LINE__,__PRETTY_FUNCTION__,typeid(&DocWrapper::LoadXML),info::function::argument(_T("const tchar *"),xml)),
296  );
297  }
298  }
299 
300  inline void __fastcall
302  if (doc->load(filename)==VARIANT_FALSE) {
303  throw exception(
305  info::function(__LINE__,__PRETTY_FUNCTION__,typeid(&DocWrapper::Load),info::function::argument(_T("const tchar *"),filename)),
307  );
308  }
309  }
310 
311  inline void __fastcall
313  IUnknown *itf;
315  if (FAILED(hr) || doc->load(itf)==VARIANT_FALSE) {
316  throw exception(
318  info::function(__LINE__,__PRETTY_FUNCTION__,typeid(&DocWrapper::Load),info::function::argument(_T("const IStreamPtr &"),tostring(file_ptr))),
320  );
321  }
322  }
323 
324  inline void __fastcall
326 // TODO JMG doc->insertBefore(doc->createProcessingInstruction(_T("xml"),_T("version='1.0'")),static_cast<_variant_t>(doc->firstChild));
327  if (doc->save(filename)!=S_OK) {
328  throw exception(
330  info::function(__LINE__,__PRETTY_FUNCTION__,typeid(&DocWrapper::Save),info::function::argument(_T("const tchar *"),filename)),
332  );
333  }
334  }
335 
336  inline MSXML2::IXMLDOMDocumentPtr __fastcall
337  DocWrapper::MakeDoc(void) {
339  }
340 
341 } } } }