BrowserScriptHost.cs :  » Script » IronRuby » Microsoft » Scripting » Silverlight » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Script » IronRuby 
IronRuby » Microsoft » Scripting » Silverlight » BrowserScriptHost.cs
/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 *
 * ***************************************************************************/

#if CLR2
using Microsoft.Scripting.Utils;
#endif
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Resources;
using Microsoft.Scripting.Hosting;

namespace Microsoft.Scripting.Silverlight{

    /// <summary>
    /// ScriptHost for use inside the browser.
    /// </summary>
    public sealed class BrowserScriptHost : ScriptHost {

        public BrowserScriptHost() {
        }

        public override PlatformAdaptationLayer/*!*/ PlatformAdaptationLayer {
            get {
                return BrowserPAL.PAL;
            }
        }
    }

    /// <summary>
    /// Base class for all browser-based PlatformAdaptationLayers. 
    /// Delegates compatible operations to a BrowserVirtualFileSystem.
    /// </summary>
    // BUG: should be internal, but Ruby is refusing to call members if so
    public abstract class BrowserPAL : PlatformAdaptationLayer {

        public BrowserVirtualFilesystem VirtualFilesystem { get; internal set; }

        protected static BrowserPAL _PAL;
        internal static BrowserPAL PAL {
            get {
                if (_PAL == null) PAL = HttpPAL.PAL;
                return _PAL;
            }
            set { _PAL = value; }
        }

        /// <summary>
        /// Get the virtual filesystem's current storage unit. It is "object"
        /// based since the CurrentStorageUnit can be different types.
        /// </summary>
        public object CurrentStorageUnit {
            get { return VirtualFilesystem.CurrentStorageUnit; }
            set { VirtualFilesystem.CurrentStorageUnit = value; }
        }

        /// <summary>
        /// Executes "action" in the context of the "storageUnit". 
        /// </summary>
        /// <param name="xapFile"></param>
        /// <param name="action"></param>
        public void UsingStorageUnit(object storageUnit, Action action) {
            VirtualFilesystem.UsingStorageUnit(storageUnit, action);
        }

        public override bool FileExists(string path) {
            return VirtualFilesystem.GetFile(path) != null;
        }

        public override Assembly LoadAssemblyFromPath(string path) {
            Stream stream = VirtualFilesystem.GetFile(path);
            if (stream == null) {
                throw new FileNotFoundException(
                    string.Format("could not find assembly: {0} (check the {1})", 
                        path, VirtualFilesystem.Name()
                    )
                );
            }
            return new AssemblyPart().Load(stream);
        }

        /// <exception cref="ArgumentException">Invalid path.</exception>
        public override string/*!*/ GetFullPath(string/*!*/ path) {
            return VirtualFilesystem.NormalizePath(path);
        }

        /// <exception cref="ArgumentException">Invalid path.</exception>
        public override bool IsAbsolutePath(string/*!*/ path) {
            return PathToUri(path).IsAbsoluteUri;
        }

        public override Stream OpenInputFileStream(string path) {
            Stream result = VirtualFilesystem.GetFile(GetFullPath(path));
            if (result == null)
                throw new IOException(
                    String.Format("file {0} not found (check the {1})", 
                        path, VirtualFilesystem.Name()
                    )
                );
            return result;
        }

        public override Stream OpenInputFileStream
        (string path, FileMode mode, FileAccess access, FileShare share) {
            if (mode != FileMode.Open || access != FileAccess.Read) {
                throw new IOException(
                    string.Format("can only read files from the {0}",
                        VirtualFilesystem.Name()
                    )
                );
            }
            return OpenInputFileStream(path);
        }

        public override Stream OpenInputFileStream
        (string path, FileMode mode, FileAccess access, FileShare share, int bufferSize) {
            return OpenInputFileStream(path, mode, access, share);
        }

        /// <summary>
        /// Convert a string path to a Uri
        /// </summary>
        /// <param name="path">relative or absolute path</param>
        /// <returns>normalized URI</returns>
        private Uri/*!*/ PathToUri(string/*!*/ path) {
            try {
                return new Uri(VirtualFilesystem.NormalizePath(path), UriKind.RelativeOrAbsolute);
            } catch (UriFormatException e) {
                throw new ArgumentException("The specified path is invalid", e);
            }
        }
    }

    /// <summary>
    /// PlatformAdaptationLayer for use with a read-only XAP file.
    /// </summary>
    internal sealed class XapPAL : BrowserPAL {
        internal static new readonly BrowserPAL PAL = new XapPAL();

        private XapPAL() {
            VirtualFilesystem = new XapVirtualFilesystem();
        }
    }

    /// <summary>
    /// PlatformAdaptationLayer to download and cache files over HTTP.
    /// </summary>
    internal sealed class HttpPAL : BrowserPAL {
        internal static new readonly BrowserPAL PAL = new HttpPAL();

        private HttpPAL() {
            VirtualFilesystem = new HttpVirtualFilesystem();
        }
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.