99using System . Linq ;
1010using System . Text ;
1111using System . Threading . Tasks ;
12+ using System . Xml ;
1213using System . Xml . Linq ;
1314
1415namespace StructuredXmlEditor . Tools
@@ -73,9 +74,17 @@ public string ExampleDocument
7374 public string TransformError { get ; set ; }
7475 public SideBySideDiffModel DiffModel { get ; set ; }
7576
77+ //-----------------------------------------------------------------------
78+ public TransformPreview Preview { get ; set ; }
79+
7680 //-----------------------------------------------------------------------
7781 public Command < object > DoTransformCMD { get { return new Command < object > ( ( e ) => DoTransform ( ) ) ; } }
7882
83+ //-----------------------------------------------------------------------
84+ public Command < object > ReturnCMD { get { return new Command < object > ( ( e ) => Return ( ) ) ; } }
85+ public Command < object > SaveCMD { get { return new Command < object > ( ( e ) => Save ( ) ) ; } }
86+ public Command < object > SaveAllCMD { get { return new Command < object > ( ( e ) => SaveAll ( ) ) ; } }
87+
7988 //-----------------------------------------------------------------------
8089 public DataTransformerTool ( Workspace workspace ) : base ( workspace , "Data Transformer Tool" )
8190 {
@@ -118,7 +127,7 @@ public void UpdatePreview()
118127 //-----------------------------------------------------------------------
119128 public void DoTransform ( )
120129 {
121- var transformCount = 0 ;
130+ var preview = new TransformPreview ( ) ;
122131
123132 var projectDir = Path . GetDirectoryName ( Workspace . ProjectRoot ) ;
124133 var files = Directory . EnumerateFiles ( projectDir , "*" , SearchOption . AllDirectories ) . ToList ( ) ;
@@ -130,18 +139,98 @@ public void DoTransform()
130139 {
131140 try
132141 {
133- var success = DataTransformer . TransformDocument ( file ) ;
142+ var doc = XDocument . Load ( file ) ;
143+ var transformed = DataTransformer . TransformDocument ( doc . Root ) ;
144+ var asString = new StringBuilder ( ) ;
134145
135- if ( success )
146+ XmlWriterSettings settings = new XmlWriterSettings
147+ {
148+ Indent = true ,
149+ IndentChars = "\t " ,
150+ NewLineChars = "\r \n " ,
151+ NewLineHandling = NewLineHandling . Replace ,
152+ OmitXmlDeclaration = true ,
153+ Encoding = new UTF8Encoding ( false )
154+ } ;
155+
156+ using ( XmlWriter writer = XmlTextWriter . Create ( asString , settings ) )
136157 {
137- transformCount ++ ;
158+ doc . Save ( writer ) ;
159+ }
160+
161+ var original = File . ReadAllText ( file ) ;
162+
163+ var builder = new SideBySideDiffBuilder ( new Differ ( ) ) ;
164+ var diff = builder . BuildDiffModel ( original , asString . ToString ( ) ) ;
165+
166+ if ( transformed )
167+ {
168+ preview . Files . Add ( new Tuple < string , string , string , SideBySideDiffModel > ( file , Path . GetFileNameWithoutExtension ( file ) , asString . ToString ( ) , diff ) ) ;
138169 }
139170 }
140171 catch ( Exception ) { }
141172 }
142173 }
143174
144- Message . Show ( $ "Transformed { transformCount } files.", "Completed transform" ) ;
175+ if ( preview . Files . Count == 0 )
176+ {
177+ Message . Show ( "No matching files found in project" , "Completed transform" ) ;
178+ }
179+ else
180+ {
181+ Preview = preview ;
182+ RaisePropertyChangedEvent ( nameof ( Preview ) ) ;
183+ }
184+ }
185+
186+ //-----------------------------------------------------------------------
187+ public void Return ( )
188+ {
189+ Preview = null ;
190+ RaisePropertyChangedEvent ( nameof ( Preview ) ) ;
191+ }
192+
193+ //-----------------------------------------------------------------------
194+ public void Save ( )
195+ {
196+ var selected = Preview ? . Selected ;
197+
198+ if ( selected == null ) return ;
199+
200+ File . WriteAllText ( selected . Item1 , selected . Item3 ) ;
201+ Preview . Files . Remove ( selected ) ;
202+ Preview . Selected = null ;
203+
204+ if ( Preview . Files . Count == 0 )
205+ {
206+ Return ( ) ;
207+ }
208+ }
209+
210+ //-----------------------------------------------------------------------
211+ public void SaveAll ( )
212+ {
213+ foreach ( var file in Preview . Files )
214+ {
215+ File . WriteAllText ( file . Item1 , file . Item3 ) ;
216+ }
217+ Return ( ) ;
218+ }
219+ }
220+
221+ //-----------------------------------------------------------------------
222+ public class TransformPreview : NotifyPropertyChanged
223+ {
224+ public DeferableObservableCollection < Tuple < string , string , string , SideBySideDiffModel > > Files { get ; } = new DeferableObservableCollection < Tuple < string , string , string , SideBySideDiffModel > > ( ) ;
225+ public Tuple < string , string , string , SideBySideDiffModel > Selected
226+ {
227+ get { return m_selected ; }
228+ set
229+ {
230+ m_selected = value ;
231+ RaisePropertyChangedEvent ( ) ;
232+ }
145233 }
234+ private Tuple < string , string , string , SideBySideDiffModel > m_selected ;
146235 }
147236}
0 commit comments