|
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.Enums; |
|
23 using SymbianParserLib.Elements; |
|
24 using SymbianParserLib.Elements.SubFields; |
|
25 |
|
26 namespace SymbianParserLib.RegExTranslators |
|
27 { |
|
28 internal abstract class RegExTranslatorBase |
|
29 { |
|
30 #region Constructors |
|
31 public RegExTranslatorBase() |
|
32 { |
|
33 } |
|
34 #endregion |
|
35 |
|
36 #region API |
|
37 public abstract ParserField Process( Capture aCapture, int aStartAt, ParserLine aLine ); |
|
38 #endregion |
|
39 |
|
40 #region Properties |
|
41 #endregion |
|
42 |
|
43 #region Internal methods |
|
44 protected ParserField CreateField( string aRegEx, string aFieldName, TParserValueType aValueType, int aCapturePos, int aCaptureLength ) |
|
45 { |
|
46 ParserField ret = CreateField( aRegEx, aFieldName, aValueType, aCapturePos, aCaptureLength, true ); |
|
47 return ret; |
|
48 } |
|
49 |
|
50 protected ParserField CreateField( string aRegEx, string aFieldName, TParserValueType aValueType, int aCapturePos, int aCaptureLength, bool aRequiresNumberedCaptureGroup ) |
|
51 { |
|
52 string fieldName = CompressName( aFieldName ); |
|
53 ParserField ret = new ParserField( fieldName ); |
|
54 |
|
55 // Update format specifier |
|
56 ParserFieldFormatSpecifier formatSpecifier = ret.FormatSpecifier; |
|
57 formatSpecifier.OriginalLocation = aCapturePos; |
|
58 formatSpecifier.OriginalLength = aCaptureLength; |
|
59 |
|
60 // Surround in numbered group if needed... |
|
61 StringBuilder finalRegEx = new StringBuilder( aRegEx ); |
|
62 if ( aRequiresNumberedCaptureGroup ) |
|
63 { |
|
64 finalRegEx.Insert( 0, "(" ); |
|
65 finalRegEx.Append( ")" ); |
|
66 } |
|
67 |
|
68 formatSpecifier.RegularExpressionString = finalRegEx.ToString(); |
|
69 formatSpecifier.ExpectedType = aValueType; |
|
70 |
|
71 return ret; |
|
72 } |
|
73 |
|
74 private static string CompressName( string aUncompressedFieldName ) |
|
75 { |
|
76 Regex spaces = new Regex(@"\s+"); |
|
77 string[] fields = spaces.Split( aUncompressedFieldName ); |
|
78 // |
|
79 StringBuilder ret = new StringBuilder(); |
|
80 foreach ( string field in fields ) |
|
81 { |
|
82 ret.Append( field ); |
|
83 } |
|
84 // |
|
85 return ret.ToString(); |
|
86 } |
|
87 #endregion |
|
88 |
|
89 #region Internal constants |
|
90 #endregion |
|
91 |
|
92 #region From System.Object |
|
93 public override string ToString() |
|
94 { |
|
95 return base.ToString(); |
|
96 } |
|
97 #endregion |
|
98 |
|
99 #region Data members |
|
100 #endregion |
|
101 } |
|
102 } |