public static void outputNode(YamlNode node, string ind = "")
 {
     Console.WriteLine(ind + "NODE:\n{0}", ind + node.Tag);
     if (node.GetType() == typeof(YamlMapping))
     {
         foreach (YamlNode n in ((YamlMapping)node).Keys)
         {
             outputNode(n, ind + "-");
         }
     }
     else if (node.GetType() == typeof(YamlScalar))
     {
         Console.WriteLine("{0}\n", ind + ((YamlScalar)node));
     }
 }
    public static IEnumerable <ParsingEvent> ConvertToEventStream(YamlNode node)
    {
        var scalar = node as YamlScalarNode;

        if (scalar != null)
        {
            return(ConvertToEventStream(scalar));
        }

        var sequence = node as YamlSequenceNode;

        if (sequence != null)
        {
            return(ConvertToEventStream(sequence));
        }

        var mapping = node as YamlMappingNode;

        if (mapping != null)
        {
            return(ConvertToEventStream(mapping));
        }

        throw new NotSupportedException(string.Format("Unsupported node type: {0}", node.GetType().Name));
    }
Exemplo n.º 3
0
        // NOTE: This is a heuristic ONLY! And is also not used at the moment because sequence matching isn't in place.
        // It could also be massively improved.
        public static float YamlNodesHeuristic(YamlNode a, YamlNode b)
        {
            if (a.GetType() != b.GetType())
            {
                return(0.0f);
            }
            switch (a)
            {
            case YamlSequenceNode x:
                return(YamlSequencesHeuristic(x, (YamlSequenceNode)b));

            case YamlMappingNode y:
                return(YamlMappingsHeuristic(y, (YamlMappingNode)b));

            case YamlScalarNode z:
                return((z.Value == ((YamlScalarNode)b).Value) ? 1.0f : 0.0f);

            default:
                throw new ArgumentException($"Unrecognized YAML node type: {a.GetType()}", nameof(a));
            }
        }
Exemplo n.º 4
0
        private IKey TransformKey(YamlNode node)
        {
            IValue v = TransformValue(node);

            if (v is IKey key)
            {
                return(key);
            }

            throw new NotImplementedException(
                      $"Unsupported node type for key: {node.GetType()}"
                      );
        }
Exemplo n.º 5
0
    private T YamlExpect <T>(YamlNode yaml) where T : YamlNode
    {
        T expected = yaml as T;

        if (expected == null)
        {
            ThrowError(
                yaml,
                $"YAML node type {typeof(T).Name} expected, not {yaml.GetType().Name}"
                );
        }
        return(expected);
    }
Exemplo n.º 6
0
        public static bool TriTypeMatch(YamlNode ours, YamlNode based, YamlNode other)
        {
            var refType = other.GetType();

            if (refType != based.GetType())
            {
                return(false);
            }
            if (refType != ours.GetType())
            {
                return(false);
            }
            return(true);
        }
        private static ISettingsNode ConvertNode(YamlNode node, string name = null)
        {
            switch (node)
            {
            case YamlMappingNode mappingNode:
                return(ConvertNode(mappingNode, name));

            case YamlSequenceNode sequenceNode:
                return(ConvertNode(sequenceNode, name));

            case YamlScalarNode scalarNode:
                return(ConvertNode(scalarNode, name));
            }

            throw new ArgumentOutOfRangeException(nameof(node), node.GetType().Name, "Unknown type of YamlNode.");
        }
Exemplo n.º 8
0
        private static IEnumerable <ParsingEvent> ConvertToEventStream(YamlNode node)
        {
            if (node is YamlScalarNode scalar)
            {
                return(ConvertToEventStream(scalar));
            }

            if (node is YamlSequenceNode sequence)
            {
                return(ConvertToEventStream(sequence));
            }

            if (node is YamlMappingNode mapping)
            {
                return(ConvertToEventStream(mapping));
            }

            throw new NotSupportedException($"Unsupported node type: {node.GetType().Name}");
        }
Exemplo n.º 9
0
        public bool MapEntityRecursiveAndBadly(YamlNode node, string path)
        {
            switch (node)
            {
            case YamlSequenceNode subSequence:
                var idx = 0;
                foreach (var val in subSequence)
                {
                    if (!MapEntityRecursiveAndBadly(val, path + "/" + (idx++)))
                    {
                        return(false);
                    }
                }
                return(true);

            case YamlMappingNode subMapping:
                foreach (var kvp in subMapping)
                {
                    if (!MapEntityRecursiveAndBadly(kvp.Key, path))
                    {
                        return(false);
                    }
                }
                foreach (var kvp in subMapping)
                {
                    if (!MapEntityRecursiveAndBadly(kvp.Value, path + "/" + kvp.Key.ToString()))
                    {
                        return(false);
                    }
                }
                return(true);

            case YamlScalarNode subScalar:
                return(MapEntityProperty(subScalar, path));

            default:
                throw new ArgumentException($"Unrecognized YAML node type: {node.GetType()} at {path}");
            }
        }
Exemplo n.º 10
0
        public static YamlNode CopyYamlNodes(YamlNode other)
        {
            switch (other)
            {
            case YamlSequenceNode subSequence:
                YamlSequenceNode tmp1 = new YamlSequenceNode();
                MergeYamlSequences(tmp1, new YamlSequenceNode(), subSequence, "");
                return(tmp1);

            case YamlMappingNode subMapping:
                YamlMappingNode tmp2 = new YamlMappingNode();
                MergeYamlMappings(tmp2, new YamlMappingNode(), subMapping, "", new string[] {});
                return(tmp2);

            case YamlScalarNode subScalar:
                YamlScalarNode tmp3 = new YamlScalarNode();
                CopyYamlScalar(tmp3, subScalar);
                return(tmp3);

            default:
                throw new ArgumentException($"Unrecognized YAML node type for copy: {other.GetType()}", nameof(other));
            }
        }
Exemplo n.º 11
0
        private void VisitYamlNode(string context, YamlNode node)
        {
            switch (node)
            {
            case YamlScalarNode scalarNode:
                VisitYamlScalarNode(context, scalarNode);
                break;

            case YamlMappingNode mappingNode:
                VisitYamlMappingNode(context, mappingNode);
                break;

            case YamlSequenceNode sequenceNode:
                VisitYamlSequenceNode(context, sequenceNode);
                break;

            default:
                throw new ArgumentOutOfRangeException(
                          nameof(node),
                          $"Unsupported YAML node type '{node.GetType().Name} was found. " +
                          $"Path '{_currentPath}', line {node.Start.Line} position {node.Start.Column}.");
            }
        }
Exemplo n.º 12
0
        static void reifyInfoSchema(YamlNode schemas)
        {
            schemaMapping.Add("ErrorResponse", "infoSchema");
            {
                YamlScalarNode inc = new YamlScalarNode("!include " + fileManager.schemaIncludePath("ErrorResponse"));
                inc.Style = ScalarStyle.Raw;

                //Raml 0.8
                if (schemas.GetType() == typeof(YamlSequenceNode))
                {
                    YamlMappingNode nd = new YamlMappingNode();
                    nd.Add("infoSchema", inc);
                    YamlSequenceNode schemasSeq = (YamlSequenceNode)schemas;
                    schemasSeq.Add(nd);
                }
                else
                {
                    //Raml 1.0
                    YamlMappingNode schemasMap = (YamlMappingNode)schemas;
                    schemasMap.Add("infoSchema", inc);
                }
            }
        }
Exemplo n.º 13
0
        private static Config AddToConfig(YamlNode yamlNode, Config parent)
        {
            Config node = new Config();
            if (yamlNode is YamlMappingNode)
            {
                YamlMappingNode mapping = yamlNode as YamlMappingNode;
                node.Children = new List<Config>();
                foreach (var child in mapping.Children)
            #if DEBUG
                    if (child.Key is YamlScalarNode)
            #endif
                        AddToConfig(child.Value, node).Name = (child.Key as YamlScalarNode).Value;
            #if DEBUG
                    else // going on the assumption that a Key is *always* a string. Can't wrap my head around how it wouldn't be.
                        throw new InvalidCastException("YamlMappingNode's key is NOT a YamlScalarNode: " + child.Key.GetType().FullName);
            #endif
            }
            else if (yamlNode is YamlSequenceNode)
            {
                YamlSequenceNode sequence = yamlNode as YamlSequenceNode;
                node.Children = new List<Config>();
                foreach( var child in sequence.Children )
                    AddToConfig(child, node);
            }
            else if (yamlNode is YamlScalarNode)
            {
                YamlScalarNode scalar = yamlNode as YamlScalarNode;
                node.Value = scalar.Value;
            }
            else
                throw new InvalidCastException("Unrecognised YamlNode type: " + yamlNode.GetType().FullName);

            if ( parent != null )
                parent.Children.Add(node);
            return node;
        }
 private static Exception UnsupportedMergeKeys(YamlNode node, YamlNode parent, string path) =>
 new FormatException($"Unsupported YAML merge keys '{node.GetType().Name} was found. Path '{path}', line {parent.Start.Line} position {parent.Start.Column}.");
Exemplo n.º 15
0
        static int visitOutboundConnectedElements(bool ensureVisible, EA.Repository Repository, EA.Element clientElement, YamlNode parent, Func <EA.Repository, EA.Connector, EA.Element, EA.Element, bool> filter, Func <EA.Connector, EA.Element, EA.Element, string> name, Func <EA.Repository, EA.Element, EA.Connector, EA.Element, YamlNode> properties)
        {
            int outboundCount = 0;

            //logger.log("Processing Connections from:" + clientElement.Name);
            foreach (EA.Connector con in clientElement.Connectors)
            {
                //logger.log("Processing Connector:" + con.Name);

                if (ensureVisible)
                {
                    if (!DiagramManager.isVisible(con))
                    {
                        continue;
                    }
                }

                EA.Element supplierElement = null;
                if (clientElement.ElementID == con.ClientID)
                {
                    supplierElement = Repository.GetElementByID(con.SupplierID);
                    //logger.log("Found resource:" + supplierElement.Name);

                    EA.Element supplierClassifier = null;
                    if (supplierElement.ClassifierID != 0)
                    {
                        supplierClassifier = Repository.GetElementByID(supplierElement.ClassifierID);
                    }

                    //logger.log("Classifier");
                    if (!filter(Repository, con, supplierElement, supplierClassifier))
                    {
                        continue;
                    }

                    outboundCount += 1;

                    //logger.log("Filtered");

                    String nm = name(con, supplierElement, clientElement);

                    YamlNode o = properties(Repository, supplierElement, con, clientElement);
                    if (o == null)
                    {
                        continue;
                    }
                    else if (parent.GetType().Name.StartsWith("YamlSequenceNode"))
                    {
                        YamlSequenceNode seq = (YamlSequenceNode)parent;
                        if (o.GetType().Name.StartsWith("YamlSequenceNode"))
                        {
                            YamlSequenceNode oseq = (YamlSequenceNode)o;
                            foreach (YamlNode n in oseq.Children)
                            {
                                seq.Add(n);
                            }
                        }
                        else
                        {
                            seq.Add(o);
                        }
                    }
                    else if (parent.GetType().Name.StartsWith("YamlMappingNode"))
                    {
                        if (nm == null)
                        {
                            continue;
                        }
                        YamlMappingNode map = (YamlMappingNode)parent;
                        try
                        {
                            map.Add(nm, o);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Duplicate entry:" + nm, ex);
                        }
                    }
                }
            }
            //logger.log("Processed Connections from:" + clientElement.Name);
            return(outboundCount);
        }
Exemplo n.º 16
0
 private string GetScalarNodeKey(YamlNode node)
 {
     if (node is YamlScalarNode scalarNode)
     {
         return(scalarNode.Value);
     }
     throw new ArgumentException($"Keys should be scalar nodes. Received: {node.GetType().Name}");
 }
Exemplo n.º 17
0
 private string GetScalarNodeValue(YamlNode node)
 {
     if (node is YamlScalarNode scalarNode)
     {
         return(scalarNode.Value);
     }
     throw new ArgumentException($"Unexpected node type nodes. Expected {nameof(YamlScalarNode)}. Received: {node.GetType().Name}");
 }
Exemplo n.º 18
0
        private IValue TransformValue(YamlNode node)
        {
            switch (node)
            {
            case YamlScalarNode scalar:
                switch (scalar.Tag)
                {
                case "tag:yaml.org,2002:null":
                    return(default(Null));

                case "tag:yaml.org,2002:bool":
                    string bLit = scalar.Value.ToLower();
                    return(new Bencodex.Types.Boolean(
                               bLit == "on" || bLit == "true" ||
                               bLit == "y" || bLit == "yes"
                               ));

                case "tag:yaml.org,2002:int":
                    return(new Integer(scalar.Value));

                case "tag:yaml.org,2002:binary":
                    return(new Binary(
                               Convert.FromBase64String(scalar.Value)
                               ));

                case null:
                case "":
                    if (scalar.Style == ScalarStyle.Plain)
                    {
                        switch (scalar.Value.ToLower())
                        {
                        case "null":
                        case "":
                        case null:
                            return(default(Null));

                        case "on":
                        case "true":
                        case "y":
                        case "yes":
                            return(new Bencodex.Types.Boolean(true));

                        case "false":
                        case "off":
                        case "n":
                        case "no":
                            return(default(Bencodex.Types.Boolean));
                        }

                        try
                        {
                            return(new Integer(scalar.Value));
                        }
                        catch (FormatException)
                        {
                            return(new Text(scalar.Value));
                        }
                    }

                    return(new Text(scalar.Value));

                default:
                    throw new NotImplementedException(
                              $"unsupported tag: \"{scalar.Tag}\""
                              );
                }

            case YamlMappingNode map:
                var transformedPairs = map.Select(kv =>
                                                  KeyValuePair.Create(
                                                      TransformKey(kv.Key),
                                                      TransformValue(kv.Value)
                                                      )
                                                  );
                return(new Dictionary(transformedPairs));

            case YamlSequenceNode seq:
                return(new List(seq.Children.Select(TransformValue)));

            default:
                throw new NotImplementedException(
                          $"Unsupported node type for vlaue: {node.GetType()}"
                          );
            }
        }
Exemplo n.º 19
0
        public static void MergeYamlNodes(YamlNode ours, YamlNode based, YamlNode other, string path)
        {
            if (!TriTypeMatch(ours, based, other))
            {
                throw new ArgumentException($"Node type mismatch at {path}");
            }
            switch (other)
            {
            case YamlSequenceNode subSequence:
                MergeYamlSequences((YamlSequenceNode)ours, (YamlSequenceNode)based, subSequence, path);
                break;

            case YamlMappingNode subMapping:
                MergeYamlMappings((YamlMappingNode)ours, (YamlMappingNode)based, subMapping, path, new string[] {});
                break;

            case YamlScalarNode subScalar:
                // Console.WriteLine(path + " - " + ours + " || " + based + " || " + other);
                var scalarA = (YamlScalarNode)ours;
                var scalarB = (YamlScalarNode)based;
                var scalarC = subScalar;
                var aeb     = (scalarA.Value == scalarB.Value);
                var cneb    = (scalarC.Value != scalarB.Value);
                if (aeb || cneb)
                {
                    CopyYamlScalar(scalarA, scalarC);
                }
                // Console.WriteLine(path + " . " + ours + " || " + based + " || " + other);
                break;

            default:
                throw new ArgumentException($"Unrecognized YAML node type at {path}: {other.GetType()}", nameof(other));
            }
        }
 private static Exception UnsupportedNodeType(YamlNode node, string path) =>
 new FormatException($"Unsupported YAML node type '{node.GetType().Name} was found. Path '{path}', line {node.Start.Line} position {node.Start.Column}.");
Exemplo n.º 21
0
 private static object RenderYamlNodeToObject(YamlNode node)
 {
     if (node is YamlMappingNode)
     {
         return(RenderYamlMappingNodeToObject(node as YamlMappingNode));
     }
     if (node is YamlSequenceNode)
     {
         return(RenderYamlSequenceNodeToObject(node as YamlSequenceNode));
     }
     if (node is YamlScalarNode)
     {
         return(node.ToString());
     }
     throw new ArgumentOutOfRangeException($"The YamlNode provided is not defined to be processed, type: {node.GetType()}");
 }