示例#1
0
        /// <summary>
        /// Processes the view - using the templateName
        /// to obtain the correct template
        /// and writes the results to the System.IO.TextWriter.
        /// </summary>
        /// <param name="templateName"></param>
        /// <param name="output"></param>
        /// <param name="context"></param>
        /// <param name="controller"></param>
        /// <param name="controllerContext"></param>
        /// <exception cref="MonoRailException"><c>MonoRailException</c>.</exception>
        public override void Process(string templateName, TextWriter output, IEngineContext context, IController controller,
                                     IControllerContext controllerContext)
        {
            var viewFileName = string.Concat(templateName, ViewFileExtension);

            var viewSource = ViewSourceLoader.GetViewSource(viewFileName);

            if (viewSource == null)
            {
                throw new MonoRailException(404, "No such view", string.Format("Missing or invalid view: {0}", viewFileName));
            }
            var layoutNames = controllerContext.LayoutNames;

            var view = new RubyView(viewFileName, output, viewSource, _scriptRuntime);

            if (layoutNames != null)
            {
                var last = view;

                for (int i = layoutNames.Length - 1; i >= 0; i--)
                {
                    Debug.Write(i);
                    IViewSource layoutSource;
                    var         layoutFileName = string.Concat("Layouts\\", layoutNames[i].Trim(), ViewFileExtension);
                    layoutSource = ViewSourceLoader.GetViewSource(layoutFileName);
                    if (layoutSource == null)
                    {
                        throw new MonoRailException(string.Format("Layout '{0}' cannot be found or loaded.", layoutNames[i].Trim()));
                    }
                    last.Parent = new RubyView(layoutFileName, output, layoutSource, _scriptRuntime);
                    last        = last.Parent;
                }
            }
            view.Render();
        }
示例#2
0
        public override bool HasTemplate(string templateName)
        {
            string fileName  = GetFileName(templateName);
            string className = GetClassName(NormalizeFileName(fileName));
            Type   viewType  = compilations[className] as Type;

            if (viewType != null)
            {
                return(true);
            }
            else
            {
                return(ViewSourceLoader.HasTemplate(fileName));
            }
        }
示例#3
0
        // create an input from a resource name
        public ICompilerInput CreateInput(string name)
        {
            var viewSrc = ViewSourceLoader.GetViewSource(name);

            if (viewSrc == null)
            {
                throw new MonoRailException("{0} is not a valid view", name);
            }
            // I need to do it this way because I can't tell
            // when to dispose of the stream.
            // It is not expected that this will be a big problem, the string
            // will go away after the compile is done with them.
            using (var stream = new StreamReader(viewSrc.OpenViewStream()))
            {
                return(new StringInput(name, stream.ReadToEnd()));
            }
        }
示例#4
0
        // If batch compilation is set to true, this would return all the view scripts
        // in the director (not recursive!)
        // Otherwise, it would return just the single file
        private IDictionary <ICompilerInput, string> GetInput(string filename, bool batch)
        {
            var input2FileName = new Dictionary <ICompilerInput, string>();

            if (batch == false)
            {
                input2FileName.Add(CreateInput(filename), filename);
                return(input2FileName);
            }
            // use the System.IO.Path to get the folder name even though
            // we are using the ViewSourceLoader to load the actual file
            var directory = Path.GetDirectoryName(filename);

            foreach (var file in ViewSourceLoader.ListViews(directory, this.ViewFileExtension, this.JSGeneratorFileExtension))
            {
                var input = CreateInput(file);
                input2FileName.Add(input, file);
            }
            return(input2FileName);
        }
示例#5
0
        public void Initialize()
        {
            var props = new ExtendedProperties();

            if (ViewSourceLoader.HasSource("nvelocity.properties"))
            {
                using (var stream = ViewSourceLoader.GetViewSource("nvelocity.properties").OpenViewStream())
                {
                    props.Load(stream);
                }
            }

            // Set up a custom directive manager
            props.SetProperty("directive.manager",
                              "Castle.MonoRail.Framework.Views.NVelocity.CustomDirectiveManager; Castle.MonoRail.Framework.Views.NVelocity");

            InitializeVelocityProperties(props);

            velocity.SetApplicationAttribute(ServiceProvider, provider);

            velocity.Init(props);
        }
示例#6
0
        private void LoadMacros(ExtendedProperties props)
        {
            var macros = ViewSourceLoader.ListViews("macros", this.ViewFileExtension, this.JSGeneratorFileExtension);

            var macroList = new ArrayList(macros);

            if (macroList.Count > 0)
            {
                var libPropValue = props.GetProperty(RuntimeConstants.VM_LIBRARY);

                if (libPropValue is ICollection)
                {
                    macroList.AddRange((ICollection)libPropValue);
                }
                else if (libPropValue is string)
                {
                    macroList.Add(libPropValue);
                }

                props.AddProperty(RuntimeConstants.VM_LIBRARY, macroList);
            }

            props.AddProperty(RuntimeConstants.VM_LIBRARY_AUTORELOAD, true);
        }
示例#7
0
        public IList <SparkViewDescriptor> CreateDescriptors(SparkBatchEntry entry)
        {
            var descriptors = new List <SparkViewDescriptor>();

            var metaDesc = _controllerDescriptorProvider.BuildDescriptor(entry.ControllerType);

            var controllerName = metaDesc.ControllerDescriptor.Name;
            var controllerPath = controllerName;

            if (!string.IsNullOrEmpty(metaDesc.ControllerDescriptor.Area))
            {
                controllerPath = metaDesc.ControllerDescriptor.Area + "\\" + controllerName;
            }

            var viewNames    = new List <string>();
            var includeViews = entry.IncludeViews;

            if (includeViews.Count == 0)
            {
                includeViews = new[] { "*" }
            }
            ;

            var accessors = new List <SparkViewDescriptor.Accessor>();

            foreach (var helper in metaDesc.Helpers)
            {
                var typeName     = helper.HelperType.FullName;
                var propertyName = helper.Name ?? helper.HelperType.Name;
                accessors.Add(new SparkViewDescriptor.Accessor
                {
                    Property = typeName + " " + propertyName,
                    GetValue = "Helper<" + typeName + ">(\"" + propertyName + "\")"
                });
            }

            foreach (var include in includeViews)
            {
                if (include.EndsWith("*"))
                {
                    foreach (var fileName in ViewSourceLoader.ListViews(controllerPath))
                    {
                        // ignore files which are not spark extension
                        if (!string.Equals(Path.GetExtension(fileName), ".spark", StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }

                        var potentialMatch = Path.GetFileNameWithoutExtension(fileName);
                        if (!TestMatch(potentialMatch, include))
                        {
                            continue;
                        }

                        var isExcluded = false;
                        foreach (var exclude in entry.ExcludeViews)
                        {
                            if (!TestMatch(potentialMatch, RemoveSuffix(exclude, ".spark")))
                            {
                                continue;
                            }

                            isExcluded = true;
                            break;
                        }
                        if (!isExcluded)
                        {
                            viewNames.Add(potentialMatch);
                        }
                    }
                }
                else
                {
                    // explicitly included views don't test for exclusion
                    viewNames.Add(RemoveSuffix(include, ".spark"));
                }
            }

            foreach (var viewName in viewNames)
            {
                var layoutNamesList = entry.LayoutNames;
                if (layoutNamesList.Count == 0)
                {
                    var action = metaDesc.Actions[viewName];
                    if (action != null)
                    {
                        var actionDesc = metaDesc.ActionDescriptors[action];
                        if (actionDesc != null && actionDesc.Layout != null)
                        {
                            layoutNamesList = new[] { actionDesc.Layout.LayoutNames }
                        }
                        ;
                    }
                }
                if (layoutNamesList.Count == 0)
                {
                    if (metaDesc.Layout != null)
                    {
                        layoutNamesList = new[] { metaDesc.Layout.LayoutNames }
                    }
                    ;
                    else
                    {
                        layoutNamesList = new[] { new string[0] }
                    };
                }

                foreach (var layoutNames in layoutNamesList)
                {
                    descriptors.Add(CreateDescriptor(
                                        entry.ControllerType.Namespace,
                                        controllerPath,
                                        viewName,
                                        layoutNames,
                                        accessors));
                }
            }

            return(descriptors);
        }
示例#8
0
 IViewFile IViewFolder.GetViewSource(string path)
 {
     return(new ViewFile(ViewSourceLoader.GetViewSource(path)));
 }
示例#9
0
 bool IViewFolder.HasView(string path)
 {
     return(ViewSourceLoader.HasSource(path));
 }
示例#10
0
 IList <string> IViewFolder.ListViews(string path)
 {
     return(ViewSourceLoader.ListViews(path));
 }
示例#11
0
 /// <summary>
 /// Evaluates whether the specified template exists.
 /// </summary>
 /// <returns><c>true</c> if it exists</returns>
 public virtual bool HasJsGenerationTemplate(String templateName)
 {
     return(ViewSourceLoader.HasSource(ResolveJSTemplateName(templateName)));
 }
示例#12
0
 /// <summary>
 /// Evaluates whether the specified template exists.
 /// </summary>
 /// <returns><c>true</c> if it exists</returns>
 public virtual bool HasTemplate(String templateName)
 {
     return
         (ViewSourceLoader.HasSource(ResolveTemplateName(templateName)) ||
          ViewSourceLoader.HasSource(ResolveJSTemplateName(templateName)));
 }
 /// <summary>
 /// Evaluates whether the specified template exists.
 /// </summary>
 /// <param name="templateName"></param>
 /// <returns><c>true</c> if it exists</returns>
 public override bool HasTemplate(String templateName)
 {
     return(ViewSourceLoader.HasSource(templateName + ".aspx"));
 }