public ModularLanguageDefinition(XmlElement configurationElement) { Contract.Requires<ArgumentNullException>(configurationElement != null); XmlNamespaceManager nm = Sage.XmlNamespaces.Manager; this.Name = configurationElement.GetAttribute("name"); this.CaseSensitive = !configurationElement.GetAttribute("caseSensitive").ContainsAnyOf("false", "no", "0"); XmlElement selection = configurationElement.SelectSingleElement("mod:escape", nm); if (selection != null) this.EscapeChar = selection.InnerText.Trim().Substring(0, 1); selection = configurationElement.SelectSingleElement("mod:regexp", nm); if (selection != null) { var pair = selection.InnerText.Trim().Split(' '); if (pair.Length == 2) { this.RegexStart = pair[0]; this.RegexEnd = pair[1]; } } foreach (XmlElement commentNode in configurationElement.SelectNodes("mod:comments/mod:linecomment", nm)) this.LineCommentDelimiters.Add(commentNode.InnerText.Trim()); foreach (XmlElement commentNode in configurationElement.SelectNodes("mod:comments/mod:comment", nm)) { var pair = commentNode.InnerText.Trim().Split(' '); if (pair.Length == 2) this.CommentDelimiters.Add(new Delimiters(pair[0], pair[1])); } foreach (XmlElement quoteNode in configurationElement.SelectNodes("mod:quotes/mod:quote", nm)) this.QuoteDelimiters.Add(quoteNode.InnerText.Trim()); foreach (XmlElement groupNode in configurationElement.SelectNodes("mod:keywords/mod:group", nm)) this.Expressions.Add(new ExpressionGroup(groupNode, this.CaseSensitive)); }
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; }
private static void InitializeWizardPipeline(XmlElement element) { using (new ProfileSection("Initialize wizard pipeline")) { ProfileSection.Argument("element", element); string name1 = element.Name; try { XmlElement argsElement = element.SelectSingleElement("args"); Type args = argsElement != null ? Type.GetType(argsElement.GetAttribute("type")).IsNotNull( "Cannot find the {0} type".FormatWith(argsElement.GetAttribute("type"))) : null; XmlElement finish = element.SelectSingleElement("finish"); string title = element.GetAttribute("title"); var steps = element.SelectSingleElement("steps").IsNotNull( "Can't find the steps element in the WizardPipelines.config file").ChildNodes.OfType<XmlElement>(). Select( step => new StepInfo(step.GetAttribute("name"), Type.GetType(step.GetAttribute("type")), step.GetAttribute("param"))).ToArray(); string cancelButtonText = element.GetAttribute("cancelButton"); string startButtonText = element.GetAttribute("startButton"); string finishText = element.GetAttribute("finishText"); FinishAction[] finishActions = finish != null ? GetFinishActions(finish, args).ToArray() : null; var finishActionHives = GetFinishActionHives(finish, args); WizardPipeline wizardPipeline = new WizardPipeline(name1, title, steps, args, startButtonText, cancelButtonText, finishText, finishActions, finishActionHives); Definitions.Add(name1, wizardPipeline); ProfileSection.Result("Done"); } catch (Exception ex) { WindowHelper.HandleError("WizardPipelineManager failed to load the {0} pipeline".FormatWith(name1), true, ex); ProfileSection.Result("Failed"); } } }
/// <inheritdoc/> public void Parse(XmlElement element) { this.Name = element.GetAttribute("name"); this.Language = element.GetAttribute("language"); XmlElement formatElement = element.SelectSingleElement("p:format", XmlNamespaces.Manager); try { culture = new CultureInfo(formatElement.GetAttribute("culture")); } catch (ArgumentException) { culture = new CultureInfo(DefaultLocale); } var shortDate = formatElement.GetAttribute("shortDate"); var longDate = formatElement.GetAttribute("longDate"); shortDateFormat = string.IsNullOrEmpty(shortDate) ? "d" : shortDate; longDateFormat = string.IsNullOrEmpty(longDate) ? "D" : longDate; this.DictionaryNames = new List<string>(element.GetAttribute("dictionaryNames") .Replace(" ", string.Empty) .Split(',') .Distinct()); this.ResourceNames = new List<string>(element.GetAttribute("resourceNames") .Replace(" ", string.Empty) .Split(',') .Distinct()); }
private void SynchronizeElements(XmlElement targetElement, XmlElement sourceElement) { foreach (XmlAttribute attr in sourceElement.Attributes) { if (targetElement.Attributes[attr.Name] == null) targetElement.SetAttribute(attr.Name, attr.Value); } foreach (XmlElement sourceChild in sourceElement.SelectNodes("*")) { XmlNamespaceManager nm = new XmlNamespaceManager(new NameTable()); nm.AddNamespace("temp", sourceElement.NamespaceURI); XmlElement targetChild = targetElement.SelectSingleElement(string.Format("temp:{0}", sourceChild.LocalName), nm); if (targetChild == null) { targetChild = targetElement.OwnerDocument.CreateElement(sourceChild.Name, sourceChild.NamespaceURI); targetElement.AppendElement(targetElement.OwnerDocument.ImportNode(sourceChild, true)); } this.SynchronizeElements(targetChild, sourceChild); } }
internal void Parse(XmlElement configNode) { Contract.Requires<ArgumentNullException>(configNode != null); this.ValidationResult = ResourceManager.ValidateElement(configNode, ConfigSchemaPath); if (!this.ValidationResult.Success) return; var nm = XmlNamespaces.Manager; var packageElement = configNode.SelectSingleNode("p:package", nm); this.Type = (ProjectType) Enum.Parse(typeof(ProjectType), configNode.Name, true); if (this.Type == ProjectType.Project && packageElement != null) { this.Type = ProjectType.ExtensionProject; } foreach (XmlElement child in configNode.SelectNodes( string.Format("*[namespace-uri() != '{0}']", XmlNamespaces.ProjectConfigurationNamespace))) { customElements.Add(child); } foreach (XmlElement element in configNode.SelectNodes("p:dependencies/p:extension", nm)) { string extensionName = element.GetAttribute("name"); if (!this.Dependencies.Contains(extensionName)) this.Dependencies.Add(extensionName); } XmlNode variablesNode = configNode.SelectSingleNode("p:variables", nm); if (variablesNode != null) { if (this.VariablesNode != null) { foreach (XmlElement variableNode in variablesNode.SelectNodes("*")) { var importReady = this.VariablesNode.OwnerDocument.ImportNode(variableNode, true); var id = variableNode.GetAttribute("id"); var current = this.VariablesNode.SelectSingleNode( string.Format("intl:variable[@id='{0}']", id), nm); if (current != null) this.VariablesNode.ReplaceChild(importReady, current); else this.VariablesNode.AppendChild(importReady); } } else { this.VariablesNode = variablesNode; } this.Variables = new Dictionary<string, NameValueCollection>(); var variables = variablesNode.SelectNodes("intl:variable", nm); foreach (XmlElement elem in variables) { var values = elem.SelectNodes("intl:value", nm); var valueDictionary = new NameValueCollection(); if (values.Count == 0) { valueDictionary["default"] = elem.InnerText; } else { foreach (XmlElement val in values) valueDictionary.Add(val.GetAttribute("locale"), val.InnerText.Trim()); } this.Variables[elem.GetAttribute("id")] = valueDictionary; } } XmlElement routingNode = configNode.SelectSingleElement("p:routing", nm); XmlElement pathsNode = configNode.SelectSingleElement("p:paths", nm); string nodeValue = configNode.GetAttribute("name"); if (!string.IsNullOrEmpty(nodeValue)) this.Name = nodeValue; nodeValue = configNode.GetAttribute("sharedCategory"); if (!string.IsNullOrEmpty(nodeValue)) this.SharedCategory = nodeValue; nodeValue = configNode.GetAttribute("defaultLocale"); if (!string.IsNullOrEmpty(nodeValue)) this.DefaultLocale = nodeValue; nodeValue = configNode.GetAttribute("defaultCategory"); if (!string.IsNullOrEmpty(nodeValue)) this.DefaultCategory = nodeValue; nodeValue = configNode.GetAttribute("autoInternationalize"); if (!string.IsNullOrEmpty(nodeValue)) this.AutoInternationalize = nodeValue.ContainsAnyOf("yes", "1", "true"); nodeValue = configNode.GetAttribute("debugMode"); if (!string.IsNullOrEmpty(nodeValue)) this.IsDebugEnabled = nodeValue.ContainsAnyOf("yes", "1", "true"); if (pathsNode != null) { this.PathTemplates.Parse(pathsNode); XmlNode node = pathsNode.SelectSingleNode("p:AssetPath", nm); if (node != null) { nodeValue = node.InnerText; if (!string.IsNullOrEmpty(nodeValue)) this.AssetPath = nodeValue.TrimEnd('/') + "/"; } } if (routingNode != null) this.Routing.Parse(routingNode); XmlElement linkingNode = configNode.SelectSingleElement("p:linking", nm); if (linkingNode != null) this.Linking.Parse(linkingNode); XmlElement environmentNode = configNode.SelectSingleElement("p:environment", nm); if (environmentNode != null) this.Environment.Parse(environmentNode); XmlElement cachingNode = configNode.SelectSingleElement("p:viewcaching", nm); if (cachingNode != null) this.ViewCaching.Parse(cachingNode); foreach (XmlElement libraryNode in configNode.SelectNodes("p:libraries/p:library", nm)) { ResourceLibraryInfo info = new ResourceLibraryInfo(libraryNode, this.Id); this.ResourceLibraries.Add(info.Name, info); } foreach (XmlElement moduleNode in configNode.SelectNodes("p:modules/p:module", nm)) { ModuleConfiguration moduleConfig = new ModuleConfiguration(moduleNode, this.Id); var moduleKey = string.IsNullOrWhiteSpace(moduleConfig.Category) ? moduleConfig.Name : moduleConfig.Category + "/" + moduleConfig.Name; this.Modules.Add(moduleKey, moduleConfig); } var localeNodes = configNode.SelectNodes("p:internationalization/p:locale", nm); if (localeNodes.Count != 0) { this.Locales = new Dictionary<string, LocaleInfo>(); this.Categories[this.DefaultCategory].Locales.Clear(); foreach (XmlElement locale in localeNodes) { var name = locale.GetAttribute("name"); var info = new LocaleInfo(locale); this.Locales[name] = info; this.Categories[this.DefaultCategory].Locales.Add(name); } } if (this.Locales.Count != 0 && !this.Locales.ContainsKey(this.DefaultLocale)) { string firstLocale = this.Locales.First().Key; log.ErrorFormat("The default locale '{0}' doesn't exist, resetting it to '{1}'.", this.DefaultLocale, firstLocale); this.DefaultLocale = firstLocale; } var categoryNodes = configNode.SelectNodes("p:categories/p:category", nm); if (categoryNodes.Count != 0) { this.Categories.Clear(); foreach (XmlElement categoryElement in categoryNodes) { var category = new CategoryInfo(categoryElement); this.Categories[category.Name] = category; var undefinedLocales = category.Locales.Where(name => !this.Locales.ContainsKey(name)).ToList(); if (undefinedLocales.Count != 0) { log.ErrorFormat("Category '{0}' is configured to use these unsupported locales: {1}.", category.Name, string.Join(",", undefinedLocales)); } } } if (this.Categories.Count != 0 && !this.Categories.ContainsKey(this.DefaultCategory)) { string firstCategory = this.Categories.First().Key; log.ErrorFormat("The default category '{0}' doesn't exist, resetting it to '{1}'.", this.DefaultCategory, firstCategory); this.DefaultCategory = firstCategory; } foreach (XmlElement viewNode in configNode.SelectNodes("p:metaViews/p:metaView", nm)) { var name = viewNode.GetAttribute("name"); var info = new MetaViewInfo(viewNode); this.MetaViews[name] = info; } foreach (XmlElement viewNode in configNode.SelectNodes("p:errorViews/p:view", nm)) { var error = viewNode.GetAttribute("error"); var info = new ErrorViewInfo(viewNode); this.ErrorViews[error] = info; } this.Package = new PackageConfiguration(packageElement); if (this.Changed != null) this.Changed(this, EventArgs.Empty); }