private static void Main(string[] args) { string filePath = @"..\..\Snippet.snippet"; SnippetFile snippetFile = SnippetSerializer.DeserializeFile(filePath); SnippetCollection snippetCollection = snippetFile.Snippets; if (snippetCollection.Count == 1) { Snippet snippet = snippetCollection[0]; var settings = new SaveSettings() { IndentChars = " ", OmitCodeSnippetsElement = true, OmitXmlDeclaration = true, Comment = " comment " }; string oldValue = File.ReadAllText(filePath, Encoding.UTF8); Console.WriteLine(oldValue); Console.WriteLine(); string newValue = SnippetSerializer.CreateXml(snippet, settings); Console.WriteLine(newValue); Console.WriteLine(); Console.WriteLine(string.Equals(oldValue, newValue, StringComparison.Ordinal)); } Console.WriteLine("*** END ***"); Console.ReadKey(); }
private static void SaveSnippets2(List <Snippet> snippets) { var settings = new SaveSettings() { Comment = XmlComment, OmitCodeSnippetsElement = true, IndentChars = " ", OmitXmlDeclaration = true }; foreach (Snippet snippet in snippets .Select(f => SnippetChecker.CloneAndSortCollections(f))) { Console.WriteLine(); Console.WriteLine($"saving snippet \"{snippet.Title}\""); try { using (var fs = new FileStream(snippet.FilePath, FileMode.Create)) snippet.Save(fs, settings); Console.WriteLine("saved"); } catch (IOException ex) { Console.WriteLine(ex.Message); } } }
private static void SaveSnippets(List <Snippet> snippets) { var settings = new SaveSettings() { Comment = XmlComment, }; foreach (Snippet snippet in snippets .Select(f => SnippetChecker.GetChangedSnippetOrDefault(f)) .Where(f => f != null)) { Console.WriteLine(); Console.WriteLine($"saving snippet \"{snippet.Title}\""); try { snippet.Save(snippet.FilePath + ".modified", settings); Console.WriteLine("saved"); } catch (IOException ex) { Console.WriteLine(ex.Message); } } }
/// <summary> /// Serializes enumerable collection of <see cref="Snippet"/> to text, optionally using <see cref="SaveSettings"/> to modify serialization process. /// </summary> /// <param name="snippets">Enumerable collection of <see cref="Snippet"/> to be serialized.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> /// <returns>XML text that represents a specified collection of <see cref="Snippet"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="snippets"/> or <paramref name="settings"/> is <c>null</c>.</exception> public static string CreateXml(IEnumerable <Snippet> snippets, SaveSettings settings) { if (snippets == null) { throw new ArgumentNullException(nameof(snippets)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } using (var memoryStream = new MemoryStream()) { using (var streamWriter = new StreamWriter(memoryStream, _utf8EncodingNoBom)) { using (XmlWriter xmlWriter = XmlWriter.Create(streamWriter, GetXmlWriterSettings(settings))) Serialize(xmlWriter, snippets, settings); } #if NETFRAMEWORK return(_utf8EncodingNoBom.GetString(memoryStream.ToArray())); #else byte[] bytes = memoryStream.ToArray(); return(_utf8EncodingNoBom.GetString(bytes, 0, bytes.Length)); #endif } }
public static void SaveSnippet(Snippet snippet, string filePath, bool onlyIfChanged = true) { if (snippet == null) { throw new ArgumentNullException(nameof(snippet)); } var settings = new SaveSettings() { OmitXmlDeclaration = true, OmitCodeSnippetsElement = true, IndentChars = " ", Comment = "Copyright (c) Josef Pihrt. All rights reserved. Licensed under the Apache License, Version 2.0." }; if (!onlyIfChanged || !File.Exists(filePath) || !string.Equals( File.ReadAllText(filePath, Encoding.UTF8), SnippetSerializer.CreateXml(snippet, settings), StringComparison.Ordinal)) { using (var fileStream = new FileStream(filePath, FileMode.Create)) { Console.WriteLine($"saving {filePath}"); SnippetSerializer.Serialize(fileStream, snippet, settings); } Console.WriteLine(); } }
public static void SaveSnippet(Snippet snippet, string filePath, bool onlyIfChanged = true) { if (snippet == null) { throw new ArgumentNullException(nameof(snippet)); } SaveSettings settings = CreateSaveSettings(); if (!onlyIfChanged || !File.Exists(filePath) || !string.Equals( File.ReadAllText(filePath, Encoding.UTF8), SnippetSerializer.CreateXml(snippet, settings), StringComparison.Ordinal)) { using (var fileStream = new FileStream(filePath, FileMode.Create)) { Console.WriteLine($"saving {filePath}"); SnippetSerializer.Serialize(fileStream, snippet, settings); } Console.WriteLine(); } }
private static XmlWriterSettings GetXmlWriterSettings(SaveSettings settings) { XmlWriterSettings xmlWriterSettings = XmlWriterSettings; if (!settings.HasDefaultValues) { xmlWriterSettings = CreateXmlWriterSettings(); xmlWriterSettings.IndentChars = settings.IndentChars; xmlWriterSettings.OmitXmlDeclaration = settings.OmitXmlDeclaration; } return(xmlWriterSettings); }
/// <summary> /// Serializes the specified <see cref="SnippetFile"/> to the file, optionally using <see cref="SaveSettings"/> to modify serialization process. /// </summary> /// <param name="snippetFile">A snippet file to be serialized.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> /// <exception cref="ArgumentNullException"><paramref name="snippetFile"/> is <c>null</c>.</exception> /// <exception cref="ArgumentNullException"><paramref name="settings"/> is <c>null</c>.</exception> public static void Serialize(SnippetFile snippetFile, SaveSettings settings) { if (snippetFile == null) { throw new ArgumentNullException(nameof(snippetFile)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } Serialize(snippetFile.FullName, snippetFile.Snippets, settings); }
private static void Main(string[] args) { #if DEBUG string dirPath = @"..\..\..\..\SnippetEssentials\SnippetEssentials.CSharp"; #else string dirPath = @"..\SnippetEssentials\SnippetEssentials.CSharp"; #endif if (!Directory.Exists(dirPath)) { Console.WriteLine($"Directory not found: {dirPath}"); Console.ReadKey(); return; } var snippets = new List <Snippet>(LoadSnippets(dirPath).OrderBy(f => f.FilePath)); var snippetFile = new SnippetFile(Path.Combine(dirPath, "snippets.xml")); if (File.Exists(snippetFile.FullName)) { Console.WriteLine($"File already exists: {snippetFile.FullName}"); Console.ReadKey(); return; } foreach (Snippet snippet in snippets) { string category = Path.GetDirectoryName(snippet.FilePath) .Replace(dirPath, string.Empty) .TrimStart(Path.DirectorySeparatorChar) .Replace(Path.DirectorySeparatorChar, '.'); snippet.Keywords.Add("Category:" + category); snippet.Keywords.Add("FullyQualifiedName:" + category + "." + Path.GetFileNameWithoutExtension(snippet.FilePath)); snippetFile.Snippets.Add(snippet); string relativePath = snippet.FilePath.Replace(dirPath, string.Empty); Console.WriteLine(relativePath); } var saveSettings = new SaveSettings(); SnippetSerializer.Serialize(snippetFile, saveSettings); Console.WriteLine("FINISHED"); Console.ReadKey(); }
/// <summary> /// Maps a specified <see cref="Snippet"/> to the newly created <see cref="CodeSnippetElement"/>, optionally modifying serialization process. /// </summary> /// <param name="snippet">A <see cref="Snippet"/> to be serialized.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> /// <returns>Newly created <see cref="CodeSnippetElement"/>.</returns> public static CodeSnippetElement MapToElement(Snippet snippet, SaveSettings settings) { if (snippet == null) throw new ArgumentNullException(nameof(snippet)); if (settings == null) throw new ArgumentNullException(nameof(settings)); var context = new SerializationContext(snippet, settings); SerializeVersion(snippet.FormatVersion, context); context.Element.Header = CreateHeaderElement(context); context.Element.Snippet = CreateSnippetElement(context); return context.Element; }
/// <summary> /// Serializes enumerable collection of <see cref="Snippet"/> to the specified <see cref="Stream"/>, optionally using <see cref="SaveSettings"/> to modify serialization process. /// </summary> /// <param name="stream">The stream to output this <see cref="Snippet"/> to.</param> /// <param name="snippets">Enumerable collection of <see cref="Snippet"/> to be serialized.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> /// <exception cref="ArgumentNullException"><paramref name="stream"/> or <paramref name="snippets"/> or <paramref name="settings"/> is <c>null</c>.</exception> public static void Serialize(Stream stream, IEnumerable <Snippet> snippets, SaveSettings settings) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (snippets == null) { throw new ArgumentNullException(nameof(snippets)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } using (XmlWriter xmlWriter = XmlWriter.Create(stream, GetXmlWriterSettings(settings))) Serialize(xmlWriter, snippets, settings); }
/// <summary> /// Serializes the specified <see cref="Snippet"/> the specified snippet file, optionally using <see cref="SaveSettings"/> to modify serialization process. /// </summary> /// <param name="filePath">The absolute or relative path to the file.</param> /// <param name="snippet">A <see cref="Snippet"/> to be serialized.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> /// <exception cref="ArgumentNullException"><paramref name="filePath"/> or <paramref name="snippet"/> or <paramref name="settings"/> is <c>null</c>.</exception> public static void Serialize(string filePath, Snippet snippet, SaveSettings settings) { if (filePath == null) { throw new ArgumentNullException(nameof(filePath)); } if (snippet == null) { throw new ArgumentNullException(nameof(snippet)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } using (var stream = new FileStream(filePath, FileMode.CreateNew, FileAccess.Write, FileShare.None)) Serialize(stream, snippet, settings); }
/// <summary> /// Maps a specified <see cref="Snippet"/> to the newly created <see cref="CodeSnippetElement"/>, optionally modifying serialization process. /// </summary> /// <param name="snippet">A <see cref="Snippet"/> to be serialized.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> /// <returns>Newly created <see cref="CodeSnippetElement"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="snippet"/> or <paramref name="settings"/> is <c>null</c>.</exception> public static CodeSnippetElement MapToElement(Snippet snippet, SaveSettings settings) { if (snippet == null) { throw new ArgumentNullException(nameof(snippet)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } var context = new SerializationContext(snippet, settings); SerializeVersion(snippet.FormatVersion, context); context.Element.Header = CreateHeaderElement(context); context.Element.Snippet = CreateSnippetElement(context); return(context.Element); }
/// <summary> /// Maps each element of <see cref="CodeSnippetElement"/> sequence to the newly created <see cref="CodeSnippetElement"/>, optionally modifying serialization process. /// </summary> /// <param name="snippets">An enumerable collection of code snippets to be serialized.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> /// <returns>An enumerable collection of <see cref="CodeSnippetElement"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="snippets"/> is <c>null</c>.</exception> public static IEnumerable <CodeSnippetElement> MapToElement(IEnumerable <Snippet> snippets, SaveSettings settings) { if (snippets == null) { throw new ArgumentNullException(nameof(snippets)); } if (settings == null) { throw new ArgumentNullException(nameof(settings)); } return(MapToElement()); IEnumerable <CodeSnippetElement> MapToElement() { foreach (Snippet snippet in snippets) { yield return(SnippetMapper.MapToElement(snippet, settings)); } } }
private static void Serialize(XmlWriter xmlWriter, CodeSnippetElement[] elements, SaveSettings settings) { xmlWriter.WriteStartDocument(); if (!string.IsNullOrEmpty(settings.Comment)) { xmlWriter.WriteComment(settings.Comment); } if (settings.OmitCodeSnippetsElement && elements.Length == 1) { CodeSnippetElementXmlSerializer.Serialize(xmlWriter, elements[0], Namespaces); } else { CodeSnippetsElementXmlSerializer.Serialize(xmlWriter, new CodeSnippetsElement() { Snippets = elements }, Namespaces); } }
private static void Serialize(XmlWriter xmlWriter, IEnumerable <Snippet> snippets, SaveSettings settings) { Serialize(xmlWriter, SnippetMapper.MapToElement(snippets, settings).ToArray(), settings); }
private static void Serialize(XmlWriter xmlWriter, Snippet snippet, SaveSettings settings) { Serialize(xmlWriter, new CodeSnippetElement[] { SnippetMapper.MapToElement(snippet, settings) }, settings); }
/// <summary> /// Serializes the current instance to the specified file. /// </summary> /// <param name="filePath">The absolute or relative path to the file.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> public void Save(string filePath, SaveSettings settings) { SnippetSerializer.Serialize(filePath, this, settings); }
/// <summary> /// Serializes the current instance to the specified <see cref="Stream"/>, optionally specifying serialization process. /// </summary> /// <param name="stream">The stream to output this <see cref="Snippet"/> to.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> public void Save(Stream stream, SaveSettings settings) { SnippetSerializer.Serialize(stream, this, settings); }
/// <summary> /// Initializes a new instance of the <see cref="SerializationContext"/> class with a specified code snippet and and settings. /// </summary> /// <param name="snippet">A <see cref="Snippet"/> instance to serialize.</param> /// <param name="settings">A <see cref="SaveSettings"/> that enables to modify code snippet serialization process.</param> public SerializationContext(Snippet snippet, SaveSettings settings) { Snippet = snippet; Settings = settings; Element = new CodeSnippetElement(); }
/// <summary> /// Maps each element of <see cref="CodeSnippetElement"/> sequence to the newly created <see cref="CodeSnippetElement"/>, optionally modifying serialization process. /// </summary> /// <param name="snippets">An enumerable collection of code snippets to be serialized.</param> /// <param name="settings">A <see cref="SaveSettings"/> that modify serialization process.</param> /// <returns>An enumerable collection of <see cref="CodeSnippetElement"/>.</returns> public static IEnumerable<CodeSnippetElement> MapToElement(IEnumerable<Snippet> snippets, SaveSettings settings) { if (snippets == null) throw new ArgumentNullException(nameof(snippets)); if (settings == null) throw new ArgumentNullException(nameof(settings)); foreach (Snippet snippet in snippets) yield return MapToElement(snippet, settings); }