* MonoTouch/MonoPInvokeCallbackAttribute.cs: Added.
[mono.git] / mcs / class / System / System.Text.RegularExpressions / PatternData.jvm.cs
1 //
2 // PatternData.jvm.cs
3 //
4 // Author:
5 //      Arina Itkes  <arinai@mainsoft.com>
6 //
7 // Copyright (C) 2007 Mainsoft, Inc.
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31
32
33 using System;
34 using System.Collections.Generic;
35 using System.Text;
36 using System.Collections;
37 using java.util.regex;
38 using java.lang;
39
40 namespace System.Text.RegularExpressions
41 {
42
43         sealed class PatternData
44         {
45                 readonly Pattern _javaPattern;
46                 readonly PatternGrouping _patternGrouping;
47
48                 #region Properties
49
50                 internal int [] NetToJavaNumbersMap {
51                         get { return _patternGrouping.NetToJavaNumbersMap; }
52                 }
53
54                 internal IDictionary GroupNameToNumberMap {
55                         get { return _patternGrouping.GroupNameToNumberMap; }
56                 }
57
58                 internal string [] GroupNumberToNameMap {
59                         get { return _patternGrouping.GroupNumberToNameMap; }
60                 }
61
62                 internal int [] JavaToNetGroupNumbersMap {
63                         get { return _patternGrouping.JavaToNetGroupNumbersMap; }
64                 }
65
66                 internal int GroupCount {
67                         get { return _patternGrouping.GroupCount; }
68                 }
69
70                 internal Pattern JavaPattern {
71                         get { return _javaPattern; }
72                 }
73
74                 internal bool SameGroupsFlag {
75                         get { return _patternGrouping.SameGroupsFlag; }
76                 }
77
78                 #endregion Properties
79
80                 #region Ctors
81
82                 internal PatternData (
83                         RegexOptions options,
84                         string reformattedPattern,
85                         PatternGrouping patternGrouping) {
86
87                         this._patternGrouping = patternGrouping;
88
89                         _javaPattern = Pattern.compile (reformattedPattern, GetJavaFlags (options));
90
91                         FillGroups (options);
92                 }
93
94
95                 #endregion Ctors
96
97                 #region Private methods
98
99                 private void FillGroups (RegexOptions options) {
100                         if (_patternGrouping.GroupCount >= 0) {
101                                 return;
102                         }
103
104                         Matcher m = JavaPattern.matcher ((CharSequence) (object) String.Empty);
105
106                         if ((options & RegexOptions.ExplicitCapture) == RegexOptions.ExplicitCapture) {
107                                 _patternGrouping.SetGroups (0, m.groupCount());
108                         }
109                         else {
110                                 _patternGrouping.SetGroups (m.groupCount(), m.groupCount ());
111                         }
112                 }
113
114                 private int GetJavaFlags (RegexOptions options) {
115                         int flags = Pattern.UNIX_LINES; // .NET treats only the "\n" character as newline, UNIX_LINES implies the same behavior in Java.
116
117                         if ((options & RegexOptions.IgnoreCase) == RegexOptions.IgnoreCase) {
118                                 flags |= Pattern.CASE_INSENSITIVE;
119                         }
120                         if ((options & RegexOptions.Multiline) == RegexOptions.Multiline) {
121                                 flags |= Pattern.MULTILINE;
122                         }
123                         if ((options & RegexOptions.Singleline) == RegexOptions.Singleline) {
124                                 flags |= Pattern.DOTALL;
125                         }
126                         if ((options & RegexOptions.IgnorePatternWhitespace) == RegexOptions.IgnorePatternWhitespace) {
127                                 flags |= Pattern.COMMENTS;
128                         }
129                         return flags;
130                 }
131
132                 #endregion Private methods
133         }
134 }