Esempio n. 1
0
        public override void Read(IList <string> dataSources,
                                  SnippetProvider provider)
        {
            if (dataSources == null)
            {
                throw new ArgumentNullException("dataSources",
                                                "The data sources cannot be null (or Nothing).");
            }
            if (provider == null)
            {
                throw new ArgumentNullException("provider",
                                                "The snippet provider cannot be null (or Nothing).");
            }

            int itemCount = dataSources.Count;

            for (int i = 0; i < itemCount; i++)
            {
                string dataSource = dataSources[i];
                if (String.IsNullOrEmpty(dataSource) == false)
                {
                    this.Read(dataSource, provider);
                }
            }
        }
Esempio n. 2
0
        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).");
            }

            _provider = provider;

            // The data source is the root directory of the snippets...
            try
            {
                DirectoryInfo   root  = new DirectoryInfo(dataSource);
                DirectoryInfo[] areas = root.GetDirectories();

                foreach (DirectoryInfo area in areas)
                {
                    DirectoryInfo[] examples = area.GetDirectories();

                    foreach (DirectoryInfo example in examples)
                    {
                        string path;
                        if (_exampleIndex.TryGetValue(example.Name, out path))
                        {
                            this.WriteMessage(MessageLevel.Warn, String.Format(
                                                  "The example '{0}' under folder '{1}' already exists under '{2}'", example.Name, example.FullName, path));
                        }

                        _exampleIndex[example.Name] = example.FullName;

                        this.ParseExample(example);
                    }
                }
            }
            catch (Exception ex)
            {
                this.WriteMessage(MessageLevel.Error,
                                  String.Format("The loading of examples failed: {0}", ex.Message));
                throw;
            }
        }
        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;
        }
Esempio n. 4
0
 public abstract void Read(IList <string> dataSources, SnippetProvider provider);
Esempio n. 5
0
 public abstract void Read(string dataSource, SnippetProvider provider);
Esempio n. 6
0
        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));
            }
        }