public void OnSaveSettings()
        {
            XmlDocument settingsDoc = new XmlDocument();
            XmlElement root = settingsDoc.AppendElement("ProjectT");

            root.AppendAttribute("Version", s_version);

            foreach (IModule module in Modules)
            {
                XmlElement moduleElement = root.AppendElement(module.Name);
                module.OnSaveSettings(moduleElement);
            }

            settingsDoc.Save(settingsFile);
        }
Exemple #2
0
        public bool AddHistoryItemOld(HistoryItemOld HistoryItemOld)
        {
            if (!string.IsNullOrEmpty(xmlPath))
            {
                lock (thisLock)
                {
                    XmlDocument xml = new XmlDocument();

                    if (File.Exists(xmlPath))
                    {
                        xml.Load(xmlPath);
                    }

                    if (xml.ChildNodes.Count == 0)
                    {
                        xml.AppendChild(xml.CreateXmlDeclaration("1.0", "UTF-8", null));
                        xml.AppendElement("HistoryItemOlds");
                    }

                    XmlNode rootNode = xml.ChildNodes[1];

                    if (rootNode.Name == "HistoryItemOlds")
                    {
                        HistoryItemOld.ID = Helpers.GetRandomAlphanumeric(12);

                        XmlNode HistoryItemOldNode = rootNode.PrependElement("HistoryItemOld");

                        HistoryItemOldNode.AppendElement("ID", HistoryItemOld.ID);
                        HistoryItemOldNode.AppendElement("Filename", HistoryItemOld.Filename);
                        HistoryItemOldNode.AppendElement("Filepath", HistoryItemOld.Filepath);
                        HistoryItemOldNode.AppendElement("DateTimeUtc", HistoryItemOld.DateTimeUtc.ToString("o"));
                        HistoryItemOldNode.AppendElement("Type", HistoryItemOld.Type);
                        HistoryItemOldNode.AppendElement("Host", HistoryItemOld.Host);
                        HistoryItemOldNode.AppendElement("URL", HistoryItemOld.URL);
                        HistoryItemOldNode.AppendElement("ThumbnailURL", HistoryItemOld.ThumbnailURL);
                        HistoryItemOldNode.AppendElement("DeletionURL", HistoryItemOld.DeletionURL);
                        HistoryItemOldNode.AppendElement("ShortenedURL", HistoryItemOld.ShortenedURL);

                        xml.Save(xmlPath);

                        return true;
                    }
                }
            }

            return false;
        }
        protected virtual void SaveSettings()
        {
            var settingsDoc = new XmlDocument();
            var root = settingsDoc.AppendElement(SettingsNode);

            root.AppendAttribute("Version", Version);

            foreach (IModule module in Modules)
            {
                XmlElement moduleElement = root.AppendElement(module.GetCodeName());
                module.OnSaveSettings(moduleElement);
            }

            try
            {
                settingsDoc.Save(SettingsFile);
            }
            catch (System.Exception)
            {
                // TODO: log error saving file
            }
        }
Exemple #4
0
        internal void GlobalizeResources()
        {
            log.InfoFormat("Globalizing resources for category '{0}' in directory '{1}'",
                arguments["category"], categoryPath);

            DateTime started = DateTime.Now;
            XmlDocument summaryDoc = new XmlDocument();
            XmlElement internationalization = summaryDoc.AppendElement(summaryDoc.CreateElement("internationalization"));

            internationalization.SetAttribute("date", started.ToString("dd-MM-yyyy"));
            internationalization.SetAttribute("time", started.ToString("hh:mm:ss"));

            XmlElement result = internationalization.AppendElement(summaryDoc.CreateElement("result"));
            XmlElement categorySummary = result.AppendElement(summaryDoc.CreateElement("category"));
            categorySummary.SetAttribute("name", arguments["category"]);

            CategoryInfo info = context.ProjectConfiguration.Categories[arguments["category"]];
            XmlElement localesElement = categorySummary.AppendElement(summaryDoc.CreateElement("locales"));
            foreach (string name in info.Locales)
            {
                LocaleInfo locale = context.ProjectConfiguration.Locales[name];
                XmlElement categoryNode = localesElement.AppendElement(summaryDoc.CreateElement("locale"));
                categoryNode.SetAttribute("name", name);
                categoryNode.SetAttribute("dictionaries", string.Join(", ", locale.DictionaryNames));
            }

            log.DebugFormat("Category {0} is configured for {1} locales: {2}",
                arguments["category"], info.Locales.Count, string.Join(",", info.Locales.ToArray()));

            XmlElement resourcesElement = categorySummary.AppendElement(summaryDoc.CreateElement("resources"));
            resourcesElement.SetAttribute("path", arguments["targetPath"]);

            if (Directory.Exists(categoryPath))
            {
                var files = this.GetFiles();
                if (files.Count() == 0)
                {
                    log.Info("No globalizable files found.");
                }
                else
                {
                    IEnumerable<XmlResource> resources = this.GetGlobalizableResources();
                    foreach (XmlResource resource in resources)
                    {
                        log.InfoFormat("Processing {0}", resource.Name.Signature);
                        InternationalizationSummary summary = resource.Globalize();
                        XmlElement summaryElement = summary.ToXml(summaryDoc);
                        resourcesElement.AppendChild(summaryElement);
                    }
                }
            }
            else
            {
                log.DebugFormat("The target directory '{0}' for category {1} doesn't exist.",
                    categoryPath, arguments["category"]);
            }

            DateTime completed = DateTime.Now;
            TimeSpan elapsed = new TimeSpan(completed.Ticks - started.Ticks);
            internationalization.SetAttribute("duration", string.Format("{0}.{1:D3}s", elapsed.Seconds, elapsed.Milliseconds));

            if (!string.IsNullOrWhiteSpace(arguments["reportpath"]))
                this.SummarizeOverview(summaryDoc, arguments["reportpath"]);
        }
        private EntityResult GetClientResourceReader(SageContext context, string sourcePath)
        {
            XmlDocument document = new XmlDocument();
            XmlElement rootNode = document.AppendElement(document.CreateElement("kelp:resources", Kelp.XmlNamespaces.KelpNamespace));
            XmlReader reader = new XmlNodeReader(rootNode);

            string sourcePathAbsolute = context.Path.Resolve(sourcePath.StartsWith("/") ? sourcePath : "~/" + sourcePath);
            string sourcePathRelative = context.Path.GetRelativeWebPath(sourcePathAbsolute, true);
            if (sourcePathAbsolute == null)
                return new EntityResult { Entity = reader, Dependencies = new List<string>() };

            string type = Path.GetExtension(sourcePathAbsolute).Replace(".", string.Empty).ToLower();
            if (!File.Exists(sourcePathAbsolute))
            {
                XmlElement resourceNode = rootNode.AppendElement(document.CreateElement("kelp:resource", Kelp.XmlNamespaces.KelpNamespace));
                resourceNode.SetAttribute("type", type);
                resourceNode.SetAttribute("path", sourcePathAbsolute);
                resourceNode.SetAttribute("src", sourcePathRelative);
                resourceNode.SetAttribute("exists", "false");
                log.ErrorFormat("The resource include file for '{0}' doesn't exist", sourcePathRelative);
                return new EntityResult { Entity = reader, Dependencies = new List<string>() };
            }

            CodeFile file = CodeFile.Create(sourcePathAbsolute, sourcePathRelative);
            if (context.ProjectConfiguration.IsDebugEnabled)
            {
                try
                {
                    foreach (string absolutePath in file.References.Keys)
                    {
                        string relativePath = file.References[absolutePath];
                        XmlElement resourceNode = rootNode.AppendElement(document.CreateElement("kelp:resource", Kelp.XmlNamespaces.KelpNamespace));
                        resourceNode.SetAttribute("path", absolutePath);
                        resourceNode.SetAttribute("type", type);

                        if (File.Exists(absolutePath))
                        {
                            string path = string.Format(RelativeFilePathInDevelopmentSetup, context.Path.GetRelativeWebPath(absolutePath, true));
                            resourceNode.SetAttribute("src", path);
                            resourceNode.SetAttribute("exists", "true");
                        }
                        else
                        {
                            resourceNode.SetAttribute("src", relativePath);
                            resourceNode.SetAttribute("exists", "false");
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("Could not process the code file includes: " + ex.Message);
                }
            }
            else
            {
                XmlElement resourceNode = rootNode.AppendElement(document.CreateElement("kelp:resource", Kelp.XmlNamespaces.KelpNamespace));
                resourceNode.SetAttribute("src", string.Format(RelativeFilePathInProductionSetup, sourcePath, file.ETag));
                resourceNode.SetAttribute("type", type);
                resourceNode.SetAttribute("exists", "true");
            }

            return new EntityResult { Entity = reader, Dependencies = new List<string>(file.References.Keys) };
        }
Exemple #6
0
        /// <summary>
        /// Renders the exception to the specified <paramref name="writer"/>
        /// </summary>
        /// <param name="writer">The writer to render the exception to.</param>
        public virtual void RenderWithoutContext(TextWriter writer)
        {
            Contract.Requires<ArgumentNullException>(writer != null);

            XmlDocument document = new XmlDocument();
            XmlElement documentElement = document.AppendElement(this.ConvertToXml(this.Exception, document));
            Exception inner = this.Exception.InnerException;

            while (inner != null)
            {
                documentElement.AppendChild(this.ConvertToXml(inner, document));
                inner = inner.InnerException;
            }

            documentElement.SetAttribute("date", DateTime.Now.ToString("dd-MM-yyyy"));
            documentElement.SetAttribute("time", DateTime.Now.ToString("hh:mm:ss"));

            XmlReader xslReader = this.StylesheetPath.StartsWith(EmbeddedResourceResolver.Scheme)
                ? XmlReader.Create(EmbeddedResourceResolver.GetStream(this.StylesheetPath), null, this.StylesheetPath)
                : XmlReader.Create(this.StylesheetPath);

            XslCompiledTransform xslTransform = new XslCompiledTransform();
            xslTransform.Load(xslReader, new XsltSettings(true, true), new UrlResolver());

            XmlWriter xmlwr = XmlWriter.Create(writer, xslTransform.OutputSettings);
            xslTransform.Transform(document, xmlwr);
        }
Exemple #7
0
        /// <summary>
        /// Renders the exception to the specified <paramref name="writer"/>
        /// </summary>
        /// <param name="writer">The writer to render the exception to.</param>
        /// <param name="context">The context under which this code is executing.</param>
        public virtual void Render(TextWriter writer, SageContext context)
        {
            Contract.Requires<ArgumentNullException>(context != null);
            Contract.Requires<ArgumentNullException>(writer != null);

            XmlDocument document = new XmlDocument();
            XmlElement documentElement = document.AppendElement(this.ConvertToXml(this.Exception, document));
            Exception inner = this.Exception.InnerException;

            while (inner != null)
            {
                documentElement.AppendChild(this.ConvertToXml(inner, document));
                inner = inner.InnerException;
            }

            documentElement.AppendChild(context.ToXml(document));
            documentElement.SetAttribute("date", DateTime.Now.ToString("dd-MM-yyyy"));
            documentElement.SetAttribute("time", DateTime.Now.ToString("hh:mm:ss"));

            XsltTransform processor = XsltTransform.Create(context, this.StylesheetPath);
            XmlWriter xmlwr = XmlWriter.Create(writer, processor.OutputSettings);

            processor.Transform(documentElement, xmlwr, context, this.GetTransformArguments(context));
        }
Exemple #8
0
        /// <summary>
        /// Wraps the previously processed view configuration input XML with the standard XML envelope that contains 
        /// information about the current request, and the resources referenced by the modules and libraries in use by the 
        /// the view.
        /// </summary>
        /// <param name="viewContext">The view context that contains the <see cref="ViewModel"/> that resulted from
        /// previously processing the view configuration.</param>
        /// <returns>
        /// The actual XML document that will be used as input for the final XSLT transform.
        /// </returns>
        public XmlDocument PrepareViewXml(ViewContext viewContext)
        {
            var startTime = DateTime.Now.Ticks;
            ViewModel model = viewContext.ViewData.Model as ViewModel;

            string action = "action";
            if (model != null)
            {
                action = model.ViewConfiguration.Name;
            }
            else if (this.ViewData["Action"] != null)
            {
                action = this.ViewData["Action"].ToString();
            }

            XmlDocument result = new XmlDocument();
            XmlElement viewRoot = result.AppendElement("sage:view", XmlNamespaces.SageNamespace);
            viewRoot.SetAttribute("controller", this.Name);
            viewRoot.SetAttribute("action", action);
            viewRoot.AppendElement(this.Context.ToXml(result));

            log.DebugFormat("Document create");
            XmlElement responseNode = viewRoot.AppendElement("sage:response", XmlNamespaces.SageNamespace);

            if (messages.Count > 0)
            {
                XmlElement messagesNode = responseNode.AppendElement("sage:messages", XmlNamespaces.SageNamespace);
                foreach (var message in messages)
                {
                    messagesNode.AppendChild(message.ToXml(result));
                }
            }

            try
            {
                if (model != null && model.ConfigNode != null)
                {
                    //// make sure resources are ordered properly:
                    ////  1. by resource type (icon, style, script)
                    ////  2. by project dependency
                    ////  3. by module dependency
                    var installOrder = Project.InstallOrder;
                    var inputResources = model.Resources
                        .Where(r => r.IsValidFor(this.Context.UserAgentID))
                        .OrderBy((r1, r2) =>
                            {
                                if (r1.Type != r2.Type)
                                    return r1.Type.CompareTo(r2.Type);

                                if (r1.ProjectId == r2.ProjectId)
                                    return model.Resources.IndexOf(r1).CompareTo(model.Resources.IndexOf(r2));

                                return installOrder.IndexOf(r1.ProjectId).CompareTo(installOrder.IndexOf(r2.ProjectId));
                            })
                        .ToList();

                    log.DebugFormat("Resources ordered");
                    if (inputResources.Count != 0)
                    {
                        XmlElement resourceRoot = responseNode.AppendElement("sage:resources", XmlNamespaces.SageNamespace);

                        List<Resource> headResources = inputResources.Where(r => r.Location == Sage.ResourceLocation.Head).ToList();
                        List<Resource> bodyResources = inputResources.Where(r => r.Location == Sage.ResourceLocation.Body).ToList();
                        List<Resource> dataResources = inputResources.Where(r => r.Location == Sage.ResourceLocation.Data).ToList();

                        if (dataResources.Count != 0)
                        {
                            XmlNode dataNode = resourceRoot.AppendElement("sage:data", XmlNamespaces.SageNamespace);
                            foreach (Resource resource in dataResources)
                            {
                                var time = DateTime.Now;
                                dataNode.AppendChild(resource.ToXml(result, this.Context));
                                var elapsed = (DateTime.Now - time).Milliseconds;
                                if (elapsed > 0)
                                    log.DebugFormat("Added '{0}' to sage:data in {1}ms", resource.Name, elapsed);
                            }
                        }

                        if (headResources.Count != 0)
                        {
                            XmlNode headNode = resourceRoot.AppendElement("sage:head", XmlNamespaces.SageNamespace);
                            foreach (Resource resource in headResources)
                            {
                                var time = DateTime.Now;
                                headNode.AppendChild(resource.ToXml(result, this.Context));
                                var elapsed = (DateTime.Now - time).Milliseconds;
                                if (elapsed > 0)
                                    log.DebugFormat("Added '{0}' to sage:head in {1}ms", resource.Name.Or(resource.Path), elapsed);
                            }
                        }

                        if (bodyResources.Count != 0)
                        {
                            XmlNode bodyNode = resourceRoot.AppendElement("sage:body", XmlNamespaces.SageNamespace);
                            foreach (Resource resource in bodyResources.OrderBy(r => r.Extension))
                            {
                                var time = DateTime.Now;
                                bodyNode.AppendChild(resource.ToXml(result, this.Context));
                                var elapsed = (DateTime.Now - time).Milliseconds;
                                if (elapsed > 0)
                                    log.DebugFormat("Added '{0}' to sage:body in {1}ms", resource.Name.Or(resource.Path), elapsed);
                            }
                        }
                    }

                    responseNode
                        .AppendElement("sage:model", XmlNamespaces.SageNamespace)
                        .AppendChild(result.ImportNode(model.ConfigNode, true));
                }

                log.DebugFormat("Resources added");
            }
            catch (Exception ex)
            {
                throw new SageHelpException(new ProblemInfo(ProblemType.ResourceProcessingError), ex);
            }

            var viewDataItems = this.ViewData.Keys.Where(k => k != "messages").ToList();
            foreach (var key in viewDataItems)
            {
                object value = viewContext.ViewData[key];
                if (value == null)
                    continue;

                if (value is XmlNode)
                {
                    XmlNode valueNode = (XmlNode) value;
                    if (valueNode.NodeType == XmlNodeType.Document)
                    {
                        XmlDocument doc = (XmlDocument) valueNode;
                        XmlNode importedNode = result.ImportNode(((XmlDocument) valueNode).DocumentElement, true);
                        if (doc.DocumentElement != null) responseNode.AppendChild(importedNode);
                    }
                    else
                        responseNode.AppendChild(result.ImportNode(valueNode, true));
                }
                else if (value is IXmlConvertible)
                {
                    XmlElement valueElement = ((IXmlConvertible) value).ToXml(result);
                    if (valueElement != null)
                        responseNode.AppendChild(valueElement);
                }
                else
                {
                    XmlElement elem = (XmlElement) responseNode.AppendChild(result.CreateElement("sage:value", XmlNamespaces.SageNamespace));
                    elem.SetAttribute("id", key);
                    elem.InnerText = value.ToString();
                }

                log.DebugFormat("Added '{0}' to sage:response", key);
            }

            try
            {
                var finalResult = this.FilterViewXml(viewContext, result);

                var elapsed = new TimeSpan(DateTime.Now.Ticks - startTime);
                log.DebugFormat("Completed preparing view XML view in {0}ms", elapsed.Milliseconds);

                return finalResult;
            }
            catch (Exception ex)
            {
                throw new SageHelpException(new ProblemInfo(ProblemType.ViewXmlFilteringError), ex);
            }
        }