public override void Register(SnippetInfo info, SnippetItem item)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info",
                                                "The snippet information cannot be null (or Nothing).");
            }
            if (item == null)
            {
                throw new ArgumentNullException("item",
                                                "The snippet item cannot be null (or Nothing).");
            }
            if (_dicSnippets == null)
            {
                _dicSnippets = new Dictionary <SnippetInfo, IList <SnippetItem> >();
            }

            IList <SnippetItem> listSnippets;

            if (_dicSnippets.TryGetValue(info, out listSnippets) == false)
            {
                listSnippets = new List <SnippetItem>();
                _dicSnippets.Add(info, listSnippets);
            }
            listSnippets.Add(item);
        }
        public override IList <SnippetItem> this[SnippetInfo info]
        {
            get
            {
                if (info == null)
                {
                    throw new ArgumentNullException("info",
                                                    "The snippet information cannot be null (or Nothing).");
                }
                if (_itemCount <= 0 || _databaseInstance == null ||
                    _databaseSession == null || _databaseTable == null)
                {
                    return(null);
                }

                List <SnippetItem> listInfo = null;

                string exampleId = info.ExampleId;
                string snippetId = info.SnippetId;

                // We are about to set up an index range on the name index.
                Api.JetSetCurrentIndex(_databaseSession, _databaseTable, "identifier");

                Api.MakeKey(_databaseSession, _databaseTable, exampleId,
                            Encoding.Unicode, MakeKeyGrbit.NewKey);
                Api.MakeKey(_databaseSession, _databaseTable, snippetId,
                            Encoding.Unicode, MakeKeyGrbit.None);
                if (Api.TrySeek(_databaseSession, _databaseTable, SeekGrbit.SeekGE))
                {
                    Api.MakeKey(_databaseSession, _databaseTable, exampleId,
                                Encoding.Unicode, MakeKeyGrbit.NewKey);
                    Api.MakeKey(_databaseSession, _databaseTable, snippetId,
                                Encoding.Unicode, MakeKeyGrbit.None);
                    if (Api.TrySetIndexRange(_databaseSession, _databaseTable,
                                             SetIndexRangeGrbit.RangeUpperLimit | SetIndexRangeGrbit.RangeInclusive))
                    {
                        listInfo = new List <SnippetItem>();
                        do
                        {
                            string snippetLang = Api.RetrieveColumnAsString(
                                _databaseSession, _databaseTable, _columnLang);
                            string snippetText = Api.RetrieveColumnAsString(
                                _databaseSession, _databaseTable, _columnText);
                            listInfo.Add(new SnippetItem(snippetLang, snippetText));
                        }while (Api.TryMoveNext(_databaseSession, _databaseTable));
                    }
                }

                return(listInfo);
            }
        }
        public override void Register(SnippetInfo info, SnippetItem item)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info",
                                                "The snippet information cannot be null (or Nothing).");
            }
            if (item == null)
            {
                throw new ArgumentNullException("item",
                                                "The snippet item cannot be null (or Nothing).");
            }

            this.Register(info.ExampleId, info.SnippetId,
                          item.Language, item.Text);
        }
        public override IList <SnippetItem> this[SnippetInfo info]
        {
            get
            {
                if (info == null)
                {
                    throw new ArgumentNullException("info",
                                                    "The snippet information cannot be null (or Nothing).");
                }
                IList <SnippetItem> listSnippets;
                if (_dicSnippets != null &&
                    _dicSnippets.TryGetValue(info, out listSnippets))
                {
                    return(listSnippets);
                }

                return(null);
            }
        }
Esempio n. 5
0
        public static SnippetInfo[] ParseReference(string reference)
        {
            if (reference == null)
            {
                return(null);
            }
            // Let's try to help careless mistakes...
            reference = reference.Trim().Replace(" ", String.Empty);
            int    index     = reference.IndexOf('#');
            string exampleId = reference.Substring(0, index);

            string[] snippetIds = reference.Substring(index + 1).Split(
                new char[] { ',' });
            int itemCount = snippetIds.Length;

            SnippetInfo[] arrayInfo = new SnippetInfo[itemCount];

            for (int i = 0; i < itemCount; i++)
            {
                arrayInfo[i] = new SnippetInfo(exampleId, snippetIds[i]);
            }

            return(arrayInfo);
        }
Esempio n. 6
0
 public abstract void Register(SnippetInfo Info, SnippetItem item);
Esempio n. 7
0
 public abstract IList <SnippetItem> this[SnippetInfo info]
 {
     get;
 }
Esempio n. 8
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));
            }
        }
Esempio n. 9
0
        /// <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);
            }
        }