File: JavascriptXmlWriterWrapper.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (191 lines) | stat: -rw-r--r-- 5,972 bytes parent folder | download | duplicates (7)
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
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------

namespace System.ServiceModel.Channels
{
    using System.Globalization;
    using System.IO;
    using System.Text;
    using System.Xml;

    class JavascriptXmlWriterWrapper : XmlDictionaryWriter
    {
        Encoding encoding;
        Stream stream;
        XmlDictionaryWriter xmlJsonWriter;
        byte[] encodedClosingFunctionCall;

        public JavascriptXmlWriterWrapper(Encoding encoding)
        {
            this.encoding = encoding;
            this.encodedClosingFunctionCall = this.encoding.GetBytes(");");
        }

        public JavascriptCallbackResponseMessageProperty JavascriptResponseMessageProperty
        {
            get;
            set;
        }

        public XmlDictionaryWriter XmlJsonWriter
        {
            get { return this.xmlJsonWriter; }
        }

        public override void Close()
        {
            this.xmlJsonWriter.Close();
        }

        public override void Flush()
        {
            this.xmlJsonWriter.Flush();
        }

        public override string LookupPrefix(string ns)
        {
            return this.xmlJsonWriter.LookupPrefix(ns);
        }

        public override void WriteBase64(byte[] buffer, int index, int count)
        {
            this.xmlJsonWriter.WriteBase64(buffer, index, count);
        }

        public override void WriteCData(string text)
        {
            this.xmlJsonWriter.WriteCData(text);
        }

        public override void WriteCharEntity(char ch)
        {
            this.xmlJsonWriter.WriteCharEntity(ch);
        }

        public override void WriteChars(char[] buffer, int index, int count)
        {
            this.xmlJsonWriter.WriteChars(buffer, index, count);
        }

        public override void WriteComment(string text)
        {
            this.xmlJsonWriter.WriteComment(text);
        }

        public override void WriteDocType(string name, string pubid, string sysid, string subset)
        {
            this.xmlJsonWriter.WriteDocType(name, pubid, sysid, subset);
        }

        public override void WriteEndAttribute()
        {
            this.xmlJsonWriter.WriteEndAttribute();
        }

        public override void WriteEndDocument()
        {
            this.xmlJsonWriter.WriteEndDocument();

            if (this.JavascriptResponseMessageProperty != null &&
                !String.IsNullOrEmpty(this.JavascriptResponseMessageProperty.CallbackFunctionName))
            {
                this.xmlJsonWriter.Flush();
                if (this.JavascriptResponseMessageProperty.StatusCode != null && (int)this.JavascriptResponseMessageProperty.StatusCode != 200)
                {
                    byte[] buffer = this.encoding.GetBytes(String.Format(CultureInfo.InvariantCulture, ",{0}", (int)this.JavascriptResponseMessageProperty.StatusCode));
                    this.stream.Write(buffer, 0, buffer.Length);
                }
                this.stream.Write(this.encodedClosingFunctionCall, 0, this.encodedClosingFunctionCall.Length);
            }
        }

        public override void WriteEndElement()
        {
            this.xmlJsonWriter.WriteEndElement();
        }

        public override void WriteEntityRef(string name)
        {
            this.xmlJsonWriter.WriteEntityRef(name);
        }

        public override void WriteFullEndElement()
        {
            this.xmlJsonWriter.WriteFullEndElement();
        }

        public override void WriteProcessingInstruction(string name, string text)
        {
            this.xmlJsonWriter.WriteProcessingInstruction(name, text);
        }

        public override void WriteRaw(string data)
        {
            this.xmlJsonWriter.WriteRaw(data);
        }

        public override void WriteRaw(char[] buffer, int index, int count)
        {
            this.xmlJsonWriter.WriteRaw(buffer, index, count);
        }

        public override void WriteStartAttribute(string prefix, string localName, string ns)
        {
            this.xmlJsonWriter.WriteStartAttribute(prefix, localName, ns);
        }

        public override void WriteStartDocument(bool standalone)
        {
            StartJsonMessage();
            this.xmlJsonWriter.WriteStartDocument(standalone);
        }

        public override void WriteStartDocument()
        {
            StartJsonMessage();
            this.xmlJsonWriter.WriteStartDocument();
        }

        void StartJsonMessage()
        {
            if (this.JavascriptResponseMessageProperty != null &&
                !String.IsNullOrEmpty(this.JavascriptResponseMessageProperty.CallbackFunctionName))
            {
                byte[] buffer = this.encoding.GetBytes(String.Format(CultureInfo.InvariantCulture, "{0}(", this.JavascriptResponseMessageProperty.CallbackFunctionName));
                this.stream.Write(buffer, 0, buffer.Length);
            }
        }

        public override void WriteStartElement(string prefix, string localName, string ns)
        {
            this.xmlJsonWriter.WriteStartElement(prefix, localName, ns);
        }

        public override WriteState WriteState
        {
            get { return this.xmlJsonWriter.WriteState; }
        }

        public override void WriteString(string text)
        {
            this.xmlJsonWriter.WriteString(text);
        }

        public override void WriteSurrogateCharEntity(char lowChar, char highChar)
        {
            this.xmlJsonWriter.WriteSurrogateCharEntity(lowChar, highChar);
        }

        public override void WriteWhitespace(string ws)
        {
            this.xmlJsonWriter.WriteWhitespace(ws);
        }

        public void SetOutput(Stream stream, XmlDictionaryWriter writer)
        {
            this.stream = stream;
            this.xmlJsonWriter = writer;
        }
    }
}