summaryrefslogtreecommitdiff
path: root/modules/CIAO/tools/XML/XML_Helper.tpp
blob: a21ca5db698fcf19312fb8df56935fe89b65ec63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//$Id$
#include "XML_Helper.h"
#include "ace/Auto_Ptr.h"
#include "ace/Log_Msg.h"
//#include "ciao/Logger/Log_Macros.h"
#include "xercesc/util/XMLUniDefs.hpp"
#include "xercesc/dom/DOM.hpp"
#include "XML_Error_Handler.h"
#include "XML_Schema_Resolver.h"
#include "xercesc/framework/LocalFileFormatTarget.hpp"
#include "xercesc/dom/DOM.hpp"
#include "XercesString.h"

#if XERCES_VERSION_MAJOR == 3
#include "xercesc/dom/DOMLSSerializer.hpp"
#endif

namespace CIAO
{
  namespace XML
  {
    using xercesc::XMLException;
    using xercesc::XMLString;
    using xercesc::DOMImplementation;
    using xercesc::DOMImplementationRegistry;
//    using xercesc::DOMBuilder;
    using xercesc::DOMImplementationLS;
    using xercesc::XMLUni;
    using xercesc::DOMDocument;
    using xercesc::DOMException;
    using xercesc::DOMDocumentType;
    using xercesc::XercesDOMParser;
/*
    template <typename Resolver, typename Error>
    XML_Helper<Resolver, Error>::XML_Helper (void)
      : initialized_ (false)
    {
      this->init_parser ();
    }
*/    
    // TODO this is stub implementation
    template <typename Resolver, typename Error>
    XML_Helper<Resolver, Error>::XML_Helper (Resolver &resolver, Error &eh)
      : initialized_ (false),
        resolver_ (resolver),
        e_handler_ (eh)
    {
      this->init_parser ();
    }

    template <typename Resolver, typename Error>
    XML_Helper<Resolver, Error>::~XML_Helper (void)
    {
      this->terminate_parser ();
    }
    
    template <typename Resolver, typename Error>
    bool
    XML_Helper<Resolver, Error>::is_initialized (void) const
    {
      return this->initialized_ == true;
    }
    
    template <typename Resolver, typename Error>
    void
    XML_Helper<Resolver, Error>::init_parser (void)
    {
      if (this->initialized_)
        return;

      //      CIAO_DEBUG ((LM_TRACE, CLINFO
      //"XML_Helper<>::is_initialized - Initializing the Xerces runtime \n"));
      // Initialize the Xerces run-time
      try
        {
          xercesc::XMLPlatformUtils::Initialize();
        }
      catch (const XMLException& e)
        {
          char* message =
            XMLString::transcode (e.getMessage());
          ACE_Auto_Basic_Array_Ptr<char> cleanup_message (message);

          throw;
        }
      catch (...)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) Some other exception,"
                      " returning\n"));

          return;
        }

      // Instantiate the DOM parser.
      static const XMLCh gLS[] = { xercesc::chLatin_L,
                                   xercesc::chLatin_S,
                                   xercesc::chNull };

      // Get an implementation of the Load-Store (LS) interface
      // and cache it for later use
      impl_ =
        DOMImplementationRegistry::getDOMImplementation(gLS);

      this->initialized_ = true;
      return;
    }
    
    template <typename Resolver, typename Error>
    XERCES_CPP_NAMESPACE::DOMDocument *
    XML_Helper<Resolver, Error>::create_dom (const ACE_TCHAR *root,
                                             const ACE_TCHAR *ns,
                                             DOMDocumentType *doctype) const
    {
      if (root == 0 || ns == 0)
        return 0;

      return this->impl_->createDocument (XStr (ns),
                                          XStr (root),
                                          doctype);
    }
    
    template <typename Resolver, typename Error>
    XERCES_CPP_NAMESPACE::DOMDocumentType *
    XML_Helper<Resolver, Error>::create_doctype (const ACE_TCHAR *qn,
                                                 const ACE_TCHAR *pid,
                                                 const ACE_TCHAR *sid) const
    {
      return this->impl_->createDocumentType (XStr (qn),
                                              XStr (pid),
                                              XStr (sid));
    }
    template <typename Resolver, typename Error>
    XERCES_CPP_NAMESPACE::DOMDocument *
    XML_Helper<Resolver, Error>::create_dom (const ACE_TCHAR *url) const
    {

      if (url == 0)
        return 0;

      try
        {
          if (this->parser_.get () == 0)
            this->parser_.reset ((new xercesc::XercesDOMParser ()));
          
          // Perform Namespace processing.
          this->parser_->setDoNamespaces (true);
          
          // Discard comment nodes in the document
          this->parser_->setCreateCommentNodes (false);

          // Disable datatype normalization. The XML 1.0 attribute value
          // normalization always occurs though.
          // this->parser_->setFeature (XMLUni::fgDOMDatatypeNormalization, true);

          // Do not create EntityReference nodes in the DOM tree. No
          // EntityReference nodes will be created, only the nodes
          // corresponding to their fully expanded sustitution text will be
          // created.
          this->parser_->setCreateEntityReferenceNodes (false);
          
          // Perform Validation
          this->parser_->setValidationScheme (xercesc::AbstractDOMParser::Val_Always);

          // Do not include ignorable whitespace in the DOM tree.
          this->parser_->setIncludeIgnorableWhitespace (false);

          // Enable the parser's schema support.
          this->parser_->setDoSchema (true);

          // Enable full schema constraint checking, including checking which
          // may be time-consuming or memory intensive. Currently, particle
          // unique attribution constraint checking and particle derivation
          // restriction checking are controlled by this option.
          this->parser_->setValidationSchemaFullChecking (true);

          // The parser will treat validation error as fatal and will exit.
          this->parser_->setValidationConstraintFatal (true);

          this->parser_->setErrorHandler (&e_handler_);

          this->parser_->setEntityResolver (&resolver_);

          this->parser_->parse (ACE_TEXT_ALWAYS_CHAR (url));
          
          if (e_handler_.getErrors ())
            return 0;
          
          return this->parser_->getDocument ();
        }
      catch (const DOMException& e)
        {
          const unsigned int maxChars = 2047;
          XMLCh errText[maxChars + 1];

          if (DOMImplementation::loadDOMExceptionMsg (e.code,
                                                      errText,
                                                      maxChars))
            {
              char* message =
                XMLString::transcode (errText);
              ACE_Auto_Basic_Array_Ptr<char> cleanup_message (message);
            }
          return 0;

        }
      catch (const XMLException& e)
        {
          char* message = XMLString::transcode (e.getMessage());
          ACE_Auto_Basic_Array_Ptr<char> cleanup_message (message);
          throw 0;
        }
      catch (...)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "(%P|%t) Caught an unknown exception \n"));
          throw;
        }

      return 0;
    }

    template <typename Resolver, typename Error>
    void
    XML_Helper<Resolver, Error>::terminate_parser (void)
    {
      try
        {
          xercesc::XMLPlatformUtils::Terminate();
        }
      catch (const XMLException& e)
        {
          char* message =
            XMLString::transcode (e.getMessage());
          ACE_Auto_Basic_Array_Ptr<char> cleanup_message (message);
          throw;
        }

      this->initialized_ = false;
      return;
    }
    
    template <typename Resolver, typename Error>
    bool 
    XML_Helper<Resolver, Error>::write_DOM (XERCES_CPP_NAMESPACE::DOMDocument *doc,
                                            const ACE_TCHAR *file) const
    {
      try
        {
          bool retn;
#if XERCES_VERSION_MAJOR == 3
          XERCES_CPP_NAMESPACE::DOMLSSerializer *serializer (impl_->createLSSerializer ());
          XERCES_CPP_NAMESPACE::DOMConfiguration *ser_config (serializer->getDomConfig ());
          XERCES_CPP_NAMESPACE::DOMLSOutput *output (impl_->createLSOutput ());
          
          if (ser_config->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true))
            ser_config->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, true);
          
          XMLFormatTarget *format_target = new XERCES_CPP_NAMESPACE::LocalFileFormatTarget (ACE_TEXT_ALWAYS_CHAR (file));
          
          output->setByteStream (format_target);
          
          retn = serializer->write (doc, output);
          
          output->release ();
          serializer->release ();
          return retn;
#else          
          std::auto_ptr <XERCES_CPP_NAMESPACE::DOMWriter> writer (impl_->createDOMWriter());
          
          if (writer->canSetFeature (XMLUni::fgDOMWRTFormatPrettyPrint,
                                     true))
            writer->setFeature (XMLUni::fgDOMWRTFormatPrettyPrint, true);
          
          std::auto_ptr <xercesc::XMLFormatTarget> ft (new xercesc::LocalFileFormatTarget(file));
          retn = writer->writeNode(ft.get (), *doc);
          
          return retn;
#endif
        }
      catch (const xercesc::XMLException &e)
        {
          char* message =
            XMLString::transcode (e.getMessage());
          ACE_Auto_Basic_Array_Ptr<char> cleanup_message (message);

          char* name =
            XMLString::transcode (e.getType());
          ACE_Auto_Basic_Array_Ptr<char> cleanup_name (name);
          
          ACE_ERROR ((LM_ERROR, "Caught exception while serializing DOM to file.\n"
                      "Name: %s\n"
                      "Message: %s\n"
                      "SrcFile: %s\n"
                      "SrcLine: %d\n",
                      name,
                      message,
                      e.getSrcFile (),
                      e.getSrcLine ()));
          return false;
        }
    }
  }
}