Ejemplo n.º 1
0
        /// <summary>
        ///     Serializes the specified value.
        /// </summary>
        /// <param name="prop">The property.</param>
        /// <param name="parentObject">The parent object.</param>
        /// <param name="options">The options.</param>
        /// <returns>XElement.</returns>
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            var tag  = prop.GetAttribute <DFeCollectionAttribute>();
            var list = (ICollection)prop.GetValue(parentObject, null) ?? new ArrayList();

            if (list.Count < tag.MinSize || list.Count > tag.MaxSize && tag.MaxSize > 0)
            {
                var msg = list.Count > tag.MaxSize ? DFeSerializer.ErrMsgMaiorMaximo : DFeSerializer.ErrMsgMenorMinimo;
                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, msg);
            }

            if (list.Count == 0 && tag.MinSize == 0 && tag.Ocorrencia == Ocorrencia.NaoObrigatoria)
            {
                return(null);
            }

            var itemType   = GetItemType(prop.PropertyType) ?? GetItemType(list.GetType());
            var objectType = ObjectType.From(itemType);

            if (objectType == ObjectType.PrimitiveType)
            {
                return(SerializePrimitive(prop, parentObject, list, tag, options));
            }

            return(!prop.HasAttribute <DFeItemAttribute>()
                ? SerializeObjects(list, tag, options)
                : SerializeChild(list, tag, prop.GetAttributes <DFeItemAttribute>(), options));
        }
Ejemplo n.º 2
0
 public static XObject Serialize(DFeBaseAttribute tag, object value, SerializerOptions options)
 {
     try
     {
         var estaVazio          = value == null || value.ToString().IsNullOrEmpty();
         var conteudoProcessado = ProcessValue(ref estaVazio, tag.Tipo, value, tag.Ocorrencia, tag.Min, null, null);
         return(ProcessContent(tag, conteudoProcessado, estaVazio, options));
     }
     catch (Exception ex)
     {
         options.AddAlerta(tag.Id, tag.Name, tag.Descricao, ex.ToString());
         return(null);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        ///     Serializes a fundamental primitive object (e.g. string, int etc.) into a XElement using options.
        /// </summary>
        /// <param name="tag">The name of the primitive to serialize.</param>
        /// <param name="item">The item.</param>
        /// <param name="prop">The property.</param>
        /// <param name="options">Indicates how the output is formatted or serialized.</param>
        /// <param name="idx"></param>
        /// <returns>The XElement representation of the primitive.</returns>
        public static XObject Serialize(DFeBaseAttribute tag, object item, PropertyInfo prop, SerializerOptions options, int idx = -1)
        {
            try
            {
                var value              = prop.GetValueOrIndex(item, idx);
                var estaVazio          = value == null || value.ToString().IsNullOrEmpty();
                var conteudoProcessado = ProcessValue(ref estaVazio, tag.Tipo, value, tag.Ocorrencia, tag.Min, prop, item);

                return(ProcessContent(tag, conteudoProcessado, estaVazio, options));
            }
            catch (Exception ex)
            {
                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, ex.ToString());
                return(null);
            }
        }
Ejemplo n.º 4
0
        public static XObject[] Serialize(PropertyInfo prop, object parentObject, SerializerOptions options)
        {
            Guard.Against <VipException>(!prop.HasAttribute <DFeDictionaryAttribute>(),
                                         $"Atributo necessário não encontrado [{nameof(DFeDictionaryAttribute)}]");
            Guard.Against <VipException>(!prop.HasAttribute <DFeDictionaryKeyAttribute>(),
                                         $"Atributo necessário não encontrado [{nameof(DFeDictionaryKeyAttribute)}]");
            Guard.Against <VipException>(!prop.HasAttribute <DFeDictionaryValueAttribute>(),
                                         $"Atributo necessário não encontrado [{nameof(DFeDictionaryValueAttribute)}]");

            var tag      = prop.GetAttribute <DFeDictionaryAttribute>();
            var keyAtt   = prop.GetAttribute <DFeDictionaryKeyAttribute>();
            var valueAtt = prop.GetAttribute <DFeDictionaryValueAttribute>();

            Guard.Against <ArgumentNullException>(!keyAtt.AsAttribute && tag.ItemName.IsNullOrEmpty(),
                                                  "Se a Key não é um atributo é necessario informar o [ItemName]");

            var dictionary = (IDictionary)prop.GetValue(parentObject, null);

            if (dictionary.Count < tag.MinSize || dictionary.Count > tag.MaxSize && tag.MaxSize > 0)
            {
                var msg = dictionary.Count > tag.MaxSize ? DFeSerializer.ErrMsgMaiorMaximo : DFeSerializer.ErrMsgMenorMinimo;
                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, msg);
            }

            if (dictionary.Count == 0 && tag.MinSize == 0 && tag.Ocorrencia == Ocorrencia.NaoObrigatoria)
            {
                return(null);
            }

            var args = dictionary.GetType().GetGenericArguments();

            Guard.Against <ArgumentException>(args.Length != 2);

            var keyType   = ObjectType.From(args[0]);
            var valueType = ObjectType.From(args[1]);

            Guard.Against <VipException>(keyType != ObjectType.PrimitiveType && keyAtt.AsAttribute);

            var list = new List <XElement>();

            var dicENumerator = dictionary.GetEnumerator();

            while (dicENumerator.MoveNext())
            {
                var key   = dicENumerator.Entry.Key;
                var value = dicENumerator.Entry.Value;

                var keyElement = keyType == ObjectType.PrimitiveType
                    ? PrimitiveSerializer.Serialize(keyAtt, key, options)
                    : ObjectSerializer.Serialize(key, key.GetType(), keyAtt.Name, keyAtt.Namespace, options);

                var valueElement = valueType == ObjectType.PrimitiveType
                    ? (XElement)PrimitiveSerializer.Serialize(valueAtt, value, options)
                    : ObjectSerializer.Serialize(value, value.GetType(), valueAtt.Name, valueAtt.Namespace, options);

                if (keyAtt.AsAttribute)
                {
                    valueElement.AddAttribute((XAttribute)keyElement);
                    list.Add(valueElement);
                }
                else
                {
                    var itemElement = new XElement(tag.ItemName);
                    itemElement.AddChild((XElement)keyElement);
                    itemElement.AddChild(valueElement);

                    list.Add(itemElement);
                }
            }

            var element = new XElement(tag.Name, tag.Namespace);

            element.AddChild(list.ToArray());

            return(new XObject[] { element });
        }
Ejemplo n.º 5
0
        private static XObject ProcessContent(DFeBaseAttribute tag, string conteudoProcessado, bool estaVazio, SerializerOptions options)
        {
            string alerta;

            if (tag.Ocorrencia == Ocorrencia.Obrigatoria && estaVazio && tag.Min > 0)
            {
                alerta = DFeSerializer.ErrMsgVazio;
            }
            else
            {
                alerta = string.Empty;
            }

            if (conteudoProcessado.IsNullOrEmpty() && conteudoProcessado.Length < tag.Min && alerta.IsNullOrEmpty() && conteudoProcessado.Length > 1)
            {
                alerta = DFeSerializer.ErrMsgMenor;
            }

            if (!string.IsNullOrEmpty(conteudoProcessado.Trim()) && conteudoProcessado.Length > tag.Max)
            {
                alerta = DFeSerializer.ErrMsgMaior;
            }

            if (!string.IsNullOrEmpty(alerta.Trim()) && DFeSerializer.ErrMsgVazio.Equals(alerta) && !estaVazio)
            {
                alerta += $" [{tag.Name}]";
            }

            options.AddAlerta(tag.Id, tag.Name, tag.Descricao, alerta);

            XObject xmlTag = null;

            if (tag.Ocorrencia == Ocorrencia.Obrigatoria && estaVazio)
            {
                xmlTag = tag is DFeElementAttribute ? (XObject) new XElement(tag.Name) : new XAttribute(tag.Name, "");
            }

            if (estaVazio)
            {
                return(xmlTag);
            }

            var elementValue = options.RemoverAcentos ? conteudoProcessado.RemoveAccent() : conteudoProcessado;

            elementValue = options.RemoverEspacos ? elementValue.Trim() : elementValue;

            switch (tag)
            {
            case DFeAttributeAttribute _: return(new XAttribute(tag.Name, elementValue));

            case DFeDictionaryKeyAttribute keyAtt when keyAtt.AsAttribute: return(new XAttribute(keyAtt.Name, elementValue));

            case DFeElementAttribute elementAtt:
                if (elementValue.IsCData())
                {
                    elementValue = elementValue.RemoveCData();
                    return(new XElement(tag.Name, new XCData(elementValue)));
                }
                else
                {
                    return(elementAtt.UseCData ? new XElement(tag.Name, new XCData(elementValue)) : new XElement(tag.Name, elementValue));
                }

            default:
                return(new XElement(tag.Name, elementValue));
            }
        }