aboutsummaryrefslogtreecommitdiff
path: root/xsd/cxx/literal-map.cxx
blob: 4a46ce75739c10ee7a59357cacebef349f0b5ce1 (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
// file      : xsd/cxx/literal-map.cxx
// copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <memory>  // std::auto_ptr
#include <cstddef> // std::size_t
#include <fstream>
#include <iostream>

#include <xercesc/util/XMLUni.hpp>

#include <xercesc/framework/LocalFileInputSource.hpp>

#include <xercesc/sax/Locator.hpp>
#include <xercesc/sax/SAXParseException.hpp>
#include <xercesc/sax2/DefaultHandler.hpp>
#include <xercesc/sax2/SAX2XMLReader.hpp>
#include <xercesc/sax2/XMLReaderFactory.hpp>

#include <xsd-frontend/xml.hxx>

#include <cxx/literal-map.hxx>

using namespace std;
using namespace xercesc;
namespace XML = XSDFrontend::XML;

namespace CXX
{
  class Handler: public DefaultHandler
  {
  public:
    struct Failed {};

    Handler (String const& file, StringLiteralMap& map)
        : state_ (s_init), file_ (file), map_ (map)
    {
    }

    virtual void
    setDocumentLocator (const Locator* const l)
    {
      locator_ = l;
    }

    virtual void
    startElement (const XMLCh* const,
                  const XMLCh* const lname,
                  const XMLCh* const,
                  const xercesc::Attributes&)
    {
      String n (XML::transcode (lname));

      if (n == L"string-literal-map" && state_ == s_init)
        state_ = s_map;
      else if (n == L"entry" && state_ == s_map)
      {
        str_seen_ = false;
        lit_seen_ = false;
        state_ = s_entry;
      }
      else if (n == L"string" && state_ == s_entry)
      {
        str_seen_ = true;
        str_.clear ();
        state_ = s_string;
      }
      else if (n == L"literal" && state_ == s_entry)
      {
        lit_seen_ = true;
        lit_.clear ();
        state_ = s_literal;
      }
      else
      {
        wcerr << file_ << ":" << line () << ":" << col () << ": error: "
              << "unexpected element '" << n << "'" << endl;
        throw Failed ();
      }
    }

    virtual void
    endElement (const XMLCh* const,
                const XMLCh* const lname,
                const XMLCh* const)
    {
      String n (XML::transcode (lname));

      if (n == L"string-literal-map")
        state_ = s_init;
      else if (n == L"entry")
      {
        if (!str_seen_)
        {
          wcerr << file_ << ":" << line () << ":" << col () << ": error: "
                << "expected 'string' element" << endl;
          throw Failed ();
        }

        if (!lit_seen_)
        {
          wcerr << file_ << ":" << line () << ":" << col () << ": error: "
                << "expected 'literal' element" << endl;
          throw Failed ();
        }

        map_[str_] = lit_;
        state_ = s_map;
      }
      else if (n == L"string")
        state_ = s_entry;
      else if (n == L"literal")
        state_ = s_entry;
    }

    virtual void
    characters (const XMLCh* const s, const XMLSize_t length)
    {
      String str (XML::transcode (s, length));

      if (state_ == s_string)
        str_ += str;
      else if (state_ == s_literal)
        lit_ += str;
      else
      {
        for (size_t i (0); i < str.size (); ++i)
        {
          wchar_t c (str[i]);

          if (c != 0x20 && c != 0x0A && c != 0x0D && c != 0x09)
          {
            wcerr << file_ << ":" << line () << ":" << col () << ": error: "
                  << "unexpected character data" << endl;
            throw Failed ();
          }
        }
      }
    }

    // Error hanlding.
    //
    enum Severity {s_warning, s_error, s_fatal};

    virtual void
    warning (const SAXParseException& e)
    {
      handle (e, s_warning);
    }

    virtual void
    error (const SAXParseException& e)
    {
      handle (e, s_error);
    }

    virtual void
    fatalError (const SAXParseException& e)
    {
      handle (e, s_fatal);
    }

    virtual void
    resetErrors ()
    {
    }

    void
    handle (const SAXParseException& e, Severity s)
    {
      String msg (XML::transcode (e.getMessage ()));

      wcerr << file_ << ":"
            << e.getLineNumber () << ":" << e.getColumnNumber () << ": "
            << (s == s_warning ? "warning: " : "error: ") << msg << endl;

      if (s != s_warning)
        throw Failed ();
    }

    size_t
    line () const
    {
      return locator_ != 0
        ? static_cast<size_t> (locator_->getLineNumber ())
        : 0;
    }

    size_t
    col () const
    {
      return locator_ != 0
        ? static_cast<size_t> (locator_->getColumnNumber ())
        : 0;
    }

  private:
    const Locator* locator_;

    enum
    {
      s_init,
      s_map,
      s_entry,
      s_string,
      s_literal
    } state_;

    String file_;
    StringLiteralMap& map_;

    bool str_seen_;
    bool lit_seen_;

    String str_;
    String lit_;
  };

  bool
  read_literal_map (NarrowString const& file, StringLiteralMap& map)
  {
    try
    {
      // Try to open the file with fstream. This way we get to
      // report the error in a consistent manner.
      //
      {
        ifstream ifs (file.c_str ());
        if (!ifs.is_open ())
        {
          wcerr << file.c_str () << ": unable to open in read mode" << endl;
          return false;
        }
      }

      String wfile (file);

      LocalFileInputSource is (XML::XMLChString (wfile).c_str ());
      Handler h (wfile, map);

      auto_ptr<SAX2XMLReader> parser (
        XMLReaderFactory::createXMLReader ());

      parser->setFeature (XMLUni::fgSAX2CoreNameSpaces, true);
      parser->setFeature (XMLUni::fgSAX2CoreNameSpacePrefixes, true);
      parser->setFeature (XMLUni::fgSAX2CoreValidation, false);
      parser->setFeature (XMLUni::fgXercesSchema, false);
      parser->setFeature (XMLUni::fgXercesSchemaFullChecking, false);

      parser->setErrorHandler (&h);
      parser->setContentHandler (&h);

      parser->parse (is);
    }
    catch (Handler::Failed const&)
    {
      return false;
    }

    return true;
  }
}