-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleCubeDescriptionFactory.cs
168 lines (147 loc) · 4.82 KB
/
SimpleCubeDescriptionFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace CubeGenerator
{
delegate SimpleCubeDescriptionFactory ParserAction(string value);
public delegate void ChangeEventHandler(SimpleCubeDescription description, String action,String change);
public class SimpleCubeDescriptionFactory
{
private SimpleCubeDescription cubeDescriptionPrototype;
public List<SimpleCubeDescription> cubeDescriptionList;
private List<String> measureList;
private List<String> dimensionList;
private Dictionary<Regex, ParserAction> parseCommand;
private Dictionary<State, ParserAction> contextCommand;
enum State{New,Property,Measure,Dimension};
private State activeState;
public event ChangeEventHandler Changed;
public SimpleCubeDescriptionFactory()
{
measureList = new List<String> ();
dimensionList = new List<String> ();
cubeDescriptionList = new List<SimpleCubeDescription>();
cubeDescriptionPrototype = new SimpleCubeDescription();
parseCommand = new Dictionary<Regex, ParserAction>()
{
{new Regex(@"Counter table name:\s+(?<word>\w+)\s?",RegexOptions.IgnoreCase),this.setCounterTableName},
{new Regex(@"OLAP cube source:\s+(?<word>\w+)\s?" ,RegexOptions.IgnoreCase),this.setOlapCubeSource},
{new Regex(@"OLAP cube Name:\s+(?<word>\w+)\s?" ,RegexOptions.IgnoreCase),this.setOlapCubeName},
{new Regex(@"Measures\s+(?<word>[\w ]+)\s?" ,RegexOptions.IgnoreCase),this.addMeasure},
{new Regex(@"Dimensions\s+(?<word>[\w ]+)\s?" ,RegexOptions.IgnoreCase),this.addDimension},
{new Regex(@"Generate\s?" ,RegexOptions.IgnoreCase),this.create},
{new Regex(@"\s+(?<word>[\w ]+)\s?" ,RegexOptions.IgnoreCase),this.addOther}
};
contextCommand = new Dictionary<State, ParserAction>()
{
{State.New, this.skip},
{State.Property, this.skip},
{State.Measure, this.addMeasure},
{State.Dimension,this.addDimension}
};
}
private String extractString( Object gr )
{
if (gr is Group[])
{
return "";
}
if (gr is String) {
return (String)gr;
}
return "";
}
public SimpleCubeDescriptionFactory parseLine(String line)
{
foreach( KeyValuePair<Regex,ParserAction> pair in parseCommand )
{
MatchCollection matches = pair.Key.Matches(line);
if (matches.Count > 0)
{
pair.Value(matches[0].Groups["word"].Value.Trim().ToUpper().Replace(' ','_'));
break;
}
}
return this;
}
public void isNull()
{
if( cubeDescriptionPrototype == null )
cubeDescriptionPrototype = new SimpleCubeDescription();
}
protected virtual void OnChange(String action,String value)
{
if (Changed != null)
Changed (this.cubeDescriptionPrototype, action, value);
}
public SimpleCubeDescriptionFactory setCounterTableName(String name)
{
isNull();
activeState = State.Property;
cubeDescriptionPrototype.counterTableName = name;
OnChange ("counterTableName", name);
return this;
}
public SimpleCubeDescriptionFactory setOlapCubeSource(String name)
{
isNull();
activeState = State.Property;
cubeDescriptionPrototype.olapCubeSource = name;
OnChange ("olapCubeSource", name);
return this;
}
public SimpleCubeDescriptionFactory setOlapCubeName(String name)
{
isNull();
activeState = State.Property;
cubeDescriptionPrototype.olapCubeName = name;
OnChange("olapCubeName",name);
return this;
}
public SimpleCubeDescriptionFactory addMeasure(String value)
{
isNull();
activeState = State.Measure;
measureList.Add(value);
OnChange ("addMeasure", value);
return this;
}
public SimpleCubeDescriptionFactory addDimension(String value)
{
isNull();
activeState = State.Dimension;
dimensionList.Add(value);
OnChange ("addDimension", value);
return this;
}
public SimpleCubeDescriptionFactory addOther(String value)
{
contextCommand[activeState](value);
return this;
}
public SimpleCubeDescriptionFactory create(String value){
isNull();
cubeDescriptionPrototype.measures = measureList.ToArray();
cubeDescriptionPrototype.dimensions = dimensionList.ToArray();
cubeDescriptionList.Add(cubeDescriptionPrototype);
cubeDescriptionPrototype = new SimpleCubeDescription();
measureList.Clear();
dimensionList.Clear();
OnChange ("create", value);
return this;
}
public SimpleCubeDescriptionFactory skip(String value)
{
return this;
}
public SimpleCubeDescription[] parseStream(StreamReader streamReader)
{
while(!streamReader.EndOfStream){
string line = streamReader.ReadLine ();
parseLine (line);
}
return cubeDescriptionList.ToArray ();
}
}
}