internal XmlWriter CreateWriter(TextWriter output) { if (output == null) { throw new ArgumentNullException(nameof(output)); } XmlWriter writer; // create raw writer if (this.Indent) { writer = new XmlEncodedRawTextWriterIndent(output, this); } else { writer = new XmlEncodedRawTextWriter(output, this); } // wrap with well-formed writer writer = new XmlWellFormedWriter(writer, this); if (_useAsync) { writer = new XmlAsyncCheckWriter(writer); } return(writer); }
// // Static methods for creating writers // // Creates an XmlWriter for writing into the provided file. public static XmlWriter Create(string outputFileName) { if (outputFileName == null) { throw new ArgumentNullException(nameof(outputFileName)); } // Avoid using XmlWriter.Create(string, XmlReaderSettings), as it references a lot of types // that then can't be trimmed away. var fs = new FileStream(outputFileName, FileMode.Create, FileAccess.Write, FileShare.Read); try { var settings = new XmlWriterSettings() { CloseOutput = true }; XmlWriter writer = new XmlEncodedRawTextWriter(fs, settings); return(new XmlWellFormedWriter(writer, settings)); } catch { fs.Dispose(); throw; } }
private static XmlWriter CreateWriterImpl(TextWriter output, XmlWriterSettings settings) { Debug.Assert(output != null); Debug.Assert(settings != null); XmlWriter writer; switch (settings.OutputMethod) { case XmlOutputMethod.Xml: if (settings.Indent) { writer = new XmlEncodedRawTextWriterIndent(output, settings); } else { writer = new XmlEncodedRawTextWriter(output, settings); } break; case XmlOutputMethod.Html: if (settings.Indent) { writer = new HtmlEncodedRawTextWriterIndent(output, settings); } else { writer = new HtmlEncodedRawTextWriter(output, settings); } break; case XmlOutputMethod.Text: writer = new TextEncodedRawTextWriter(output, settings); break; case XmlOutputMethod.AutoDetect: writer = new XmlAutoDetectWriter(output, settings); break; default: Debug.Assert(false, "Invalid XmlOutputMethod setting."); return(null); } // Wrap with Xslt/XQuery specific writer if needed; // XmlOutputMethod.AutoDetect writer does this lazily when it creates the underlying Xml or Html writer. if (settings.OutputMethod != XmlOutputMethod.AutoDetect) { if (settings.IsQuerySpecific) { // Create QueryOutputWriter if CData sections or DocType need to be tracked writer = new QueryOutputWriter((XmlRawWriter)writer, settings); } } writer = new XmlWellFormedWriter(writer, settings); return(writer); }
// Creates an XmlWriter for writing into the provided TextWriter. public static XmlWriter Create(TextWriter output) { if (output == null) { throw new ArgumentNullException(nameof(output)); } // Avoid using XmlWriter.Create(TextWriter, XmlReaderSettings), as it references a lot of types // that then can't be trimmed away. XmlWriterSettings settings = XmlWriterSettings.s_defaultWriterSettings; XmlWriter writer = new XmlEncodedRawTextWriter(output, settings); return(new XmlWellFormedWriter(writer, settings)); }
// // Internal properties // internal XmlWriter CreateWriter(Stream output) { if (output == null) { throw new ArgumentNullException(nameof(output)); } XmlWriter writer; // create raw writer Debug.Assert(Encoding.UTF8.WebName == "utf-8"); if (this.Encoding.WebName == "utf-8") { // Encoding.CodePage is not supported in Silverlight // create raw UTF-8 writer if (this.Indent) { writer = new XmlUtf8RawTextWriterIndent(output, this); } else { writer = new XmlUtf8RawTextWriter(output, this); } } else { // create raw writer for other encodings if (this.Indent) { writer = new XmlEncodedRawTextWriterIndent(output, this); } else { writer = new XmlEncodedRawTextWriter(output, this); } } // wrap with well-formed writer writer = new XmlWellFormedWriter(writer, this); if (_useAsync) { writer = new XmlAsyncCheckWriter(writer); } return(writer); }
internal XmlWriter CreateWriter(TextWriter output) { XmlWriter writer; if (output == null) { throw new ArgumentNullException("output"); } switch (this.OutputMethod) { case XmlOutputMethod.Xml: if (!this.Indent) { writer = new XmlEncodedRawTextWriter(output, this); break; } writer = new XmlEncodedRawTextWriterIndent(output, this); break; case XmlOutputMethod.Html: if (!this.Indent) { writer = new HtmlEncodedRawTextWriter(output, this); break; } writer = new HtmlEncodedRawTextWriterIndent(output, this); break; case XmlOutputMethod.Text: writer = new TextEncodedRawTextWriter(output, this); break; case XmlOutputMethod.AutoDetect: writer = new XmlAutoDetectWriter(output, this); break; default: return(null); } if ((this.OutputMethod != XmlOutputMethod.AutoDetect) && this.IsQuerySpecific) { writer = new QueryOutputWriter((XmlRawWriter)writer, this); } return(new XmlWellFormedWriter(writer, this)); }
internal XmlWriter CreateWriter(TextWriter output) { if (output == null) { throw new ArgumentNullException(nameof(output)); } XmlWriter writer; // create raw writer switch (this.OutputMethod) { case XmlOutputMethod.Xml: if (this.Indent) { writer = new XmlEncodedRawTextWriterIndent(output, this); } else { writer = new XmlEncodedRawTextWriter(output, this); } break; case XmlOutputMethod.Html: if (this.Indent) { writer = new HtmlEncodedRawTextWriterIndent(output, this); } else { writer = new HtmlEncodedRawTextWriter(output, this); } break; case XmlOutputMethod.Text: writer = new TextEncodedRawTextWriter(output, this); break; case XmlOutputMethod.AutoDetect: writer = new XmlAutoDetectWriter(output, this); break; default: Debug.Fail("Invalid XmlOutputMethod setting."); return(null); } // XmlOutputMethod.AutoDetect writer does this lazily when it creates the underlying Xml or Html writer. if (this.OutputMethod != XmlOutputMethod.AutoDetect) { if (this.IsQuerySpecific) { // Create QueryOutputWriter if CData sections or DocType need to be tracked writer = new QueryOutputWriter((XmlRawWriter)writer, this); } } // wrap with well-formed writer writer = new XmlWellFormedWriter(writer, this); if (_useAsync) { writer = new XmlAsyncCheckWriter(writer); } return(writer); }
internal XmlWriter CreateWriter(Stream output) { if (output == null) { throw new ArgumentNullException(nameof(output)); } XmlWriter writer; // create raw writer Debug.Assert(Encoding.UTF8.WebName == "utf-8"); if (this.Encoding.WebName == "utf-8") { // Encoding.CodePage is not supported in Silverlight // create raw UTF-8 writer switch (this.OutputMethod) { case XmlOutputMethod.Xml: if (this.Indent) { writer = new XmlUtf8RawTextWriterIndent(output, this); } else { writer = new XmlUtf8RawTextWriter(output, this); } break; case XmlOutputMethod.Html: if (this.Indent) { writer = new HtmlUtf8RawTextWriterIndent(output, this); } else { writer = new HtmlUtf8RawTextWriter(output, this); } break; case XmlOutputMethod.Text: writer = new TextUtf8RawTextWriter(output, this); break; case XmlOutputMethod.AutoDetect: writer = new XmlAutoDetectWriter(output, this); break; default: Debug.Fail("Invalid XmlOutputMethod setting."); return(null); } } else { // Otherwise, create a general-purpose writer than can do any encoding switch (this.OutputMethod) { case XmlOutputMethod.Xml: if (this.Indent) { writer = new XmlEncodedRawTextWriterIndent(output, this); } else { writer = new XmlEncodedRawTextWriter(output, this); } break; case XmlOutputMethod.Html: if (this.Indent) { writer = new HtmlEncodedRawTextWriterIndent(output, this); } else { writer = new HtmlEncodedRawTextWriter(output, this); } break; case XmlOutputMethod.Text: writer = new TextEncodedRawTextWriter(output, this); break; case XmlOutputMethod.AutoDetect: writer = new XmlAutoDetectWriter(output, this); break; default: Debug.Fail("Invalid XmlOutputMethod setting."); return(null); } } // Wrap with Xslt/XQuery specific writer if needed; // XmlOutputMethod.AutoDetect writer does this lazily when it creates the underlying Xml or Html writer. if (this.OutputMethod != XmlOutputMethod.AutoDetect) { if (this.IsQuerySpecific) { // Create QueryOutputWriter if CData sections or DocType need to be tracked writer = new QueryOutputWriter((XmlRawWriter)writer, this); } } // wrap with well-formed writer writer = new XmlWellFormedWriter(writer, this); if (_useAsync) { writer = new XmlAsyncCheckWriter(writer); } return(writer); }
private unsafe void WriteUriAttributeText(char *pSrc, char *pSrcEnd) { if (endsWithAmpersand) { if (pSrcEnd - pSrc > 0 && pSrc[0] != '{') { OutputRestAmps(); } this.endsWithAmpersand = false; } fixed(char *pDstBegin = bufChars) { char *pDst = pDstBegin + this.bufPos; char ch = (char)0; for (;;) { char *pDstEnd = pDst + (pSrcEnd - pSrc); if (pDstEnd > pDstBegin + bufLen) { pDstEnd = pDstBegin + bufLen; } while (pDst < pDstEnd && (((xmlCharType.charProperties[(ch = *pSrc)] & XmlCharType.fAttrValue) != 0) && ch < 0x80)) { *pDst++ = (char)ch; pSrc++; } Debug.Assert(pSrc <= pSrcEnd); // end of value if (pSrc >= pSrcEnd) { break; } // end of buffer if (pDst >= pDstEnd) { bufPos = (int)(pDst - pDstBegin); FlushBuffer(); pDst = pDstBegin + 1; continue; } // some character needs to be escaped switch (ch) { case '&': if (pSrc + 1 == pSrcEnd) { endsWithAmpersand = true; } else if (pSrc[1] != '{') { pDst = XmlEncodedRawTextWriter.AmpEntity(pDst); break; } *pDst++ = (char)ch; break; case '"': pDst = QuoteEntity(pDst); break; case '<': case '>': case '\'': case (char)0x9: *pDst++ = (char)ch; break; case (char)0xD: // do not normalize new lines in attributes - just escape them pDst = CarriageReturnEntity(pDst); break; case (char)0xA: // do not normalize new lines in attributes - just escape them pDst = LineFeedEntity(pDst); break; default: const string hexDigits = "0123456789ABCDEF"; fixed(byte *pUriEscapingBuffer = uriEscapingBuffer) { byte *pByte = pUriEscapingBuffer; byte *pEnd = pByte; XmlUtf8RawTextWriter.CharToUTF8(ref pSrc, pSrcEnd, ref pEnd); while (pByte < pEnd) { *pDst++ = (char)'%'; *pDst++ = (char)hexDigits[*pByte >> 4]; *pDst++ = (char)hexDigits[*pByte & 0xF]; pByte++; } } continue; } pSrc++; } bufPos = (int)(pDst - pDstBegin); } }
// // &{ split cases // 1). HtmlAttributeText("a&"); // HtmlAttributeText("{b}"); // // 2). HtmlAttributeText("a&"); // EndAttribute(); // 3).split with Flush by the user // HtmlAttributeText("a&"); // FlushBuffer(); // HtmlAttributeText("{b}"); // // Solutions: // case 1)hold the & output as & // if the next income character is {, output { // else output amp; // private unsafe void WriteHtmlAttributeText(char *pSrc, char *pSrcEnd) { if (endsWithAmpersand) { if (pSrcEnd - pSrc > 0 && pSrc[0] != '{') { OutputRestAmps(); } endsWithAmpersand = false; } fixed(char *pDstBegin = bufChars) { char *pDst = pDstBegin + this.bufPos; char ch = (char)0; for (;;) { char *pDstEnd = pDst + (pSrcEnd - pSrc); if (pDstEnd > pDstBegin + bufLen) { pDstEnd = pDstBegin + bufLen; } while (pDst < pDstEnd && (((xmlCharType.charProperties[(ch = *pSrc)] & XmlCharType.fAttrValue) != 0))) { *pDst++ = (char)ch; pSrc++; } Debug.Assert(pSrc <= pSrcEnd); // end of value if (pSrc >= pSrcEnd) { break; } // end of buffer if (pDst >= pDstEnd) { bufPos = (int)(pDst - pDstBegin); FlushBuffer(); pDst = pDstBegin + 1; continue; } // some character needs to be escaped switch (ch) { case '&': if (pSrc + 1 == pSrcEnd) { endsWithAmpersand = true; } else if (pSrc[1] != '{') { pDst = XmlEncodedRawTextWriter.AmpEntity(pDst); break; } *pDst++ = (char)ch; break; case '"': pDst = QuoteEntity(pDst); break; case '<': case '>': case '\'': case (char)0x9: *pDst++ = (char)ch; break; case (char)0xD: // do not normalize new lines in attributes - just escape them pDst = CarriageReturnEntity(pDst); break; case (char)0xA: // do not normalize new lines in attributes - just escape them pDst = LineFeedEntity(pDst); break; default: EncodeChar(ref pSrc, pSrcEnd, ref pDst); continue; } pSrc++; } bufPos = (int)(pDst - pDstBegin); } }
internal XmlWriter CreateWriter(Stream output) { XmlWriter writer; if (output == null) { throw new ArgumentNullException("output"); } if (!(this.Encoding.WebName == "utf-8")) { switch (this.OutputMethod) { case XmlOutputMethod.Xml: if (!this.Indent) { writer = new XmlEncodedRawTextWriter(output, this); } else { writer = new XmlEncodedRawTextWriterIndent(output, this); } goto Label_010B; case XmlOutputMethod.Html: if (!this.Indent) { writer = new HtmlEncodedRawTextWriter(output, this); } else { writer = new HtmlEncodedRawTextWriterIndent(output, this); } goto Label_010B; case XmlOutputMethod.Text: writer = new TextEncodedRawTextWriter(output, this); goto Label_010B; case XmlOutputMethod.AutoDetect: writer = new XmlAutoDetectWriter(output, this); goto Label_010B; } return(null); } switch (this.OutputMethod) { case XmlOutputMethod.Xml: if (!this.Indent) { writer = new XmlUtf8RawTextWriter(output, this); break; } writer = new XmlUtf8RawTextWriterIndent(output, this); break; case XmlOutputMethod.Html: if (!this.Indent) { writer = new HtmlUtf8RawTextWriter(output, this); break; } writer = new HtmlUtf8RawTextWriterIndent(output, this); break; case XmlOutputMethod.Text: writer = new TextUtf8RawTextWriter(output, this); break; case XmlOutputMethod.AutoDetect: writer = new XmlAutoDetectWriter(output, this); break; default: return(null); } Label_010B: if ((this.OutputMethod != XmlOutputMethod.AutoDetect) && this.IsQuerySpecific) { writer = new QueryOutputWriter((XmlRawWriter)writer, this); } return(new XmlWellFormedWriter(writer, this)); }
private static XmlWriter CreateWriterImpl(Stream output, Encoding encoding, bool closeOutput, XmlWriterSettings settings) { Debug.Assert(output != null); Debug.Assert(encoding != null); Debug.Assert(settings != null); XmlWriter writer; if (encoding.CodePage == 65001) { // If the encoding is UTF-8, create a special-purpose writer switch (settings.OutputMethod) { case XmlOutputMethod.Xml: if (settings.Indent) { writer = new XmlUtf8RawTextWriterIndent(output, encoding, settings, closeOutput); } else { writer = new XmlUtf8RawTextWriter(output, encoding, settings, closeOutput); } break; case XmlOutputMethod.Html: if (settings.Indent) { writer = new HtmlUtf8RawTextWriterIndent(output, encoding, settings, closeOutput); } else { writer = new HtmlUtf8RawTextWriter(output, encoding, settings, closeOutput); } break; case XmlOutputMethod.Text: writer = new TextUtf8RawTextWriter(output, encoding, settings, closeOutput); break; case XmlOutputMethod.AutoDetect: writer = new XmlAutoDetectWriter(output, encoding, settings); break; default: Debug.Assert(false, "Invalid XmlOutputMethod setting."); return(null); } } else { // Otherwise, create a general-purpose writer than can do any encoding switch (settings.OutputMethod) { case XmlOutputMethod.Xml: if (settings.Indent) { writer = new XmlEncodedRawTextWriterIndent(output, encoding, settings, closeOutput); } else { writer = new XmlEncodedRawTextWriter(output, encoding, settings, closeOutput); } break; case XmlOutputMethod.Html: if (settings.Indent) { writer = new HtmlEncodedRawTextWriterIndent(output, encoding, settings, closeOutput); } else { writer = new HtmlEncodedRawTextWriter(output, encoding, settings, closeOutput); } break; case XmlOutputMethod.Text: writer = new TextEncodedRawTextWriter(output, encoding, settings, closeOutput); break; case XmlOutputMethod.AutoDetect: writer = new XmlAutoDetectWriter(output, encoding, settings); break; default: Debug.Assert(false, "Invalid XmlOutputMethod2 setting."); return(null); } } // Wrap with Xslt/XQuery specific writer if needed; // XmlOutputMethod.AutoDetect writer does this lazily when it creates the underlying Xml or Html writer. if (settings.OutputMethod != XmlOutputMethod.AutoDetect) { if (settings.IsQuerySpecific) { // Create QueryOutputWriter if CData sections or DocType need to be tracked writer = new QueryOutputWriter((XmlRawWriter)writer, settings); } } // wrap with well-formed writer writer = new XmlWellFormedWriter(writer, settings); return(writer); }
private unsafe void WriteUriAttributeText(char *pSrc, char *pSrcEnd) { if (this.endsWithAmpersand) { if ((((long)((pSrcEnd - pSrc) / 2)) > 0L) && (pSrc[0] != '{')) { this.OutputRestAmps(); } this.endsWithAmpersand = false; } fixed(char *chRef = base.bufChars) { char *chPtr2; char *pDst = chRef + base.bufPos; char ch = '\0'; Label_0053: chPtr2 = pDst + ((char *)(((long)((pSrcEnd - pSrc) / 2)) * 2L)); if (chPtr2 > (chRef + base.bufLen)) { chPtr2 = chRef + base.bufLen; } while (((pDst < chPtr2) && ((this.xmlCharType.charProperties[ch = pSrc[0]] & 0x80) != 0)) && (ch < '\x0080')) { pDst++; pDst[0] = ch; pSrc++; } if (pSrc >= pSrcEnd) { goto Label_0227; } if (pDst >= chPtr2) { base.bufPos = (int)((long)((pDst - chRef) / 2)); this.FlushBuffer(); pDst = chRef + 1; goto Label_0053; } switch (ch) { case '\t': case '\'': case '<': case '>': pDst++; pDst[0] = ch; goto Label_021C; case '\n': pDst = XmlEncodedRawTextWriter.LineFeedEntity(pDst); goto Label_021C; case '\r': pDst = XmlEncodedRawTextWriter.CarriageReturnEntity(pDst); goto Label_021C; case '"': pDst = XmlEncodedRawTextWriter.QuoteEntity(pDst); goto Label_021C; case '&': if ((pSrc + 1) != pSrcEnd) { break; } this.endsWithAmpersand = true; goto Label_015C; default: fixed(byte *numRef = this.uriEscapingBuffer) { byte *numPtr = numRef; byte *numPtr2 = numPtr; XmlUtf8RawTextWriter.CharToUTF8(ref pSrc, pSrcEnd, ref numPtr2); while (numPtr < numPtr2) { pDst++; pDst[0] = '%'; pDst++; pDst[0] = "0123456789ABCDEF"[numPtr[0] >> 4]; pDst++; pDst[0] = "0123456789ABCDEF"[numPtr[0] & 15]; numPtr++; } } goto Label_0053; } if (pSrc[1] != '{') { pDst = XmlEncodedRawTextWriter.AmpEntity(pDst); goto Label_021C; } Label_015C: pDst++; pDst[0] = ch; Label_021C: pSrc++; goto Label_0053; Label_0227: base.bufPos = (int)((long)((pDst - chRef) / 2)); } }
private unsafe void WriteHtmlAttributeText(char *pSrc, char *pSrcEnd) { if (this.endsWithAmpersand) { if ((((long)((pSrcEnd - pSrc) / 2)) > 0L) && (pSrc[0] != '{')) { this.OutputRestAmps(); } this.endsWithAmpersand = false; } fixed(char *chRef = base.bufChars) { char *chPtr2; char *pDst = chRef + base.bufPos; char ch = '\0'; Label_0053: chPtr2 = pDst + ((char *)(((long)((pSrcEnd - pSrc) / 2)) * 2L)); if (chPtr2 > (chRef + base.bufLen)) { chPtr2 = chRef + base.bufLen; } while ((pDst < chPtr2) && ((this.xmlCharType.charProperties[ch = pSrc[0]] & 0x80) != 0)) { pDst++; pDst[0] = ch; pSrc++; } if (pSrc >= pSrcEnd) { goto Label_0195; } if (pDst >= chPtr2) { base.bufPos = (int)((long)((pDst - chRef) / 2)); this.FlushBuffer(); pDst = chRef + 1; goto Label_0053; } switch (ch) { case '\t': case '\'': case '<': case '>': pDst++; pDst[0] = ch; goto Label_018A; case '\n': pDst = XmlEncodedRawTextWriter.LineFeedEntity(pDst); goto Label_018A; case '\r': pDst = XmlEncodedRawTextWriter.CarriageReturnEntity(pDst); goto Label_018A; case '"': pDst = XmlEncodedRawTextWriter.QuoteEntity(pDst); goto Label_018A; case '&': if ((pSrc + 1) != pSrcEnd) { break; } this.endsWithAmpersand = true; goto Label_014B; default: base.EncodeChar(ref pSrc, pSrcEnd, ref pDst); goto Label_0053; } if (pSrc[1] != '{') { pDst = XmlEncodedRawTextWriter.AmpEntity(pDst); goto Label_018A; } Label_014B: pDst++; pDst[0] = ch; Label_018A: pSrc++; goto Label_0053; Label_0195: base.bufPos = (int)((long)((pDst - chRef) / 2)); } }