The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl.XsltOld \ StateMachine

  1. //------------------------------------------------------------------------------
  2. // <copyright file="StateMachine.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. namespace System.Xml.Xsl.XsltOld
  16. {
  17.     using Res = System.Xml.Utils.Res;
  18.     using System;
  19.     using System.Diagnostics;
  20.     using System.Xml;
  21.     using System.Xml.XPath;
  22.    
  23.     internal class StateMachine
  24.     {
  25.         // Constants for the state table
  26.         private const int Init = 0;
  27.         // Initial state
  28.         private const int Elem = 1;
  29.         // Element was output
  30.         private const int NsN = 2;
  31.         // Namespace name was output
  32.         private const int NsV = 3;
  33.         // Namespace value was output (some more can follow)
  34.         private const int Ns = 4;
  35.         // Namespace was output
  36.         private const int AttrN = 5;
  37.         // Attribute name was output
  38.         private const int AttrV = 6;
  39.         // Attribute value was output (some more can follow)
  40.         private const int Attr = 7;
  41.         // Attribute was output
  42.         private const int InElm = 8;
  43.         // Filling in element, general state text
  44.         private const int EndEm = 9;
  45.         // After end element event - next end element doesn't generate token
  46.         private const int InCmt = 10;
  47.         // Adding text to a comment
  48.         private const int InPI = 11;
  49.         // Adding text to a processing instruction
  50.         private const int StateMask = 15;
  51.         // State mask
  52.         internal const int Error = 16;
  53.         // Invalid XML state
  54.         private const int Ignor = 32;
  55.         // Ignore this transition
  56.         private const int Assrt = 48;
  57.         // Assrt
  58.         private const int U = 256;
  59.         // Depth up
  60.         private const int D = 512;
  61.         // Depth down
  62.         internal const int DepthMask = 768;
  63.         // Depth mask
  64.         internal const int DepthUp = U;
  65.         internal const int DepthDown = D;
  66.        
  67.         private const int C = 1024;
  68.         // BeginChild
  69.         private const int H = 2048;
  70.         // HadChild
  71.         private const int M = 4096;
  72.         // EmptyTag
  73.         internal const int BeginChild = C;
  74.         internal const int HadChild = H;
  75.         internal const int EmptyTag = M;
  76.        
  77.         private const int B = 8192;
  78.         // Begin Record
  79.         private const int E = 16384;
  80.         // Record finished
  81.         internal const int BeginRecord = B;
  82.         internal const int EndRecord = E;
  83.        
  84.         private const int S = 32768;
  85.         // Push namespace scope
  86.         private const int P = 65536;
  87.         // Pop current namepsace scope
  88.         internal const int PushScope = S;
  89.         internal const int PopScope = P;
  90.         // Next event must pop scope
  91.         //
  92.         // Runtime state
  93.         //
  94.        
  95.         private int _State;
  96.        
  97.         internal StateMachine()
  98.         {
  99.             _State = Init;
  100.         }
  101.        
  102.         internal int State {
  103.             get { return _State; }
  104.            
  105. // Hope you know what you are doing ...
  106.             set { _State = value; }
  107.         }
  108.        
  109.         internal void Reset()
  110.         {
  111.             _State = Init;
  112.         }
  113.        
  114.         static internal int StateOnly(int state)
  115.         {
  116.             return state & StateMask;
  117.         }
  118.        
  119.         internal int BeginOutlook(XPathNodeType nodeType)
  120.         {
  121.             int newState = s_BeginTransitions[(int)nodeType][_State];
  122.             Debug.Assert(newState != Assrt);
  123.             return newState;
  124.         }
  125.        
  126.         internal int Begin(XPathNodeType nodeType)
  127.         {
  128.             int newState = s_BeginTransitions[(int)nodeType][_State];
  129.             Debug.Assert(newState != Assrt);
  130.            
  131.             if (newState != Error && newState != Ignor) {
  132.                 _State = newState & StateMask;
  133.             }
  134.             return newState;
  135.         }
  136.        
  137.         internal int EndOutlook(XPathNodeType nodeType)
  138.         {
  139.             int newState = s_EndTransitions[(int)nodeType][_State];
  140.             Debug.Assert(newState != Assrt);
  141.             return newState;
  142.         }
  143.        
  144.         internal int End(XPathNodeType nodeType)
  145.         {
  146.             int newState = s_EndTransitions[(int)nodeType][_State];
  147.             Debug.Assert(newState != Assrt);
  148.            
  149.             if (newState != Error && newState != Ignor) {
  150.                 _State = newState & StateMask;
  151.             }
  152.             return newState;
  153.         }
  154.        
  155.         private static readonly int[][] s_BeginTransitions = {new int[] {Error, Error, Error, Error, Error, Error, Error, Error, Error, Error,
  156.         Error, Error}, new int[] {Elem | B | S, Elem | U | C | B | S, Error, Error, Elem | C | B | S, Error, Error, Elem | C | B | S, Elem | B | S, Elem | B | P | S,
  157.         Error, Error}, new int[] {Error, AttrN | U, Error, Error, AttrN, Error, Error, AttrN, Error, Error,
  158.         Error, Error}, new int[] {Error, NsN | U, Error, Error, NsN, Error, Error, Error, Error, Error,
  159.         Error, Error}, new int[] {InElm | B, InElm | U | C | B, NsV | U, NsV, InElm | C | B, AttrV | U, AttrV, InElm | C | B, InElm, InElm | B | P,
  160.         InCmt, InPI}, new int[] {InElm | B, InElm | U | C | B, NsV | U, NsV, InElm | C | B, AttrV | U, AttrV, InElm | C | B, InElm, InElm | B | P,
  161.         InCmt, InPI}, new int[] {InElm | B, InElm | U | C | B, NsV | U, NsV, InElm | C | B, AttrV | U, AttrV, InElm | C | B, InElm, InElm | B | P,
  162.         InCmt, InPI}, new int[] {InPI | B, InPI | U | C | B, Error, Error, InPI | C | B, Error, Error, InPI | C | B, InPI | B, InPI | B | P,
  163.         Error, Error}, new int[] {InCmt | B, InCmt | U | C | B, Error, Error, InCmt | C | B, Error, Error, InCmt | C | B, InCmt | B, InCmt | B | P,
  164.         Error, Error}, new int[] {Error, Error, Error, Error, Error, Error, Error, Error, Error, Error,
  165.         Error, Error}
  166.             /*                                    { Init,      Elem,          NsN,    NsV,    Ns,              AttrN,  AttrV,  Attr,            InElm,          EndEm,          InCmt,  InPI  }, */           
  167.             /* Root                  */            /* Element              */            /* Attribute            */            /* Namespace            */            /* Text                  */            /* SignificantWhitespace */            /* Whitespace            */            /* ProcessingInstruction */            /* Comment              */            /* All                  */        };
  168.        
  169.         private static readonly int[][] s_EndTransitions = {new int[] {Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt,
  170.         Assrt, Assrt}, new int[] {Assrt, EndEm | B | E | P | M, Assrt, Assrt, EndEm | D | B | E | P | M, Assrt, Assrt, EndEm | D | B | E | P | M, EndEm | D | H | B | E | P, EndEm | D | H | B | E | P,
  171.         Assrt, Assrt}, new int[] {Assrt, Assrt, Assrt, Assrt, Assrt, Attr, Attr | D, Assrt, Assrt, Assrt,
  172.         Assrt, Assrt}, new int[] {Assrt, Assrt, Ns, Ns | D, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt,
  173.         Assrt, Assrt}, new int[] {Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt,
  174.         Assrt, Assrt}, new int[] {Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt,
  175.         Assrt, Assrt}, new int[] {Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt,
  176.         Assrt, Assrt}, new int[] {Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt,
  177.         Assrt, EndEm | E}, new int[] {Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt,
  178.         EndEm | E, Assrt}, new int[] {Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt, Assrt,
  179.         Assrt, Assrt}
  180.             /*                                    { Init,      Elem,          NsN,    NsV,    Ns,              AttrN,  AttrV,  Attr,            InElm,          EndEm,          InCmt,  InPI  }, */           
  181.             /* Root                  */            /* Element              */            /* Attribute            */            /* Namespace            */            /* Text                  */            /* SignificantWhitespace */            /* Whitespace            */            /* ProcessingInstruction */            /* Comment              */            /* All                  */        };
  182.     }
  183. }

Developer Fusion