[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Dotgnu-pnet-commits] CVS: pnetlib/System.Xml XmlStreamReader.cs,NONE,1.
From: |
Adam Ballai <address@hidden> |
Subject: |
[Dotgnu-pnet-commits] CVS: pnetlib/System.Xml XmlStreamReader.cs,NONE,1.1 XmlTextReader.cs,1.36,1.37 |
Date: |
Sun, 09 Mar 2003 19:10:02 -0500 |
Update of /cvsroot/dotgnu-pnet/pnetlib/System.Xml
In directory subversions:/tmp/cvs-serv11477/System.Xml
Modified Files:
XmlTextReader.cs
Added Files:
XmlStreamReader.cs
Log Message:
Put in place UCS4 encoding support
--- NEW FILE ---
/*
* XmlStreamReader.cs - Implementation of the "System.Xml.XmlStreamReader"
class.
*
* Copyright (C) 2001, 2002 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
namespace System.Xml
{
using System;
using System.Globalization;
using System.Text;
using System.IO;
internal class XmlStreamReader : TextReader
{
#if !ECMA_COMPAT
// The null stream reader.
public new static readonly XmlStreamReader Null
= new XmlStreamReader(Stream.Null);
#endif
// Default and minimum buffer sizes to use for streams.
private const int STREAM_BUFSIZ = 1024;
private const int FILE_BUFSIZ = 4096;
private const int MIN_BUFSIZ = 128;
// Internal state.
private Stream stream;
private Encoding encoding;
private Decoder decoder;
private int bufferSize;
private byte[] inBuffer;
private int inBufferPosn;
private int inBufferLen;
private char[] outBuffer;
private int outBufferPosn;
private int outBufferLen;
private bool sawEOF;
// Constructors that are based on a stream.
public XmlStreamReader(Stream stream)
: this(stream, Encoding.UTF8, true, STREAM_BUFSIZ) {}
public XmlStreamReader(Stream stream, bool
detectEncodingFromByteOrderMarks)
: this(stream, Encoding.UTF8,
detectEncodingFromByteOrderMarks,
STREAM_BUFSIZ) {}
public XmlStreamReader(Stream stream, Encoding encoding)
: this(stream, encoding, true, STREAM_BUFSIZ) {}
public XmlStreamReader(Stream stream, Encoding encoding,
bool
detectEncodingFromByteOrderMarks)
: this(stream, encoding,
detectEncodingFromByteOrderMarks,
STREAM_BUFSIZ) {}
public XmlStreamReader(Stream stream, Encoding encoding,
bool
detectEncodingFromByteOrderMarks,
int bufferSize)
{
// Validate the parameters.
if(stream == null)
{
throw new
ArgumentNullException("stream");
}
if(encoding == null)
{
throw new
ArgumentNullException("encoding");
}
if(!stream.CanRead)
{
throw new
ArgumentException(S._("IO_NotSupp_Read"));
}
if(bufferSize <= 0)
{
throw new ArgumentOutOfRangeException
("bufferSize",S._("ArgRange_BufferSize"));
}
if(bufferSize < MIN_BUFSIZ)
{
bufferSize = MIN_BUFSIZ;
}
// Initialize this object.
this.stream = stream;
this.encoding = encoding;
this.bufferSize = bufferSize;
this.inBuffer = new byte [bufferSize];
this.inBufferPosn = 0;
this.inBufferLen = 0;
this.outBuffer = new char [bufferSize];
this.outBufferPosn = 0;
this.outBufferLen = 0;
this.sawEOF = false;
// Should we change encodings based on a byte
order mark?
if(detectEncodingFromByteOrderMarks)
{
DetectByteOrder();
}
// Get a decoder for the encoding.
decoder = encoding.GetDecoder();
}
// Constructors that are based on a filename.
public XmlStreamReader(String path)
: this(path, Encoding.UTF8, true, STREAM_BUFSIZ) {}
public XmlStreamReader(String path, bool
detectEncodingFromByteOrderMarks)
: this(path, Encoding.UTF8,
detectEncodingFromByteOrderMarks,
STREAM_BUFSIZ) {}
public XmlStreamReader(String path, Encoding encoding)
: this(path, encoding, true, STREAM_BUFSIZ) {}
public XmlStreamReader(String path, Encoding encoding,
bool
detectEncodingFromByteOrderMarks)
: this(path, encoding,
detectEncodingFromByteOrderMarks,
STREAM_BUFSIZ) {}
public XmlStreamReader(String path, Encoding encoding,
bool
detectEncodingFromByteOrderMarks,
int bufferSize)
{
// Validate the parameters.
if(path == null)
{
throw new ArgumentNullException("path");
}
if(encoding == null)
{
throw new
ArgumentNullException("encoding");
}
if(bufferSize <= 0)
{
throw new ArgumentOutOfRangeException
("bufferSize",
S._("ArgRange_BufferSize"));
}
if(bufferSize < MIN_BUFSIZ)
{
bufferSize = MIN_BUFSIZ;
}
// Attempt to open the file.
Stream stream = new FileStream(path,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
FILE_BUFSIZ);
// Initialize this object.
this.stream = stream;
this.encoding = encoding;
this.bufferSize = bufferSize;
this.inBuffer = new byte [bufferSize];
this.inBufferPosn = 0;
this.inBufferLen = 0;
this.outBuffer = new char [bufferSize];
this.outBufferPosn = 0;
this.outBufferLen = 0;
this.sawEOF = false;
// Should we change encodings based on a byte
order mark?
if(detectEncodingFromByteOrderMarks)
{
DetectByteOrder();
}
// Get a decoder for the encoding.
decoder = encoding.GetDecoder();
}
// Destructor.
~XmlStreamReader()
{
Dispose(false);
}
// Detect the byte order by inspecting the first few bytes.
private void DetectByteOrder()
{
// Pre-read the first full buffer of input data.
inBufferLen = stream.Read(inBuffer, 0,
bufferSize);
if(inBufferLen <= 0)
{
sawEOF = true;
inBufferLen = 0;
}
// Check for recognized byte order marks.
if(inBufferLen >= 2 &&
inBuffer[0] == 0xFF &&
inBuffer[1] == 0xFE)
{
// Little-endian UTF-16.
encoding = Encoding.Unicode;
inBufferPosn = 2;
}
else if(inBufferLen >= 2 &&
inBuffer[0] == 0xFE &&
inBuffer[1] == 0xFF)
{
// Big-endian UTF-16.
encoding = Encoding.BigEndianUnicode;
inBufferPosn = 2;
}
else if(inBufferLen >= 3 &&
inBuffer[0] == 0xEF &&
inBuffer[1] == 0xBB &&
inBuffer[2] == 0xBF)
{
// UTF-8.
encoding = Encoding.UTF8;
inBufferPosn = 3;
}
}
// Close this stream reader.
public override void Close()
{
Dispose(true);
}
// Discard any data that was buffered by this stream reader.
public void DiscardBufferedData()
{
// Empty the buffers.
inBufferPosn = 0;
inBufferLen = 0;
outBufferPosn = 0;
outBufferLen = 0;
sawEOF = false;
// Create a new decoder. We cannot reuse the
// old one because we have no way to reset the
// state to the default.
decoder = encoding.GetDecoder();
}
// Dispose this stream reader.
protected override void Dispose(bool disposing)
{
if(stream != null)
{
stream.Close();
stream = null;
}
inBuffer = null;
inBufferPosn = 0;
inBufferLen = 0;
outBuffer = null;
outBufferPosn = 0;
outBufferLen = 0;
bufferSize = 0;
sawEOF = true;
base.Dispose(disposing);
}
// Read byte data from the stream and convert it into characters.
private void ReadChars()
{
int len, outLen;
while(outBufferPosn >= outBufferLen && !sawEOF)
{
// Move the previous left-over buffer
contents down.
if((inBufferLen - inBufferPosn) <
bufferSize)
{
if(inBufferPosn < inBufferLen)
{
Array.Copy
(inBuffer,
inBufferPosn,
inBuffer, 0,
inBufferLen - inBufferPosn);
inBufferLen -=
inBufferPosn;
}
else
{
inBufferLen = 0;
}
inBufferPosn = 0;
// Read new bytes into the
buffer.
if(stream == null)
{
throw new
IOException(S._("IO_StreamClosed"));
}
len = stream.Read(inBuffer,
inBufferPosn,
bufferSize - inBufferPosn);
if(len <= 0)
{
sawEOF = true;
}
else
{
inBufferLen += len;
}
}
// Determine the maximum number of
bytes that
// we can afford to convert into
characters.
len =
encoding.GetMaxByteCount(bufferSize);
if(len > inBufferLen)
{
len = inBufferLen;
}
// Convert the bytes into characters.
outLen = decoder.GetChars(inBuffer,
inBufferPosn, len,
outBuffer, 0);
outBufferPosn = 0;
outBufferLen = outLen;
inBufferPosn += len;
}
}
// Peek at the next character from this stream reader.
public override int Peek()
{
if(outBufferPosn < outBufferLen)
{
// We already have a character
available.
return (int)(outBuffer[outBufferPosn]);
}
else
{
// Read another buffer of characters.
ReadChars();
if(outBufferPosn < outBufferLen)
{
return
(int)(outBuffer[outBufferPosn]);
}
else
{
return -1;
}
}
}
// Read a single character from this stream reader.
public override int Read()
{
if(outBufferPosn < outBufferLen)
{
// We already have a character
available.
return
(int)(outBuffer[outBufferPosn++]);
}
else
{
// Read another buffer of characters.
ReadChars();
if(outBufferPosn < outBufferLen)
{
return
(int)(outBuffer[outBufferPosn++]);
}
else
{
return -1;
}
}
}
// Read a buffer of characters from this stream reader.
public override int Read(char[] buffer, int index, int count)
{
// Validate the parameters.
if(buffer == null)
{
throw new
ArgumentNullException("buffer");
}
if(index < 0)
{
throw new ArgumentOutOfRangeException
("index",
S._("ArgRange_Array"));
}
if(count < 0)
{
throw new ArgumentOutOfRangeException
("count",
S._("ArgRange_Array"));
}
if((buffer.Length - index) < count)
{
throw new ArgumentException
(S._("Arg_InvalidArrayRange"));
}
// Read data from the input stream into the
buffer.
int len = 0;
int templen;
while(count > 0)
{
// Re-fill the character buffer if
necessary.
if(outBufferPosn >= outBufferLen)
{
ReadChars();
if(outBufferPosn >=
outBufferLen)
{
break;
}
}
// Copy data to the result buffer.
templen = outBufferLen - outBufferPosn;
if(templen > count)
{
templen = count;
}
Array.Copy(outBuffer, outBufferPosn,
buffer, index,
templen);
outBufferPosn += templen;
index += templen;
count -= templen;
len += templen;
}
return len;
}
// Read a line of characters from this stream reader.
public override String ReadLine()
{
StringBuilder builder = new StringBuilder();
int ch;
for(;;)
{
// Re-fill the character buffer if
necessary.
if(outBufferPosn >= outBufferLen)
{
ReadChars();
if(outBufferPosn >=
outBufferLen)
{
break;
}
}
// Process characters until we reach a
line terminator.
while(outBufferPosn < outBufferLen)
{
ch = outBuffer[outBufferPosn++];
if(ch == 13)
{
// Peek at the next
character to determine
// if this is a CRLF or
CR line terminator.
if(outBufferPosn >=
outBufferLen)
{
ReadChars();
}
if(outBufferPosn <
outBufferLen &&
outBuffer[outBufferPosn] == '\u000A')
{
++outBufferPosn;
}
return
builder.ToString();
}
else if(ch == 10)
{
// This is an LF line
terminator.
return
builder.ToString();
}
else
{
builder.Append((char)ch);
}
}
}
if(builder.Length != 0)
{
return builder.ToString();
}
else
{
return null;
}
}
// Read the entire contents of this stream reader until EOF.
public override String ReadToEnd()
{
StringBuilder builder = new StringBuilder();
for(;;)
{
// Re-fill the character buffer if
necessary.
if(outBufferPosn >= outBufferLen)
{
ReadChars();
if(outBufferPosn >=
outBufferLen)
{
break;
}
}
// Append the character buffer to the
builder.
builder.Append(outBuffer, outBufferPosn,
outBufferLen
- outBufferPosn);
outBufferPosn = outBufferLen;
}
if(builder.Length != 0)
{
return builder.ToString();
}
else
{
return String.Empty;
}
}
// Get the base stream underlying this stream reader.
public virtual Stream BaseStream
{
get
{
return stream;
}
}
// Get the current encoding in use by this stream reader.
public virtual Encoding CurrentEncoding
{
get
{
return encoding;
}
}
}; // class XmlStreamReader
}; // namespace System.Xml
Index: XmlTextReader.cs
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnetlib/System.Xml/XmlTextReader.cs,v
retrieving revision 1.36
retrieving revision 1.37
diff -C2 -r1.36 -r1.37
*** XmlTextReader.cs 5 Mar 2003 12:42:47 -0000 1.36
--- XmlTextReader.cs 10 Mar 2003 00:10:00 -0000 1.37
***************
*** 135,139 ****
baseURI = String.Empty;
}
! StreamReader sr = new StreamReader(input, true);
encoding = sr.CurrentEncoding;
reader = sr;
--- 135,139 ----
baseURI = String.Empty;
}
! XmlStreamReader sr = new XmlStreamReader(input,
true);
encoding = sr.CurrentEncoding;
reader = sr;
***************
*** 168,175 ****
if(input is StreamReader)
{
! encoding =
((StreamReader)input).CurrentEncoding;
}
}
- [TODO]
public XmlTextReader(Stream xmlFragment, XmlNodeType fragType,
XmlParserContext context)
--- 168,174 ----
if(input is StreamReader)
{
! encoding =
((XmlStreamReader)input).CurrentEncoding;
}
}
public XmlTextReader(Stream xmlFragment, XmlNodeType fragType,
XmlParserContext context)
***************
*** 190,201 ****
{
baseURI = String.Empty;
! // TODO: find byte order mark
! // // EF BB BF = utf8
! // // FE FF = utf16/ucs-2 little endian
! // // FF FE = utf16/ucs-2, big endian
! // // FF FE 00 00 = UTF-32/ucs-4,
little endian
! // // 00 00 FE FF = UTC-32/UCS-4, big
endian
! // else ...
! // encoding =
System.Text.UT8Encoding(true);
}
else
--- 189,194 ----
{
baseURI = String.Empty;
! XmlStreamReader sr = new
XmlStreamReader(xmlFragment, true);
! encoding = sr.CurrentEncoding;
}
else
***************
*** 210,214 ****
}
- [TODO]
public XmlTextReader(String xmlFragment, XmlNodeType fragType,
XmlParserContext context)
--- 203,206 ----
***************
*** 229,237 ****
{
baseURI = String.Empty;
! // TODO: find byte order mark
! // else ...
! // encoding =
System.Text.UT8Encoding(true);
! StringReader sr = new
StringReader(xmlFragment);
! encoding = Encoding.ASCII;
reader = sr;
}
--- 221,226 ----
{
baseURI = String.Empty;
! XmlStreamReader sr = new
XmlStreamReader(xmlFragment);
! encoding = sr.CurrentEncoding;
reader = sr;
}
***************
*** 239,248 ****
{
baseURI = context.BaseURI;
- encoding = context.Encoding;
xmlLang = context.XmlLang;
xmlSpace = context.XmlSpace;
contextSupport = true;
! // TODO: figure out how Encoding works
for this thing.
! StringReader sr = new
StringReader(xmlFragment);
reader = sr;
}
--- 228,243 ----
{
baseURI = context.BaseURI;
xmlLang = context.XmlLang;
xmlSpace = context.XmlSpace;
contextSupport = true;
! XmlStreamReader sr = new
XmlStreamReader(xmlFragment);
! if(context.Encoding == null)
! {
! encoding = sr.CurrentEncoding;
! }
! else
! {
! encoding = context.Encoding;
! }
reader = sr;
}
***************
*** 272,276 ****
baseURI = String.Empty;
}
! StreamReader sr = new StreamReader(url, true);
encoding = sr.CurrentEncoding;
reader = sr;
--- 267,271 ----
baseURI = String.Empty;
}
! XmlStreamReader sr = new XmlStreamReader(url,
true);
encoding = sr.CurrentEncoding;
reader = sr;
***************
*** 343,347 ****
{
String tmp = reader.ReadToEnd();
- Console.WriteLine(tmp);
StringReader s = new StringReader(tmp);
Close();
--- 338,341 ----
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Dotgnu-pnet-commits] CVS: pnetlib/System.Xml XmlStreamReader.cs,NONE,1.1 XmlTextReader.cs,1.36,1.37,
Adam Ballai <address@hidden> <=