示例#1
0
        /// <summary>
        /// Searches for a specified inclusion target.
        /// </summary>
        /// <param name="context">Inclustion resolution context.</param>
        /// <param name="path">Path to the file to search.</param>
        /// <param name="pathIsValid">Function deciding about file existence. Only path that passes this function is returned.</param>
        /// <param name="errorMessage">Warning which should be reported by the compiler or a <B>null</B> reference. The error message can be set iff the returned path is empty.</param>
        /// <returns>
        /// A canonical path to the target file or a <B>null</B> reference if the file path is not valid or the file not exists.
        /// </returns>
        internal static FullPath FindInclusionTargetPath(InclusionResolutionContext context, string path, Predicate <FullPath> /*!*/ pathIsValid, out string errorMessage)
        {
            Debug.Assert(context != null && path != null);
            Debug.Assert(pathIsValid != null);

            try
            {
                string root = Path.GetPathRoot(path);

                if (root == "\\")
                {
                    // incomplete absolute path //

                    // the path is at least one character long - the first character is slash that should be trimmed out:
                    path = Path.Combine(Path.GetPathRoot(context.WorkingDirectory), path.Substring(1));
                }
                else if (root == "")
                {
                    // relative path //

                    // search in search paths at first (accepts empty path list as well):
                    FullPath result = SearchInSearchPaths(context, path, pathIsValid /*, out errorMessage*/);

                    // if an error message occurred, immediately return
                    //if (errorMessage != null)
                    //    return FullPath.Empty;

                    // if the file is found then it exists so we can return immediately:
                    if (!result.IsEmpty)
                    {
                        errorMessage = null;
                        return(result);
                    }

                    // not found => the path is combined with the directory where the script being compiled is stored:
                    path = Path.Combine(context.ScriptDirectory, path);
                }

                // canonizes the complete absolute path:
                path = Path.GetFullPath(path);
            }
            catch (SystemException e)
            {
                errorMessage = e.Message + "\n" + e.StackTrace;
                return(FullPath.Empty);
            }

            FullPath full_path = new FullPath(path, false);

            // file does not exists:
            if (!pathIsValid(full_path) /*IsPathValidForInclusion(context, full_path, pathIsValid, out errorMessage)*/)
            {
                errorMessage = "Script cannot be included with current configuration.";
                return(FullPath.Empty);
            }

            errorMessage = null;
            return(full_path);
        }
示例#2
0
        ///// <summary>
        ///// Tests whether path can be used for script inclusion.
        ///// </summary>
        ///// <param name="context">Inclusion context containing information about include which is being evaluated.</param>
        ///// <param name="fullPath">FullPath value.</param>
        ///// <param name="pathIsValid">Function deciding about file existence.</param>
        ///// <param name="errorMessage">Error message containing description of occured error. If no error occured, null value is returned.</param>
        ///// <returns>True is path is valid for inclusion, otherwise false.</returns>
        //internal static bool IsPathValidForInclusion(InclusionResolutionContext context, FullPath fullPath, Predicate<FullPath>/*!*/pathIsValid, out string errorMessage)
        //{
        //    errorMessage = null;

        //    //return
        //    //    (context.ApplicationContext.ScriptLibraryDatabase != null && context.ApplicationContext.ScriptLibraryDatabase.ContainsScript(fullPath)) ||
        //    //    (fileExists != null && fileExists(fullPath)) ||
        //    //    (fullPath.FileExists);

        //    Debug.Assert(pathIsValid != null);

        //    return pathIsValid(fullPath);
        //}

        /// <summary>
        /// Searches for an existing file among files which names are combinations of a relative path and one of the
        /// paths specified in a list.
        /// </summary>
        /// <param name="context">Inclusion context containing information about include which is being evaluated.</param>
        /// <param name="relativePath">The relative path.</param>
        /// <param name="pathIsValid">Function deciding file existence.</param>
        /// <returns>Full path to a first existing file or an empty path.</returns>
        private static FullPath SearchInSearchPaths(InclusionResolutionContext context, string relativePath, Predicate <FullPath> /*!*/ pathIsValid)
        {
            // TODO: review this when script libraries are united with precompiled web
            if (context.SearchPaths == String.Empty)
            {
                return(FullPath.Empty);
            }

            Debug.Assert(pathIsValid != null);

            string path;

            for (int i = 0, j = 0; j >= 0; i = j + 1)
            {
                j    = context.SearchPaths.IndexOf(Path.PathSeparator, i);
                path = (j >= 0) ? context.SearchPaths.Substring(i, j - i) : context.SearchPaths.Substring(i);

                FullPath result = FullPath.Empty;

                // TODO: exceptions should be handled better, not as part of algorithm's logic
                try
                {
                    string path_root = Path.GetPathRoot(path);

                    // makes the path complete and absolute:
                    if (path_root == "\\")
                    {
                        path = Path.Combine(Path.GetPathRoot(context.WorkingDirectory), path.Substring(1));
                    }
                    else if (path_root == "")
                    {
                        path = Path.Combine(context.WorkingDirectory, path);
                    }

                    // combines the search path with the relative path:
                    path = Path.GetFullPath(Path.Combine(path, relativePath));

                    // prepare the FullPath version
                    result = new FullPath(path, false);
                }
                catch (SystemException)
                {
                    continue;
                }

                // this function might throw an exception in case of ambiguity
                if (pathIsValid(result) /*IsPathValidForInclusion(context, result, pathIsValid, out errorMessage)*/)
                {
                    return(result);
                }

                //if (errorMessage != null)
                //    return FullPath.Empty;
            }

            //errorMessage = null;
            return(FullPath.Empty);
        }
示例#3
0
		/// <summary>
		/// Searches for a specified inclusion target.
		/// </summary>
        /// <param name="context">Inclustion resolution context.</param>
        /// <param name="path">Path to the file to search.</param>
        /// <param name="pathIsValid">Function deciding about file existence. Only path that passes this function is returned.</param>
        /// <param name="errorMessage">Warning which should be reported by the compiler or a <B>null</B> reference. The error message can be set iff the returned path is empty.</param>
		/// <returns>
		/// A canonical path to the target file or a <B>null</B> reference if the file path is not valid or the file not exists.
		/// </returns>
		internal static FullPath FindInclusionTargetPath(InclusionResolutionContext context, string path, Predicate<FullPath>/*!*/pathIsValid, out string errorMessage)
		{
            Debug.Assert(context != null && path != null);
            Debug.Assert(pathIsValid != null);

            try
            {
                string root = Path.GetPathRoot(path);

                if (root == "\\")
                {
                    // incomplete absolute path //

                    // the path is at least one character long - the first character is slash that should be trimmed out: 
                    path = Path.Combine(Path.GetPathRoot(context.WorkingDirectory), path.Substring(1));
                }
                else if (root == "")
                {
                    // relative path //

                    // search in search paths at first (accepts empty path list as well):
                    FullPath result = SearchInSearchPaths(context, path, pathIsValid/*, out errorMessage*/);

                    // if an error message occurred, immediately return
                    //if (errorMessage != null)
                    //    return FullPath.Empty;
                    
                    // if the file is found then it exists so we can return immediately:
                    if (!result.IsEmpty)
                    {
                        errorMessage = null;
                        return result;
                    }

                    // not found => the path is combined with the directory where the script being compiled is stored:
                    path = Path.Combine(context.ScriptDirectory, path);
                }

                // canonizes the complete absolute path:
                path = Path.GetFullPath(path);
            }
            catch (SystemException e)
            {
                errorMessage = e.Message + "\n" + e.StackTrace;
                return FullPath.Empty;
            }

			FullPath full_path = new FullPath(path, false);

			// file does not exists:
            if (!pathIsValid(full_path)/*IsPathValidForInclusion(context, full_path, pathIsValid, out errorMessage)*/)
            {
                errorMessage = "Script cannot be included with current configuration.";
                return FullPath.Empty;
            }

            errorMessage = null;
            return full_path;
		}
示例#4
0
        ///// <summary>
        ///// Tests whether path can be used for script inclusion.
        ///// </summary>
        ///// <param name="context">Inclusion context containing information about include which is being evaluated.</param>
        ///// <param name="fullPath">FullPath value.</param>
        ///// <param name="pathIsValid">Function deciding about file existence.</param>
        ///// <param name="errorMessage">Error message containing description of occured error. If no error occured, null value is returned.</param>
        ///// <returns>True is path is valid for inclusion, otherwise false.</returns>
        //internal static bool IsPathValidForInclusion(InclusionResolutionContext context, FullPath fullPath, Predicate<FullPath>/*!*/pathIsValid, out string errorMessage)
        //{
        //    errorMessage = null;

        //    //return
        //    //    (context.ApplicationContext.ScriptLibraryDatabase != null && context.ApplicationContext.ScriptLibraryDatabase.ContainsScript(fullPath)) ||
        //    //    (fileExists != null && fileExists(fullPath)) ||
        //    //    (fullPath.FileExists);

        //    Debug.Assert(pathIsValid != null);

        //    return pathIsValid(fullPath);
        //}

        /// <summary>
        /// Searches for an existing file among files which names are combinations of a relative path and one of the 
        /// paths specified in a list.
        /// </summary>
        /// <param name="context">Inclusion context containing information about include which is being evaluated.</param>
        /// <param name="relativePath">The relative path.</param>
        /// <param name="pathIsValid">Function deciding file existence.</param>
        /// <returns>Full path to a first existing file or an empty path.</returns>
        private static FullPath SearchInSearchPaths(InclusionResolutionContext context, string relativePath, Predicate<FullPath>/*!*/pathIsValid)
        {
            // TODO: review this when script libraries are united with precompiled web
            if (context.SearchPaths == String.Empty)
                return FullPath.Empty;
            
            Debug.Assert(pathIsValid != null);

            string path;

            for (int i = 0, j = 0; j >= 0; i = j + 1)
            {
                j = context.SearchPaths.IndexOf(Path.PathSeparator, i);
                path = (j >= 0) ? context.SearchPaths.Substring(i, j - i) : context.SearchPaths.Substring(i);

                FullPath result = FullPath.Empty;

                // TODO: exceptions should be handled better, not as part of algorithm's logic
                try
                {
                    string path_root = Path.GetPathRoot(path);

                    // makes the path complete and absolute:
                    if (path_root == "\\")
                    {
                        path = Path.Combine(Path.GetPathRoot(context.WorkingDirectory), path.Substring(1));
                    }
                    else if (path_root == "")
                    {
                        path = Path.Combine(context.WorkingDirectory, path);
                    }

                    // combines the search path with the relative path:
                    path = Path.GetFullPath(Path.Combine(path, relativePath));

                    // prepare the FullPath version
                    result = new FullPath(path, false);
                }
                catch (SystemException)
                {
                    continue;
                }

                // this function might throw an exception in case of ambiguity
                if (pathIsValid(result)/*IsPathValidForInclusion(context, result, pathIsValid, out errorMessage)*/)
                    return result;
                
                //if (errorMessage != null)
                //    return FullPath.Empty;
            }

            //errorMessage = null;
            return FullPath.Empty;
        }