The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl \ QueryReaderSettings

  1. //------------------------------------------------------------------------------
  2. // <copyright file="QueryReaderSettings.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.IO;
  19. using System.Xml;
  20. namespace System.Xml.Xsl
  21. {
  22.     internal class QueryReaderSettings
  23.     {
  24.         private bool validatingReader;
  25.         private XmlReaderSettings xmlReaderSettings;
  26.         private XmlNameTable xmlNameTable;
  27.         private EntityHandling entityHandling;
  28.         private bool namespaces;
  29.         private bool normalization;
  30.         private bool prohibitDtd;
  31.         private WhitespaceHandling whitespaceHandling;
  32.         private XmlResolver xmlResolver;
  33.        
  34.         public QueryReaderSettings(XmlNameTable xmlNameTable)
  35.         {
  36.             Debug.Assert(xmlNameTable != null);
  37.             xmlReaderSettings = new XmlReaderSettings();
  38.             xmlReaderSettings.NameTable = xmlNameTable;
  39.             xmlReaderSettings.ConformanceLevel = ConformanceLevel.Document;
  40.             xmlReaderSettings.XmlResolver = null;
  41.             xmlReaderSettings.ProhibitDtd = true;
  42.             xmlReaderSettings.CloseInput = true;
  43.         }
  44.        
  45.         public QueryReaderSettings(XmlReader reader)
  46.         {
  47.             #pragma warning disable 618
  48.             XmlValidatingReader valReader = reader as XmlValidatingReader;
  49.             #pragma warning restore 618
  50.             if (valReader != null) {
  51.                 // Unwrap validation reader
  52.                 validatingReader = true;
  53.                 reader = valReader.Impl.Reader;
  54.             }
  55.             xmlReaderSettings = reader.Settings;
  56.             if (xmlReaderSettings != null) {
  57.                 xmlReaderSettings = xmlReaderSettings.Clone();
  58.                 xmlReaderSettings.NameTable = reader.NameTable;
  59.                 xmlReaderSettings.CloseInput = true;
  60.                 xmlReaderSettings.LineNumberOffset = 0;
  61.                 xmlReaderSettings.LinePositionOffset = 0;
  62.                 XmlTextReaderImpl impl = reader as XmlTextReaderImpl;
  63.                 if (impl != null) {
  64.                     xmlReaderSettings.XmlResolver = impl.GetResolver();
  65.                 }
  66.             }
  67.             else {
  68.                 xmlNameTable = reader.NameTable;
  69.                 XmlTextReader xmlTextReader = reader as XmlTextReader;
  70.                 if (xmlTextReader != null) {
  71.                     XmlTextReaderImpl impl = xmlTextReader.Impl;
  72.                     entityHandling = impl.EntityHandling;
  73.                     namespaces = impl.Namespaces;
  74.                     normalization = impl.Normalization;
  75.                     prohibitDtd = impl.ProhibitDtd;
  76.                     whitespaceHandling = impl.WhitespaceHandling;
  77.                     xmlResolver = impl.GetResolver();
  78.                 }
  79.                 else {
  80.                     entityHandling = EntityHandling.ExpandEntities;
  81.                     namespaces = true;
  82.                     normalization = true;
  83.                     prohibitDtd = true;
  84.                     whitespaceHandling = WhitespaceHandling.All;
  85.                     xmlResolver = null;
  86.                 }
  87.             }
  88.         }
  89.        
  90.         public XmlReader CreateReader(Stream stream, string baseUri)
  91.         {
  92.             XmlReader reader;
  93.             if (xmlReaderSettings != null) {
  94.                 reader = XmlTextReader.Create(stream, xmlReaderSettings, baseUri);
  95.             }
  96.             else {
  97.                 XmlTextReaderImpl readerImpl = new XmlTextReaderImpl(baseUri, stream, xmlNameTable);
  98.                 readerImpl.EntityHandling = entityHandling;
  99.                 readerImpl.Namespaces = namespaces;
  100.                 readerImpl.Normalization = normalization;
  101.                 readerImpl.ProhibitDtd = prohibitDtd;
  102.                 readerImpl.WhitespaceHandling = whitespaceHandling;
  103.                 readerImpl.XmlResolver = xmlResolver;
  104.                 reader = readerImpl;
  105.             }
  106.             if (validatingReader) {
  107.                 #pragma warning disable 618
  108.                 reader = new XmlValidatingReader(reader);
  109.                 #pragma warning restore 618
  110.             }
  111.             return reader;
  112.         }
  113.        
  114.         public XmlNameTable NameTable {
  115.             get { return xmlReaderSettings != null ? xmlReaderSettings.NameTable : xmlNameTable; }
  116.         }
  117.     }
  118. }

Developer Fusion