public void NewLinesRoundtrip(string value)
        {
            var sw = new StringWriter();
            var yw = new YamlWriter(sw);

            yw.WriteProperty("Foo", value);

            var text = sw.ToString();

            // Validate it passes YamlDotNet
            var valueFromYaml = ParseSinglePropertyViaYamlDotNot(text);
            var valueWithoutR = value.Replace("\r", ""); // yamlDotNet doesn't do \R

            Assert.AreEqual(valueWithoutR, valueFromYaml);

            // Validate it passes our subset.
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);
            var p  = y.ReadNext();

            Assert.AreEqual(YamlTokenKind.Property, p.Kind);
            Assert.AreEqual("Foo", p.Property);

            Assert.AreEqual(value, p.Value);
        }
Exemplo n.º 2
0
        public void Write1()
        {
            var sw = new StringWriter();
            var yw = new YamlWriter(sw);

            yw.WriteProperty("P0", "abc");
            yw.WriteStartObject("Obj1");
            yw.WriteProperty("P1a", "A#B");     // induced multiline, |- since no trailing \n
            yw.WriteProperty("P1b", "B");
            yw.WriteStartObject("Obj2");
            yw.WriteProperty("P2a", "A");
            yw.WriteEndObject();
            yw.WriteProperty("P1c", "C");
            yw.WriteEndObject();

            var t        = sw.ToString();
            var expected = @"P0: =abc
Obj1:
    P1a: |-
        =A#B
    P1b: =B
    Obj2:
        P2a: =A
    P1c: =C
";

            Assert.AreEqual(expected.Replace("\r\n", "\n"), t.Replace("\r\n", "\n"));
        }
Exemplo n.º 3
0
        public virtual void WriteSerializedRole(IRoleData roleData, Stream outputStream)
        {
            Assert.ArgumentNotNull(roleData, nameof(roleData));
            Assert.ArgumentNotNull(outputStream, "outputStream");

            var memberOfRoles = roleData.MemberOfRoles;

            using (var writer = new YamlWriter(outputStream, 4096, true))
            {
                writer.WriteMap("Role", roleData.RoleName);

                if (memberOfRoles.Any())
                {
                    writer.WriteMap("MemberOf");
                    writer.IncreaseIndent();

                    foreach (var memberOfRole in memberOfRoles)
                    {
                        writer.WriteMap("Role", memberOfRole);
                    }

                    writer.DecreaseIndent();
                }
            }
        }
Exemplo n.º 4
0
        private static void WriteAnything(YamlWriter yaml, string propName, object obj)
        {
            if (obj == null)
            {
                // Exclude default values.
                // This provides more stable output as new things are added in the future.
                return;
            }
            else if (obj is bool valBool)
            {
                // Exclude default values.
                if (valBool)
                {
                    yaml.WriteProperty(propName, valBool);
                }
            }
            else if (obj is string valString)
            {
                yaml.WriteProperty(propName, valString, false);
            }
            else if (obj is int valInt)
            {
                yaml.WriteProperty(propName, valInt);
            }
            else if (obj is double valDouble)
            {
                yaml.WriteProperty(propName, valDouble);
            }
            else if (obj.GetType().IsEnum)
            {
                var valEnum = obj.ToString();
                yaml.WriteProperty(propName, valEnum, false);
            }
            else
            {
                // Dictionary / nested object?
                if (obj is IDictionary dict)
                {
                    if (dict.Count > 0)
                    {
                        var list = new List <KeyValuePair <string, object> >();
                        foreach (DictionaryEntry kv in dict)
                        {
                            list.Add(new KeyValuePair <string, object>((string)kv.Key, kv.Value));
                        }


                        yaml.WriteStartObject(propName);
                        WriteCanonicalList(yaml, list);
                        yaml.WriteEndObject();
                    }
                }
                else
                {
                    yaml.WriteStartObject(propName);
                    WriteObject(yaml, obj);
                    yaml.WriteEndObject();
                }
            }
        }
Exemplo n.º 5
0
        public void NewLinesRoundtrip(string value)
        {
            var sw = new StringWriter();
            var yw = new YamlWriter(sw);

            yw.WriteProperty("Foo", value);

            var text = sw.ToString();

            var normalizedValue = NormNewlines(value);

            // Validate it passes YamlDotNet
            var valueFromYaml = ParseSinglePropertyViaYamlDotNot(text);

            Assert.AreEqual(normalizedValue, NormNewlines(valueFromYaml));

            // Validate it passes our subset.
            var sr = new StringReader(text);
            var y  = new YamlLexer(sr);
            var p  = y.ReadNext();

            Assert.AreEqual(YamlTokenKind.Property, p.Kind);
            Assert.AreEqual("Foo", p.Property);

            Assert.AreEqual(normalizedValue, NormNewlines(p.Value));
        }
Exemplo n.º 6
0
        public void TestBasic()
        {
            var yaml = @"Id: Google.Chrome
Name: Chrome
Publisher: Google
Version: 78.0.3904.87
Homepage: https://www.google.com/chrome/
InstallerType: MSI
AppMoniker: chrome
License: Google Chrome and Chrome OS Additional Terms of Service
LicenseUrl: https://www.google.com/chrome/terms/

# ======================= Installers  ==================


Installers:
  - Arch: x64
    Url: https://dl.google.com/edgedl/chrome/install/GoogleChromeStandaloneEnterprise64.msi
    Sha256: F17FDA159EC69C0F9E96656642E5F2A5DE547BED784978A354C51D70E88C93DC
    Language: en-US";


            var reader = new YamlReader();

            using TextReader textReader = new StringReader(yaml);
            var deserialized = reader.Read(textReader);
            var writer       = new YamlWriter();

            using TextWriter textWriter = new StringWriter();
            writer.Write(deserialized, textWriter);
        }
Exemplo n.º 7
0
        public void WriteYaml(YamlWriter writer)
        {
            writer.WriteBeginListItem("Language", Language);

            if (UnversionedFields.Count > 0)
            {
                writer.WriteMap("Fields");
                writer.IncreaseIndent();

                foreach (var unversionedField in UnversionedFields)
                {
                    unversionedField.WriteYaml(writer);
                }

                writer.DecreaseIndent();
            }

            writer.WriteMap("Versions");

            writer.IncreaseIndent();

            foreach (var version in Versions)
            {
                version.WriteYaml(writer);
            }

            writer.DecreaseIndent();
        }
Exemplo n.º 8
0
        public void WriteYaml(YamlWriter writer)
        {
            // at times we may receive an empty language with no fields or versions
            // we avoid writing it the output, as it's irrelevant
            if (UnversionedFields.Count == 0 && Versions.Count == 0)
            {
                return;
            }

            writer.WriteBeginListItem("Language", Language);

            if (UnversionedFields.Count > 0)
            {
                writer.WriteMap("Fields");
                writer.IncreaseIndent();

                foreach (var unversionedField in UnversionedFields)
                {
                    unversionedField.WriteYaml(writer);
                }

                writer.DecreaseIndent();
            }

            writer.WriteMap("Versions");

            writer.IncreaseIndent();

            foreach (var version in Versions)
            {
                version.WriteYaml(writer);
            }

            writer.DecreaseIndent();
        }
Exemplo n.º 9
0
        // Write a list of properties (such as an object or dictionary) in an ordered way.
        private static void WriteCanonicalList(YamlWriter yaml, List <KeyValuePair <string, object> > list)
        {
            // Critical to sort to preserve a canonical order.
            list.Sort((kv1, kv2) => kv1.Key.CompareTo(kv2.Key));

            foreach (var kv in list)
            {
                WriteAnything(yaml, kv.Key, kv.Value);
            }
        }
Exemplo n.º 10
0
        public void RoundTrip_does_not_report_parsing_errors()
        {
            var builder = new StringBuilder();

            using (var writer = new StringWriter(builder))
            {
                YamlWriter.Write(writer, _objectUnderTest);
            }

            Assert.That(builder.ToString(), Does.Contain("parsingErrorsDetected: false"));
        }
Exemplo n.º 11
0
        // Write the object out to the stream in a canonical way. Such as:
        // - object ordering
        // - encodings
        // - multi-line
        // - newlines
        public static void CanonicalWrite(TextWriter writer, object obj)
        {
            if (obj == null)
            {
                throw new ArgumentNullException(nameof(obj));
            }

            var yaml = new YamlWriter(writer);

            WriteObject(yaml, obj);
        }
Exemplo n.º 12
0
        public static void Write(TextWriter writer, IDictionary <string, string> properties)
        {
            var yaml = new YamlWriter(writer);

            // Sort by keys to enforce canonical format.
            foreach (var kv in properties.OrderBy(x => x.Key))
            {
                yaml.WriteProperty(kv.Key, kv.Value);
            }
            writer.Flush();
        }
Exemplo n.º 13
0
        public void WriteEscapes(string value)
        {
            var sw = new StringWriter();
            var yw = new YamlWriter(sw);

            yw.WriteProperty("Foo", value);

            var text = sw.ToString();

            // We use a | escape.
            Assert.IsTrue(text.StartsWith("Foo: |"));
        }
Exemplo n.º 14
0
        private static string CreateYaml(File file)
        {
            var builder = new StringBuilder();

            using (var writer = new StringWriter(builder))
            {
                YamlWriter.Write(writer, file);
            }

            var yaml = builder.ToString();

            return(yaml);
        }
        public static string PrettyPrint(IRNode node)
        {
            StringWriter    sw     = new StringWriter();
            var             yaml   = new YamlWriter(sw);
            PAWriterVisitor pretty = new PAWriterVisitor();

            var context = new Context
            {
                _yaml = yaml
            };

            node.Accept(pretty, context);

            return(sw.ToString());
        }
Exemplo n.º 16
0
        public void WriteYaml(YamlWriter writer)
        {
            writer.WriteBeginListItem("Version", VersionNumber.ToString());

            if (Fields.Any())
            {
                writer.WriteMap("Fields");
                writer.IncreaseIndent();

                foreach (var field in Fields)
                {
                    field.WriteYaml(writer);
                }

                writer.DecreaseIndent();
            }
        }
Exemplo n.º 17
0
        private static void WriteObject(YamlWriter yaml, object obj)
        {
            // Generic object.
            var outerType = obj.GetType();

            var list = new List <KeyValuePair <string, object> >();

            foreach (var prop in outerType.GetProperties(BindingFlags.Instance | BindingFlags.Public)
                     .OrderBy(x => x.Name))
            {
                var obj3 = prop.GetValue(obj);

                list.Add(new KeyValuePair <string, object>(prop.Name, obj3));
            } // loop properties

            WriteCanonicalList(yaml, list);
        }
        public virtual void WriteSerializedUser(SyncUser userData, Stream outputStream)
        {
            Assert.ArgumentNotNull(userData, nameof(userData));
            Assert.ArgumentNotNull(outputStream, "outputStream");

            using (var writer = new YamlWriter(outputStream, 4096, true))
            {
                writer.WriteMap("Username", userData.UserName);
                writer.WriteMap("Email", userData.Email);
                writer.WriteMap("Comment", userData.Comment ?? string.Empty);
                writer.WriteMap("Created", userData.CreationDate.ToString("O"));
                writer.WriteMap("IsApproved", userData.IsApproved.ToString());

                if (userData.ProfileProperties.Any())
                {
                    writer.WriteMap("Properties");
                    writer.IncreaseIndent();

                    userData.ProfileProperties.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.Ordinal));
                    userData.ProfileProperties.ForEach(profileProperty =>
                    {
                        writer.WriteBeginListItem("Key", profileProperty.Name);
                        writer.WriteMap("Value", GetSerializedProfileContent(profileProperty));
                        writer.WriteMap("ValueType", profileProperty.Content.GetType().AssemblyQualifiedName);
                        writer.WriteMap("IsCustom", profileProperty.IsCustomProperty.ToString());
                    });

                    writer.DecreaseIndent();
                }

                if (userData.Roles.Any())
                {
                    userData.Roles.Sort();

                    writer.WriteMap("Roles");
                    writer.IncreaseIndent();

                    userData.Roles.ForEach(roleName =>
                    {
                        writer.WriteMap("MemberOf", roleName);
                    });

                    writer.DecreaseIndent();
                }
            }
        }
Exemplo n.º 19
0
        private void ExecuteYamlWriter(Action <YamlWriter> actions, string expectedOutput)
        {
            using (var ms = new MemoryStream())
            {
                using (var writer = new YamlWriter(ms, 4096, true))
                {
                    actions(writer);
                }

                ms.Position = 0;

                using (var sr = new StreamReader(ms))
                {
                    string result = sr.ReadToEnd();
                    Assert.Equal(expectedOutput, result);
                }
            }
        }
Exemplo n.º 20
0
		private void ExecuteYamlWriter(Action<YamlWriter> actions, string expectedOutput)
		{
			using (var ms = new MemoryStream())
			{
				using (var writer = new YamlWriter(ms, 4096, true))
				{
					actions(writer);
				}

				ms.Position = 0;

				using (var sr = new StreamReader(ms))
				{
					string result = sr.ReadToEnd();
					Assert.Equal(expectedOutput, result);
				}
			}
		}
Exemplo n.º 21
0
        private void ExecuteYamlWriter(Action <YamlWriter> actions, string expectedOutput, string errorMessage)
        {
            using (var ms = new MemoryStream())
            {
                using (var writer = new YamlWriter(ms, 4096, true))
                {
                    actions(writer);
                }

                ms.Position = 0;

                using (var sr = new StreamReader(ms))
                {
                    string result = sr.ReadToEnd();

                    result.Should().Be(expectedOutput, errorMessage);
                }
            }
        }
Exemplo n.º 22
0
 internal static unsafe string Dump(IList<YDocument> documents)
 {
     IntPtr pNativeEmitter;
     var writer = new YamlWriter();
     if (1 != CreateEmitter(&pNativeEmitter, WriteYamlContent))
     {
         throw new Exception("Failed to create a native emitter");
     }
     try
     {
         emitters.Add(pNativeEmitter, writer);
         GenerateEvents(pNativeEmitter, documents);
         emitters.Remove(pNativeEmitter);
     }
     finally
     {
         DestroyEmitter(pNativeEmitter);
     }
     return writer.Builder.ToString();
 }
Exemplo n.º 23
0
        public void WriteYaml(YamlWriter writer)
        {
            writer.WriteBeginListItem("ID", Id.ToString("D"));

            if (!string.IsNullOrWhiteSpace(NameHint))
            {
                writer.WriteMap("Hint", NameHint);
            }

            if (BlobId.HasValue)
            {
                writer.WriteMap("BlobID", BlobId.Value.ToString("D"));
            }

            if (Type != null)
            {
                writer.WriteMap("Type", Type);
            }

            writer.WriteMap("Value", Value);
        }
Exemplo n.º 24
0
        public virtual void WriteYaml(YamlWriter writer)
        {
            writer.WriteMap("ID", Id.ToString("D"));
            writer.WriteMap("Parent", ParentId.ToString("D"));
            writer.WriteMap("Template", TemplateId.ToString("D"));
            writer.WriteMap("Path", Path);
            writer.WriteMap("DB", DatabaseName);

            if (BranchId != default(Guid))
            {
                writer.WriteMap("BranchID", BranchId.ToString());
            }

            if (SharedFields.Any())
            {
                writer.WriteMap("SharedFields");
                writer.IncreaseIndent();

                foreach (var field in SharedFields)
                {
                    field.WriteYaml(writer);
                }

                writer.DecreaseIndent();
            }

            if (Languages.Any())
            {
                writer.WriteMap("Languages");
                writer.IncreaseIndent();

                foreach (var language in Languages)
                {
                    language.WriteYaml(writer);
                }

                writer.DecreaseIndent();
            }
        }
Exemplo n.º 25
0
		public virtual void WriteYaml(YamlWriter writer)
		{
			writer.WriteMap("ID", Id.ToString("D"));
			writer.WriteMap("Parent", ParentId.ToString("D"));
			writer.WriteMap("Template", TemplateId.ToString("D"));
			writer.WriteMap("Path", Path);
			writer.WriteMap("DB", DatabaseName);

			if (BranchId != default(Guid)) writer.WriteMap("BranchID", BranchId.ToString());

			if (SharedFields.Any())
			{
				writer.WriteMap("SharedFields");
				writer.IncreaseIndent();

				foreach (var field in SharedFields)
				{
					field.WriteYaml(writer);
				}

				writer.DecreaseIndent();
			}

			if (Languages.Any())
			{
				writer.WriteMap("Languages");
				writer.IncreaseIndent();

				foreach (var language in Languages)
				{
					language.WriteYaml(writer);
				}

				writer.DecreaseIndent();
			}
		}
        public static async Task <int> Main(string[] args)
        {
            // check for GMaster or PlasticSCM or SemanticMerge arguments (to allow debugging without the tools)
            if (args.Length == 2)
            {
                var shell    = args[0]; // reserved for future usage
                var flagFile = args[1];

                SystemFile.WriteAllBytes(flagFile, new byte[] { 0x42 });
            }

            var watch   = Stopwatch.StartNew();
            var gcWatch = Stopwatch.StartNew();

            while (true)
            {
                var inputFile = await Console.In.ReadLineAsync();

                if (inputFile == null || "end".Equals(inputFile, StringComparison.OrdinalIgnoreCase))
                {
                    // session is done
                    Tracer.Trace($"Terminating as session was ended (instance {InstanceId:B})");
                    return(0);
                }

                var encodingToUse = await Console.In.ReadLineAsync();

                var outputFile = await Console.In.ReadLineAsync();

                try
                {
                    try
                    {
                        watch.Restart();

                        var file = Parser.Parse(inputFile, encodingToUse);

                        using (var writer = SystemFile.CreateText(outputFile))
                        {
                            YamlWriter.Write(writer, file);
                        }

                        var parseErrors = file.ParsingErrorsDetected == true;
                        if (parseErrors)
                        {
                            var parsingError = file.ParsingErrors[0];
                            Tracer.Trace(parsingError.ErrorMessage);
                            Tracer.Trace(parsingError.Location);
                        }

                        // clean-up after big files
                        if (IsBigFile(inputFile))
                        {
                            gcWatch.Restart();

                            GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
                            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, false, true);

                            Tracer.Trace($"Garbage collection took {gcWatch.Elapsed:s\\.fff} secs  (instance {InstanceId:B})");
                        }

                        Console.WriteLine(parseErrors ? "KO" : "OK");
                    }
                    finally
                    {
                        Tracer.Trace($"Parsing took {watch.Elapsed:s\\.fff} secs  (instance {InstanceId:B})");
                    }
                }
                catch (Exception ex)
                {
                    Tracer.Trace($"Exception: {ex}");

                    var stackTraceLines = ex.StackTrace?.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) ?? Enumerable.Empty <string>();
                    foreach (var stackTraceLine in stackTraceLines)
                    {
                        Tracer.Trace(stackTraceLine);
                    }

                    Console.WriteLine("KO");

                    throw;
                }
            }
        }
        public static async Task <int> Main(string[] args)
        {
            // check for GMaster or PlasticSCM or SemanticMerge arguments (to allow debugging without the tools)
            if (args.Length == 2)
            {
                var shell    = args[0]; // reserved for future usage
                var flagFile = args[1];

                SystemFile.WriteAllBytes(flagFile, new byte[] { 0x42 });
            }

            var watch   = Stopwatch.StartNew();
            var gcWatch = Stopwatch.StartNew();

            while (true)
            {
                var inputFile = await Console.In.ReadLineAsync();

                if (inputFile == null || "end".Equals(inputFile, StringComparison.OrdinalIgnoreCase))
                {
                    // session is done
                    Tracer.Trace($"Terminating as session was ended (instance {InstanceId:B})");
                    return(0);
                }

                var encodingToUse = await Console.In.ReadLineAsync();

                var outputFile = await Console.In.ReadLineAsync();

                try
                {
                    var parseErrors = false;
                    try
                    {
                        watch.Restart();

                        // we find a flavor here, as we want to support different main methods, based on file ending
                        var flavor = XmlFlavorFinder.Find(inputFile);

                        var file = Parser.Parse(inputFile, encodingToUse, flavor);

                        using (var writer = SystemFile.CreateText(outputFile))
                        {
                            YamlWriter.Write(writer, file);
                        }

                        parseErrors = file.ParsingErrorsDetected == true;
                        if (parseErrors)
                        {
                            var parsingError = file.ParsingErrors[0];
                            Tracer.Trace(parsingError.ErrorMessage);
                            Tracer.Trace(parsingError.Location);
                        }

                        // clean-up after big files
                        if (IsBigFile(inputFile))
                        {
                            gcWatch.Restart();

                            GCSettings.LargeObjectHeapCompactionMode = GCLargeObjectHeapCompactionMode.CompactOnce;
                            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, false, true);

                            Tracer.Trace($"Garbage collection took {gcWatch.Elapsed:s\\.fff} secs  (instance {InstanceId:B})");
                        }

                        Console.WriteLine(parseErrors ? "KO" : "OK");
                    }
                    finally
                    {
                        Tracer.Trace($"Parsing took {watch.Elapsed:s\\.fff} secs  (instance {InstanceId:B}), errors found: {parseErrors}");
                    }
                }
                catch (Exception ex)
                {
                    Tracer.Trace($"Exception: {ex}", ex);

                    Console.WriteLine("KO");

                    return(0);
                }
            }
        }
Exemplo n.º 28
0
 public YamlTermGenerator()
 {
     yamlWriter = new YamlWriter(new StreamWriter(stream));
     writer     = yamlWriter;
 }
Exemplo n.º 29
0
		private void ExecuteYamlWriter(Action<YamlWriter> actions, string expectedOutput, string errorMessage)
		{
			using (var ms = new MemoryStream())
			{
				using (var writer = new YamlWriter(ms, 4096, true))
				{
					actions(writer);
				}

				ms.Position = 0;

				using (var sr = new StreamReader(ms))
				{
					string result = sr.ReadToEnd();

					result.Should().Be(expectedOutput, errorMessage);
				}
			}
		}