This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor, File Merger Reorganization
- Loading branch information
Showing
51 changed files
with
103,298 additions
and
445 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,162 @@ | ||
using System; | ||
using System.Data; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Xml; | ||
using System.Xml.Serialization; | ||
using DiffPlex; | ||
using DiffPlex.DiffBuilder; | ||
using DiffPlex.DiffBuilder.Model; | ||
using PiggySync.Domain; | ||
using PiggySync.Domain.Concrete; | ||
using PiggySync.Model; | ||
using System.Xml.Linq; | ||
using PiggySync.Common; | ||
|
||
namespace PiggySync.FileMerger | ||
{ | ||
public class FileMerger | ||
{ | ||
private readonly string fileAPath; | ||
private readonly string fileBPath; | ||
private readonly string fileCPath; | ||
private readonly string resultPath; | ||
private MergePattern pattern; | ||
bool usePattern; | ||
|
||
public FileMerger (string fileAPath, string fileBPath, string fileCPath, string resultPath) | ||
{ | ||
string extension; | ||
if ((extension = Path.GetExtension (fileAPath)) != Path.GetExtension (fileBPath)) | ||
{ | ||
throw new PiggyFileException ("File extensions dosn't math"); | ||
} | ||
var file = FileTools.TextFile (extension, fileAPath, fileBPath); | ||
usePattern = string.IsNullOrWhiteSpace (file.TemplatePath); | ||
|
||
if (!usePattern) | ||
{ | ||
var serializer = new XmlSerializer (typeof(MergePattern)); | ||
pattern = (MergePattern)serializer.Deserialize (File.OpenRead (file.TemplatePath)); | ||
} | ||
this.fileAPath = fileAPath; | ||
this.fileBPath = fileBPath; | ||
this.fileCPath = fileCPath; | ||
this.resultPath = resultPath; | ||
} | ||
|
||
public bool MergeFiles () | ||
{ | ||
string fileA = File.ReadAllText (fileAPath); | ||
string fileB = File.ReadAllText (fileBPath); | ||
string fileC = File.ReadAllText (fileCPath); | ||
string merged = String.Empty; | ||
|
||
var differ = new Differ (); | ||
var inlineBuilder = new InlineDiffBuilder (differ); | ||
var diferenceAB = inlineBuilder.BuildDiffModel (fileA, fileB); | ||
var diferenceAC = inlineBuilder.BuildDiffModel (fileA, fileC); | ||
|
||
foreach (var line in diferenceAB.Lines) | ||
{ | ||
if (line.Type == ChangeType.Inserted) | ||
{ | ||
merged = String.Format ("{0}\n{1}", merged, line.Text); | ||
} | ||
else if (line.Type == ChangeType.Deleted) | ||
{ | ||
//merged += String.Format ("- {0}\n", line.Text); | ||
} | ||
else | ||
{ | ||
merged += String.Format ("{0}\n", line.Text); | ||
} | ||
} | ||
File.WriteAllText (resultPath, merged); | ||
return Validator.ValidateXML (ref merged); | ||
} | ||
} | ||
public class FileMerger | ||
{ | ||
private readonly string fileAPath; | ||
private readonly string fileBPath; | ||
private readonly string resultPath; | ||
private MergePattern pattern; | ||
private string extension; | ||
|
||
public FileMerger(string fileAPath, string fileBPath, string resultPath) | ||
{ | ||
if ((extension = Path.GetExtension(fileAPath)) != Path.GetExtension(fileBPath)) | ||
{ | ||
throw new PiggyFileException("File extensions dosn't math"); | ||
} | ||
|
||
this.fileAPath = fileAPath; | ||
this.fileBPath = fileBPath; | ||
this.resultPath = resultPath; | ||
} | ||
|
||
public bool MergeFiles() | ||
{ | ||
var file = FileTools.TextFile(extension, fileAPath, fileBPath); | ||
if (file == null) | ||
{ | ||
throw new PiggyFileException("Not a text file"); | ||
} | ||
if (!string.IsNullOrWhiteSpace(file.TemplatePath)) | ||
{ | ||
var serializer = new XmlSerializer(typeof (MergePattern)); | ||
pattern = (MergePattern) serializer.Deserialize(File.OpenRead(file.TemplatePath)); | ||
} | ||
switch (extension) | ||
{ | ||
case "xml": | ||
return MergeXmlFiles(); // && Validator.va; | ||
case "json": | ||
return MergeJsonFile(); | ||
default: | ||
if (pattern != null) | ||
{ | ||
return ConvertAndMerge(pattern); | ||
} | ||
return MergeTextFiles(); | ||
} | ||
} | ||
|
||
private bool ConvertAndMerge(MergePattern pattern) | ||
{ | ||
try | ||
{ | ||
var converter = new Converter(); | ||
var text1 = File.ReadAllText(fileAPath); | ||
var text2 = File.ReadAllText(fileAPath); | ||
|
||
var xmlReader1 = new XmlTextReader(new StringReader(converter.FileToXml(text1,pattern))); | ||
var xmlReader2 = new XmlTextReader(new StringReader(converter.FileToXml(text2,pattern))); | ||
|
||
var mergedXml = MergeXmlStreams(xmlReader1, xmlReader2); | ||
var mergedText = converter.XmlToFile(mergedXml,pattern); | ||
File.WriteAllText(resultPath, mergedText); | ||
return true; | ||
} | ||
catch (System.Exception ex) | ||
{ | ||
Debug.WriteLine(ex.Message); | ||
return false; | ||
} | ||
} | ||
|
||
public bool MergeTextFiles() | ||
{ | ||
string fileA = File.ReadAllText(fileAPath); | ||
string fileB = File.ReadAllText(fileBPath); | ||
string merged = String.Empty; | ||
|
||
var differ = new Differ(); | ||
var inlineBuilder = new InlineDiffBuilder(differ); | ||
var diferenceA = inlineBuilder.BuildDiffModel(fileB, fileA); | ||
|
||
foreach (var diffPieceA in diferenceA.Lines) | ||
{ | ||
if (diffPieceA.Type == ChangeType.Inserted) | ||
{ | ||
merged = String.Format("{0}\n{1}", merged, diffPieceA.Text); | ||
} | ||
else if (diffPieceA.Type != ChangeType.Deleted) | ||
{ | ||
merged += String.Format("{0}\n", diffPieceA.Text); | ||
} | ||
} | ||
File.WriteAllText(resultPath, merged); | ||
return true; | ||
} | ||
|
||
private bool MergeXmlFiles() | ||
{ | ||
try | ||
{ | ||
var xmlReader1 = new XmlTextReader(fileAPath); | ||
var xmlReader2 = new XmlTextReader(fileAPath); | ||
|
||
var merged = MergeXmlStreams(xmlReader1, xmlReader2); | ||
File.WriteAllText(resultPath, merged); | ||
return true; | ||
} | ||
catch (System.Exception ex) | ||
{ | ||
Debug.WriteLine(ex.Message); | ||
return false; | ||
} | ||
|
||
} | ||
|
||
private string MergeXmlStreams(XmlTextReader xmlreader1, XmlTextReader xmlreader2) | ||
{ | ||
var ds = new DataSet(); | ||
ds.ReadXml(xmlreader1); | ||
var ds2 = new DataSet(); | ||
ds2.ReadXml(xmlreader2); | ||
ds.Merge(ds2); | ||
return ds.GetXml(); | ||
} | ||
|
||
private bool MergeJsonFile() | ||
{ | ||
try | ||
{ | ||
var converter = new Converter(); | ||
var json1 = File.ReadAllText(fileAPath); | ||
var json2 = File.ReadAllText(fileAPath); | ||
|
||
var xmlReader1 = new XmlTextReader(new StringReader(converter.JsonToXml(json1))); | ||
var xmlReader2 = new XmlTextReader(new StringReader(converter.JsonToXml(json2))); | ||
|
||
var mergedXml = MergeXmlStreams(xmlReader1, xmlReader2); | ||
var mergedJson = converter.XmlToJson(mergedXml); | ||
File.WriteAllText(resultPath, mergedJson); | ||
return true; | ||
} | ||
catch (System.Exception ex) | ||
{ | ||
Debug.WriteLine(ex.Message); | ||
return false; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,27 @@ | ||
using System; | ||
using System.IO; | ||
|
||
namespace PiggySync.FileMerger | ||
{ | ||
public class Program | ||
{ | ||
public static void main(string[] args) | ||
{ | ||
if (args.Length != 5) | ||
{ | ||
Console.WriteLine ("USAGE: {0} filAPath, fileBPath, fileCPath, resultPath", args [0]); | ||
} | ||
else | ||
{ | ||
if (new FileMerger (args [1], args [2], args [3], args [4]).MergeFiles ()) | ||
{ | ||
Console.WriteLine ("Succesfull merge"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine ("Merge failed."); | ||
} | ||
} | ||
} | ||
} | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
if (args.Length != 5) | ||
{ | ||
Console.WriteLine("USAGE: {0} filAPath, fileBPath, resultPath", args[0]); | ||
} | ||
else | ||
{ | ||
if (new FileMerger(args[1], args[2], args[3]).MergeTextFiles()) | ||
{ | ||
Console.WriteLine("Succesfull merge"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Merge failed."); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.