Example #1
0
        public override PageDefinition Parse(string resourceLocation)
        {
            var result = new PageDefinition();
            try
            {
                // TODO Valutare se usare Xpath per la lettura

                XDocument xdoc = _resourceProvider.ResourceToXml(resourceLocation);

                var rootElement = xdoc.Element(PAGE);
                if (rootElement == null)
                    throw new ApplicationException(string.Format("Element root '{0}' not found.", PAGE));

                ParseModules(xdoc, result);

                var paramsElement = xdoc.Root.Element(PARAMETERS);
                if (paramsElement != null)
                    ParseParameters(paramsElement, result);

                ParseActions(xdoc, result);
            }
            catch (Exception ex)
            {
                throw new XrcException(string.Format("Failed to parse '{0}'.", resourceLocation), ex);
            }

            return result;
        }
Example #2
0
        // TODO E' possibile semplificare e irrobustire questo codice?
        // TODO Potrebero esserci problemi di cache e dipendenze? Da ottimizzare in qualche modo?
        public override PageDefinition Parse(string resourceLocation)
        {
            var result = new PageDefinition();

            var action = new PageAction("GET");

            var moduleDefinitionList = new ModuleDefinitionList();
            var pageParameters = new PageParameterList();

            var viewComponentDefinition = _viewCatalog.Get(typeof(XsltView).Name);
            if (viewComponentDefinition == null)
                throw new XrcException(string.Format("View '{0}' not found on catalog.", typeof(XsltView).Name));

            var view = new ViewDefinition(viewComponentDefinition, null);

            XDocument xsltContent = _resourceProvider.ResourceToXml(resourceLocation);
            AddProperty(viewComponentDefinition, view, "Xslt", xsltContent);

            string dataVirtualPath = resourceLocation.Replace(".xrc.xslt", ".xml");
            if (_resourceProvider.ResourceExists(dataVirtualPath))
            {
                AddProperty(viewComponentDefinition, view, "Data", _resourceProvider.ResourceToXml(dataVirtualPath));
            }

            action.Views.Add(view);
            result.Actions.Add(action);

            return result;
        }
Example #3
0
        public override PageDefinition Parse(string resourceLocation)
        {
            var result = new PageDefinition();

            var action = new PageAction("GET");

            var moduleDefinitionList = new ModuleDefinitionList();
            var pageParameters = new PageParameterList();

            var viewComponentDefinition = _viewCatalog.Get(typeof(XHtmlView).Name);
            if (viewComponentDefinition == null)
                throw new XrcException(string.Format("View '{0}' not found on catalog.", typeof(XHtmlView).Name));

            var view = new ViewDefinition(viewComponentDefinition, null);
            string propertyName = "Content";
            var viewProperty = viewComponentDefinition.Type.GetProperty(propertyName);
            if (viewProperty == null)
                throw new XrcException(string.Format("Property '{0}' for type '{1}' not found.", propertyName, viewComponentDefinition.Type.FullName));

            XDocument content = _resourceProvider.ResourceToXml(resourceLocation);
            var propertyValue = new XValue(viewProperty.PropertyType, content);

            view.Properties.Add(new XProperty(viewProperty, propertyValue));
            action.Views.Add(view);
            result.Actions.Add(action);

            return result;
        }
        PageDefinition GetInheritedDefinitions(PageDirectory directory)
        {
            PageDefinition directoryDefinition;
            if (directory.ConfigFile != null)
                directoryDefinition = _pageParser.Parse(directory.ConfigFile.ResourceLocation);
            else
                directoryDefinition = new PageDefinition();

            if (directory.Parent != null)
                return GetInheritedDefinitions(directory.Parent).Combine(directoryDefinition);

            return directoryDefinition;
        }
        public void It_Should_be_possible_to_union_a_result_actions()
        {
            var target = new PageDefinition();
            target.Actions.Add(new PageAction("GET"));
            target.Actions.Add(new PageAction("POST"));

            var other = new PageDefinition();
            other.Actions.Add(new PageAction("GET"));
            other.Actions.Add(new PageAction("DELETE"));

            var result = target.Combine(other);

            Assert.AreEqual(3, result.Actions.Count);
            Assert.AreNotEqual(target.Actions["GET"], result.Actions["GET"]);
            Assert.AreEqual(other.Actions["GET"], result.Actions["GET"]);
        }
        public void It_Should_be_possible_to_union_a_result_modules()
        {
            var target = new PageDefinition();
            target.Modules.Add(new ModuleDefinition("m1", new ComponentDefinition("m1", typeof(string))));
            target.Modules.Add(new ModuleDefinition("m2", new ComponentDefinition("m2", typeof(string))));

            var other = new PageDefinition();
            other.Modules.Add(new ModuleDefinition("m1", new ComponentDefinition("m1", typeof(string))));
            other.Modules.Add(new ModuleDefinition("m3", new ComponentDefinition("m3", typeof(string))));

            var result = target.Combine(other);

            Assert.AreEqual(3, result.Modules.Count);
            Assert.AreNotEqual(target.Modules["m1"], result.Modules["m1"]);
            Assert.AreEqual(other.Modules["m1"], result.Modules["m1"]);
        }
        public void It_Should_be_possible_to_union_a_result_parameters()
        {
            var target = new PageDefinition();
            target.Parameters.Add(new PageParameter("p1", new XValue(typeof(string), "v1")));
            target.Parameters.Add(new PageParameter("p2", new XValue(typeof(string), "v2")));

            var other = new PageDefinition();
            other.Parameters.Add(new PageParameter("p1", new XValue(typeof(string), "v1.1")));
            other.Parameters.Add(new PageParameter("p3", new XValue(typeof(string), "v3")));

            var result = target.Combine(other);

            Assert.AreEqual(3, result.Parameters.Count);
            Assert.AreNotEqual(target.Parameters["p1"], result.Parameters["p1"]);
            Assert.AreEqual(other.Parameters["p1"], result.Parameters["p1"]);
        }
        public void Default_layout_should_not_override_specific_layout_set_to_empty()
        {
            var root = new TestPageStructure1().GetRoot();
            var layout = root.DefaultLayoutFile;
            var config = root.ConfigFile;
            var file = root.Files["about.xrc"];

            var pageDefinition = new PageDefinition();
            pageDefinition.Actions.Add(new PageAction("GET") { Layout = "" });

            var target = CreateTarget();
            SetupUrl(file.BuildUrl(), new PageLocatorResult(file, new UriSegmentParameterList()), pageDefinition);
            _pageParser.Setup(p => p.Parse(config.ResourceLocation)).Returns(new PageDefinition());

            var page = target.GetPage(file.BuildUrl());
            var actionGet = page.Actions["get"];
            Assert.AreEqual("", actionGet.Layout);
        }
Example #9
0
        public PageDefinition Combine(PageDefinition other)
        {
            var result = new PageDefinition();

            foreach (var a in Actions)
                result.Actions[a.Method] = a;
            foreach (var a in other.Actions)
                result.Actions[a.Method] = a;

            foreach (var a in Parameters)
                result.Parameters[a.Name] = a;
            foreach (var a in other.Parameters)
                result.Parameters[a.Name] = a;

            foreach (var a in Modules)
                result.Modules[a.Name] = a;
            foreach (var a in other.Modules)
                result.Modules[a.Name] = a;

            return result;
        }
Example #10
0
        private ViewDefinition ParseView(XElement xElement, PageDefinition parserResult)
        {
            string viewTypeName = xElement.Name.LocalName;
            ComponentDefinition viewComponent = _viewCatalog.Get(viewTypeName);
            if (viewComponent == null)
                throw new XrcException(string.Format("View '{0}' not found.", viewTypeName));

            if (!typeof(IView).IsAssignableFrom(viewComponent.Type))
                throw new ApplicationException(string.Format("Type '{0}' is not a view.", viewComponent.Type));

            string slot = string.Empty;
            if (xElement.Attribute(SLOT) != null)
                slot = xElement.AttributeAs<string>(SLOT);

            ViewDefinition view = new ViewDefinition(viewComponent, slot);

            // TODO Escludere le property con un namespace? O usare un namespace particolare per le property?
            foreach (var element in xElement.Elements())
            {
                var property = ParseProperty(element, viewComponent.Type, parserResult);

                view.Properties.Add(property);
            }

            return view;
        }
Example #11
0
        private XProperty ParseProperty(XElement element, Type ownerType, PageDefinition parserResult)
        {
            string propertyName = element.Name.LocalName;
            var property = ownerType.GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
            if (property == null)
            {
                //XProperty propertyFile = ParsePropertyFile(item, ownerType, propertyName, element.Value);
                //if (propertyFile != null)
                //    return propertyFile;

                throw new ApplicationException(string.Format("Property '{0}' not found on type '{1}.", propertyName, ownerType));
            }

            if (element.HasElements)
            {
                if (property.PropertyType == typeof(XDocument))
                {
                    if (element.Elements().Count() > 1)
                        throw new ApplicationException(string.Format("Element '{0}' not recognized for property '{1}', there is no root.", element.Name, property.Name));
                    else
                    {
                        // TODO Valutare se c'รจ un modo migliore per passare da un XElement a un XDocument
                        // o valutare se usare invece XElement direttamente e non richiedere un XDocument
                        string xmlContent = element.Elements().First().ToString();
                        var xValue = new XValue(property.PropertyType, XDocument.Parse(xmlContent));
                        return new XProperty(property, xValue);
                    }
                }
                else
                    throw new ApplicationException(string.Format("Element '{0}' not recognized for property '{1}'", element.Name, property.Name));
            }
            else if (element.IsEmpty == false)
            {
                var xValue = _scriptService.Parse(element.Value, property.PropertyType, parserResult.Modules, parserResult.Parameters);
                return new XProperty(property, xValue);
            }
            else
                throw new ApplicationException(string.Format("Invalid element '{0}', value not defined.", element.Name));
        }
Example #12
0
        private void ParseParameters(XElement paramsElement, PageDefinition parserResult)
        {
            foreach (var addElement in paramsElement.Elements(ADD))
            {
                string key = addElement.AttributeAs<string>(KEY);
                string value = addElement.AttributeAsOrDefault<string>(VALUE);
                string typeName = addElement.AttributeAsOrDefault<string>(TYPE);
                if (string.IsNullOrWhiteSpace(typeName))
                    typeName = typeof(string).FullName;
                Type type = Type.GetType(typeName, true, true);
                bool allowRequestOverride = addElement.AttributeAsOrDefault<bool>(ALLOWREQUESTOVERRIDE);

                var xValue = _scriptService.Parse(value, type, parserResult.Modules, parserResult.Parameters);

                parserResult.Parameters.Add(new PageParameter(key, xValue, allowRequestOverride));
            }
        }
Example #13
0
        private void ParseModules(XDocument doc, PageDefinition parserResult)
        {
            foreach (var attr in doc.Root.Attributes())
            {
                if (attr.Name.Namespace == XNamespace.Xmlns)
                {
                    Uri moduleUri = new Uri(attr.Value);
                    if (moduleUri.Scheme == MODULE_PREFIX)
                    {
                        string name = attr.Name.LocalName;

                        var moduleDefinition = new ModuleDefinition(name, _moduleCatalog.Get(moduleUri.Segments[0]));
                        parserResult.Modules.Add(moduleDefinition);
                    }
                }
            }
        }
        public void Slot_pages_should_not_have_a_default_layout()
        {
            var root = new TestPageStructure1().GetRoot();
            var layout = root.DefaultLayoutFile;
            var config = root.ConfigFile;
            var fileNormal = root.Directories["news"].Files["index.xrc"];
            var fileslot = root.Directories["news"].Files["_slot1.xrc"];

            var pageNormalDefinition = new PageDefinition();
            pageNormalDefinition.Actions.Add(new PageAction("GET"));
            var pageSlotDefinition = new PageDefinition();
            pageSlotDefinition.Actions.Add(new PageAction("GET"));

            var target = CreateTarget();
            SetupUrl(fileNormal.BuildUrl(), new PageLocatorResult(fileNormal, new UriSegmentParameterList()), pageNormalDefinition);
            SetupUrl(fileslot.BuildUrl(), new PageLocatorResult(fileslot, new UriSegmentParameterList()), pageSlotDefinition);
            _pageParser.Setup(p => p.Parse(config.ResourceLocation)).Returns(new PageDefinition());

            var page = target.GetPage(fileNormal.BuildUrl());
            var actionGet = page.Actions["get"];
            Assert.AreEqual("~/_layout", actionGet.Layout);

            var pageSlot = target.GetPage(fileslot.BuildUrl());
            var actionGetSlot = pageSlot.Actions["get"];
            Assert.IsNull(actionGetSlot.Layout);
        }
        private void SetupUrl(XrcUrl url, PageLocatorResult locatorResult, PageDefinition parserResult)
        {
            _pageLocator.Setup(p => p.Locate(url)).Returns(locatorResult);

            if (locatorResult != null)
                _pageParser.Setup(p => p.Parse(locatorResult.Page.ResourceLocation)).Returns(parserResult);
        }
        public void It_Should_be_possible_to_parse_page_and_get_default_layout_only_on_GET()
        {
            var root = new TestPageStructure1().GetRoot();
            var layout = root.DefaultLayoutFile;
            var config = root.ConfigFile;
            var file = root.Files["about.xrc"];

            var pageDefinition = new PageDefinition();
            pageDefinition.Actions.Add(new PageAction("GET"));
            pageDefinition.Actions.Add(new PageAction("POST"));

            var target = CreateTarget();
            SetupUrl(file.BuildUrl(), new PageLocatorResult(file, new UriSegmentParameterList()), pageDefinition);
            _pageParser.Setup(p => p.Parse(config.ResourceLocation)).Returns(new PageDefinition());

            var page = target.GetPage(file.BuildUrl());
            var actionGet = page.Actions["get"];
            Assert.AreEqual("~/_layout", actionGet.Layout);

            var actionPost = page.Actions["post"];
            Assert.IsNull(actionPost.Layout);
        }
        void SetLayout(PageDefinition pageDefinition, PageFile pageFile, UriSegmentParameterList uriParameters)
        {
            if (pageFile.IsSlot || pageFile.IsDefaultLayout)
                return;

            if (!pageDefinition.Actions.Contains("GET"))
                return;

            if (pageFile.DefaultLayoutFile == null)
                return;

            var action = pageDefinition.Actions["GET"];
            if (action != null && action.Layout == null)
                action.Layout = pageFile.DefaultLayoutFile.BuildUrl(uriParameters).ToString();
        }
        public void It_Should_be_possible_to_parse_folder_parameters()
        {
            var root = new TestPageStructure1().GetRoot();
            var config = root.ConfigFile;
            var file = root.Files["about.xrc"];

            var configParserResult = new PageDefinition();
            configParserResult.Parameters.Add(new PageParameter("folderParameter1", new XValue("folder")));
            configParserResult.Parameters.Add(new PageParameter("folderParameter2", new XValue("folder")));

            var pageParserResult = new PageDefinition();
            pageParserResult.Parameters.Add(new PageParameter("folderParameter2", new XValue("page")));

            var target = CreateTarget();
            SetupUrl(file.BuildUrl(), new PageLocatorResult(file, new UriSegmentParameterList()), pageParserResult);
            _pageParser.Setup(p => p.Parse(config.ResourceLocation)).Returns(configParserResult);

            var page = target.GetPage(file.BuildUrl());
            Assert.AreEqual("folder", page.PageParameters["folderParameter1"].Value.ToString());
            Assert.AreEqual("page", page.PageParameters["folderParameter2"].Value.ToString());
        }
Example #19
0
        private void ParseActions(XDocument xdoc, PageDefinition parserResult)
        {
            foreach (var actionElement in xdoc.Root.Elements(ACTION))
            {
                string method = actionElement.AttributeAsOrDefault<string>(METHOD);
                if (method == null)
                    method = DEFAULT_METHOD;
                var action = new PageAction(method);
                if (actionElement.Attribute(LAYOUT) != null)
                    action.Layout = actionElement.AttributeAs<string>(LAYOUT);

                foreach (var subElement in actionElement.Elements())
                {
                    if (subElement.Name.Namespace != XMLNS)
                        continue;

                    if (subElement.Name == OUTPUTCACHE)
                    {
                    }
                    else if (subElement.Name == CATCHEXCEPTION)
                    {
                        var url = subElement.AttributeAsOrDefault<string>(URL);
                        action.CatchException = new PageCatch(url);
                    }
                    else
                    {
                        var view = ParseView(subElement, parserResult);
                        action.Views.Add(view);
                    }
                }

                parserResult.Actions.Add(action);
            }
        }
        public void It_Should_be_possible_to_parse_page_and_get_default_layout_also_when_is_not_present()
        {
            var root = new TestPageStructure2().GetRoot();
            var layout = root.DefaultLayoutFile;
            var file = root.Files["about.xrc"];

            var pageDefinition = new PageDefinition();
            pageDefinition.Actions.Add(new PageAction("GET"));

            var target = CreateTarget();
            SetupUrl(file.BuildUrl(), new PageLocatorResult(file, new UriSegmentParameterList()), pageDefinition);

            var page = target.GetPage(file.BuildUrl());
            var actionGet = page.Actions["get"];
            Assert.IsNull(actionGet.Layout);
        }