Exemplo n.º 1
0
        //=====================================================================

        /// <summary>
        /// Snippet loading logic
        /// </summary>
        /// <param name="file">The file from which to load the snippets</param>
        private void LoadContent(string file)
        {
            SnippetIdentifier key = new SnippetIdentifier();
            string            language;

            WriteMessage(MessageLevel.Info, "Loading code snippet file '{0}'.", file);

            try
            {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.CheckCharacters = false;
                XmlReader reader = XmlReader.Create(file, settings);

                try
                {
                    reader.MoveToContent();
                    while (!reader.EOF)
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.Name == "item")
                        {
                            key = new SnippetIdentifier(reader.GetAttribute("id"));
                            reader.Read();
                        }
                        else if (reader.NodeType == XmlNodeType.Element && reader.Name == "sampleCode")
                        {
                            language = reader.GetAttribute("language");

                            string content = reader.ReadString();

                            // If the element is empty, ReadString does not advance the reader, so we must do it
                            // manually.
                            if (String.IsNullOrEmpty(content))
                            {
                                reader.Read();
                            }

                            if (!content.IsLegalXmlText())
                            {
                                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture,
                                                                                  "Snippet '{0}' language '{1}' contains illegal characters.", key, language));
                            }

                            content = StripLeadingSpaces(content);

                            StoredSnippet        snippet = new StoredSnippet(content, language);
                            List <StoredSnippet> values;

                            if (!snippets.TryGetValue(key, out values))
                            {
                                values = new List <StoredSnippet>();
                                snippets.Add(key, values);
                            }

                            values.Add(snippet);
                        }
                        else
                        {
                            reader.Read();
                        }
                    }
                }
                catch (XmlException e)
                {
                    WriteMessage(MessageLevel.Warn, "The contents of the snippet file '{0}' are not " +
                                 "well-formed XML. The error message is: {1}. Some snippets may be lost.", file, e.Message);
                }
                finally
                {
                    reader.Close();
                }
            }
            catch (IOException e)
            {
                WriteMessage(MessageLevel.Error, "An access error occurred while attempting to read the " +
                             "snippet file '{0}'. The error message is: {1}", file, e.Message);
            }
        }
Exemplo n.º 2
0
		// snippet loading logic

		private void LoadContent(string file) {

			SnippetIdentifier key = new SnippetIdentifier();
			string language;

			WriteMessage(MessageLevel.Info, String.Format("Loading code snippet file '{0}'.", file));
            try {
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.CheckCharacters = false;
                XmlReader reader = XmlReader.Create(file, settings);

                try {
                    reader.MoveToContent();
                    while (!reader.EOF) {
                        if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "item")) {
                            key = new SnippetIdentifier(reader.GetAttribute("id"));
                            reader.Read();
                        } else if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "sampleCode")) {
                            language = reader.GetAttribute("language");

                            string content = reader.ReadString();

                            // If the element is empty, ReadString does not advance the reader, so we must do it manually
                            if (String.IsNullOrEmpty(content)) reader.Read();

                            if (!IsLegalXmlText(content)) {
                                Console.WriteLine("Snippet '{0}' language '{1}' contains illegal characters.", key, language);
                                throw new InvalidOperationException();
                            }

                            content = StripLeadingSpaces(content);

                            StoredSnippet snippet = new StoredSnippet(content, language);
                            List<StoredSnippet> values;
                            if (!snippets.TryGetValue(key, out values)) {
                                values = new List<StoredSnippet>();
                                snippets.Add(key, values);
                            }
                            values.Add(snippet);
                        } else {
                            reader.Read();
                        }
                    }
                } catch (XmlException e) {
                    WriteMessage(MessageLevel.Warn, String.Format("The contents of the snippet file '{0}' are not well-formed XML. The error message is: {1}. Some snippets may be lost.", file, e.Message));
                } finally {
			    	reader.Close();
			    }

            } catch (IOException e) {
                WriteMessage(MessageLevel.Error, String.Format("An access error occured while attempting to read the snippet file '{0}'. The error message is: {1}", file, e.Message));
            }

		}