Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Configuration / System / Configuration / XmlUtilWriter.cs / 1305376 / XmlUtilWriter.cs
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Configuration {
using System.Configuration.Internal;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Xml;
using System.Net;
//
// A utility class for writing XML to a TextWriter.
//
// When this class is used to copy an XML document that may include a " 0xFFFD)) {
appendCharEntity = true;
}
else {
switch (ch)
{
case '<':
entityRef = "lt";
break;
case '>':
entityRef = "gt";
break;
case '&':
entityRef = "amp";
break;
case '\'':
if (inAttribute && quoteChar == ch) {
entityRef = "apos";
}
break;
case '"':
if (inAttribute && quoteChar == ch) {
entityRef = "quot";
}
break;
case '\n':
case '\r':
appendCharEntity = inAttribute;
break;
default:
break;
}
}
if (appendCharEntity) {
charactersWritten += AppendCharEntity(ch);
}
else if (entityRef != null) {
charactersWritten += AppendEntityRef(entityRef);
}
else {
charactersWritten += Write(ch);
}
}
return charactersWritten;
}
internal int AppendEntityRef(string entityRef) {
Write('&');
Write(entityRef);
Write(';');
return entityRef.Length + 2;
}
internal int AppendCharEntity(char ch) {
string numberToWrite = ((int)ch).ToString("X", CultureInfo.InvariantCulture);
Write('&');
Write('#');
Write('x');
Write(numberToWrite);
Write(';');
return numberToWrite.Length + 4;
}
internal int AppendCData(string cdata) {
Write("");
return cdata.Length + 12;
}
internal int AppendProcessingInstruction(string name, string value) {
Write("");
Write(name);
AppendSpace();
Write(value);
Write("?>");
return name.Length + value.Length + 5;
}
internal int AppendComment(string comment) {
Write("");
return comment.Length + 7;
}
internal int AppendAttributeValue(XmlTextReader reader) {
int charactersWritten = 0;
char quote = reader.QuoteChar;
//
// In !DOCTYPE, quote is '\0' for second public attribute.
// Protect ourselves from writing invalid XML by always
// supplying a valid quote char.
//
if (quote != '"' && quote != '\'') {
quote = '"';
}
charactersWritten += Write(quote);
while (reader.ReadAttributeValue()) {
if (reader.NodeType == XmlNodeType.Text) {
charactersWritten += AppendEscapeXmlString(reader.Value, true, quote);
}
else {
charactersWritten += AppendEntityRef(reader.Name);
}
}
charactersWritten += Write(quote);
return charactersWritten;
}
// Append whitespace, ensuring there is at least one space.
internal int AppendRequiredWhiteSpace(int fromLineNumber, int fromLinePosition, int toLineNumber, int toLinePosition) {
int charactersWritten = AppendWhiteSpace(fromLineNumber, fromLinePosition, toLineNumber, toLinePosition);
if (charactersWritten == 0) {
charactersWritten += AppendSpace();
}
return charactersWritten;
}
// Append whitespce
internal int AppendWhiteSpace(int fromLineNumber, int fromLinePosition, int toLineNumber, int toLinePosition) {
int charactersWritten = 0;
while (fromLineNumber++ < toLineNumber) {
charactersWritten += AppendNewLine();
fromLinePosition = 1;
}
charactersWritten += AppendSpaces(toLinePosition - fromLinePosition);
return charactersWritten;
}
//
// Append indent
// linePosition - starting line position
// indent - number of spaces to indent each unit of depth
// depth - depth to indent
// newLine - insert new line before indent?
//
internal int AppendIndent(int linePosition, int indent, int depth, bool newLine) {
int charactersWritten = 0;
if (newLine) {
charactersWritten += AppendNewLine();
}
int c = (linePosition - 1) + (indent * depth);
charactersWritten += AppendSpaces(c);
return charactersWritten;
}
//
// AppendSpacesToLinePosition
//
// Write spaces up to the line position, taking into account the
// current line position of the writer.
//
internal int AppendSpacesToLinePosition(int linePosition) {
Debug.Assert(_trackPosition, "_trackPosition");
if (linePosition <= 0) {
return 0;
}
int delta = linePosition - _linePosition;
if (delta < 0 && IsLastLineBlank) {
SeekToLineStart();
}
return AppendSpaces(linePosition - _linePosition);
}
//
// Append a new line
//
internal int AppendNewLine() {
return Write(NL);
}
//
// AppendSpaces
//
// Write spaces to the writer provided. Since we do not want waste
// memory by allocating do not use "new String(' ', count)".
//
internal int AppendSpaces(int count) {
int c = count;
while (c > 0) {
if (c >= 8) {
Write(SPACES_8);
c -= 8;
}
else if (c >= 4) {
Write(SPACES_4);
c -= 4;
}
else if (c >= 2) {
Write(SPACES_2);
c -= 2;
}
else {
Write(SPACE);
break;
}
}
return (count > 0) ? count : 0;
}
//
// Append a single space character
//
internal int AppendSpace() {
return Write(SPACE);
}
//
// Reset the stream to the beginning of the current blank line.
//
internal void SeekToLineStart() {
Debug.Assert(_isLastLineBlank, "_isLastLineBlank");
RestoreStreamCheckpoint(_lineStartCheckpoint);
}
// Create a checkpoint that can be restored with RestoreStreamCheckpoint().
internal object CreateStreamCheckpoint() {
return new StreamWriterCheckpoint(this);
}
// Restore the writer state that was recorded with CreateStreamCheckpoint().
internal void RestoreStreamCheckpoint(object o) {
StreamWriterCheckpoint checkpoint = (StreamWriterCheckpoint) o;
Flush();
_lineNumber = checkpoint._lineNumber;
_linePosition = checkpoint._linePosition;
_isLastLineBlank = checkpoint._isLastLineBlank;
_baseStream.Seek(checkpoint._streamPosition, SeekOrigin.Begin);
_baseStream.SetLength(checkpoint._streamLength);
_baseStream.Flush();
}
// Class that contains the state of the writer and its underlying stream.
private class StreamWriterCheckpoint {
internal int _lineNumber; // line number
internal int _linePosition; // line position
internal bool _isLastLineBlank; // is the last line blank?
internal long _streamLength; // length of the stream
internal long _streamPosition; // position of the stream pointer
internal StreamWriterCheckpoint(XmlUtilWriter writer) {
writer.Flush();
_lineNumber = writer._lineNumber;
_linePosition = writer._linePosition;
_isLastLineBlank = writer._isLastLineBlank;
writer._baseStream.Flush();
_streamPosition = writer._baseStream.Position;
_streamLength = writer._baseStream.Length;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//------------------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//-----------------------------------------------------------------------------
namespace System.Configuration {
using System.Configuration.Internal;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Xml;
using System.Net;
//
// A utility class for writing XML to a TextWriter.
//
// When this class is used to copy an XML document that may include a " 0xFFFD)) {
appendCharEntity = true;
}
else {
switch (ch)
{
case '<':
entityRef = "lt";
break;
case '>':
entityRef = "gt";
break;
case '&':
entityRef = "amp";
break;
case '\'':
if (inAttribute && quoteChar == ch) {
entityRef = "apos";
}
break;
case '"':
if (inAttribute && quoteChar == ch) {
entityRef = "quot";
}
break;
case '\n':
case '\r':
appendCharEntity = inAttribute;
break;
default:
break;
}
}
if (appendCharEntity) {
charactersWritten += AppendCharEntity(ch);
}
else if (entityRef != null) {
charactersWritten += AppendEntityRef(entityRef);
}
else {
charactersWritten += Write(ch);
}
}
return charactersWritten;
}
internal int AppendEntityRef(string entityRef) {
Write('&');
Write(entityRef);
Write(';');
return entityRef.Length + 2;
}
internal int AppendCharEntity(char ch) {
string numberToWrite = ((int)ch).ToString("X", CultureInfo.InvariantCulture);
Write('&');
Write('#');
Write('x');
Write(numberToWrite);
Write(';');
return numberToWrite.Length + 4;
}
internal int AppendCData(string cdata) {
Write("");
return cdata.Length + 12;
}
internal int AppendProcessingInstruction(string name, string value) {
Write("");
Write(name);
AppendSpace();
Write(value);
Write("?>");
return name.Length + value.Length + 5;
}
internal int AppendComment(string comment) {
Write("");
return comment.Length + 7;
}
internal int AppendAttributeValue(XmlTextReader reader) {
int charactersWritten = 0;
char quote = reader.QuoteChar;
//
// In !DOCTYPE, quote is '\0' for second public attribute.
// Protect ourselves from writing invalid XML by always
// supplying a valid quote char.
//
if (quote != '"' && quote != '\'') {
quote = '"';
}
charactersWritten += Write(quote);
while (reader.ReadAttributeValue()) {
if (reader.NodeType == XmlNodeType.Text) {
charactersWritten += AppendEscapeXmlString(reader.Value, true, quote);
}
else {
charactersWritten += AppendEntityRef(reader.Name);
}
}
charactersWritten += Write(quote);
return charactersWritten;
}
// Append whitespace, ensuring there is at least one space.
internal int AppendRequiredWhiteSpace(int fromLineNumber, int fromLinePosition, int toLineNumber, int toLinePosition) {
int charactersWritten = AppendWhiteSpace(fromLineNumber, fromLinePosition, toLineNumber, toLinePosition);
if (charactersWritten == 0) {
charactersWritten += AppendSpace();
}
return charactersWritten;
}
// Append whitespce
internal int AppendWhiteSpace(int fromLineNumber, int fromLinePosition, int toLineNumber, int toLinePosition) {
int charactersWritten = 0;
while (fromLineNumber++ < toLineNumber) {
charactersWritten += AppendNewLine();
fromLinePosition = 1;
}
charactersWritten += AppendSpaces(toLinePosition - fromLinePosition);
return charactersWritten;
}
//
// Append indent
// linePosition - starting line position
// indent - number of spaces to indent each unit of depth
// depth - depth to indent
// newLine - insert new line before indent?
//
internal int AppendIndent(int linePosition, int indent, int depth, bool newLine) {
int charactersWritten = 0;
if (newLine) {
charactersWritten += AppendNewLine();
}
int c = (linePosition - 1) + (indent * depth);
charactersWritten += AppendSpaces(c);
return charactersWritten;
}
//
// AppendSpacesToLinePosition
//
// Write spaces up to the line position, taking into account the
// current line position of the writer.
//
internal int AppendSpacesToLinePosition(int linePosition) {
Debug.Assert(_trackPosition, "_trackPosition");
if (linePosition <= 0) {
return 0;
}
int delta = linePosition - _linePosition;
if (delta < 0 && IsLastLineBlank) {
SeekToLineStart();
}
return AppendSpaces(linePosition - _linePosition);
}
//
// Append a new line
//
internal int AppendNewLine() {
return Write(NL);
}
//
// AppendSpaces
//
// Write spaces to the writer provided. Since we do not want waste
// memory by allocating do not use "new String(' ', count)".
//
internal int AppendSpaces(int count) {
int c = count;
while (c > 0) {
if (c >= 8) {
Write(SPACES_8);
c -= 8;
}
else if (c >= 4) {
Write(SPACES_4);
c -= 4;
}
else if (c >= 2) {
Write(SPACES_2);
c -= 2;
}
else {
Write(SPACE);
break;
}
}
return (count > 0) ? count : 0;
}
//
// Append a single space character
//
internal int AppendSpace() {
return Write(SPACE);
}
//
// Reset the stream to the beginning of the current blank line.
//
internal void SeekToLineStart() {
Debug.Assert(_isLastLineBlank, "_isLastLineBlank");
RestoreStreamCheckpoint(_lineStartCheckpoint);
}
// Create a checkpoint that can be restored with RestoreStreamCheckpoint().
internal object CreateStreamCheckpoint() {
return new StreamWriterCheckpoint(this);
}
// Restore the writer state that was recorded with CreateStreamCheckpoint().
internal void RestoreStreamCheckpoint(object o) {
StreamWriterCheckpoint checkpoint = (StreamWriterCheckpoint) o;
Flush();
_lineNumber = checkpoint._lineNumber;
_linePosition = checkpoint._linePosition;
_isLastLineBlank = checkpoint._isLastLineBlank;
_baseStream.Seek(checkpoint._streamPosition, SeekOrigin.Begin);
_baseStream.SetLength(checkpoint._streamLength);
_baseStream.Flush();
}
// Class that contains the state of the writer and its underlying stream.
private class StreamWriterCheckpoint {
internal int _lineNumber; // line number
internal int _linePosition; // line position
internal bool _isLastLineBlank; // is the last line blank?
internal long _streamLength; // length of the stream
internal long _streamPosition; // position of the stream pointer
internal StreamWriterCheckpoint(XmlUtilWriter writer) {
writer.Flush();
_lineNumber = writer._lineNumber;
_linePosition = writer._linePosition;
_isLastLineBlank = writer._isLastLineBlank;
writer._baseStream.Flush();
_streamPosition = writer._baseStream.Position;
_streamLength = writer._baseStream.Length;
}
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- StylusTouchDevice.cs
- CollectionChangeEventArgs.cs
- CommandValueSerializer.cs
- TypeSemantics.cs
- BrushConverter.cs
- SmtpFailedRecipientException.cs
- CompositeActivityCodeGenerator.cs
- ProcessStartInfo.cs
- ServiceNameCollection.cs
- WindowsEditBox.cs
- Message.cs
- StandardMenuStripVerb.cs
- TemplateColumn.cs
- Msec.cs
- TabControlToolboxItem.cs
- SplineQuaternionKeyFrame.cs
- SudsWriter.cs
- CanonicalFontFamilyReference.cs
- TextDecorationUnitValidation.cs
- _NegoStream.cs
- XamlTypeMapper.cs
- PartialToken.cs
- List.cs
- TypeGeneratedEventArgs.cs
- XsltContext.cs
- ClientTargetSection.cs
- WriteStateInfoBase.cs
- ApplicationFileParser.cs
- DictationGrammar.cs
- Parameter.cs
- ControlValuePropertyAttribute.cs
- SqlTriggerContext.cs
- Pair.cs
- OdbcUtils.cs
- EDesignUtil.cs
- LicenseContext.cs
- CommandID.cs
- FieldNameLookup.cs
- ScriptManager.cs
- SessionStateUtil.cs
- PropertyEntry.cs
- SharedDp.cs
- PolyQuadraticBezierSegment.cs
- ResolveMatchesApril2005.cs
- ExtensionQuery.cs
- ViewBase.cs
- ColorAnimationBase.cs
- XmlKeywords.cs
- PrimitiveType.cs
- NameValuePermission.cs
- HostUtils.cs
- RegexCode.cs
- PropertyEmitter.cs
- PieceDirectory.cs
- BamlLocalizationDictionary.cs
- DataTemplate.cs
- XmlKeywords.cs
- RootContext.cs
- Stylus.cs
- IIS7WorkerRequest.cs
- HttpCachePolicyElement.cs
- TypedRowGenerator.cs
- Application.cs
- DtrList.cs
- AsmxEndpointPickerExtension.cs
- Int64AnimationUsingKeyFrames.cs
- SystemIcmpV4Statistics.cs
- UnmanagedMarshal.cs
- ILGenerator.cs
- AlgoModule.cs
- CachedCompositeFamily.cs
- MarkedHighlightComponent.cs
- InfocardClientCredentials.cs
- WebDisplayNameAttribute.cs
- Socket.cs
- SingleObjectCollection.cs
- NextPreviousPagerField.cs
- DataTablePropertyDescriptor.cs
- ArraySubsetEnumerator.cs
- Collection.cs
- XmlEnumAttribute.cs
- SqlRecordBuffer.cs
- PointAnimation.cs
- SqlDataSourceConfigureSortForm.cs
- CategoryNameCollection.cs
- StandardToolWindows.cs
- X509ChainPolicy.cs
- WrappingXamlSchemaContext.cs
- FieldReference.cs
- _PooledStream.cs
- _LazyAsyncResult.cs
- Buffer.cs
- NullableBoolConverter.cs
- AnimationClockResource.cs
- FieldTemplateFactory.cs
- TypeToArgumentTypeConverter.cs
- XmlBinaryReader.cs
- WebPartConnectionCollection.cs
- SoapFormatter.cs
- SQLBoolean.cs