Exemple #1
0
        /// <summary>
        /// Parse an object from <paramref name="s"/>
        /// </summary>
        public T ParseAttributes <T>(XmlReader r, DatatypeR2FormatterParseResult result) where T : ANY, IPrimitiveDataValue, IQuantity, new()
        {
            string val = null;

            // Get the value
            if (r.GetAttribute("value") != null)
            {
                val = r.GetAttribute("value");
            }

            // Parse the QTY part
            QTYFormatter baseFormatter = new QTYFormatter();

            baseFormatter.Host = this.Host;

            // Parse attributes part of QTY
            T retVal = baseFormatter.ParseAttributes <T>(r, result);

            IRealValue rvRetVal = retVal as IRealValue;

            // If the value was interpreted
            if (!String.IsNullOrEmpty(val))
            {
                try
                {
                    retVal.Value = Util.FromWireFormat(val, retVal.GetType().GetProperty("Value").PropertyType);
                    if (rvRetVal != null && val.Contains(DatatypeR2Formatter.FormatterCulture.NumberFormat.NumberDecimalSeparator))
                    {
                        rvRetVal.Precision = val.Length - val.IndexOf(DatatypeR2Formatter.FormatterCulture.NumberFormat.NumberDecimalSeparator) - 1;
                    }
                }
                catch (Exception e)
                {
                    result.Code = ResultCode.Error;
                    result.AddResultDetail(new ResultDetail(ResultDetailType.Error, e.Message, r.ToString(), e));
                }
            }

            // REturn the retVal
            return(retVal);
        }
Exemple #2
0
        /// <summary>
        /// Parse the RTO
        /// </summary>
        public object Parse(System.Xml.XmlReader s, DatatypeR2FormatterParseResult result)
        {
            // Create the types
            Type rtoType = typeof(RTO <,>);

            QTYFormatter baseFormatter = (new QTYFormatter()
            {
                Host = this.Host
            });
            // Quantity value, just read into a REAL
            var qty = baseFormatter.ParseAttributes <REAL>(s, result);

            // Temporary values
            IGraphable denominator = null,
                       numerator   = null;

            // Elements
            // This type is a little different in R2, basically we can't determine the generic
            // parameters by the XSI Type so we have to parse the num/denom manually, and
            // ensure that we have both, then we construct the object and set the properties
            #region Elements
            if (!s.IsEmptyElement)
            {
                int    sDepth = s.Depth;
                string sName  = s.Name;

                s.Read();
                // string Name
                while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName))
                {
                    string oldName = s.Name; // Name
                    try
                    {
                        // Numerator
                        if (s.LocalName == "numerator")
                        {
                            var parseResult = Host.Parse(s, GenericArguments[0]);
                            result.Code = parseResult.Code;
                            result.AddResultDetail(parseResult.Details);
                            numerator = parseResult.Structure;
                        }
                        // Denominator
                        else if (s.LocalName == "denominator")
                        {
                            var parseResult = Host.Parse(s, GenericArguments[1]);
                            result.Code = parseResult.Code;
                            result.AddResultDetail(parseResult.Details);
                            denominator = parseResult.Structure;
                        }
                        else
                        {
                            baseFormatter.ParseElementsInline(s, qty, result);
                        }
                    }
                    catch (MessageValidationException e)
                    {
                        result.AddResultDetail(new MARC.Everest.Connectors.ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, e));
                    }
                    finally
                    {
                        if (s.Name == oldName)
                        {
                            s.Read();
                        }
                    }
                }
            }
            #endregion

            try
            {
                // Construct the generic type (if possible)
                Type rtoGenericType = rtoType.MakeGenericType(new Type[]
                {
                    numerator == null ? typeof(IQuantity) : numerator.GetType(),
                    denominator == null ? typeof(IQuantity) : denominator.GetType()
                });

                // Create an instance of rto from the rtoType
                object instance = rtoGenericType.GetConstructor(Type.EmptyTypes).Invoke(null);

                // Get the values from the QTY and copy
                IQuantity rtoQty = instance as IQuantity;
                DatatypeR2Formatter.CopyBaseAttributes(qty, rtoQty as ANY);
                rtoQty.Expression      = qty.Expression;
                rtoQty.OriginalText    = qty.OriginalText;
                rtoQty.UncertainRange  = qty.UncertainRange;
                rtoQty.Uncertainty     = qty.Uncertainty;
                rtoQty.UncertaintyType = qty.UncertaintyType;

                // rto properties
                PropertyInfo numeratorProperty   = rtoGenericType.GetProperty("Numerator"),
                             denominatorProperty = rtoGenericType.GetProperty("Denominator");

                // Set the previously found properties
                numeratorProperty.SetValue(instance, Util.FromWireFormat(numerator, numeratorProperty.PropertyType), null);
                denominatorProperty.SetValue(instance, Util.FromWireFormat(denominator, denominatorProperty.PropertyType), null);

                // Validate
                ANYFormatter anyFormatter = new ANYFormatter();
                string       pathName     = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name;
                anyFormatter.Validate(instance as ANY, pathName, result);

                return(instance);
            }
            catch (Exception e)
            {
                result.AddResultDetail(new ResultDetail(ResultDetailType.Error, e.Message, e));
                return(null);
            }
        }