Represents the result returned by a module after processing a request.
Esempio n. 1
0
        public ModuleResult ProcessElement(XmlElement moduleElement, ViewConfiguration configuration)
        {
            ModuleResult result = new ModuleResult(moduleElement);
            XmlDocument ownerDoc = moduleElement.OwnerDocument;
            XmlElement dataElement = ownerDoc.CreateElement("mod:data", XmlNamespaces.ModulesNamespace);
            XmlElement metaElement = dataElement.AppendElement("mod:meta", XmlNamespaces.ModulesNamespace);

            SageContext context = configuration.Context;
            foreach (MetaViewInfo info in context.ProjectConfiguration.MetaViews.Values)
            {
                XmlElement viewElement = metaElement.AppendElement("mod:view", XmlNamespaces.ModulesNamespace);
                viewElement.SetAttribute("name", info.Name);
                viewElement.InnerText = info.Description;
            }

            result.AppendDataNode(dataElement);
            return result;
        }
Esempio n. 2
0
        public ModuleResult ProcessElement(XmlElement moduleElement, ViewConfiguration configuration)
        {
            SageContext context = configuration.Context;
            Initialize(context);

            if (languages.Count == 0)
            {
                log.ErrorFormat("The syntax highligher module isn't configured with any languages. Until this is fixed, the module will not work.");
                return new ModuleResult(ModuleResultStatus.ConfigurationError);
            }

            XmlNode languageNode = moduleElement.SelectSingleNode("mod:config/mod:language", nm);
            XmlElement codeNode = moduleElement.SelectSingleElement("mod:config/mod:code", nm);
            XmlNodeList keywordGroups = moduleElement.SelectNodes("mod:config/mod:keywords/mod:group", nm);
            XmlElement digitsNode = moduleElement.SelectSingleElement("mod:config/mod:digits", nm);

            if (languageNode == null)
                log.ErrorFormat("The required element mod:language is missing from the module configuration");

            if (codeNode == null)
                log.ErrorFormat("The required element mod:code is missing from the module configuration");

            if (languageNode == null || codeNode == null)
                return new ModuleResult(ModuleResultStatus.MissingParameters);

            string language = languageNode.InnerText.Trim();
            string sourceCode = codeNode.InnerText.Trim();
            string sourcePath = codeNode.GetAttribute("src");

            if (string.IsNullOrWhiteSpace(language))
            {
                log.ErrorFormat("The mod:language is missing the required text value");
                return new ModuleResult(ModuleResultStatus.MissingParameters);
            }

            if (!languages.ContainsKey(language))
            {
                log.ErrorFormat("The specified language '{0}' is not recognized. Valid languages are: '{1}'.",
                    language, string.Join(", ", languages.Keys.ToArray()));

                return new ModuleResult(ModuleResultStatus.MissingParameters);
            }

            if (!string.IsNullOrEmpty(sourcePath) && string.IsNullOrWhiteSpace(sourceCode))
            {
                string expanded = context.Path.Resolve(sourcePath);
                if (!File.Exists(expanded))
                {
                    log.ErrorFormat("The specified source code location '{0}' ('{1}') doesn't exist.",
                        sourcePath, expanded);

                    return new ModuleResult(ModuleResultStatus.NoData);
                }

                sourceCode = File.ReadAllText(expanded);
            }

            string indent = null;
            string[] sourceLines = sourceCode.Split('\n');

            foreach (string line in sourceLines)
            {
                Match m;
                if ((m = indentExpr.Match(line)).Success)
                {
                    if (indent == null || m.Groups[1].Value.Length < indent.Length)
                        indent = m.Groups[1].Value;
                }
            }

            if (!string.IsNullOrEmpty(indent))
            {
                StringBuilder trimmed = new StringBuilder();
                Regex cleanup = new Regex("^" + indent);
                foreach (string line in sourceLines)
                {
                    trimmed.AppendLine(cleanup.Replace(line, string.Empty));
                }

                sourceCode = trimmed.ToString();
            }

            List<ExpressionGroup> additionalGroups = new List<ExpressionGroup>();
            if (keywordGroups.Count != 0)
            {
                additionalGroups = new List<ExpressionGroup>();
                foreach (XmlElement keywordElement in keywordGroups)
                {
                    additionalGroups.Add(new ExpressionGroup(keywordElement, languages[language].CaseSensitive));
                }
            }

            SyntaxHighlighter highlighter = new SyntaxHighlighter(languages[language], additionalGroups);
            if (digitsNode != null)
            {
                int lineCountDigits = 0;
                if (int.TryParse(digitsNode.InnerText.Trim(), out lineCountDigits))
                    highlighter.LineCountDigits = lineCountDigits;
            }

            string highlighted = highlighter.Format(sourceCode);

            ModuleResult result = new ModuleResult(moduleElement);
            XmlElement dataElement = result.AppendDataElement();
            XmlElement sourceElement = dataElement.AppendElement("mod:formatted", XmlNamespaces.ModulesNamespace);
            sourceElement.InnerText = highlighted;

            return result;
        }
Esempio n. 3
0
        internal void AddModuleResult(string moduleKey, ModuleResult result)
        {
            if (!this.ModuleResults.ContainsKey(moduleKey))
                this.AddModuleType(moduleKey);

            this.ModuleResults[moduleKey].Add(result);
        }
Esempio n. 4
0
        public ModuleResult ProcessElement(XmlElement moduleElement, ViewConfiguration configuration)
        {
            SageContext context = configuration.Context;
            this.moduleElement = moduleElement;

            XmlNode configNode = moduleElement.SelectSingleNode("mod:config", XmlNamespaces.Manager);
            if (configNode == null)
            {
                log.ErrorFormat("The {0} element doesn't have the mod:config node. Skipping further work", typeof(DirectoryTreeModule).FullName);
                return new ModuleResult(moduleElement, ModuleResultStatus.MissingParameters);
            }

            XmlNode valueNode;
            if ((valueNode = configNode.SelectSingleNode("mod:path", XmlNamespaces.Manager)) == null)
            {
                log.ErrorFormat("The {0} element doesn't have the mod:config/mod:path node. Skipping further work", typeof(DirectoryTreeModule).FullName);
                return new ModuleResult(moduleElement, ModuleResultStatus.MissingParameters);
            }

            this.path = valueNode.InnerText;
            this.absolutePath = this.GetDirectoryPath(moduleElement, context);

            if (configNode.SelectSingleNode("mod:recursive", XmlNamespaces.Manager) != null)
                this.recursive = true;

            if (configNode.SelectSingleNode("mod:filesOnly", XmlNamespaces.Manager) != null)
                this.filesOnly = true;

            if (configNode.SelectSingleNode("mod:directoriesOnly", XmlNamespaces.Manager) != null)
                this.directoriesOnly = true;

            if (configNode.SelectSingleNode("mod:absolutePaths", XmlNamespaces.Manager) != null)
                this.absolutePaths = true;

            if ((valueNode = configNode.SelectSingleNode("mod:pattern", XmlNamespaces.Manager)) != null)
                this.pattern = valueNode.InnerText.Trim();

            if ((valueNode = configNode.SelectSingleNode("mod:expression", XmlNamespaces.Manager)) != null)
            {
                string text = valueNode.InnerText.Trim();
                if (!string.IsNullOrEmpty(text))
                {
                    try
                    {
                        this.expression = new Regex(text);
                    }
                    catch (Exception ex)
                    {
                        log.WarnFormat("Invalid regular expression text: '{0}': {1}", text, ex.Message);
                    }
                }
            }

            if (!Directory.Exists(this.absolutePath))
            {
                log.WarnFormat("The directory '{0}' (mapped to '{1}') doesn't exist.", this.path, this.absolutePath);
                return new ModuleResult(moduleElement, ModuleResultStatus.NoData);
            }

            ModuleResult result = new ModuleResult(moduleElement);
            result.AppendDataNode(ScanDirectory(absolutePath, context));

            return result;
        }
Esempio n. 5
0
        public ModuleResult ProcessElement(XmlElement moduleElement, ViewConfiguration configuration)
        {
            SageContext context = configuration.Context;
            ModuleResult result = new ModuleResult(moduleElement);

            XmlNode pathNode = moduleElement.SelectSingleNode("mod:config/mod:source/mod:path", nm);
            XmlNode selectionNode = moduleElement.SelectSingleNode("mod:config/mod:source/mod:xpath", nm);
            XmlNodeList namespaceNodes = moduleElement.SelectNodes("mod:config/mod:source/mod:namespaces/mod:namespace", nm);

            string xpath = selectionNode != null ? selectionNode.InnerText : null;

            if (pathNode == null)
            {
                result.Status = ModuleResultStatus.None;
                return result;
            }

            string path = pathNode.InnerText.Trim();
            if (string.IsNullOrEmpty(path))
            {
                log.WarnFormat("The path element shouldn't be empty.");
                result.Status = ModuleResultStatus.ModuleWarning;
                return result;
            }

            path = context.Path.Substitute(path);
            if (!Path.IsPathRooted(path))
            {
                string configDirectory = Path.GetDirectoryName(configuration.Info.ConfigPath);
                path = Path.Combine(configDirectory, path);
            }

            string resolved = context.Path.Resolve(path);
            if (!File.Exists(resolved))
            {
                log.WarnFormat("The specified xml path '{0}' ('{1}') doesn't exist", path, resolved);
                result.Status = ModuleResultStatus.ModuleWarning;
                return result;
            }

            XmlNode selection;
            try
            {
                selection = context.Resources.LoadXml(resolved);
            }
            catch (Exception ex)
            {
                log.ErrorFormat("Failed to add specified file '{0}' ('{1}') as XML document: {2}",
                    path, resolved, ex.Message);

                result.Status = ModuleResultStatus.ModuleError;
                return result;
            }

            if (!string.IsNullOrEmpty(xpath))
            {
                try
                {
                    XmlNamespaceManager manager = new XmlNamespaceManager(new NameTable());
                    manager.AddNamespace("default", XmlNamespaces.XHtmlNamespace);

                    xpath = defaultNamespaceNodes.Replace(xpath, "$1default:$2$3");
                    foreach (XmlElement node in namespaceNodes)
                    {
                        string prefix = node.GetAttribute("prefix");
                        manager.AddNamespace(prefix, node.InnerText.Trim());
                    }

                    selection = selection.SelectSingleNode(xpath, manager);
                    if (selection == null)
                    {
                        log.WarnFormat("The xpath expression '{0}' yielded no result.", xpath);
                        result.Status = ModuleResultStatus.ModuleWarning;
                        return result;
                    }
                }
                catch (Exception ex)
                {
                    log.ErrorFormat("Attempting to select using xpath expression '{0}' resulted in error: {1}.",
                        xpath, ex.Message);

                    result.Status = ModuleResultStatus.ModuleError;
                    return result;
                }
            }
            else if (selectionNode != null)
            {
                log.WarnFormat("The selection element shouldn't be empty.");
                result.Status = ModuleResultStatus.ModuleWarning;
            }

            result.AppendDataElement(selection);
            return result;
        }
Esempio n. 6
0
        public ModuleResult ProcessElement(XmlElement moduleElement, ViewConfiguration configuration)
        {
            XmlNamespaceManager nm = XmlNamespaces.Manager;

            bool isPublic = moduleElement.GetBoolean("mod:config/mod:public", nm);
            string threadName = moduleElement.GetString("mod:config/mod:thread", nm);
            string loggerName = moduleElement.GetString("mod:config/mod:logger", DefaultLoggerName, nm);

            SageContext context = configuration.Context;
            if (!isPublic && !context.IsDeveloperRequest)
            {
                log.Warn("Skipping work for non-developer reqest. To override this set mod:config/mod:public to 'true'.");
                return null;
            }

            if (string.IsNullOrEmpty(threadName))
            {
                log.Warn("Thread name parameter is missing. Set mod:config/mod:thread be a non-empty string.");
                return new ModuleResult(ModuleResultStatus.MissingParameters);
            }

            if (!IsLoggingEnabled)
            {
                log.Warn("Logging is disabled.");
                log.WarnFormat("Make sure to enable log4net logging, and to add an appender with the following configuration:");
                log.WarnFormat(ConfigurationExample, loggerName);
                return new ModuleResult(ModuleResultStatus.NoData);
            }

            FileAppender appender = GetAppender(loggerName) as FileAppender;
            if (appender == null)
            {
                log.WarnFormat("The log appender '{0}' does not exist or is not a FileAppender.", loggerName);
                log.WarnFormat("Either create an appender with that name or specify another using mod:config/mod:logger.");
                log.WarnFormat("Make sure to enable log4net logging, and to add an appender with the following configuration:");
                log.WarnFormat(ConfigurationExample, loggerName);
                return new ModuleResult(ModuleResultStatus.NoData);
            }

            ModuleResult result = new ModuleResult(moduleElement);
            result.AppendDataElement(CreateLogElement(moduleElement.OwnerDocument, appender, threadName));

            return result;
        }