public override void Read(string dataSource, SnippetProvider provider) { if (dataSource == null) { throw new ArgumentNullException("dataSource", "The data source cannot be null (or Nothing)."); } if (dataSource.Length == 0) { throw new ArgumentException( "The data source cannot be empty.", "dataSource"); } if (provider == null) { throw new ArgumentNullException("provider", "The snippet provider cannot be null (or Nothing)."); } int tabSize = this.TabSize; SnippetInfo info = null; // just keep the compiler happy... XmlReader xmlReader = null; bool isMemoryProvider = provider.IsMemory; string snippetId = String.Empty; string snippetLang = String.Empty; string snippetText = String.Empty; string snippetGroup = String.Empty; try { this.WriteMessage(MessageLevel.Info, String.Format("Start reading code snippet file '{0}'.", dataSource)); XmlReaderSettings settings = new XmlReaderSettings(); settings.CheckCharacters = false; xmlReader = XmlReader.Create(dataSource, settings); xmlReader.MoveToContent(); string nodeName; XmlNodeType nodeType = XmlNodeType.None; // The root name is not defined, so we just loop to the end... while (xmlReader.EOF == false) { nodeType = xmlReader.NodeType; if (nodeType == XmlNodeType.Element) { nodeName = xmlReader.Name; if (String.Equals(nodeName, "item")) { if (isMemoryProvider) { info = new SnippetInfo(xmlReader.GetAttribute("id")); if (info.IsValid == false) { info = null; } } else { string identifier = xmlReader.GetAttribute("id"); if (String.IsNullOrEmpty(identifier) == false) { int index = identifier.IndexOf('#'); if (index > 0) { snippetGroup = identifier.Substring(0, index); snippetId = identifier.Substring(index + 1); } } } } else if (String.Equals(nodeName, "sampleCode")) { snippetLang = xmlReader.GetAttribute("language"); snippetText = xmlReader.ReadString(); if (String.IsNullOrEmpty(snippetLang) == false && String.IsNullOrEmpty(snippetText) == false) { StringBuilder builder = CodeFormatter.StripLeadingSpaces(snippetText, tabSize); if (isMemoryProvider) { if (info != null) { provider.Register(info, new SnippetItem( snippetLang, builder.ToString())); } } else { provider.Register(snippetGroup, snippetId, snippetLang, builder.ToString()); } } } xmlReader.Read(); } else { xmlReader.Read(); } } xmlReader.Close(); xmlReader = null; this.WriteMessage(MessageLevel.Info, String.Format("Completed the reading code snippet file '{0}'.", dataSource)); } catch (Exception ex) { if (xmlReader != null && xmlReader.ReadState != ReadState.Closed) { xmlReader.Close(); xmlReader = null; } this.WriteMessage(MessageLevel.Error, String.Format( "An exception occurred while reading code snippet file '{0}'. The error message is: {1}", dataSource, ex.Message)); } }
/// <summary> /// Parse the snippet content. /// </summary> /// <param name="text">content to be parsed</param> /// <param name="language">snippet language</param> /// <param name="extension">file extension</param> /// <param name="example">snippet example</param> private void ParseSnippetContent(string text, string language, string extension, string example) { int tabSize = this.TabSize; // parse the text for snippets for (Match match = find.Match(text); match.Success; match = find.Match(text, match.Index + 10)) { string snippetIdentifier = match.Groups["id"].Value; string snippetContent = match.Groups["tx"].Value; snippetContent = clean.Replace(snippetContent, "\n"); // if necessary, clean one more time to catch snippet // comments on consecutive lines if (clean.Match(snippetContent).Success) { snippetContent = clean.Replace(snippetContent, "\n"); } snippetContent = cleanAtStart.Replace(snippetContent, ""); snippetContent = cleanAtEnd.Replace(snippetContent, ""); // get the language/extension from our languages List, which // may contain colorization rules for the language SnippetLanguage snippetLanguage = new SnippetLanguage(language, extension); foreach (SnippetLanguage lang in _languages) { if (!lang.IsMatch(language, extension)) { continue; } snippetLanguage = lang; break; } StringBuilder builder = CodeFormatter.StripLeadingSpaces(snippetContent, tabSize); if (_provider.IsMemory) { SnippetInfo info = new SnippetInfo(example, snippetIdentifier); _provider.Register(info, new SnippetItem( snippetLanguage.LanguageId, builder.ToString())); } else { _provider.Register(example, snippetIdentifier, snippetLanguage.LanguageId, builder.ToString()); } //SnippetIdentifier identifier = new SnippetIdentifier(example, snippetIdentifier); //// BUGBUG: i don't think this ever happens, but if it did we should write an error //if (!IsLegalXmlText(snippetContent)) //{ // // WriteMessage(MessageLevel.Warn, String.Format("Snippet '{0}' language '{1}' contains illegal characters.", identifier.ToString(), snippetLanguage.LanguageId)); // continue; //} //snippetContent = StripLeadingSpaces(snippetContent); //// Add the snippet information to dictionary //Snippet snippet = new Snippet(snippetContent, snippetLanguage); //List<Snippet> values; //if (!this.exampleSnippets.TryGetValue(identifier, out values)) //{ // values = new List<Snippet>(); // this.exampleSnippets.Add(identifier, values); //} //values.Add(snippet); } }