The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl.Xslt \ QilStrConcatenator

  1. //------------------------------------------------------------------------------
  2. // <copyright file="QilStrConcatenator.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.Diagnostics;
  16. using System.Xml;
  17. using System.Xml.Schema;
  18. using System.Xml.Xsl.XPath;
  19. using System.Xml.Xsl.Qil;
  20. namespace System.Xml.Xsl.Xslt
  21. {
  22.    
  23.     internal class QilStrConcatenator
  24.     {
  25.         private XPathQilFactory f;
  26.         private BufferBuilder builder;
  27.         private QilList concat;
  28.         private bool inUse = false;
  29.        
  30.         public QilStrConcatenator(XPathQilFactory f)
  31.         {
  32.             this.f = f;
  33.             builder = new BufferBuilder();
  34.         }
  35.        
  36.         public void Reset()
  37.         {
  38.             Debug.Assert(!inUse);
  39.             inUse = true;
  40.             builder.Clear();
  41.             concat = null;
  42.         }
  43.        
  44.         private void FlushBuilder()
  45.         {
  46.             if (concat == null) {
  47.                 concat = f.BaseFactory.Sequence();
  48.             }
  49.             if (builder.Length != 0) {
  50.                 concat.Add(f.String(builder.ToString()));
  51.                 builder.Length = 0;
  52.             }
  53.         }
  54.        
  55.         public void Append(string value)
  56.         {
  57.             Debug.Assert(inUse, "Reset() wasn't called");
  58.             builder.Append(value);
  59.         }
  60.        
  61.         public void Append(char value)
  62.         {
  63.             Debug.Assert(inUse, "Reset() wasn't called");
  64.             builder.Append(value);
  65.         }
  66.        
  67.         public void Append(QilNode value)
  68.         {
  69.             Debug.Assert(inUse, "Reset() wasn't called");
  70.             if (value != null) {
  71.                 Debug.Assert(value.XmlType.TypeCode == XmlTypeCode.String);
  72.                 if (value.NodeType == QilNodeType.LiteralString) {
  73.                     builder.Append((string)(QilLiteral)value);
  74.                 }
  75.                 else {
  76.                     FlushBuilder();
  77.                     concat.Add(value);
  78.                 }
  79.             }
  80.         }
  81.        
  82.         public QilNode ToQil()
  83.         {
  84.             Debug.Assert(inUse);
  85.             // If we want allow multiple calls to ToQil() this logic should be changed
  86.             inUse = false;
  87.             if (concat == null) {
  88.                 return f.String(builder.ToString());
  89.             }
  90.             else {
  91.                 FlushBuilder();
  92.                 return f.StrConcat(concat);
  93.             }
  94.         }
  95.     }
  96. }

Developer Fusion