Пример #1
0
        ///  <summary>
        ///  Finds the appropriate file based on version, site, and section.
        ///  </summary>
        ///  <remarks>
        ///  
        ///  </remarks>
        /// <param name="request">The request object</param>
        /// <param name="fileName">The name of the file to find, including extension.</param>
        ///  <param name="section">The section of the site the file is for.</param>
        ///  <param name="site">The site the file is for.</param>
        ///  <param name="version">The version of the site the file is for.</param>
        ///  <param name="rootPath">The relative path from the application root to
        /// 			the directory to search for the file.</param>
        ///  <param name="type">The<c>FileType</c> of the file.</param>
        ///  <returns>A <c>FileRecord</c> containing the information on the file located,
        /// 			or <c>null</c> if no file was found.</returns>
        private FileRecord FindFile(
            MvcRequest request,
            String fileName,
            String section,
            String site,
            int version,
            String rootPath,
            FileType type)
        {
            bool found = false;
            FileRecord fileRec = new FileRecord {
                                                    rootPath = rootPath,
                                                    version = version.ToString(CultureInfo.InvariantCulture),
                                                    site = site,
                                                    section = section,
                                                    subPath = fileName
                                                };

            if (this.FileExists(fileRec)) {
                fileRec.fullPath = "/" + fileRec.BuildPath();
                found = true;
            } else {
                switch (type) {
                    case FileType.PAGE:
                        //  check for <version>/[configuration["defaultDirName"]/<section>/<file>
                        if (string.IsNullOrEmpty(this.defaultLanguageDirName)) {
                            throw new ApplicationException("Default directory name not found in sites.config ["
                                    + DEFAULT_LANGUAGE_DIR_NAME + "]");
                        }
                        fileRec.site = this.defaultLanguageDirName;
                        if (this.FileExists(fileRec)) {
                            fileRec.fullPath = "/" + fileRec.BuildPath();
                            found = true;
                        }
                        break;

                    case FileType.CONTENT:
                        //  check for <version>/<site>/<language>/<section>/<file>
                        //  (NOTE: this is only applicable for content)
                        fileRec.language = LanguageUtilities.GetLanguage(request, site);
                        if (this.FileExists(fileRec)) {
                            fileRec.fullPath = "/" + fileRec.BuildPath();
                            found = true;
                        }
                        break;
                }

                if (!found) {
                    try {
                        //  get the configuration section for the specified site
                        XmlConfiguration config = SitesConfig.GetInstance().GetConfig("sites", site);
                        //  get the value of the defaultPageSearchMethod setting for the site
                        String searchMethod = config.GetValue("//defaultPageSearchMethod");

                        //  if the defaultPageSearchMethod setting is to check
                        //  the previous version(s), recursively call FindFile
                        //  with the next lower version
                        if (SEARCH_METHOD_PREVIOUS_VER.Equals(searchMethod) && (version > 1)) {
                            fileRec = this.FindFile(request, fileName, section, site, version - 1, rootPath, type);
                            found = this.FileExists(fileRec);

                            //  check for <version>/[configuration["defaultDirName"]/<file>
                        } else if (SEARCH_METHOD_DEFAULT.Equals(searchMethod)) {
                            fileRec.site = this.defaultLanguageDirName;
                            fileRec.section = null;
                            fileRec.language = null;
                            if (this.FileExists(fileRec)) {
                                fileRec.fullPath = "/" + fileRec.BuildPath();
                                found = true;
                            }
                        }
                    } catch (Exception e) {
                        LogManager.GetCurrentClassLogger().Error(
                            errorMessage => errorMessage("PageFinder.FindFile : "), e);
                        fileRec = null;
                    }
                }
            }

            //  log info if the file was not found
            if (!found) {
                //  get the components of the file name
                string[] pieces = fileName.Split('.');
                //  we only want to log items like 'file.xml' and ignore ones like 'file.css.xml'
                if (pieces.Length == 2) {
                    LogManager.GetCurrentClassLogger().Warn(
                        warnMessage => warnMessage("PageFinder.FindFile: file not found: file = '{0}', section = '{1}', site = '{2}', version = {3}",
                                                    fileName,
                                                    section,
                                                    site,
                                                    version));
                }
                fileRec = null;
            }

            return fileRec;
        }
Пример #2
0
 /// <summary>
 /// Determines if the given file exists.
 /// </summary>
 /// <param name="fileRec">A <b>FileRecord</b> containing the file information 
 ///			to check the existance of.</param>
 /// <returns>True if the file exists, false if not.</returns>
 private bool FileExists(
     FileRecord fileRec)
 {
     return ((fileRec != null) && File.Exists(this.basePath + fileRec.BuildPath()));
 }