Esempio n. 1
0
        /// <summary>
        /// Tests for the existance of a path.
        /// </summary>
        /// <param name="context">The template context.</param>
        /// <param name="span">The source span.</param>
        /// <param name="path">The path to test.</param>
        /// <param name="type">The type of path to test. May be one of the following: "leaf", "container" or "any". Defaults to "any".</param>
        /// <returns>If the path exists, `true`. Otherwise, `false`.</returns>
        /// <remarks>
        /// ```template-text
        /// {{ '.\foo.txt' | fs.test }}
        /// ```
        /// ```html
        /// true
        /// ```
        /// </remarks>
        public static bool Test(TemplateContext context, SourceSpan span, string path, string type = "any")
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ScriptRuntimeException(span, string.Format(RS.FSPathRequired, "fs.test"));
            }

            PathType pathType = PathType.Any;

            if (string.IsNullOrEmpty(type))
            {
                type = "any";
            }
            else
            {
                type = type.ToLowerInvariant();
            }

            switch (type)
            {
            case "any":
                break;

            case "container":
                pathType = PathType.Container;
                break;

            case "leaf":
                pathType = PathType.Leaf;
                break;

            default:
                throw new ScriptRuntimeException(span, string.Format(RS.FSUnsupportedType, "fs.test", type));
            }

            ITemplateLoader templateLoader = context.TemplateLoader;

            if (templateLoader == null)
            {
                throw new ScriptRuntimeException(span, string.Format(RS.NoTemplateLoader, "fs.test"));
            }

            bool pathExists = false;

            try
            {
                pathExists = templateLoader.PathExists(context, span, path, pathType);
            }
            catch (Exception ex) when(!(ex is ScriptRuntimeException))
            {
            }

            return(pathExists);
        }