5 // Arina Itkes <arinai@mainsoft.com>
7 // Copyright (C) 2007 Mainsoft, Inc.
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Runtime.Serialization;
33 namespace System.Text.RegularExpressions
35 public partial class Regex : ISerializable
37 IMachineFactory _monoFactory;
38 readonly object _monoFactoryLock = new object ();
40 internal bool SameGroupNamesFlag {
42 return GetJvmMachine ().PatternData.SameGroupsFlag;
46 internal IMachine GetMonoMachine () {
48 lock (_monoFactoryLock) {
49 if (_monoFactory != null)
50 return _monoFactory.NewInstance ();
52 _monoFactory = CreateMachineFactory (this.pattern, this.Options);
55 return _monoFactory.NewInstance ();
58 internal JvmReMachine GetJvmMachine () {
59 if (machineFactory is InterpreterFactory)
62 return ((JvmReMachineFactory) machineFactory).GetMachine ();
65 internal int GetJavaNumberByNetNumber (int netNumber) {
66 if (netNumber < 0 || netNumber > group_count) {
70 return GetJvmMachine ().PatternData.NetToJavaNumbersMap [netNumber];
73 private void Init () {
75 this.machineFactory = cache.Lookup (this.pattern, this.roptions);
77 if (this.machineFactory != null) {
79 this.group_count = this.machineFactory.GroupCount;
80 this.mapping = this.machineFactory.Mapping;
81 this._groupNumberToNameMap = this.machineFactory.NamesMapping;
86 PatternData patternData = null;
87 string errorMessage = null;
89 patternData = PatternDataBuilder.GetPatternData(pattern, roptions);
94 errorMessage = "Creating pattern on JVM failed for pattern " + pattern
98 if (patternData == null){
100 if (errorMessage != null)
101 Console.WriteLine (errorMessage);
104 InitJvmRegex (patternData);
106 cache.Add (this.pattern, this.roptions, this.machineFactory);
110 private void InitJvmRegex (PatternData patternData) {
112 machineFactory = new JvmReMachineFactory (patternData);
113 this.group_count = this.machineFactory.GroupCount;
114 this.mapping = this.machineFactory.Mapping;
115 this._groupNumberToNameMap = this.machineFactory.NamesMapping;