summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/orbsvcs/Notify/XML_Loader.cpp
blob: f880fc28ecb6c348dee0285194253ccd2ebff303 (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
// $Id$

#include "orbsvcs/Notify/XML_Loader.h"
#include "orbsvcs/Notify/Topology_Object.h"

#include "ACEXML/common/DefaultHandler.h"
#include "ACEXML/parser/parser/Parser.h"
#include "ACEXML/common/FileCharStream.h"

#include "ace/Containers_T.h"
#include "tao/debug.h"
#include "ace/OS_NS_unistd.h"

//#define DEBUG_LEVEL 9
#ifndef DEBUG_LEVEL
# define DEBUG_LEVEL TAO_debug_level
#endif //DEBUG_LEVEL

using namespace TAO_Notify;

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO_Notify {
  extern const char TOPOLOGY_ID_NAME[];
}

TAO_END_VERSIONED_NAMESPACE_DECL

namespace {
  CORBA::Long makeNVPList (NVPList& nvp, ACEXML_Attributes* attrs)
  {
    CORBA::Long id = 0;
    for (size_t i = 0; i < attrs->getLength (); ++i)
    {
      const ACE_TCHAR * name = attrs->getQName (i);
      const ACE_TCHAR * value = attrs->getValue (i);
      if (ACE_OS::strcmp (name,
                          //TOPOLOGY_ID_NAME) == 0)
                          TAO_VERSIONED_NAMESPACE_NAME::TAO_Notify::TOPOLOGY_ID_NAME) == 0)
      {
        id = ACE_OS::atoi (value);
      }
      nvp.push_back (NVP (ACE_TEXT_TO_CHAR_IN(name),
                          ACE_TEXT_TO_CHAR_IN(value)));
    }
    return id;
  }
}

TAO_BEGIN_VERSIONED_NAMESPACE_DECL

namespace TAO_Notify
{
  XML_Loader::XML_Loader ()
    : input_ (0)
    , live_ (false)
  {
  }

  XML_Loader::~XML_Loader ()
  {
  }

  bool
  XML_Loader::open (const ACE_CString & base_name)
  {
    bool result = false;

    // if *.xml exists, use it
    // if it does not exist then
    // use the previous one was renamed to *.000
    // If neither *.xml nor *.000 exist then something
    // "impossible" happened (or its a new system with no saved state).

    this->file_name_ = base_name;
    this->file_name_ += ".xml";

    // 4 is "read permission"
    result =  (0 == ACE_OS::access (this->file_name_.c_str (), 4));
    if (result)
    {
      this->live_ = false;
      ACEXML_FileCharStream* fstm = new ACEXML_FileCharStream;
      // xml input source will take ownership

      if (fstm->open (ACE_TEXT_TO_TCHAR_IN(this->file_name_.c_str ())) == 0)
      {
        // InputSource takes ownership
        ACEXML_InputSource input (fstm);

        ACEXML_Parser parser;
        parser.setContentHandler (this);
        parser.setDTDHandler (this);
        parser.setErrorHandler (this);
        parser.setEntityResolver (this);

        ACEXML_TRY_NEW_ENV
        {
          parser.parse (&input ACEXML_ENV_ARG_PARAMETER);
          ACEXML_TRY_CHECK;
        }
        ACEXML_CATCH (ACEXML_Exception, ex)
        {
          // The only way to find out what it is, it to let it print itself, so...
          ACE_ERROR ((LM_ERROR, "Unable to load \"%s\".\n Will try backup file.\n", this->file_name_.c_str ()));
          ex.print ();
          result = false;
        }
        ACEXML_ENDTRY;
      }
      else
      {
        ACE_DEBUG((LM_DEBUG, ACE_TEXT("Unable to open the XML input file: %s.\n Will try backup file.\n"), file_name_.c_str()));
        result = false;
      }
    }

    if (! result)
    {
      this->file_name_ = base_name;
      this->file_name_ += ".000";
      result = (0 == ACE_OS::access (this->file_name_.c_str (), 4));
    }
    return result;
  }

  //virtual
  void
  XML_Loader::load (Topology_Object *root ACE_ENV_ARG_DECL)
  {
    ACE_ASSERT (root != 0);
    this->live_ = true;

    ACEXML_FileCharStream* fstm = new ACEXML_FileCharStream;
    // xml input source will take ownership

    if (fstm->open (ACE_TEXT_TO_TCHAR_IN(this->file_name_.c_str ())) == 0)
    {
      // InputSource takes ownership
      ACEXML_InputSource input (fstm);

      ACEXML_Parser parser;
      parser.setContentHandler (this);
      parser.setDTDHandler (this);
      parser.setErrorHandler (this);
      parser.setEntityResolver (this);

      ACEXML_TRY_NEW_ENV
      {
        object_stack_.push (root);
        parser.parse (&input ACEXML_ENV_ARG_PARAMETER);
        ACEXML_TRY_CHECK;
        ACE_ASSERT (object_stack_.size () == 1);
        Topology_Object* cur;
        object_stack_.pop (cur);
      }
      ACEXML_CATCH (ACEXML_Exception, ex)
      {
        // The only way to find out what it is, it to let it print itself, so...
        ACE_ERROR ((LM_ERROR, "Unable to load \"%s\".\n", this->file_name_.c_str ()));
        ex.print ();
        ACE_THROW(CORBA::INTERNAL());
      }
      ACEXML_ENDTRY;
    }
    else
    {
      ACE_DEBUG((LM_DEBUG, ACE_TEXT("Unable to open the XML input file: %s.\n"), file_name_.c_str()));
      ACE_THROW(CORBA::INTERNAL());
    }
  }

  void
  XML_Loader::startElement (const ACEXML_Char*,
    const ACEXML_Char*,
    const ACEXML_Char* name,
    ACEXML_Attributes* xml_attrs ACEXML_ENV_ARG_DECL)
    ACE_THROW_SPEC ( (ACEXML_SAXException))
  {
    ACE_ASSERT (name != 0);
    ACE_ASSERT (xml_attrs != 0);
    if (this->live_)
    {
      ACE_ASSERT (object_stack_.size () > 0);
      Topology_Object* cur = 0;
      if (object_stack_.top (cur) == 0)
      {
        ACE_DECLARE_NEW_ENV;
        ACE_TRY
        {
          NVPList attrs;
          CORBA::Long id = makeNVPList (attrs, xml_attrs);

          if (DEBUG_LEVEL > 5) ACE_DEBUG ((LM_INFO,
            ACE_TEXT("(%P|%t) XML_Loader: Element %s\n"),
            name
            ));

          ACE_CString cname (ACE_TEXT_TO_CHAR_IN(name));
          Topology_Object* next = cur->load_child (
            cname, id, attrs ACE_ENV_ARG_PARAMETER);
          ACE_ASSERT(next != 0);
          ACE_TRY_CHECK;
          object_stack_.push (next);
        }
        ACE_CATCHANY
        {
          ACEXML_THROW (ACEXML_SAXException (ACE_TEXT_TO_TCHAR_IN(
                            ACE_ANY_EXCEPTION._info ().c_str ())));
        }
        ACE_ENDTRY;
      }
    }
  }

  void
  XML_Loader::endElement (const ACEXML_Char*,
    const ACEXML_Char*,
    const ACEXML_Char* name ACEXML_ENV_ARG_DECL_NOT_USED)
    ACE_THROW_SPEC ( (ACEXML_SAXException))
  {
    ACE_UNUSED_ARG (name);
    if (this->live_)
    {
      ACE_ASSERT (object_stack_.size () > 0);
      if (DEBUG_LEVEL > 5) ACE_DEBUG ((LM_INFO,
        ACE_TEXT("(%P|%t) XML_Loader: End Element %s\n"),
        name
        ));
      Topology_Object* cur;
      object_stack_.pop (cur);
    }
  }

} /* namespace TAO_Notify */

TAO_END_VERSIONED_NAMESPACE_DECL