summaryrefslogtreecommitdiff
path: root/trunk/CIAO/CCF/CCF/CodeGenerationKit/IndentationXML.hpp
blob: 774a70e501f27cb19ae619ef30fcf516c10750cd (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
// file      : CCF/CodeGenerationKit/IndentationXML.hpp
// author    : Diego Sevilla Ruiz <dsevilla@ditec.um.es>
// author    : Boris Kolpackov <boris@dre.vanderbilt.edu>
// cvs-id    : $Id$

#ifndef CCF_CODE_GENERATION_KIT_INDENTATION_XML_HPP
#define CCF_CODE_GENERATION_KIT_INDENTATION_XML_HPP

#include <deque>

#include "CCF/CodeGenerationKit/IndentationBuffer.hpp"

namespace Indentation
{
  template <typename C>
  class XML : public Buffer<C>
  {
  public:
    typedef
    typename Buffer<C>::traits_type
    traits_type;

    typedef
    typename Buffer<C>::char_type
    char_type;

    typedef
    typename Buffer<C>::int_type
    int_type;

    typedef
    typename Buffer<C>::EndOfStream
    EndOfStream;

  public:
    XML (Buffer<C>& out)
        : out_ (out),
          indentation_ (0),
          base_indent_ (indentation_),
          spaces_ (2),
          construct_ (OTHER)
    {
    }

    virtual
    ~XML () throw () {}

  public:
    virtual int_type
    put (char_type c) throw (ExH::System::Exception)
    {
      int_type result = traits_type::to_int_type (c);

      try
      {
        switch (c)
        {
        case '\n':
          {
            flush_buffer ();

            // If we are inside a tag, just output an artificial tab character
            if (construct_ == INSIDE_TAG)
            {
              hold_.push_back (c);
              c = traits_type::to_int_type (' ');

              // spaces_ - 1 because at the end the last space will be
              // put into the hold_ queue.
              for (unsigned long i = 0; i < spaces_ - 1; i++)
                hold_.push_back (c);
            }

            base_indent_ = indentation_;

            break;
          }

          // Case with <?xxx and <!DOCTYPE (for example).
          // We still don't handle XML comments
        case '?':
        case '!':
          {
            if (construct_ == INSIDE_TAG && hold_.back () == '<')
              indentation_--;

            break;
          }

        case '<':
          {
            //@@ This construct must not be INSIDE_TAG
            if (construct_ == OTHER)
            {
              construct_ = INSIDE_TAG;
              indentation_++;
            }

            break;
          }

        case '>':
          {
            if (construct_ == INSIDE_TAG)
              construct_ = OTHER;

            break;
          }

        case '/':
          {
            if (construct_ == INSIDE_TAG)
            {
              if (hold_.back () == '<')
                indentation_ -= 2;
              else
                indentation_--;

              if (indentation_ < 0)
                indentation_ = 0;
            }

            break;
          }

        case '\"':
          {
            if (construct_ == INSIDE_TAG)
              construct_ = STRING_LITERAL;
            else if (construct_ == STRING_LITERAL)
              construct_ = INSIDE_TAG;

            break;
          }

        }

        hold_.push_back (c);

      }
      catch (Full const&)
      {
        result = traits_type::eof ();
      }

      return result;
    }

    virtual void
    unbuffer () throw (EndOfStream, ExH::System::Exception)
    {
      try
      {
        flush_buffer ();
      } catch (const Full&)
      {
        throw EndOfStream ("unable to flush buffer");
      }
    }

  private:
    class Full {};

    void
    flush_buffer () throw (Full)
    {
      long delta = indentation_ - base_indent_;

      if (delta > 0)
        delta = 0;

      while (!hold_.empty ())
      {
        int_type c = hold_.front ();

        write (c);

        if (c == '\n')
          output_indentation (base_indent_ + delta);

        hold_.pop_front ();
      }
    }

    void
    output_indentation (unsigned long indentation) throw (Full)
    {
      for (unsigned long i = 0; i < indentation * spaces_; i++)
      {
        write (' ');
      }
    }

    int_type
    write (int_type c) throw (Full)
    {
      int_type result;

      result = out_.put (c);

      if (result == traits_type::eof ()) throw Full ();

      return result;
    }


  private:
    Buffer<C>& out_;
    long indentation_;
    unsigned long base_indent_;
    unsigned long spaces_;

    enum Construct
    {
      OTHER,
      /* Strings literals can only happen inside tags, like:
       * <tag whatever="xxx
       */
      STRING_LITERAL,
      INSIDE_TAG
    };

    Construct construct_;

    typedef
    std::deque<int_type>
    Hold;

    Hold hold_;
  };
}

#endif // CCF_CODE_GENERATION_KIT_INDENTATION_XML_HPP