summaryrefslogtreecommitdiff
path: root/cs/src/Ice/XMLOutput.cs
blob: 447f2797c446fb05f5a350eed2a287316d54fb7d (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
295
296
297
298
// **********************************************************************
//
// Copyright (c) 2003-2008 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

namespace IceUtilInternal
{

using System.Collections;
using System.IO;
using System.Diagnostics;

public class XMLOutput : OutputBase
{
    public XMLOutput()
        : base()
    {
        elementStack_ = new Stack();
        se_ = false;
        text_ = false;
        sgml_ = false;
        escape_ = false;
    }
    
    public XMLOutput(StreamWriter writer)
        : base(writer)
    {
            elementStack_ = new Stack();
            se_ = false;
            text_ = false;
            sgml_ = false;
            escape_ = false;
    }
    
    public XMLOutput(string s)
        : base(s)
    {
            elementStack_ = new Stack();
            se_ = false;
            text_ = false;
            sgml_ = false;
            escape_ = false;
    }
    
    virtual public void 
    setSGML(bool sgml)
    {
        sgml_ = true;
    }

    public override void
    print(string s)
    {
        if(se_)
        {
            out_.Write(">");
            se_ = false;
        }
        text_ = true;
        
        if(escape_)
        {
            string escaped = escape(s);
            base.print(escaped);
        }
        else
        {
            base.print(s);
        }
    }
    
    public virtual XMLOutput
    write(string s)
    {
        print(s);
        return this;
    }
    
    public override void
    nl()
    {
        if(se_)
        {
            se_ = false;
            out_.Write(">");
        }
        base.nl();
    }
    
    public virtual XMLOutput
    se(string element)
    {
        nl();
        
        //
        // If we're not in SGML mode the output of the '>' character is
        // deferred until either the end-element (in which case a /> is
        // emitted) or until something is displayed.
        //
        if(escape_)
        {
            out_.Write('<');
            out_.Write(escape(element));
        }
        else
        {
            out_.Write('<');
            out_.Write(element);
        }
        se_ = true;
        text_ = false;
        
        int pos = element.IndexOf(' ');
        if (pos == - 1)
        {
            pos = element.IndexOf('\t');
        }
        if (pos == - 1)
        {
            elementStack_.Push(element);
        }
        else
        {
            elementStack_.Push(element.Substring(0, pos - 1));
        }
        
        ++pos_; // TODO: ???
        inc();
        separator_ = false;
        return this;
    }
    
    public virtual XMLOutput
    ee()
    {
        string element = (string)elementStack_.Pop();
        
        dec();
        if(se_)
        {
            //
            // SGML (docbook) doesn't support <foo/>
            //
            if(sgml_)
            {
                out_.Write("></");
                out_.Write(element);
                out_.Write(">");
            }
            else
            {
                out_.Write("/>");
            }
        }
        else
        {
            if(!text_)
            {
                nl();
            }
            out_.Write("</");
            out_.Write(element);
            out_.Write(">");
        }
        --pos_; // TODO: ???
        
        se_ = false;
        text_ = false;
        return this;
    }
    
    public virtual XMLOutput
    attr(string name, string val)
    {
        //
        // Precondition: Attributes can only be attached to elements.
        //
        Debug.Assert(se_);
        out_.Write(" ");
        out_.Write(name);
        out_.Write("=\"");
        out_.Write(escape(val));
        out_.Write("\"");
        return this;
    }
    
    public virtual XMLOutput
    startEscapes()
    {
        escape_ = true;
        return this;
    }
    
    public virtual XMLOutput
    endEscapes()
    {
        escape_ = false;
        return this;
    }
    
    public virtual string
    currentElement()
    {
        if(elementStack_.Count > 0)
        {
            return (string)elementStack_.Peek();
        }
        else
        {
            return "";
        }
    }
    
    private string
    escape(string input)
    {
        string v = input;
        
        //
        // Find out whether there is a reserved character to avoid
        // conversion if not necessary.
        //
        string allReserved = "<>'\"&";
        bool hasReserved = false;
        char[] arr = input.ToCharArray();
        for(int i = 0; i < arr.Length; i++)
        {
            if(allReserved.IndexOf((char)arr[i]) != - 1)
            {
                hasReserved = true;
                break;
            }
        }
        if(hasReserved)
        {
            int index;

            //
            // First convert all & to &amp;
            //
            index = v.IndexOf('&');
            if(index != - 1)
            {
                v = v.Insert(index, "amp;");
            }
            
            //
            // Next convert remaining reserved characters.
            //
            index = v.IndexOf('>');
            if(index != - 1)
            {
                string tmp = v.Substring(0, index);
                tmp += "&gt";
                tmp += v.Substring(index + 1);
                v = tmp;
            }
            index = v.IndexOf('<');
            if(index != -1)
            {
                string tmp = v.Substring(0, index);
                tmp += "&lt";
                tmp += v.Substring(index + 1);
                v = tmp;
            }
            index = v.IndexOf('\'');
            if(index != -1)
            {
                string tmp = v.Substring(0, index);
                tmp += "&apos;";
                tmp += v.Substring(index + 1);
                v = tmp;
            }
            index = v.IndexOf('"');
            if(index != -1)
            {
                string tmp = v.Substring(0, index);
                tmp += "&quot;";
                tmp += v.Substring(index + 1);
            }
        }
        return v;
    }
    
    private Stack elementStack_;
    
    internal bool se_;
    internal bool text_;
    
    private bool sgml_;
    private bool escape_;
}

}