//创建一个默认的语法醒目显示对象. void CreateDefaultHighlightingStrategy() { DefaultHighlightingStrategy defaultHighlightingStrategy = new DefaultHighlightingStrategy(); defaultHighlightingStrategy.Extensions = new string[] {}; defaultHighlightingStrategy.RuleSets.Add(new HighlightRuleSet()); highlightingDefs["Default"] = defaultHighlightingStrategy; }
public void AddSyntaxModeProvider(SyntaxModeProvider syntaxModeProvider) { foreach (SyntaxMode syntaxMode in syntaxModeProvider.SyntaxModes) { DefaultHighlightingStrategy highlightingStrategy = ParseSyntaxMode(syntaxMode, syntaxModeProvider.GetSyntaxModeXmlTextReader(syntaxMode)); highlightingStrategy.ResolveReferences(); highlightingDefs[syntaxMode.Name] = highlightingStrategy; foreach (string extension in syntaxMode.Extensions) { extensionsToName[extension.ToUpper()] = syntaxMode.Name; //对某一类文件都使用这个语法模式. } } if (!syntaxModeProviders.Contains(syntaxModeProvider)) { syntaxModeProviders.Add(syntaxModeProvider); //注意:这里添加对象. } }
//此函数接收一个syntaxMode对象和一个XmlTextReader对象为参数,分析此xmlTextReader对象所对应的语法模式文件, //并返回一个高亮度显示策略对象(DefaultHighlightingStrategy). DefaultHighlightingStrategy ParseSyntaxMode(SyntaxMode syntaxMode, XmlTextReader xmlTextReader) { try { //XmlValidatingReader validatingReader = new XmlValidatingReader(xmlTextReader); //Stream shemaStream = Assembly.GetCallingAssembly().GetManifestResourceStream("Mode.xsd"); //validatingReader.Schemas.Add("", new XmlTextReader(shemaStream)); //validatingReader.ValidationType = ValidationType.Schema; //validatingReader.ValidationEventHandler += new ValidationEventHandler (ValidationHandler); //以上定义了一个XmlValidatingReader对象,用于验证语法模式文件.(如CSharp-Mode.xshd) XmlDocument doc = new XmlDocument(); doc.Load(xmlTextReader); DefaultHighlightingStrategy highlighter = new DefaultHighlightingStrategy(doc.DocumentElement.Attributes["name"].InnerText); if (doc.DocumentElement.Attributes["extensions"] != null) { highlighter.Extensions = doc.DocumentElement.Attributes["extensions"].InnerText.Split(new char[] { ';', '|' }); } // parse environment color XmlElement environment = doc.DocumentElement["Environment"]; if (environment != null) { foreach (XmlNode node in environment.ChildNodes) { if (node is XmlElement) { XmlElement el = (XmlElement)node; highlighter.SetColorForEnvironment(el.Name, new HighlightColor(el)); } } } // parse properties,not all xshd files have if (doc.DocumentElement["Properties"] != null) { foreach (XmlElement propertyElement in doc.DocumentElement["Properties"].ChildNodes) { highlighter.Properties[propertyElement.Attributes["name"].InnerText] = propertyElement.Attributes["value"].InnerText; } } // parse digits if (doc.DocumentElement["Digits"] != null) { highlighter.DigitColor = new HighlightColor(doc.DocumentElement["Digits"]); } XmlNodeList nodes = doc.DocumentElement.GetElementsByTagName("RuleSet"); foreach (XmlElement element in nodes) { highlighter.AddRuleSet(new HighlightRuleSet(element)); } xmlTextReader.Close(); if (errors != null) { ReportErrors(syntaxMode.FileName); errors = null; return(null); } else { return(highlighter); } } catch (Exception e) { MessageBox.Show("不能加载语法模式文件.\n" + e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(null); } }
//此函数接收一个syntaxMode对象和一个XmlTextReader对象为参数,分析此xmlTextReader对象所对应的语法模式文件, //并返回一个高亮度显示策略对象(DefaultHighlightingStrategy). DefaultHighlightingStrategy ParseSyntaxMode(SyntaxMode syntaxMode, XmlTextReader xmlTextReader) { try { //XmlValidatingReader validatingReader = new XmlValidatingReader(xmlTextReader); //Stream shemaStream = Assembly.GetCallingAssembly().GetManifestResourceStream("Mode.xsd"); //validatingReader.Schemas.Add("", new XmlTextReader(shemaStream)); //validatingReader.ValidationType = ValidationType.Schema; //validatingReader.ValidationEventHandler += new ValidationEventHandler (ValidationHandler); //以上定义了一个XmlValidatingReader对象,用于验证语法模式文件.(如CSharp-Mode.xshd) XmlDocument doc = new XmlDocument(); doc.Load(xmlTextReader); DefaultHighlightingStrategy highlighter = new DefaultHighlightingStrategy(doc.DocumentElement.Attributes["name"].InnerText); if (doc.DocumentElement.Attributes["extensions"]!= null) { highlighter.Extensions = doc.DocumentElement.Attributes["extensions"].InnerText.Split(new char[] { ';', '|' }); } // parse environment color XmlElement environment = doc.DocumentElement["Environment"]; if (environment != null) { foreach (XmlNode node in environment.ChildNodes) { if (node is XmlElement) { XmlElement el = (XmlElement)node; highlighter.SetColorForEnvironment(el.Name, new HighlightColor(el)); } } } // parse properties,not all xshd files have if (doc.DocumentElement["Properties"]!= null) { foreach (XmlElement propertyElement in doc.DocumentElement["Properties"].ChildNodes) { highlighter.Properties[propertyElement.Attributes["name"].InnerText] = propertyElement.Attributes["value"].InnerText; } } // parse digits if (doc.DocumentElement["Digits"]!= null) { highlighter.DigitColor = new HighlightColor(doc.DocumentElement["Digits"]); } XmlNodeList nodes = doc.DocumentElement.GetElementsByTagName("RuleSet"); foreach (XmlElement element in nodes) { highlighter.AddRuleSet(new HighlightRuleSet(element)); } xmlTextReader.Close(); if(errors!=null) { ReportErrors(syntaxMode.FileName); errors = null; return null; } else { return highlighter; } } catch (Exception e) { MessageBox.Show("不能加载语法模式文件.\n" + e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return null; } }