コード例 #1
0
        /// <summary>
        /// Sets the params.
        /// Analysis component factory names may optionally include the "Factory" suffix.
        /// </summary>
        /// <param name="params">
        /// analysis pipeline specification: name, (optional) positionIncrementGap,
        /// (optional) offsetGap, 0+ CharFilterFactory's, 1 TokenizerFactory,
        /// and 0+ TokenFilterFactory's
        /// </param>
        public override void SetParams(string @params)
        {
            base.SetParams(@params);
            ArgType expectedArgType = ArgType.ANALYZER_ARG;

            StreamTokenizer stok = new StreamTokenizer(new StringReader(@params));

            stok.CommentChar('#');
            stok.QuoteChar('"');
            stok.QuoteChar('\'');
            stok.IsEOLSignificant = false;
            stok.OrdinaryChar('(');
            stok.OrdinaryChar(')');
            stok.OrdinaryChar(':');
            stok.OrdinaryChar(',');
            try
            {
                while (stok.NextToken() != StreamTokenizer.TT_EOF)
                {
                    switch (stok.TokenType)
                    {
                    case ',':
                    {
                        // Do nothing
                        break;
                    }

                    case StreamTokenizer.TT_WORD:
                    {
                        if (expectedArgType.Equals(ArgType.ANALYZER_ARG))
                        {
                            string argName = stok.StringValue;
                            if (!argName.Equals("name", StringComparison.OrdinalIgnoreCase) &&
                                !argName.Equals("positionIncrementGap", StringComparison.OrdinalIgnoreCase) &&
                                !argName.Equals("offsetGap", StringComparison.OrdinalIgnoreCase))
                            {
                                throw new Exception
                                          ("Line #" + GetLineNumber(stok) + ": Missing 'name' param to AnalyzerFactory: '" + @params + "'");
                            }
                            stok.NextToken();
                            if (stok.TokenType != ':')
                            {
                                throw new Exception
                                          ("Line #" + GetLineNumber(stok) + ": Missing ':' after '" + argName + "' param to AnalyzerFactory");
                            }

                            stok.NextToken();
                            string argValue = stok.StringValue;
                            switch (stok.TokenType)
                            {
                            case StreamTokenizer.TT_NUMBER:
                            {
                                argValue = stok.NumberValue.ToString(CultureInfo.InvariantCulture);
                                // Drop the ".0" from numbers, for integer arguments
                                argValue = TRAILING_DOT_ZERO_PATTERN.Replace(argValue, "", 1);
                                // Intentional fallthrough

                                if (argName.Equals("name", StringComparison.OrdinalIgnoreCase))
                                {
                                    factoryName     = argValue;
                                    expectedArgType = ArgType.ANALYZER_ARG_OR_CHARFILTER_OR_TOKENIZER;
                                }
                                else
                                {
                                    int intArgValue = 0;
                                    try
                                    {
                                        intArgValue = int.Parse(argValue, CultureInfo.InvariantCulture);
                                    }
                                    catch (FormatException e)
                                    {
                                        throw new Exception
                                                  ("Line #" + GetLineNumber(stok) + ": Exception parsing " + argName + " value '" + argValue + "'", e);
                                    }
                                    if (argName.Equals("positionIncrementGap", StringComparison.OrdinalIgnoreCase))
                                    {
                                        positionIncrementGap = intArgValue;
                                    }
                                    else if (argName.Equals("offsetGap", StringComparison.OrdinalIgnoreCase))
                                    {
                                        offsetGap = intArgValue;
                                    }
                                }
                                break;
                            }

                            case '"':
                            case '\'':
                            case StreamTokenizer.TT_WORD:
                            {
                                if (argName.Equals("name", StringComparison.OrdinalIgnoreCase))
                                {
                                    factoryName     = argValue;
                                    expectedArgType = ArgType.ANALYZER_ARG_OR_CHARFILTER_OR_TOKENIZER;
                                }
                                else
                                {
                                    int intArgValue = 0;
                                    try
                                    {
                                        intArgValue = int.Parse(argValue, CultureInfo.InvariantCulture);
                                    }
                                    catch (FormatException e)
                                    {
                                        throw new Exception
                                                  ("Line #" + GetLineNumber(stok) + ": Exception parsing " + argName + " value '" + argValue + "'", e);
                                    }
                                    if (argName.Equals("positionIncrementGap", StringComparison.OrdinalIgnoreCase))
                                    {
                                        positionIncrementGap = intArgValue;
                                    }
                                    else if (argName.Equals("offsetGap", StringComparison.OrdinalIgnoreCase))
                                    {
                                        offsetGap = intArgValue;
                                    }
                                }
                                break;
                            }

                            case StreamTokenizer.TT_EOF:
                            {
                                throw new Exception("Unexpected EOF: " + stok.ToString());
                            }

                            default:
                            {
                                throw new Exception
                                          ("Line #" + GetLineNumber(stok) + ": Unexpected token: " + stok.ToString());
                            }
                            }
                        }
                        else if (expectedArgType.Equals(ArgType.ANALYZER_ARG_OR_CHARFILTER_OR_TOKENIZER))
                        {
                            string argName = stok.StringValue;

                            if (argName.Equals("positionIncrementGap", StringComparison.OrdinalIgnoreCase) ||
                                argName.Equals("offsetGap", StringComparison.OrdinalIgnoreCase))
                            {
                                stok.NextToken();
                                if (stok.TokenType != ':')
                                {
                                    throw new Exception
                                              ("Line #" + GetLineNumber(stok) + ": Missing ':' after '" + argName + "' param to AnalyzerFactory");
                                }
                                stok.NextToken();
                                int intArgValue = (int)stok.NumberValue;
                                switch (stok.TokenType)
                                {
                                case '"':
                                case '\'':
                                case StreamTokenizer.TT_WORD:
                                {
                                    intArgValue = 0;
                                    try
                                    {
                                        intArgValue = int.Parse(stok.StringValue.Trim(), CultureInfo.InvariantCulture);
                                    }
                                    catch (FormatException e)
                                    {
                                        throw new Exception
                                                  ("Line #" + GetLineNumber(stok) + ": Exception parsing " + argName + " value '" + stok.StringValue + "'", e);
                                    }
                                    // Intentional fall-through

                                    if (argName.Equals("positionIncrementGap", StringComparison.OrdinalIgnoreCase))
                                    {
                                        positionIncrementGap = intArgValue;
                                    }
                                    else if (argName.Equals("offsetGap", StringComparison.OrdinalIgnoreCase))
                                    {
                                        offsetGap = intArgValue;
                                    }
                                    break;
                                }

                                case StreamTokenizer.TT_NUMBER:
                                {
                                    if (argName.Equals("positionIncrementGap", StringComparison.OrdinalIgnoreCase))
                                    {
                                        positionIncrementGap = intArgValue;
                                    }
                                    else if (argName.Equals("offsetGap", StringComparison.OrdinalIgnoreCase))
                                    {
                                        offsetGap = intArgValue;
                                    }
                                    break;
                                }

                                case StreamTokenizer.TT_EOF:
                                {
                                    throw new Exception("Unexpected EOF: " + stok.ToString());
                                }

                                default:
                                {
                                    throw new Exception
                                              ("Line #" + GetLineNumber(stok) + ": Unexpected token: " + stok.ToString());
                                }
                                }
                                break;
                            }
                            try
                            {
                                Type clazz;
                                clazz = LookupAnalysisClass(argName, typeof(CharFilterFactory));
                                CreateAnalysisPipelineComponent(stok, clazz);
                            }
                            catch (ArgumentException /*e*/)
                            {
                                try
                                {
                                    Type clazz;
                                    clazz = LookupAnalysisClass(argName, typeof(TokenizerFactory));
                                    CreateAnalysisPipelineComponent(stok, clazz);
                                    expectedArgType = ArgType.TOKENFILTER;
                                }
                                catch (ArgumentException e2)
                                {
                                    throw new Exception("Line #" + GetLineNumber(stok) + ": Can't find class '"
                                                        + argName + "' as CharFilterFactory or TokenizerFactory", e2);
                                }
                            }
                        }
                        else
                        {         // expectedArgType = ArgType.TOKENFILTER
                            string className = stok.StringValue;
                            Type   clazz;
                            try
                            {
                                clazz = LookupAnalysisClass(className, typeof(TokenFilterFactory));
                            }
                            catch (ArgumentException e)
                            {
                                throw new Exception
                                          ("Line #" + GetLineNumber(stok) + ": Can't find class '" + className + "' as TokenFilterFactory", e);
                            }
                            CreateAnalysisPipelineComponent(stok, clazz);
                        }
                        break;
                    }

                    default:
                    {
                        throw new Exception("Line #" + GetLineNumber(stok) + ": Unexpected token: " + stok.ToString());
                    }
                    }
                }
            }
            catch (Exception e)
            {
                if (e.Message.StartsWith("Line #", StringComparison.Ordinal))
                {
                    throw e;
                }
                else
                {
                    throw new Exception("Line #" + GetLineNumber(stok) + ": ", e);
                }
            }

            AnalyzerFactory analyzerFactory = new AnalyzerFactory
                                                  (charFilterFactories, tokenizerFactory, tokenFilterFactories);

            analyzerFactory.SetPositionIncrementGap(positionIncrementGap.GetValueOrDefault());
            analyzerFactory.SetOffsetGap(offsetGap.GetValueOrDefault());
            RunData.AnalyzerFactories[factoryName] = analyzerFactory;
        }