Mercurial > hg > ucis.core
annotate Xml/Socket.cs @ 111:df53bdd49507 default tip
Merge
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Fri, 07 Nov 2014 18:37:39 +0100 |
parents | 0fc3f42a8555 |
children |
rev | line source |
---|---|
0 | 1 ???using System; |
2 using System.Text; | |
3 using System.IO; | |
4 using System.Xml; | |
5 | |
6 namespace UCIS.Xml { | |
7 public class XmlSocket : XmlWriter { | |
8 protected Stream pStream; | |
9 protected XmlWriter pWriter; | |
10 protected XmlReaderSettings pXMLReaderSettings;// = new XmlReaderSettings(); | |
11 | |
12 public Stream BaseStream { get { return pStream; } } | |
13 | |
14 public override XmlWriterSettings Settings { get { return WriterSettings; } } | |
15 public XmlWriterSettings WriterSettings { get; private set; } | |
16 public XmlReaderSettings ReaderSettings { get { return pXMLReaderSettings; } } | |
17 | |
18 public XmlSocket(Stream Stream, Encoding Encoding) { | |
19 WriterSettings = new XmlWriterSettings(); | |
20 WriterSettings.ConformanceLevel = ConformanceLevel.Document; | |
21 WriterSettings.NewLineHandling = NewLineHandling.Entitize; | |
22 WriterSettings.OmitXmlDeclaration = true; | |
23 WriterSettings.CheckCharacters = false; | |
24 WriterSettings.Encoding = Encoding; | |
25 WriterSettings.CloseOutput = false; | |
26 | |
27 pXMLReaderSettings = new XmlReaderSettings(); | |
28 pXMLReaderSettings.ConformanceLevel = ConformanceLevel.Document; | |
29 pXMLReaderSettings.CloseInput = true; | |
30 pXMLReaderSettings.IgnoreComments = true; | |
31 pXMLReaderSettings.IgnoreProcessingInstructions = true; | |
32 pXMLReaderSettings.IgnoreWhitespace = true; | |
33 pXMLReaderSettings.ValidationType = ValidationType.None; | |
34 pXMLReaderSettings.ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None; | |
35 pXMLReaderSettings.CheckCharacters = false; | |
36 | |
37 pStream = Stream; | |
38 } | |
28 | 39 public XmlSocket(Stream Stream) : this(Stream, new UTF8Encoding(false)) { } |
0 | 40 |
41 public virtual MemoryStream ReadRawDocument() { | |
42 MemoryStream Buffer = new MemoryStream(); | |
43 byte[] ByteBuffer = new byte[1]; | |
44 int ByteCount = 0; | |
45 while (true) { | |
46 ByteCount = pStream.Read(ByteBuffer, 0, 1); | |
47 if (ByteCount == 0) { | |
48 throw new EndOfStreamException(); | |
49 } else if (ByteBuffer[0] == 0) { | |
50 if (Buffer.Length > 0) break; | |
51 } else { | |
52 Buffer.WriteByte(ByteBuffer[0]); | |
53 } | |
54 } | |
55 Buffer.Flush(); | |
56 Buffer.Seek(0, SeekOrigin.Begin); | |
57 | |
58 return Buffer; | |
59 } | |
60 public virtual XmlDocument ReadDocument() { | |
109
0fc3f42a8555
Small improvements, return TCPSocket in HTTPContext if applicable
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
61 MemoryStream Buffer = ReadRawDocument(); |
0fc3f42a8555
Small improvements, return TCPSocket in HTTPContext if applicable
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
62 XmlDocument Doc = new XmlDocument(); |
0fc3f42a8555
Small improvements, return TCPSocket in HTTPContext if applicable
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
63 try { |
0fc3f42a8555
Small improvements, return TCPSocket in HTTPContext if applicable
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
64 using (XmlReader XMLReader = XmlReader.Create(Buffer, pXMLReaderSettings)) Doc.Load(XMLReader); |
0fc3f42a8555
Small improvements, return TCPSocket in HTTPContext if applicable
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
65 } catch (Exception ex) { |
0fc3f42a8555
Small improvements, return TCPSocket in HTTPContext if applicable
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
66 Buffer.Seek(0, SeekOrigin.Begin); |
0fc3f42a8555
Small improvements, return TCPSocket in HTTPContext if applicable
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
67 throw new IOException("Could not parse XML document: \"" + Encoding.UTF8.GetString(Buffer.ToArray()) + "\"", ex); |
0 | 68 } |
109
0fc3f42a8555
Small improvements, return TCPSocket in HTTPContext if applicable
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
69 return Doc; |
0fc3f42a8555
Small improvements, return TCPSocket in HTTPContext if applicable
Ivo Smits <Ivo@UCIS.nl>
parents:
28
diff
changeset
|
70 } |
0 | 71 |
72 protected virtual void CreateWriter() { | |
73 pWriter = XmlWriter.Create(pStream, WriterSettings); | |
74 } | |
75 protected virtual void CloseWriter() { | |
76 pWriter.Flush(); | |
77 pWriter.Close(); | |
78 pStream.WriteByte(0); | |
79 pStream.Flush(); | |
80 } | |
81 public override void Flush() { | |
82 if (pWriter != null) { | |
83 pWriter.Flush(); | |
84 } else { | |
85 pStream.Flush(); | |
86 } | |
87 } | |
88 public override void Close() { | |
89 if (pWriter != null) { | |
90 pWriter.Close(); | |
91 pWriter = null; | |
92 } | |
93 pStream.Close(); | |
94 } | |
95 | |
96 public void WriteDocument(XmlDocument Document) { | |
97 CreateWriter(); | |
98 Document.WriteTo(pWriter); | |
99 CloseWriter(); | |
100 } | |
101 | |
102 public virtual void WriteRawDocument(string data) { | |
103 byte[] Buffer = WriterSettings.Encoding.GetBytes(data); | |
104 pStream.Write(Buffer, 0, Buffer.Length); | |
105 pStream.WriteByte(0); | |
106 pStream.Flush(); | |
107 } | |
108 | |
109 public override void WriteStartDocument() { | |
110 CreateWriter(); | |
111 pWriter.WriteStartDocument(); | |
112 } | |
113 public override void WriteStartDocument(bool standalone) { | |
114 CreateWriter(); | |
115 pWriter.WriteStartDocument(standalone); | |
116 } | |
117 public override void WriteEndDocument() { | |
118 pWriter.WriteEndDocument(); | |
119 CloseWriter(); | |
120 } | |
121 public void WriteDocumentString(string localName, string value) { | |
122 WriteStartDocument(); | |
123 WriteElementString(localName, value); | |
124 WriteEndDocument(); | |
125 } | |
126 | |
127 public override System.Xml.WriteState WriteState { | |
128 get { | |
129 if (pWriter != null) { | |
130 return pWriter.WriteState; | |
131 } else { | |
132 return WriteState.Start; | |
133 } | |
134 } | |
135 } | |
136 | |
137 public override string LookupPrefix(string ns) { | |
138 return pWriter.LookupPrefix(ns); | |
139 } | |
140 public override void WriteBase64(byte[] buffer, int index, int count) { | |
141 pWriter.WriteBase64(buffer, index, count); | |
142 } | |
143 public override void WriteCData(string text) { | |
144 pWriter.WriteCData(text); | |
145 } | |
146 public override void WriteCharEntity(char ch) { | |
147 pWriter.WriteCharEntity(ch); | |
148 } | |
149 public override void WriteChars(char[] buffer, int index, int count) { | |
150 pWriter.WriteChars(buffer, index, count); | |
151 } | |
152 public override void WriteComment(string text) { | |
153 pWriter.WriteComment(text); | |
154 } | |
155 public override void WriteDocType(string name, string pubid, string sysid, string subset) { | |
156 pWriter.WriteDocType(name, pubid, sysid, subset); | |
157 } | |
158 public override void WriteEndAttribute() { | |
159 pWriter.WriteEndAttribute(); | |
160 } | |
161 public override void WriteEndElement() { | |
162 pWriter.WriteEndElement(); | |
163 } | |
164 public override void WriteEntityRef(string name) { | |
165 pWriter.WriteEntityRef(name); | |
166 } | |
167 public override void WriteFullEndElement() { | |
168 pWriter.WriteFullEndElement(); | |
169 } | |
170 public override void WriteProcessingInstruction(string name, string text) { | |
171 pWriter.WriteProcessingInstruction(name, text); | |
172 } | |
173 public override void WriteRaw(char[] buffer, int index, int count) { | |
174 pWriter.WriteRaw(buffer, index, count); | |
175 } | |
176 public override void WriteRaw(string data) { | |
177 pWriter.WriteRaw(data); | |
178 } | |
179 public override void WriteStartAttribute(string prefix, string localName, string ns) { | |
180 pWriter.WriteStartAttribute(prefix, localName, ns); | |
181 } | |
182 public override void WriteStartElement(string prefix, string localName, string ns) { | |
183 pWriter.WriteStartElement(prefix, localName, ns); | |
184 } | |
185 public override void WriteString(string text) { | |
186 pWriter.WriteString(text); | |
187 } | |
188 public override void WriteSurrogateCharEntity(char lowChar, char highChar) { | |
189 pWriter.WriteSurrogateCharEntity(lowChar, highChar); | |
190 } | |
191 public override void WriteWhitespace(string ws) { | |
192 pWriter.WriteWhitespace(ws); | |
193 } | |
194 } | |
195 } |