summaryrefslogtreecommitdiff
path: root/TAO/CIAO/DAnCE/tests/Config_Handlers/Handler_Test.i
blob: eb6041b83d085483d03c0ea11d1f3661ede64b7e (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
// $Id$

#include "ace/Get_Opt.h"
#include "ace/Auto_Ptr.h"
#include "ace/Log_Msg.h"
#include "ace/OS_main.h"
#include "ace/Auto_Ptr.h"
#include "tao/Exception.h"
#include "Config_Handlers/XercesString.h"
#include <xercesc/util/XMLUniDefs.hpp>
#include "Config_Handlers/Config_Handler_export.h"
#include "Config_Handlers/DnC_Dump.h"
#include "Config_Handlers/Config_Error_Handler.h"

using Config_Handler::XStr;
using xercesc::XMLUni;
using xercesc::XMLString;
using xercesc::XMLException;
using xercesc::DOMException;
using xercesc::DOMBuilder;
using xercesc::DOMImplementationRegistry;
using xercesc::DOMImplementationLS;
using xercesc::DOMImplementation;
using xercesc::DOMAttr;
using xercesc::DOMNamedNodeMap;
using xercesc::DOMLocator;
using xercesc::DOMError;
using xercesc::DOMNodeList;
using xercesc::DOMDocument;
using xercesc::DOMDocumentTraversal;
using xercesc::DOMNodeIterator;
using xercesc::DOMNode;
using xercesc::DOMNodeFilter;

inline void
usage (const ACE_TCHAR* program)
{
  ACE_ERROR ((LM_ERROR,
              ACE_TEXT ("Usage: %s -u <URI>\n")
              ACE_TEXT (" <URI>: URI identifying the resource\n"),
              program));
}

template <typename HANDLER, typename DATA>
int run_test (int argc, ACE_TCHAR *argv[], void (HANDLER::*func) (DATA&))
{
  ACE_TCHAR* url = 0;

  ACE_Get_Opt get_opt (argc, argv, ACE_TEXT ("u:"));
  int c;

  while ((c = get_opt ()) != EOF)
    {
      switch (c)
        {
          case 'u':
            url = get_opt.opt_arg();
            break;
          default:
            usage(argv[0]);
            return -1;
        }
    }

  if (url == 0) {
    usage(argv[0]);
    return -1;
  }

  // Initialize the ORB so that CORBA::Any will work
  //
  CORBA::ORB_var orb = CORBA::ORB_init (argc, argv, "");

  // 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);
      ACE_DEBUG ((LM_DEBUG, "Error during initialization : %s\n", message));
      return 1;
    }

  try
    {

      // 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
      DOMImplementation* impl
        = DOMImplementationRegistry::getDOMImplementation(gLS);

      auto_ptr<DOMImplementation> cleanup_impl (impl);

      // Create a DOMBuilder
      DOMBuilder* parser =
        ((DOMImplementationLS*)impl)->createDOMBuilder(DOMImplementationLS::MODE_SYNCHRONOUS, 0);

      auto_ptr<DOMBuilder> cleanup_builder (parser);

      // Discard comment nodes in the document
      parser->setFeature (XMLUni::fgDOMComments, false);

      // Disable datatype normalization. The XML 1.0 attribute value
      // normalization always occurs though.
      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.
      parser->setFeature (XMLUni::fgDOMEntities, false);

      // Perform Namespace processing.
      parser->setFeature (XMLUni::fgDOMNamespaces, true);

      // Perform Validation
      parser->setFeature (XMLUni::fgDOMValidation, true);

      // Do not include ignorable whitespace in the DOM tree.
      parser->setFeature (XMLUni::fgDOMWhitespaceInElementContent, false);

      // Enable the parser schema support.
      parser->setFeature (XMLUni::fgXercesSchema, 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.
      parser->setFeature (XMLUni::fgXercesSchemaFullChecking, true);

      // The parser will treat validation error as fatal and will exit.
      parser->setFeature (XMLUni::fgXercesValidationErrorAsFatal, true);

      CIAO::Config_Handler::Config_Error_Handler handler;
      parser->setErrorHandler(&handler);

      DOMDocument* doc = parser->parseURI(url);
      auto_ptr<DOMDocument> cleanup_doc (doc);

      if (handler.getErrors())
        {
          return 1;
        }
      HANDLER obj (doc, DOMNodeFilter::SHOW_ELEMENT |
                        DOMNodeFilter::SHOW_TEXT);

      DATA data;
      (obj.*func)(data);
      Deployment::DnC_Dump::dump (data);
      //domain_handler.dump(domain);

      // parser->release ();
    }
  catch (const DOMException& e)
    {
      const unsigned int maxChars = 2047;
      XMLCh errText[maxChars + 1];

      ACE_ERROR ((LM_ERROR, "\nException occured while parsing %s: \n", url));
      ACE_ERROR ((LM_ERROR, "DOMException code: %d\n ", e.code));
      if (DOMImplementation::loadDOMExceptionMsg(e.code, errText, maxChars))
        {
          char* message = XMLString::transcode (errText);
          ACE_Auto_Basic_Array_Ptr<char> cleanup_message (message);
          ACE_ERROR ((LM_ERROR, "Message is: %s\n", message));
        }
      return 1;
    }
  catch (const XMLException& e)
    {
      char* message = XMLString::transcode (e.getMessage());
      ACE_Auto_Basic_Array_Ptr<char> cleanup_message (message);
      ACE_ERROR ((LM_ERROR, "\nException occured: %s\n ", message));
      return 1;
    }
  catch (CORBA::Exception& ex)
    {
      ACE_PRINT_EXCEPTION (ex, "Caught CORBA Exception: ");
      return 1;
    }
  catch (...)
    {
      ACE_ERROR ((LM_ERROR, "Caught unknown exception\n"));
      return 1;
    }

  xercesc::XMLPlatformUtils::Terminate();
  return 0;
}