Exemple #1
0
        /// <summary>
        /// Return value from path going from this object
        /// </summary>
        /// <param name="propertyPath"> Path formated like <ObjectReference>.<ListProperty>.<ListIndex>.<PropertyName> </param>
        /// <returns></returns>
        public NdfValueWrapper GetValueFromQuery(string query)
        {
            string rest = string.Empty;
            string next = NdfQueryReader.ParseNextStep(query, out rest);

            if (!string.IsNullOrEmpty(next))
            {
                NdfPropertyValue nextproperty = GetProperty(next);
                if (nextproperty == null)
                {
                    switch (nextproperty.Type)
                    {
                        case Types.NdfType.ObjectReference:
                            NdfObjectReference reference = nextproperty.Value as NdfObjectReference;
                            return reference.Instance.GetValueFromQuery(rest);

                        case Types.NdfType.MapList:
                            NdfMapList mapList = nextproperty.Value as NdfMapList;
                            return mapList.GetValueFromQuery(rest); 

                        case Types.NdfType.List:
                            NdfCollection list = nextproperty.Value as NdfCollection;
                            return list.GetValueFromQuery(rest); 

                        default:
                            return nextproperty.Value;
                    }
                }
            }

            throw(new Exception("Something went wrong with this path: " + query != string.Empty ? query:"Empty Path"));
        }
Exemple #2
0
        /// <summary>
        /// Return true if it succeded getting a property from the query and output it.
        /// </summary>
        /// <param name="query"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGetValueFromQuery(string query, out NdfValueWrapper value)
        {
            string rest = string.Empty;
            string next = NdfQueryReader.ParseNextStep(query, out rest);

            if(!string.IsNullOrEmpty(next))
            {
                NdfPropertyValue nextproperty;

                if (TryGetProperty(next, out nextproperty))
                {
                    

                    switch (nextproperty.Type)
                    {
                        case Types.NdfType.ObjectReference:
                            NdfObjectReference reference = nextproperty.Value as NdfObjectReference;
                            if (string.IsNullOrEmpty(rest))
                            {
                                value = reference;
                                return true;
                            }
                            return reference.Instance.TryGetValueFromQuery(rest, out value);

                        case Types.NdfType.MapList:
                            NdfMapList mapList = nextproperty.Value as NdfMapList;
                            if (string.IsNullOrEmpty(rest))
                            {
                                value = mapList;
                                return true;
                            }
                            return mapList.TryGetValueFromQuery(rest, out value);

                        case Types.NdfType.List:
                            NdfCollection list = nextproperty.Value as NdfCollection;
                            if (string.IsNullOrEmpty(rest))
                            {
                                value = list;
                                return true;
                            }
                            return list.TryGetValueFromQuery(rest, out value);

                        case Types.NdfType.Unknown :
                            break;
                        case Types.NdfType.Unset:
                            break;

                        default:
                            value = nextproperty.Value;
                            return true;
                    }
                }
            }
            value = null;
            return false;
        }
        /// <summary>
        /// Reads the value of a Property inside a object instance.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="triggerBreak"></param>
        /// <param name="prop"></param>
        /// <returns>A NdfValueWrapper Instance.</returns>
        protected NdfValueWrapper ReadValue(MemoryStream ms, out bool triggerBreak, NdfPropertyValue prop)
        {
            var             buffer = new byte[4];
            uint            contBufferlen;
            NdfValueWrapper value;

            triggerBreak = false;

            ms.Read(buffer, 0, buffer.Length);
            var type = NdfTypeManager.GetType(buffer);

            if (type == NdfType.Reference)
            {
                ms.Read(buffer, 0, buffer.Length);
                type = NdfTypeManager.GetType(buffer);
            }

            switch (type)
            {
            case NdfType.WideString:
            case NdfType.List:
            case NdfType.MapList:
                ms.Read(buffer, 0, buffer.Length);
                contBufferlen = BitConverter.ToUInt32(buffer, 0);
                break;

            default:
                contBufferlen = NdfTypeManager.SizeofType(type);
                break;
            }

            if (type == NdfType.Unknown)
            {
                //var t = _unknownTypes.SingleOrDefault(x => Utils.ByteArrayCompare(x, buffer));

                //if (t == default(byte[]))
                //{
                //    _unknownTypes.Add(buffer);
                //    _unknownTypesCount.Add(1);
                //}
                //else
                //    _unknownTypesCount[_unknownTypes.IndexOf(t)]++;

                triggerBreak = true;
                return(new NdfUnkown(buffer, ms.Position));
            }

            if (type == NdfType.List || type == NdfType.MapList)
            {
                NdfCollection lstValue;

                if (type == NdfType.List)
                {
                    lstValue = new NdfCollection(ms.Position);
                }
                else
                {
                    lstValue = new NdfMapList(ms.Position);
                }

                CollectionItemValueHolder res;

                for (int i = 0; i < contBufferlen; i++)
                {
                    if (type == NdfType.List)
                    {
                        res = new CollectionItemValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset);
                    }
                    else
                    {
                        res = new CollectionItemValueHolder(
                            new NdfMap(
                                new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset),
                                new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset),
                                ms.Position),
                            this, prop.InstanceOffset);
                    }

                    lstValue.Add(res);

                    if (triggerBreak)
                    {
                        break;
                    }
                }

                value = lstValue;
            }
            else if (type == NdfType.Map)
            {
                value = new NdfMap(
                    new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset),
                    new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset),
                    ms.Position);
            }
            else
            {
                var contBuffer = new byte[contBufferlen];
                ms.Read(contBuffer, 0, contBuffer.Length);

                if (prop != null)
                {
                    prop.ValueData = contBuffer;
                }

                value = NdfTypeManager.GetValue(contBuffer, type, this, ms.Position - contBuffer.Length);
            }

            return(value);
        }