Internal class to enable reuse of the string reader by Analyzer#tokenStream(String,String)
Наследование: System.IO.TextReader
Пример #1
0
        /// <summary>
        /// Returns a <see cref="Analysis.TokenStream"/> suitable for <paramref name="fieldName"/>, tokenizing
        /// the contents of <paramref name="text"/>.
        /// <para/>
        /// This method uses <see cref="CreateComponents(string, TextReader)"/> to obtain an
        /// instance of <see cref="TokenStreamComponents"/>. It returns the sink of the
        /// components and stores the components internally. Subsequent calls to this
        /// method will reuse the previously stored components after resetting them
        /// through <see cref="TokenStreamComponents.SetReader(TextReader)"/>.
        /// <para/>
        /// <b>NOTE:</b> After calling this method, the consumer must follow the
        /// workflow described in <see cref="Analysis.TokenStream"/> to properly consume its contents.
        /// See the <see cref="Lucene.Net.Analysis"/> namespace documentation for
        /// some examples demonstrating this.
        /// </summary>
        /// <param name="fieldName">the name of the field the created <see cref="Analysis.TokenStream"/> is used for</param>
        /// <param name="text">the <see cref="string"/> the streams source reads from </param>
        /// <returns><see cref="Analysis.TokenStream"/> for iterating the analyzed content of <c>reader</c></returns>
        /// <exception cref="ObjectDisposedException"> if the Analyzer is disposed. </exception>
        /// <exception cref="IOException"> if an i/o error occurs (may rarely happen for strings). </exception>
        /// <seealso cref="GetTokenStream(string, TextReader)"/>
        public TokenStream GetTokenStream(string fieldName, string text)
        {
            TokenStreamComponents components = reuseStrategy.GetReusableComponents(this, fieldName);
            ReusableStringReader  strReader  =
                (components == null || components.reusableStringReader == null)
                    ? new ReusableStringReader()
                    : components.reusableStringReader;

            strReader.SetValue(text);
            var r = InitReader(fieldName, strReader);

            if (components == null)
            {
                components = CreateComponents(fieldName, r);
                reuseStrategy.SetReusableComponents(this, fieldName, components);
            }
            else
            {
                components.SetReader(r);
            }
            components.reusableStringReader = strReader;
            return(components.TokenStream);
        }
        public virtual void Test()
        {
            ReusableStringReader reader = new ReusableStringReader();
            Assert.AreEqual(-1, reader.Read());
            Assert.AreEqual(-1, reader.Read(new char[1], 0, 1));
            Assert.AreEqual(-1, reader.Read(new char[2], 1, 1));
            //Assert.AreEqual(-1, reader.Read(CharBuffer.wrap(new char[2])));

            reader.Value = "foobar";
            char[] buf = new char[4];
            Assert.AreEqual(4, reader.Read(buf, 0, 4));
            Assert.AreEqual("foob", new string(buf));
            Assert.AreEqual(2, reader.Read(buf, 0, 2));
            Assert.AreEqual("ar", new string(buf, 0, 2));
            Assert.AreEqual(-1, reader.Read(buf, 2, 0));
            reader.Close();

            reader.Value = "foobar";
            Assert.AreEqual(0, reader.Read(buf, 1, 0));
            Assert.AreEqual(3, reader.Read(buf, 1, 3));
            Assert.AreEqual("foo", new string(buf, 1, 3));
            Assert.AreEqual(2, reader.Read(buf, 2, 2));
            Assert.AreEqual("ba", new string(buf, 2, 2));
            Assert.AreEqual('r', (char)reader.Read());
            Assert.AreEqual(-1, reader.Read(buf, 2, 0));
            reader.Close();

            reader.Value = "foobar";
            StringBuilder sb = new StringBuilder();
            int ch;
            while ((ch = reader.Read()) != -1)
            {
                sb.Append((char)ch);
            }
            reader.Close();
            Assert.AreEqual("foobar", sb.ToString());
        }