Ejemplo n.º 1
0
 public static TydFile FromContent(string content, string filePath)
 {
     try
     {
         var tydNodeList = TydFromText.Parse(content);
         var tydDoc      = new TydDocument(tydNodeList);
         return(FromDocument(tydDoc, filePath));
     }
     catch (Exception e)
     {
         throw new Exception("Exception loading " + filePath + ": " + e);
     }
 }
Ejemplo n.º 2
0
        ///<summary>
        /// Create a new TydFile by loading data from a file at the given path.
        ///</summary>
        public static TydFile FromFile(string filePath, bool treatXmlAsOneObject = false)
        {
            try
            {
                if (Path.GetExtension(filePath).ToLowerInvariant() == ".xml")
                {
                    //File is xml format
                    //Load it and convert the tyd _nodes from it
                    var contents = File.ReadAllText(filePath);
                    var xmlDoc   = new XmlDocument();
                    xmlDoc.LoadXml(contents);
                    var nodes = new List <TydNode>();
                    if (treatXmlAsOneObject)
                    {
                        nodes.Add(TydXml.TydNodeFromXmlDocument(xmlDoc));
                    }
                    else
                    {
                        nodes.AddRange(TydXml.TydNodesFromXmlDocument(xmlDoc));
                    }

                    return(FromDocument(new TydDocument(nodes), filePath));
                }
                else
                {
                    //If it's any extension besides xml, we assume the file is Tyd format
                    string readContents;
                    using (var streamReader = new StreamReader(filePath))
                    {
                        readContents = streamReader.ReadToEnd();
                    }
                    var tydNodeList = TydFromText.Parse(readContents);
                    var tydDoc      = new TydDocument(tydNodeList);
                    return(FromDocument(tydDoc, filePath));
                }
            }
            catch (Exception e)
            {
                throw new Exception("Exception loading " + filePath + ": " + e);
            }
        }
Ejemplo n.º 3
0
        //This is a set of heuristics to try to determine if we should write a string quoted or naked.
        public static bool ShouldWriteWithQuotes(string value)
        {
            var len = 0;

            //Check the string character-by-character
            for (var i = 0; i < value.Length; i++)
            {
                var c = value[i];

                if (!TydFromText.IsSymbolChar(c) && c != '.')
                {
                    return(true);
                }

                //Chars that imply we should use quotes
                //Some of these are heuristics, like space.
                //Some absolutely require quotes, like the double-quote itself. They'll break naked strings if unescaped (and naked strings are always written unescaped).
                //Note that period is not on this list; it commonly appears as a decimal in numbers.
                if (c == ' ' ||
                    c == '\n' ||
                    c == '\t' ||
                    c == '"' ||
                    c == Constants.CommentChar ||
                    c == Constants.RecordEndChar ||
                    c == Constants.AttributeStartChar ||
                    c == Constants.TableStartChar ||
                    c == Constants.TableEndChar ||
                    c == Constants.ListStartChar ||
                    c == Constants.ListEndChar
                    )
                {
                    return(true);
                }
                if (!char.IsWhiteSpace(c))
                {
                    len++;
                }
            }

            return(len == 0);
        }