summaryrefslogtreecommitdiff
path: root/TAO/CIAO/DAnCE/Config_Handlers/Process_Element_T.cpp
blob: 56d0f530315aa76873a87113f27eab9b04a7d56e (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
//=====================================================================
/**
 *  @file Process_Element_T.cpp
 *
 *  $Id$
 *
 *  @author Emre Turkay  <turkaye@dre.vanderbilt.edu>
 *          Jaiganesh Balasubramanian <jai@dre.vanderbilt.edu>
 */
//=====================================================================

#include "Process_Element_T.h"
#include "Config_Handlers/Config_Error_Handler.h"
#include "ace/Auto_Ptr.h"
#include "Utils.h"
#include <iostream>
#include <memory>

BEGIN_DEPLOYMENT_NAMESPACE

class parser_error { };

template<typename DATA, typename OBJECT, typename SEQUENCE, typename FUNCTION>
bool
process_sequence_local(DOMDocument* doc, DOMNodeIterator* iter, DOMNode* node,
                       XStr& node_name, const char* name,
                       SEQUENCE& seq, OBJECT* obj, FUNCTION func)
{
  bool result = (node_name == XStr (ACE_TEXT (name)));

  if (result == true)
    {
      Process_Member_Function<OBJECT, DATA>
        pf(obj, func);
      process_sequential_element (node, doc, iter, seq, &pf, obj->id_map());
    }

  return result;
}

template<typename DATA, typename OBJECT, typename SEQUENCE, typename FUNCTION>
bool
process_sequence_remote(DOMDocument* doc, DOMNodeIterator* iter, DOMNode*
node,
                        XStr& node_name, const char* name,
                        SEQUENCE& seq, FUNCTION func,
                        REF_MAP& id_map)
{
  bool result = (node_name == XStr (ACE_TEXT (name)));

  if (result == true)
    {
      OBJECT obj (doc, iter, false);

      Process_Member_Function_Remote<OBJECT, DATA>
        pf(obj, func);
      process_sequential_element (node, doc, iter, seq, &pf, id_map);
    }

  return result;
}

template<typename DATA, typename SEQUENCE, typename FUNCTION>
bool
process_sequence_common(DOMDocument* doc, DOMNodeIterator* iter, DOMNode*
node,
                        XStr& node_name, const char* name,
                        SEQUENCE& seq, FUNCTION func,
                        REF_MAP& id_map)
{
  bool result = (node_name == XStr (ACE_TEXT (name)));

  if (result == true)
    {
      Process_Static_Function<DATA>
        pf(func);
      process_sequential_element (node, doc, iter, seq, &pf, id_map);
    }

  return result;
}

template<typename DATA, typename OBJECT, typename ELEMENT, typename FUNCTION>
bool
process_element(DOMDocument* doc, DOMNodeIterator* iter, DOMNode* node,
                XStr& node_name, const char* name,
                ELEMENT& elem, OBJECT* obj, FUNCTION func,
                REF_MAP& id_map)
{
  bool result = (node_name == XStr (ACE_TEXT (name)));

  if (result == true)
    {
      if (node->hasAttributes ())
        {
          DOMNamedNodeMap* named_node_map = node->getAttributes ();
          int length = named_node_map->getLength ();

          Process_Member_Function<OBJECT, DATA>
            pf(obj, func);

          if (length == 1)
              pf(doc, iter, elem);
          else
              process_element_attributes(named_node_map, doc, iter, 0, elem,
&pf, id_map);
        }
    }

  return result;
}

template<typename DATA, typename OBJECT, typename ELEMENT, typename FUNCTION>
bool
process_element_remote(DOMDocument* doc, DOMNodeIterator* iter, DOMNode* node,
                       XStr& node_name, const char *name,
                       ELEMENT& elem, FUNCTION func,
                       REF_MAP& id_map)
{
  bool result = (node_name == XStr (ACE_TEXT (name)));

  if (result == true)
    {
      OBJECT obj (iter, false);

      if (node->hasAttributes ())
        {
          DOMNamedNodeMap* named_node_map = node->getAttributes ();
          int length = named_node_map->getLength ();

          Process_Member_Function_Remote<OBJECT, DATA>
            pf(&obj, func);

          if (length == 1)
              pf(doc, iter, elem);
          else
              process_element_attributes(named_node_map, doc, iter, 0, elem,
&pf, id_map);
        }
    }

  return result;
}

template <typename VALUE, typename DATA>
void process_element_attributes(DOMNamedNodeMap* named_node_map,
                                DOMDocument* doc,
                                DOMNodeIterator* iter,
                                VALUE value,
                                DATA& data,
                                Process_Function <DATA>* func,
                                REF_MAP& id_map)
{
  char* final_url_ch;
  try
    {
      // the number of attributes
      int length = named_node_map->getLength();
      // iterate the attributes
      for (int j = 0; j < length; ++j)
        {
          DOMNode* attribute_node = named_node_map->item (j);
          XStr strattrnodename (attribute_node->getNodeName ());
          char* aceattrnodevalue_ch =
            XMLString::transcode (attribute_node->getNodeValue ());
          ACE_TString aceattrnodevalue = aceattrnodevalue_ch;
          XMLString::release (&aceattrnodevalue_ch);

          // if xmi::id is given process the element and bind the value
          if (strattrnodename == XStr (ACE_TEXT ("xmi:id")))
            {
              (*func) (doc, iter, data);
              id_map.bind (aceattrnodevalue, value);
            }
          // if href is given find out the referenced position
          // and process the element
          else if (strattrnodename == XStr (ACE_TEXT ("href")))
            {
              XMLURL xml_url (aceattrnodevalue.c_str ());
              XMLURL result (aceattrnodevalue.c_str ());
              std::string url_string = aceattrnodevalue.c_str ();
              char* doc_path_ch =
                XMLString::transcode ( doc->getDocumentURI ());
              ACE_TString doc_path = doc_path_ch;
              XMLString::release (&doc_path_ch);
              XMLCh* rel_str =
                (XMLString::transcode (doc_path.c_str ()));
              result.makeRelativeTo
                (rel_str);
              final_url_ch =
                XMLString::transcode (result.getURLText ());
              ACE_TString final_url = final_url_ch;
              XMLString::release (&final_url_ch);
              XMLString::release (&rel_str);

              DOMDocument* href_doc;

              std::auto_ptr<DOMBuilder> parser (
                      CIAO::Config_Handler::Utils::create_parser  ());

              CIAO::Config_Handler::Config_Error_Handler handler;
              parser->setErrorHandler(&handler);
                      
              if (xml_url.isRelative ())
                {
                  href_doc = parser->parseURI (final_url.c_str ());
                  if (handler.getErrors ())
                    {
                      ACE_DEBUG ((LM_DEBUG, "XML descriptor error\n"));
                      //throw parser_error ();
                      throw DOMException ();
                    }
                }
              else
                {
                  href_doc = parser->parseURI (url_string.c_str ());
                  if (handler.getErrors ())
                    {
                      ACE_DEBUG ((LM_DEBUG, "XML descriptor error\n"));
                      //throw parser_error ();
                      throw DOMException ();
                    }
                }

              DOMDocumentTraversal* traverse = href_doc;
              DOMNode* root = (href_doc->getDocumentElement ());
              unsigned long filter = DOMNodeFilter::SHOW_ELEMENT |
                DOMNodeFilter::SHOW_TEXT;
              DOMNodeIterator* href_iter = traverse->createNodeIterator
                (root,
                 filter,
                 0,
                 true);
              href_iter->nextNode ();
              (*func) (href_doc, href_iter, data);
            }
        }
    }
  catch (const DOMException& e)
    {
      const unsigned int maxChars = 2047;
      XMLCh errText[maxChars + 1];

      ACE_ERROR ((LM_ERROR, "\nException occured while parsing %s: \
                  \n",final_url_ch));
      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));
        }
      ACE_ERROR ((LM_ERROR, "Caught DOM exception\n\n"));
      return;
    }
  catch (...)
    {
      ACE_DEBUG ((LM_DEBUG, "Caught unknown exception\n\n"));
      return;
    }
}

template <typename SEQUENCE, typename DATA>
void
process_sequential_element (DOMNode* node,
                            DOMDocument* doc,
                            DOMNodeIterator* iter,
                            SEQUENCE& seq,
                            Process_Function <DATA>* func,
                            REF_MAP& id_map)
{
  if (node->hasAttributes ())
    {
      // the size of the sequence
      CORBA::ULong i = seq.length ();

      // add 1 to the size of the sequence
      seq.length (i + 1);

      // fetch attributes
      DOMNamedNodeMap* named_node_map = node->getAttributes ();

      // the number of attributes the element have
      int length = named_node_map->getLength();
      // if there is no other attribute but 'version'

      if (length == 1) // call directly the static process_ method
        (*func) (doc, iter, seq[i]);
      else             // Check the xmi::id & href attributes
        process_element_attributes(named_node_map, doc, iter, i, seq[i], func, id_map);
    }
}

END_DEPLOYMENT_NAMESPACE