1+ using System ;
2+
3+ namespace ActiveUp . Net . Common
4+ {
5+ /// <summary>
6+ /// Parse line with RFC2184 rules (https://tools.ietf.org/html/rfc2184)
7+ /// </summary>
8+ internal class Rfc2184ItemParser
9+ {
10+ private string _OriginalItem ;
11+ public bool IsSingleLine { get ; private set ; }
12+ public bool TextContainsSpecialChar { get ; private set ; }
13+ public int Index { get ; private set ; }
14+ public string Name { get ; private set ; }
15+ public string Language { get ; private set ; }
16+ public string CharSet { get ; private set ; }
17+ public string Value { get ; private set ; }
18+
19+ public Rfc2184ItemParser ( string itemLine )
20+ {
21+ _OriginalItem = itemLine ;
22+ ParseLine ( ) ;
23+ }
24+
25+ private void ParseLine ( )
26+ {
27+ ExtractNameData ( _OriginalItem . Substring ( 0 , _OriginalItem . IndexOf ( '=' ) ) ) ;
28+ ExtractValueData ( _OriginalItem . Substring ( _OriginalItem . IndexOf ( '=' ) + 1 ) . Replace ( "\" " , "" ) . Trim ( '\r ' , '\n ' ) ) ;
29+ }
30+
31+ /// <summary>
32+ /// Extract all information about FieldName.
33+ /// </summary>
34+ /// <param name="fieldName">String of fieldName.</param>
35+ private void ExtractNameData ( string fieldName )
36+ {
37+ IsSingleLine = true ;
38+ TextContainsSpecialChar = false ;
39+ Index = 0 ;
40+
41+ var nameStructure = ( fieldName ?? "" ) . ToLower ( ) . Trim ( ) ;
42+
43+ // If last char is *, then line contain CharSet or Lang.
44+ if ( nameStructure . Length > 0 && nameStructure . Substring ( nameStructure . Length - 1 ) == "*" )
45+ {
46+ TextContainsSpecialChar = true ;
47+ nameStructure = nameStructure . Substring ( 0 , nameStructure . Length - 1 ) ;
48+ }
49+
50+ var firstAsteriskIndex = nameStructure . IndexOf ( '*' ) ;
51+ if ( firstAsteriskIndex >= 0 )
52+ {
53+ Name = nameStructure . Substring ( 0 , firstAsteriskIndex ) . Trim ( ) ;
54+ var itemCount = nameStructure . Substring ( firstAsteriskIndex ) ;
55+ if ( itemCount . Contains ( "*" ) )
56+ {
57+ var actualIndex = itemCount . Replace ( "*" , "" ) ;
58+ if ( Int32 . TryParse ( actualIndex , out int newIndex ) )
59+ {
60+ Index = newIndex ;
61+ }
62+ }
63+ IsSingleLine = false ;
64+ }
65+ else
66+ {
67+ Name = nameStructure ;
68+ }
69+ }
70+
71+ /// <summary>
72+ /// Extract all information about FieldValue.
73+ /// </summary>
74+ /// <param name="fieldValue">Value string to extract information.</param>
75+ private void ExtractValueData ( string fieldValue )
76+ {
77+ if ( string . IsNullOrWhiteSpace ( fieldValue ) ) return ;
78+ var valueToProcess = ( fieldValue ?? "" ) . Trim ( ) ;
79+ if ( valueToProcess . Length > 1 && valueToProcess . Substring ( valueToProcess . Length - 1 , 1 ) == ";" ) valueToProcess = valueToProcess . Substring ( 0 , valueToProcess . Length - 1 ) ;
80+
81+ if ( ! TextContainsSpecialChar || ! valueToProcess . Contains ( "'" ) )
82+ {
83+ Value = valueToProcess ;
84+ }
85+
86+ var splitedFieldValue = valueToProcess . Split ( '\' ' ) ;
87+ if ( splitedFieldValue . Length < 3 )
88+ {
89+ CharSet = null ;
90+ Language = null ;
91+ Value = ( valueToProcess ?? "" ) . Trim ( ) ;
92+ }
93+ else
94+ {
95+ CharSet = splitedFieldValue [ 0 ] ;
96+ Language = splitedFieldValue [ 1 ] ;
97+ Value = splitedFieldValue [ 2 ] ;
98+ }
99+ }
100+ }
101+ }
0 commit comments