Subversion Repository Public Repository

WOX2

@ 14
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
299
300
301
302
303
using System;
using System.IO;
using System.Text;
using System.Xml;

/// <header>
/// <wox>
///     <version>1.0</version>
///     <author>Carlos R. Jaimez Gonzalez</author>
///     <author>Simon M. Lucas</author>
///     <site>http://woxserializer.sourceforge.net/</site>
/// </wox>
/// <wox>
///     <version>1.5</version>
///     <author>Steven M Lewis</author>
/// </wox>
/// <wox>
///     <version>2.0</version>
///     <author>George A Smith</author>
///     <svn>http://woxserializer.sourceforge.net/</svn>
///     <note>XML form for vs 2 is more compact and therefor incompatible with vs 1</note>
/// </wox>
/// </header>

namespace wox.serial
{
    public interface XMLreader : IDisposable
    {
        /// <summary>
        /// Advances the Cursor to the NEXT Start Element.
        /// Note: on creation, an XMLreader is positioned BEFORE any Elements / XML nodes
        /// </summary>
        /// <returns>true if found a Start Element</returns>
        bool ReadStartElement();

        bool HasAttributesOrElementText();

        /// <summary>
        /// Create a Subtree reader, which will an XMLreader that is positioned just before the currently read Start Element.
        /// As the next ReadStartElement would fetch the last read element "as" the root element of this XMLreader, the behavior
        /// is the same as a regular new XMLreader.
        /// Note: After the End Element of the fax root element, ReadStartElement will return false.
        /// </summary>
        XMLreader CreateSubtreeReader();

        string GetAttribute( string pName );

        string ReadElementText();
    }

    public interface XMLwriter : IDisposable
    {
        void WriteStartElement( string pName );

        void WriteAttributeString( string pName, string pValue );

        void WriteElementText( string pElementText );

        void WriteEndElement();
    }

    public class XMLreaderImpl : XMLreader
    {
        private readonly XmlReader mReader;

        public XMLreaderImpl( TextReader pReader )
        {
            mReader = new XmlTextReader( pReader )
                          {
                              WhitespaceHandling = WhitespaceHandling.None
                          };
        }

        private XMLreaderImpl( XmlReader pReader )
        {
            mReader = pReader;
        }

        public XMLreader CreateSubtreeReader()
        {
            return new XMLreaderImpl( mReader.ReadSubtree() );
        }

        public bool ReadStartElement()
        {
            while ( mReader.Read() )
            {
                if ( mReader.NodeType == XmlNodeType.Element )
                {
                    return true;
                }
            }
            return false;
        }

        public bool HasAttributesOrElementText()
        {
            return mReader.HasAttributes || !mReader.IsEmptyElement;
        }

        public string GetAttribute( string pName )
        {
            return XMLutils.DecodeText( mReader.GetAttribute( pName ) );
        }

        public string ReadElementText()
        {
            return XMLutils.DecodeText( mReader.ReadString() ?? "" );
        }

        public void Dispose()
        {
            try
            {
                mReader.Close();
            }
            catch ( Exception )
            {
                // Whatever!
            }
        }
    }

    public class XMLwriterImpl : XMLwriter
    {
        private readonly XmlTextWriter mWriter;

        public XMLwriterImpl( TextWriter pWriter, bool pPrettyPrint )
        {
            mWriter = new XmlTextWriter( pWriter );

            if ( pPrettyPrint )
            {
                mWriter.Formatting = Formatting.Indented;
            }
        }

        public void WriteStartElement( string pName )
        {
            mWriter.WriteStartElement( pName );
        }

        public void WriteAttributeString( string pName, string pValue )
        {
            mWriter.WriteAttributeString( pName, XMLutils.EncodeText( pValue ) );
        }

        public void WriteElementText( string pElementText )
        {
            mWriter.WriteString( XMLutils.EncodeText( pElementText ) );
        }

        public void WriteEndElement()
        {
            mWriter.WriteEndElement();
        }

        public void Dispose()
        {
            try
            {
                mWriter.Close();
            }
            catch ( Exception )
            {
                // Whatever!
            }
        }
    }

    public class XMLutils
    {
        public static string EncodeText( string pText )
        {
            if ( pText == null )
            {
                return null;
            }
            StringBuilder zSB = new StringBuilder();
            CharSource zSource = new CharSource( pText );
            for ( int c; -1 != (c = zSource.Get()); )
            {
                switch ( c )
                {
                    case '\\':
                        zSB.Append( '\\' );
                        zSB.Append( '\\' );
                        break;
                    case '\n':
                        zSB.Append( '\\' );
                        zSB.Append( 'n' );
                        break;
                    case '\r':
                        zSB.Append( '\\' );
                        zSB.Append( 'r' );
                        break;
                    case '\t':
                        zSB.Append( '\\' );
                        zSB.Append( 't' );
                        break;
                    default:
                        if ( (' ' <= c) && (c < 127) )
                        {
                            zSB.Append( (char) c );
                            break;
                        }
                        zSB.Append( '\\' );
                        zSB.Append( 'x' );
                        zSB.Append( ToHexDigit( c >> 24 ) );
                        zSB.Append( ToHexDigit( c >> 16 ) );
                        zSB.Append( ToHexDigit( c >> 8 ) );
                        zSB.Append( ToHexDigit( c ) );
                        break;
                }
            }
            return zSB.ToString();
        }

        public static string DecodeText( string pText )
        {
            if ( pText == null )
            {
                return null;
            }
            StringBuilder zSB = new StringBuilder();
            CharSource zSource = new CharSource( pText );
            for ( int c; -1 != (c = zSource.Get()); )
            {
                if ( (c < ' ') || (127 <= c) )
                {
                    throw new ArgumentException( "Decoding Error: Char[" + zSource.GetLastFrom() + "] Not between ' ' & Ascii 126 (inclusive)" );
                }
                if ( c != '\\' )
                {
                    zSB.Append( (char) c );
                    continue;
                }
                if ( -1 == (c = zSource.Get()) )
                {
                    throw new ArgumentException( "Decoding Error: No Char after escape \\" );
                }
                switch ( c )
                {
                    case '\\':
                        zSB.Append( (char) c );
                        break;
                    case 'n':
                        zSB.Append( '\n' );
                        break;
                    case 'r':
                        zSB.Append( '\r' );
                        break;
                    case 't':
                        zSB.Append( '\t' );
                        break;
                    case 'x':
                        int c1 = zSource.Get();
                        int c2 = zSource.Get();
                        int c3 = zSource.Get();
                        int c4 = zSource.Get();
                        if ( c4 == -1 )
                        {
                            throw new ArgumentException( "Decoding Error: Not enough Chars after \\x" );
                        }
                        try
                        {
                            int zChar = (FromHexDigit( (char) c1 ) << 24) + //
                                        (FromHexDigit( (char) c2 ) << 16) + //
                                        (FromHexDigit( (char) c3 ) << 8) + //
                                        FromHexDigit( (char) c4 );
                            zSB.Append( (char) zChar );
                        }
                        catch ( ArgumentException e )
                        {
                            throw new ArgumentException( "Decoding Error: After \\x expected 4 hex digits: " + e.Message );
                        }
                        break;
                    default:
                        throw new ArgumentException( "Decoding Error: Invalid Char '" + c + "' after escape \\" );
                }
            }
            return zSB.ToString();
        }

        public static char ToHexDigit( int pZeroTo15 )
        {
            pZeroTo15 &= 15;
            return (pZeroTo15 < 0) || (15 < pZeroTo15) ? '?' : HEX[pZeroTo15];
        }

        public static int FromHexDigit( char pHex )
        {
            int rv = HEX.IndexOf( pHex );
            if ( rv == -1 )
            {
                throw new ArgumentException( "Not a Hex digit '" + pHex + "': " + (int) pHex );
            }
            return rv;
        }

        private const String HEX = "0123456789ABCDEF";
    }
}

Commits for WOX2/trunk/CSharp/src/wox/serial/XML.cs

Diff revisions: vs.
Revision Author Commited Message
14 Diff Diff GeorgeS picture GeorgeS Fri 19 Feb, 2010 19:15:03 +0000
3 GeorgeS picture GeorgeS Thu 04 Feb, 2010 23:53:47 +0000