Exemplo n.º 1
0
        private string[] _GetDependencies(VirtualFile virtualFile)
        {
            var dir = VirtualPathUtility.GetDirectory(virtualFile.VirtualPath);

            string content;
            using (var stream = virtualFile.Open())
            using (var reader = new StreamReader(stream))
                content = reader.ReadToEnd();

            return _ReferenceRegex.Matches(content).Cast<Match>().Select(m => {
                var relativePath = m.Groups["path"].Value;
                return VirtualPathUtility.Combine(dir, relativePath);
            }).Where(m => this.ExcludedDependencies.All(e => !m.Contains(@"/" + e))).ToArray();
        }
Exemplo n.º 2
0
		private ContentRegistration AnalyzeView(HttpContextBase httpContext, VirtualFile file, string controllerName, Type modelType, List<ContentRegistration> registrations)
		{
			if (modelType == null || !typeof(ContentItem).IsAssignableFrom(modelType) || modelType.IsAbstract)
				return null;

			var model = Activator.CreateInstance(modelType) as ContentItem;

			var rd = new RouteData();
			var isPage = model != null && model.IsPage;
			RouteExtensions.ApplyCurrentPath(rd, controllerName, Path.GetFileNameWithoutExtension(file.Name), new PathData(isPage ? model : new StubItem(), isPage ? null : model));

			var cctx = new ControllerContext(httpContext, rd, new StubController());

			var result = isPage
				? viewEnginesProvider.Get().FindView(cctx, file.VirtualPath, null)
				: viewEnginesProvider.Get().FindPartialView(cctx, file.VirtualPath);


			return result.View == null ? null : RenderViewForRegistration(file, modelType, cctx, result, registrations);
		}
Exemplo n.º 3
0
        private string GetPageTitle(VirtualFile file)
        {
            string aspx;
            using (var infile = file.Open())
            {
                using (var reader = new StreamReader(infile))
                {
                    aspx = reader.ReadToEnd();
                }
            }

            var m = titleRegex.Match(aspx);
            if (m.Success && m.Groups.Count > 1)
            {
                return m.Groups[1].Value;
            }
            else
            {
                return VirtualPathUtility.GetFileName(file.VirtualPath);
            }
        }
		private ContentRegistration RenderViewForRegistration(VirtualFile file, Type modelType, ControllerContext cctx, ViewEngineResult result)
		{
			var re = new ContentRegistration(map.CreateDefinition(modelType, N2.Web.Url.RemoveAnyExtension(file.Name)));
			re.IsDefined = false;
			re.Context.TouchedPaths.Add(file.VirtualPath);

			using (StringWriter sw = new StringWriter())
			{
				var vdd = new ViewDataDictionary();
				cctx.Controller.ViewData = vdd;
				N2.Web.Mvc.Html.RegistrationExtensions.SetRegistrationExpression(cctx.HttpContext, re);

				try
				{
					logger.DebugFormat("Rendering view {0} for registrations", file.VirtualPath);
					result.View.Render(new ViewContext(cctx, result.View, vdd, new TempDataDictionary(), sw), sw);
					logger.DebugFormat("Rendered view {0}, editables = {1}, defined = {2}", file.VirtualPath, re.Definition.Editables.Count, re.IsDefined);
				}
				catch (Exception ex)
				{
					logger.Error(file.VirtualPath, ex);
					if (re.IsDefined)
						throw;
					return null;
				}
				finally
				{
					N2.Web.Mvc.Html.RegistrationExtensions.SetRegistrationExpression(cctx.HttpContext, null);
				}

				if (re.IsDefined)
				{
					return re;
				}
				return null;
			}
		}
Exemplo n.º 5
0
		internal void TransmitFile (VirtualFile vf)
		{
			TransmitFile (vf, false);
		}
Exemplo n.º 6
0
        private ViewTemplateDescription RenderViewForRegistration(VirtualFile file, Type modelType, ControllerContext cctx, ViewEngineResult result)
        {
            var re = new ContentRegistration();
            re.ContentType = modelType;
            re.Template = N2.Web.Url.RemoveAnyExtension(file.Name);
            re.IsDefined = false;
            using (StringWriter sw = new StringWriter())
            {
                var vdd = new ViewDataDictionary();
                cctx.Controller.ViewData = vdd;
                vdd["RegistrationExpression"] = re;

                try
                {
                    result.View.Render(new ViewContext(cctx, result.View, vdd, new TempDataDictionary(), sw), sw);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex);
                    if (re.IsDefined)
                        throw;
                    return null;
                }

                if (re.IsDefined)
                {
                    return new ViewTemplateDescription
                    {
                        Registration = re,
                        Definition = GetOrCreateDefinition(re),
                        TouchedPaths = new[] { file.VirtualPath }.Union(re.TouchedPaths)
                    };
                }
                return null;
            }
        }
Exemplo n.º 7
0
 private static string GetTitle(VirtualFile file)
 {
     return Normalize(Path.GetFileNameWithoutExtension(file.VirtualPath));
 }
 public VirtualPathFile(VirtualFile file)
 {
     _file = file;
 }
 public FileViewLocationResult(VirtualFile file, string name) : base(file.VirtualPath, name, () => new StreamReader(VirtualPathProviderHelper.Open(file)))
 {
     //_fileHash = VirtualPathProviderHelper.GetFileHash(file.VirtualPath);
     //this._lastModifiedUtc = file.LastWriteTimeUtc;
  }
 public WebFormVirtualFile(string virtualPath, VirtualFile actualFile, string assemblyDirective)
     : base(virtualPath) {
     _actualFile = actualFile;
     _assemblyDirective = assemblyDirective;
 }
        public static Stream Open(VirtualFile virtualFile)
        {
            using (var fs = virtualFile.Open())
            {
                var stream = new MemoryStream();
                fs.CopyTo(stream);
                stream.Position = 0;
                return stream;
            }

            /*
            using (var fs = File.OpenRead(path))
            {
                var stream = new MemoryStream();
                fs.CopyTo(stream);
                stream.Position = 0;
                return stream;
            }
             * */
        }
        // support for VirtualPathProvider
        internal void WriteVirtualFile(VirtualFile vf) {
            Debug.Trace("WriteVirtualFile", vf.Name);

            using (Stream s = vf.Open()) {
                if (UsingHttpWriter) {
                    long size = s.Length;

                    if (size > 0) {
                        // write as memory block
                        byte[] fileBytes = new byte[(int)size];
                        int bytesRead = s.Read(fileBytes, 0, (int) size);
                        _httpWriter.WriteBytes(fileBytes, 0, bytesRead);
                    }
                }
                else {
                    // Write file contents
                    WriteStreamAsText(s, 0, -1);
                }
            }
        }
 public virtual void CopyTo(VirtualFile source, String newVirtualPath)
 {
     UnifiedFile.CopyTo(source, newVirtualPath);
 }
 public virtual Boolean OnTransmitting(VirtualFile file)
 {
     return UnifiedFile.OnTransmitting(file);
 }
		void AddVirtualFile (VirtualFile file, BuildProviderCollection bpcoll)
		{
			if (file == null || BuildManager.IgnoreVirtualPath (file.VirtualPath))
				return;
			
			BuildProvider bp = GetBuildProvider (file.VirtualPath, bpcoll);
			if (bp == null)
				return;
			AddBuildProvider (bp);
		}
Exemplo n.º 16
0
 private IEnumerable<string> ReadLines(VirtualFile file)
 {
     using(var s = file.Open())
     using (var sr = new StreamReader(s))
     {
         while(sr.Peek() >= 0)
         {
             yield return sr.ReadLine();
         }
     }
 }
Exemplo n.º 17
0
 /// <summary>
 /// 从指定虚拟文件中读取文本内容
 /// </summary>
 /// <param name="file">虚拟文件</param>
 /// <returns></returns>
 public static string LoadContent( VirtualFile file )
 {
   using ( var reader = new StreamReader( file.Open(), true ) )
   {
     return reader.ReadToEnd();
   }
 }
        private VirtualFile GetModuleCustomVirtualFile(string virtualPath, VirtualFile actualFile) {
            var file = GetModuleVirtualOverride(virtualPath);
            if (file == null)
                return null;

            if (Logger.IsEnabled(LogLevel.Debug)) {
                Logger.Debug("Virtual file from module \"{0}\" served with specific assembly directive:", file.ModuleName);
                Logger.Debug("  Virtual path:       {0}", virtualPath);
                Logger.Debug("  Assembly directive: {0}", file.Directive);
            }

            return new WebFormVirtualFile(virtualPath, actualFile, file.Directive);
        }
Exemplo n.º 19
0
 public TestVirtualPathProvider(string path, VirtualFile file)
 {
     this.path = path;
     this.file = file;
 }
Exemplo n.º 20
0
        // Copy one file from the source app to the precompiled app
        private void CopyPrecompiledFile(VirtualFile vfile, string destPhysicalPath) {

            bool createStub;

            if (CompilationUtil.NeedToCopyFile(vfile.VirtualPathObject, PrecompilingForUpdatableDeployment,
                out createStub)) {

                // 
                string sourcePhysicalPath = HostingEnvironment.MapPathInternal(vfile.VirtualPath);

                // The file could already exist with updatable precompilation, since we would create the modified file
                // earlier during processing of a code beside page.
                if (File.Exists(destPhysicalPath)) {

                    // In that case, we still need to fix it up to insert the correct type string in the
                    // inherits attribute (VSWhidbey 467936)

                    // First, get the just-compiled BuildResult.  It should always exist
                    BuildResultCompiledType result = GetVPathBuildResult(null, vfile.VirtualPathObject,
                        true /*noBuild*/, false /*allowCrossApp*/) as BuildResultCompiledType;
                    Debug.Assert(result != null);

                    // VSWhidbey 527299. Need to use the same encoding of the original file to
                    // read and write to the new file.
                    Encoding encoding = Util.GetEncodingFromConfigPath(vfile.VirtualPathObject);

                    // Read in the file
                    string newAspxFile = Util.StringFromFile(destPhysicalPath, ref encoding);

                    // Replace the placeholder token by the true type with the assembly
                    newAspxFile = newAspxFile.Replace(UpdatableInheritReplacementToken,
                        Util.GetAssemblyQualifiedTypeName(result.ResultType));

                    // Write the modified file back with the correct inherits type string
                    StreamWriter writer = new StreamWriter(destPhysicalPath, false /* append */, encoding);
                    writer.Write(newAspxFile);
                    writer.Close();
                }
                else {
                    // Just copy the file to the destination
                    File.Copy(sourcePhysicalPath, destPhysicalPath, false /*overwrite*/);
                }

                // If it has a readonly attribute, clear it on the destination (VSWhidbey 122359)
                Util.ClearReadOnlyAttribute(destPhysicalPath);
            }
            else {
                if (createStub) {
                    // Create the stub file, with a helpful static message
                    StreamWriter writer = new StreamWriter(destPhysicalPath);
                    writer.Write(SR.GetString(SR.Precomp_stub_file));
                    writer.Close();
                }
            }
        }
 public void AddFile(VirtualFile file)
 {
     FileMap[NormalizeVirtualPath(file.VirtualPath)] = file;
 }
Exemplo n.º 22
0
 private static string GetUrl(VirtualFile file)
 {
     string dir = VirtualPathUtility.GetDirectory(file.VirtualPath);
     string filePath = Path.GetFileNameWithoutExtension(file.VirtualPath);
     return VirtualPathUtility.Combine(dir, filePath).ToLowerInvariant();
 }
Exemplo n.º 23
0
 public VirtualFileWrapper(VirtualFile fileToWrap)
 {
     this.UnderlyingFile = fileToWrap;
 }
Exemplo n.º 24
0
        /// <summary>
        /// Gets the file dependencies (@imports) of the LESS file being parsed.
        /// </summary>
        /// <param name="lessParser">The LESS parser.</param>
        /// <param name="virtualFile">The virtual file</param>
        /// <returns>An array of file references to the dependent file references.</returns>
        private IEnumerable<BundleFile> GetFileDependencies(Parser lessParser, VirtualFile virtualFile)
        {
            var pathResolver = this.GetPathResolver(lessParser);

            foreach (var importPath in lessParser.Importer.Imports)
            {
                yield return new BundleFile(pathResolver.GetFullPath(importPath), virtualFile);
            }

            lessParser.Importer.Imports.Clear();
        }
 internal void WriteVirtualFile(VirtualFile vf)
 {
     using (Stream stream = vf.Open())
     {
         if (this.UsingHttpWriter)
         {
             long length = stream.Length;
             if (length > 0L)
             {
                 byte[] buffer = new byte[(int) length];
                 int count = stream.Read(buffer, 0, (int) length);
                 this._httpWriter.WriteBytes(buffer, 0, count);
             }
         }
         else
         {
             this.WriteStreamAsText(stream, 0L, -1L);
         }
     }
 }
Exemplo n.º 26
0
		internal void TransmitFile (VirtualFile vf, bool final_flush)
		{
			if (vf == null)
				throw new ArgumentNullException ("vf");

			if (vf is DefaultVirtualFile) {
				TransmitFile (HostingEnvironment.MapPath (vf.VirtualPath), final_flush);
				return;
			}
			
			byte[] buf = new byte [bufLen];
			using (Stream s = vf.Open ()) {
				int readBytes;
				while ((readBytes = s.Read (buf, 0, bufLen)) > 0) {
					output_stream.Write (buf, 0, readBytes);
					output_stream.ApplyFilter (final_flush);
					Flush (false);
				}
				if (final_flush)
					Flush (true);
			}
		}
Exemplo n.º 27
0
 public FileToStreamImpl(string path)
 {
     path = HttpUtility.UrlDecode(path);
     string filePath = HttpContext.Current.Server.MapPath(path);
     if (File.Exists(filePath))
     {
         m_FileInfo = new FileInfo(filePath);
     }
     else if (HostingEnvironment.VirtualPathProvider.FileExists(path))
     {
         m_VirtualFile = HostingEnvironment.VirtualPathProvider.GetFile(path);
     }
 }