Skip to content

Commit 6cf0ac4

Browse files
committed
Improve performance of transform.
Add a condition to allow more complex filtering of elements to transform.
1 parent aa6cb0e commit 6cf0ac4

File tree

3 files changed

+167
-11
lines changed

3 files changed

+167
-11
lines changed

StructuredXmlEditor/Data/DataTransformer.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class DataTransformer
1313
{
1414
//-----------------------------------------------------------------------
1515
public List<string> ElementPaths { get; set; }
16+
public string Condition { get; set; }
1617
public string OutputTemplate { get; set; }
1718

1819
//-----------------------------------------------------------------------
@@ -131,6 +132,114 @@ public bool TransformDocument(XElement root)
131132
//-----------------------------------------------------------------------
132133
public IEnumerable<XElement> TransformElement(XElement root, XElement originalEl)
133134
{
135+
if (!string.IsNullOrWhiteSpace(Condition))
136+
{
137+
var orConditions = Condition.Split(new string[] { "||" }, StringSplitOptions.RemoveEmptyEntries);
138+
139+
var conditionTrue = false;
140+
foreach (var orCondition in orConditions)
141+
{
142+
var andConditions = orCondition.Split(new string[] { "&&" }, StringSplitOptions.RemoveEmptyEntries);
143+
144+
var partTrue = true;
145+
foreach (var andCondition in andConditions)
146+
{
147+
if (andCondition.Contains("=="))
148+
{
149+
var statementSplit = andCondition.Split(new string[] { "==" }, StringSplitOptions.RemoveEmptyEntries);
150+
var path = statementSplit[0];
151+
var checkValue = false;
152+
if (path.EndsWith(".value"))
153+
{
154+
path = path.Substring(0, path.Length - ".value".Length);
155+
checkValue = true;
156+
}
157+
158+
var value = statementSplit[1].Trim();
159+
160+
var targetEl = GetElements(originalEl, path.Trim()).FirstOrDefault();
161+
if (targetEl == null)
162+
{
163+
if (value == "null")
164+
{
165+
partTrue = true;
166+
}
167+
else
168+
{
169+
partTrue = false;
170+
}
171+
}
172+
else if (checkValue)
173+
{
174+
partTrue = value == targetEl.Value;
175+
}
176+
else
177+
{
178+
if (value == "null")
179+
{
180+
partTrue = false;
181+
}
182+
else
183+
{
184+
partTrue = true;
185+
}
186+
}
187+
}
188+
else if (andCondition.Contains("!="))
189+
{
190+
var statementSplit = andCondition.Split(new string[] { "!=" }, StringSplitOptions.RemoveEmptyEntries);
191+
var path = statementSplit[0];
192+
var checkValue = false;
193+
if (path.EndsWith(".value"))
194+
{
195+
path = path.Substring(0, path.Length - ".value".Length);
196+
checkValue = true;
197+
}
198+
199+
var value = statementSplit[1].Trim();
200+
201+
var targetEl = GetElements(originalEl, path.Trim()).FirstOrDefault();
202+
if (targetEl == null)
203+
{
204+
if (value != "null")
205+
{
206+
partTrue = true;
207+
}
208+
else
209+
{
210+
partTrue = false;
211+
}
212+
}
213+
else if (checkValue)
214+
{
215+
partTrue = value != targetEl.Value;
216+
}
217+
else
218+
{
219+
if (value != "null")
220+
{
221+
partTrue = false;
222+
}
223+
else
224+
{
225+
partTrue = true;
226+
}
227+
}
228+
}
229+
}
230+
231+
if (partTrue)
232+
{
233+
conditionTrue = true;
234+
}
235+
}
236+
237+
if (!conditionTrue)
238+
{
239+
return new List<XElement>() { originalEl };
240+
}
241+
}
242+
134243
// split the template into variable chunks
135244
var template = OutputTemplate;
136245
var split = template.Split(new char[] { '{', '}' });

StructuredXmlEditor/Tools/DataTransformerTool.cs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ public string ElementPath
3636
}
3737
private string m_elementPath = "Example.Data";
3838

39+
//-----------------------------------------------------------------------
40+
public string Condition
41+
{
42+
get { return m_Condition; }
43+
set
44+
{
45+
m_Condition = value;
46+
RaisePropertyChangedEvent();
47+
48+
UpdatePreview();
49+
}
50+
}
51+
private string m_Condition = "";
52+
3953
//-----------------------------------------------------------------------
4054
public TextEditor TextEditor { get; set; }
4155

@@ -63,7 +77,27 @@ public string ExampleDocument
6377
}
6478
private string m_exampleDocument = "";
6579

66-
public string TransformedDocument { get; set; }
80+
public string TransformedDocument
81+
{
82+
get { return m_transformedDocument; }
83+
set
84+
{
85+
if (m_transformedDocument != value)
86+
{
87+
m_transformedDocument = value;
88+
89+
Future.SafeCall(() =>
90+
{
91+
var builder = new SideBySideDiffBuilder(new Differ());
92+
DiffModel = builder.BuildDiffModel(ExampleDocument, TransformedDocument);
93+
94+
RaisePropertyChangedEvent(nameof(DiffModel));
95+
}, 500, this);
96+
}
97+
}
98+
}
99+
private string m_transformedDocument = "";
100+
67101
public bool IsDocumentMatch { get; set; }
68102
public string TransformError { get; set; }
69103
public SideBySideDiffModel DiffModel { get; set; }
@@ -95,11 +129,13 @@ public DataTransformerTool(Workspace workspace) : base(workspace, "Data Transfor
95129
TextEditor.Text = "{el||}";
96130
TextEditor.Foreground = new SolidColorBrush(Color.FromRgb(255, 255, 255));
97131
TextEditor.Background = Brushes.Transparent;
132+
TextEditor.TextChanged += (e, args) =>
133+
{
134+
UpdatePreview();
135+
};
98136
TextEditor.TextArea.TextEntered += (e, args) =>
99137
{
100138
Autocomplete(e, args);
101-
102-
UpdatePreview();
103139
};
104140

105141
ExampleDocument = "<Example><Data /></Example>";
@@ -113,6 +149,7 @@ public DataTransformerTool(Workspace workspace) : base(workspace, "Data Transfor
113149
public void UpdatePreview()
114150
{
115151
DataTransformer.ElementPaths = ElementPath.Split('\n').Select(e => e.Trim()).ToList();
152+
DataTransformer.Condition = Condition;
116153
DataTransformer.OutputTemplate = TextEditor.Text;
117154

118155
TransformError = null;
@@ -123,15 +160,12 @@ public void UpdatePreview()
123160
IsDocumentMatch = DataTransformer.TransformDocument(el);
124161

125162
TransformedDocument = el.ToString().Replace(" ", " ");
126-
127-
var builder = new SideBySideDiffBuilder(new Differ());
128-
DiffModel = builder.BuildDiffModel(ExampleDocument, TransformedDocument);
129-
130-
RaisePropertyChangedEvent(nameof(DiffModel));
131163
}
132164
catch (Exception ex)
133165
{
134166
TransformError = ex.Message;
167+
168+
TransformedDocument = "";
135169
}
136170

137171
RaisePropertyChangedEvent(nameof(IsDocumentMatch));

StructuredXmlEditor/View/CustomControls/DataTransformerToolView.xaml

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@
7373
Height="Auto" />
7474
<RowDefinition
7575
Height="Auto" />
76+
<RowDefinition
77+
Height="Auto" />
78+
<RowDefinition
79+
Height="Auto" />
7680
<RowDefinition
7781
Height="*" />
7882
<RowDefinition
@@ -94,18 +98,27 @@
9498
<TextBlock
9599
Grid.Row="2"
96100
Margin="5,5,5,0"
101+
Text="Match condition. (e.g. Child.Child.value == pie || Child.other != null)" />
102+
<local:PromptTextBox
103+
Grid.Row="3"
104+
PromptText="Match Condition"
105+
Text="{Binding Condition, UpdateSourceTrigger=PropertyChanged, Delay=200}" />
106+
107+
<TextBlock
108+
Grid.Row="4"
109+
Margin="5,5,5,0"
97110
Text="Replace template. Access content with {(el,root) | (path) | (contents,name,refkey)}." />
98111
<Border
99112
Background="{StaticResource BackgroundEditBrush}"
100113
BorderThickness="1"
101114
BorderBrush="{StaticResource BorderEditBrush}"
102-
Grid.Row="3">
115+
Grid.Row="5">
103116
<ContentPresenter
104117
Content="{Binding TextEditor}" />
105118
</Border>
106119

107120
<DockPanel
108-
Grid.Row="4"
121+
Grid.Row="6"
109122
Margin="5">
110123
<local:MultilineTextEditor
111124
DockPanel.Dock="Left"
@@ -134,7 +147,7 @@
134147
<ScrollViewer
135148
HorizontalScrollBarVisibility="Disabled"
136149
Margin="5"
137-
Grid.Row="5">
150+
Grid.Row="7">
138151
<Grid>
139152
<Grid.ColumnDefinitions>
140153
<ColumnDefinition

0 commit comments

Comments
 (0)