Пример #1
0
        private FieldExtractor GetAnnotationExtractCombo(PropertyInfo field)
        {
            FieldExtractor fieldExtractor = null;
            ComboExtract   comboExtract   = field.GetCustomAttribute <ComboExtract>();

            if (comboExtract != null)
            {
                ExtractBy[] extractBies = comboExtract.Value;
                ISelector   selector;
                switch (comboExtract.Op)
                {
                case ComboExtract.ExtractOp.And:
                    selector = new AndSelector(ExtractorUtils.GetSelectors(extractBies));
                    break;

                case ComboExtract.ExtractOp.Or:
                    selector = new OrSelector(ExtractorUtils.GetSelectors(extractBies));
                    break;

                default:
                    selector = new AndSelector(ExtractorUtils.GetSelectors(extractBies));
                    break;
                }
                fieldExtractor = new FieldExtractor(field, selector, comboExtract.Source,
                                                    comboExtract.NotNull, comboExtract.Multi || field.PropertyType.IsGenericType, null);
            }

            return(fieldExtractor);
        }
Пример #2
0
        private void Init()
        {
            InitTypeExtractors();
            _fieldExtractors = new List <FieldExtractor>();
            foreach (PropertyInfo field in _modelType.GetProperties())
            {
                FieldExtractor fieldExtractor    = GetAnnotationExtractBy(field);
                FieldExtractor fieldExtractorTmp = GetAnnotationExtractCombo(field);

                if (fieldExtractor == null && fieldExtractorTmp != null)
                {
                    fieldExtractor = fieldExtractorTmp;
                }
                fieldExtractorTmp = GetAnnotationExtractByUrl(field);
                if (fieldExtractor == null && fieldExtractorTmp != null)
                {
                    fieldExtractor = fieldExtractorTmp;
                }
                if (fieldExtractor != null)
                {
                    CheckFormat(field, fieldExtractor);
                    _fieldExtractors.Add(fieldExtractor);
                }
            }
        }
Пример #3
0
        private FieldExtractor GetAnnotationExtractBy(PropertyInfo field)
        {
            FieldExtractor fieldExtractor = null;
            ExtractBy      extractBy      = (ExtractBy)field.GetCustomAttribute(typeof(ExtractBy));

            if (extractBy != null)
            {
                ISelector selector = ExtractorUtils.GetSelector(extractBy);
                fieldExtractor = new FieldExtractor(field, selector, extractBy.Source,
                                                    extractBy.NotNull, extractBy.Multi || field.PropertyType.IsGenericType, extractBy.Expresion);
            }
            return(fieldExtractor);
        }
Пример #4
0
        private FieldExtractor GetAnnotationExtractByUrl(PropertyInfo field)
        {
            FieldExtractor fieldExtractor = null;
            ExtractByUrl   extractByUrl   = field.GetCustomAttribute <ExtractByUrl>();

            if (extractByUrl != null)
            {
                string regexPattern = extractByUrl.Value;
                if (string.IsNullOrEmpty(regexPattern.Trim()))
                {
                    regexPattern = ".*";
                }
                fieldExtractor = new FieldExtractor(field,
                                                    new RegexSelector(regexPattern), ExtractSource.Url, extractByUrl.NotNull,
                                                    extractByUrl.Multi || field.PropertyType.IsGenericType, null);
            }
            return(fieldExtractor);
        }
Пример #5
0
        private void CheckFormat(PropertyInfo field, FieldExtractor fieldExtractor)
        {
            //check custom formatter
            Attribute.Formatter formatter = field.GetCustomAttribute <Attribute.Formatter>();
            Type formatterType;

            if (formatter?.ObjectFormatter != null)
            {
                //  if (!formatter.formatter().equals(ObjectFormatter.class)) {
                IObjectFormatter objectFormatter = formatter.ObjectFormatter;
                objectFormatter.InitParam(formatter.Value);
                fieldExtractor.ObjectFormatter = objectFormatter;
                return;
                //   }
            }
            else
            {
                formatterType = FormatterFactory.GetFormatter(field.PropertyType);
            }

            if (!fieldExtractor.Multi)
            {
                IObjectFormatter objectFormatter = GetObjectFormatter(formatterType);
                if (objectFormatter == null)
                {
                    throw new Exception("Can't find formatter for field " + field.Name + " of type " + formatterType);
                }
                fieldExtractor.ObjectFormatter = objectFormatter;
            }
            else
            {
                if (!field.PropertyType.IsGenericType)
                {
                    throw new SpiderExceptoin("Field " + field.Name + " must be generice type.");
                }

                Type[] genericType = field.PropertyType.GetGenericArguments();
                if (genericType.Length != 1)
                {
                    throw new SpiderExceptoin("Field " + field.Name + " must be single generice type like List<T> Hashset<T>. Not support Diction<K,V> etc...");
                }

                MethodInfo method = fieldExtractor.Field.PropertyType.GetMethod("Add", genericType);
                if (method == null)
                {
                    throw new SpiderExceptoin("Field " + field.Name + " did not contains Add(T t) method.");
                }

                IObjectFormatter objectFormatter = GetObjectFormatter(formatterType);
                if (objectFormatter == null)
                {
                    if (formatter != null)
                    {
                        throw new SpiderExceptoin("Can't find formatter for field " + field.Name + " of type " + formatter.SubType);
                    }
                }
                else
                {
                    fieldExtractor.ObjectFormatter = objectFormatter;
                }
            }
        }