⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 slashperiod.cs

📁 ajax patterns 这是关于ajax设计模式方面的原代码
💻 CS
字号:
/*
    Copyright 2006 Christian Gross http://www.devspace.com
    From the book Ajax Patterns and Best Practices

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Text;
using System.Web;
using System.Runtime.Serialization;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text.RegularExpressions;
using System.IO;
using Devspace.Commons.Tracer;
using System.Reflection;

namespace Devspace.Ajax.Permutations {
    // Not what I like... I will work on a proper rewrite module
    // sometime in the future
/*    public class AssemblyLoader {
        Assembly _assembly;
        
        public AssemblyLoader(string path) {
            _assembly = Assembly.Load(AssemblyName.GetAssemblyName(path));
        }
        public type Instantiate< type>(string typeidentifier) where type: class {
            return _assembly.CreateInstance(typeidentifier) as type;
        }
    }
*/
    /// <summary>
    /// SlashPeriodRewriter: Class used to implement the default permutations pattern.
    /// The class also implements object pool functionality so that the object
    /// instances can be retrieved from a pool instead of being instantiated
    /// </summary>
    [Obsolete( "Will be removed in the next refactoring as the Java implementation illustrated this definition is not necessary")]
    public class SlashPeriodRewriter : RewriterBase {
        
        public SlashPeriodRewriter( HttpApplication app) : base( app) {
        }
        private bool DoesFileExistAndRewrite(string mimeType, string filename) {
            string path = _context.Request.PhysicalPath + filename;
            GenerateOutput.Write("SlashPeriodRewriter.DoesFileExistAndRewrite", "Path (" + path + ")");
            FileAttributes attributes;
            try {
                attributes = File.GetAttributes(path);
            }
            catch (FileNotFoundException) {
                return false;
            }
            if ((attributes & FileAttributes.Directory) == 0) {
                RewritePath( filename, filename);
                return true;
            }
            else {
                return false;
            }
        }
        /// <summary>
        /// Method WriteRedirection
        /// This is the magic of the permutations pattern that makes everything work.
        /// Imagine an HTML page referencing /something/resource. The mime type will
        /// mapped to an extension that exists. First tested are the standard .html,
        /// .xhtml, etc. After that the .aspx extension is tried. Where the pattern
        /// becomes extremely useful is if the same HTML code was used in Java, then
        /// appended is not the .aspx extension, but the .jsp extension. The HTML client
        /// page had to change absolutely nothing
        /// </summary>
        /// <returns>A bool</returns>
        /// <param name="mimetype">A  string</param>
        public override bool WriteRedirection(string mimetype) {
            GenerateOutput.Write("SlashPeriodRewriter.WriteRedirection", "Mimetype (" + mimetype + ")");
            if (_xml.IsMatch(mimetype)) {
                if( !DoesFileExistAndRewrite( mimetype, ".xhtml")) {
                    return DoesFileExistAndRewrite( mimetype, ".aspx");
                }
                else {
                    return true;
                }
            }
            if (_html.IsMatch(mimetype)) {
                if( !DoesFileExistAndRewrite( mimetype, ".html")) {
                    return DoesFileExistAndRewrite( mimetype, ".aspx");
                }
                else {
                    return true;
                }
            }
            if (_text.IsMatch(mimetype)) {
                return DoesFileExistAndRewrite( mimetype, ".txt");
            }
            if (String.Compare(mimetype, "*/*") == 0) {
                if( !DoesFileExistAndRewrite( mimetype, ".html")) {
                    return DoesFileExistAndRewrite( mimetype, ".aspx");
                }
                else {
                    return true;
                }
            }
            return false;
        }
    }
    
    /// <summary>
    /// SlashPeriodResource: Class used to implement the default permutations pattern.
    /// The class also implements object pool functionality so that the object
    /// instances can be retrieved from a pool instead of being instantiated
    /// </summary>
    public class SlashPeriodResource : URLRewriterBase {
        
        public SlashPeriodResource(IRewriter rewriter) :
        base( rewriter) {
        }
        public override bool IsResource(HttpRequest request) {
            GenerateOutput.Write( "SlashPeriodResource.IsResource", "Path (" + request.PhysicalPath + ")");
            GenerateOutput.Write( "SlashPeriodResource.IsResource", "URLPath (" + request.Path + ")");
            int findPeriod = request.Path.LastIndexOf( '.');
            int findSlash = request.Path.LastIndexOf( '/');
            GenerateOutput.Write( "SlashPeriodResource.IsResource", "findPeriod (" + findPeriod + ") findSlash ( " + findSlash + ")");
            if( findPeriod == -1) {
                return true;
            }
            if( findSlash > findPeriod) {
                return true;
            }
            GenerateOutput.Write( "SlashPeriodResource.IsResource", "could not find anything");
            return false;
        }
        public override void WriteRepresentation(HttpRequest request) {
            string[] elements = (string[])request.AcceptTypes.Clone();
            Array.Sort(elements, new CompareMimeTypes());
            Regex semiColon = new Regex(";");
            foreach (string type in elements) {
                String[] buffers = semiColon.Split(type);
                if (_defaultRewriter.WriteRedirection(buffers[0])) {
                    break;
                }
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -