crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianParserLib/RegExTranslators/RegExTranslatorManager.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 * 
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Collections.Generic;
       
    19 using System.Text;
       
    20 using System.Text.RegularExpressions;
       
    21 using System.Reflection;
       
    22 using SymbianParserLib.Utilities;
       
    23 using SymbianParserLib.Elements;
       
    24 using SymbianParserLib.RegExTranslators.Types;
       
    25 
       
    26 namespace SymbianParserLib.RegExTranslators
       
    27 {
       
    28     public static class TheTickCounter
       
    29     {
       
    30         public static long TickCount;
       
    31     }
       
    32 
       
    33     internal class RegExTranslatorManager
       
    34     {
       
    35         #region Constructors
       
    36         public RegExTranslatorManager()
       
    37         {
       
    38             iTranslators.AddRange( KDefaultList );
       
    39         }
       
    40         #endregion
       
    41 
       
    42         #region API
       
    43         public static ParserLine PreCachedCompiledEntry( string aKey )
       
    44         {
       
    45             ParserLine ret = TheTranslator.iCache.CreateClone( aKey );
       
    46             return ret;
       
    47         }
       
    48 
       
    49         public static void CompileToRegularExpression( ParserLine aLine )
       
    50         {
       
    51             TheTranslator.DoCompileToRegularExpression( aLine );
       
    52         }
       
    53         #endregion
       
    54 
       
    55         #region Properties
       
    56         public static RegExTranslatorManager TheTranslator
       
    57         {
       
    58             get { return iTheTranslator; }
       
    59         }
       
    60         #endregion
       
    61 
       
    62         #region Internal methods
       
    63         private static string FixupOriginalValue( string aLine )
       
    64         {
       
    65             StringBuilder ret = new StringBuilder( ParserUtils.RemoveLineEndings( aLine ) );
       
    66             
       
    67             // Escape double percents
       
    68             ret = ret.Replace( KEscapedPercent, KReplacementPercent );
       
    69 
       
    70             // Escape [ and ]
       
    71             ret = EscapeCharacter( ".", ret );
       
    72             ret = EscapeCharacter( "$", ret );
       
    73             ret = EscapeCharacter( "+", ret );
       
    74             ret = EscapeCharacter( "?", ret );
       
    75             ret = EscapeCharacter( "^", ret );
       
    76             ret = EscapeCharacter( "*", ret );
       
    77             ret = EscapeCharacter( "\\", ret );
       
    78             ret = EscapeCharacter( "[", ret );
       
    79             ret = EscapeCharacter( "]", ret );
       
    80             ret = EscapeCharacter( "(", ret );
       
    81             ret = EscapeCharacter( ")", ret );
       
    82             ret = EscapeCharacter( "{", ret );
       
    83             ret = EscapeCharacter( "}", ret );
       
    84 
       
    85             return ret.ToString();
       
    86         }
       
    87 
       
    88         private static StringBuilder EscapeCharacter( string aCharacter, StringBuilder aObject )
       
    89         {
       
    90             string replaceWith = "\\" + aCharacter;
       
    91             return aObject.Replace( aCharacter, replaceWith );
       
    92         }
       
    93 
       
    94         private void DoCompileToRegularExpression( ParserLine aLine )
       
    95         {
       
    96             System.DateTime starTime = DateTime.Now;
       
    97             string input = aLine.OriginalValue;
       
    98 
       
    99             // First phase is to replace all excaped percents with a single percent
       
   100             aLine.OriginalValue = FixupOriginalValue( aLine.OriginalValue );
       
   101 
       
   102             // Next phase is to identify all format specifiers
       
   103             MatchCollection matches = KRegExFormatSpecifier.Matches( aLine.OriginalValue );
       
   104             if ( matches.Count != 0 )
       
   105             {
       
   106                 // Get combined list of all captures spanning all matches. There must be a better way to do this?
       
   107                 List<Capture> captureList = new List<Capture>();
       
   108                 foreach ( Match m in matches )
       
   109                 {
       
   110                     if ( m.Success )
       
   111                     {
       
   112                         foreach ( Capture cap in m.Captures )
       
   113                         {
       
   114                             captureList.Add( cap );
       
   115                         }
       
   116                     }
       
   117                 }
       
   118 
       
   119                 // Convert captures into format specifiers
       
   120                 FixupFormatSpecifiers( captureList, aLine );
       
   121 
       
   122                 // Cache entry
       
   123                 iCache.Add( input, aLine );
       
   124             }
       
   125             else
       
   126             {
       
   127             }
       
   128 
       
   129             System.DateTime endTime = DateTime.Now;
       
   130             long tickDuration = ( ( endTime.Ticks - starTime.Ticks ) / 100 );
       
   131             TheTickCounter.TickCount += tickDuration;
       
   132         }
       
   133 
       
   134         private void FixupFormatSpecifiers( List<Capture> aCaptures, ParserLine aLine )
       
   135         {
       
   136             int lastCapturePos = 0;
       
   137 
       
   138             // Pull out all the format specifiers and build regular expressions
       
   139             int count = aCaptures.Count;
       
   140             for ( int i = 0; i < count; i++ )
       
   141             {
       
   142                 Capture capture = aCaptures[ i ];
       
   143 
       
   144                 // Process the capture, starting at the last capture pos
       
   145                 lastCapturePos = TryToProcessCapture( capture, lastCapturePos, aLine );
       
   146             }
       
   147 
       
   148             aLine.Finalise();
       
   149         }
       
   150 
       
   151         private int TryToProcessCapture( Capture aCapture, int aStartAt, ParserLine aLine )
       
   152         {
       
   153             // Return the end position of the current capture.
       
   154             int ret = -1;
       
   155             //
       
   156             foreach ( RegExTranslatorBase translator in iTranslators )
       
   157             {
       
   158                 ParserField field = translator.Process( aCapture, aStartAt, aLine );
       
   159                 if ( field != null )
       
   160                 {
       
   161                     aLine.Add( field );
       
   162                     ret = field.FormatSpecifier.OriginalLocation + field.FormatSpecifier.OriginalLength;
       
   163                     break;
       
   164                 }
       
   165             }
       
   166             //
       
   167             return ret;
       
   168         }
       
   169         #endregion
       
   170 
       
   171         #region Internal constants
       
   172         private static readonly RegExTranslatorBase[] KDefaultList = new RegExTranslatorBase[]
       
   173             {
       
   174                 new RegExTranslatorDecimal(),
       
   175                 new RegExTranslatorHex(),
       
   176                 new RegExTranslatorString()
       
   177             };
       
   178 
       
   179         private static readonly Regex KRegExFormatSpecifier = new Regex(
       
   180               "%{1}",
       
   181               RegexOptions.IgnoreCase
       
   182             | RegexOptions.CultureInvariant
       
   183             | RegexOptions.IgnorePatternWhitespace
       
   184             | RegexOptions.Compiled
       
   185             );
       
   186 
       
   187         private const string KEscapedPercent = "%%";
       
   188         private const string KReplacementPercent = "%";
       
   189         #endregion
       
   190 
       
   191         #region From System.Object
       
   192         public override string ToString()
       
   193         {
       
   194             return base.ToString();
       
   195         }
       
   196         #endregion
       
   197 
       
   198         #region Data members
       
   199         private static RegExTranslatorManager iTheTranslator = new RegExTranslatorManager();
       
   200         private List<RegExTranslatorBase> iTranslators = new List<RegExTranslatorBase>();
       
   201         private Cache.RegExTranslatorCache iCache = new SymbianParserLib.RegExTranslators.Cache.RegExTranslatorCache();
       
   202         #endregion
       
   203     }
       
   204 }