Beispiel #1
0
        /// <summary>
        /// Enhance JohnT: there is some evidence (see LexReference.ExtractMinimalLexReferences)
        /// that LexReferences of the three sequence types should be allowed to show up if only
        /// ONE item is present. However, such a 'relation' doesn't seem very useful, and a longer
        /// one that is reduced to a single item by filtering is even less likely to be what the user wants.
        /// </summary>
        /// <param name="hvoRef"></param>
        /// <returns></returns>
        private bool IsPublishableLexRef(int hvoRef)
        {
            var publishableItems  = VecProp(hvoRef, LexReferenceTags.kflidTargets);
            int originalItemCount = BaseSda.get_VecSize(hvoRef, LexReferenceTags.kflidTargets);

            if (originalItemCount == publishableItems.Length)
            {
                return(true);                // If filtering didn't change anything don't mess with it.
            }
            if (publishableItems.Length < 2)
            {
                return(false);
            }
            // If at least two are publishable, it depends on the type.
            // It can't be published if the first item, which represents the root of the tree, is not publishable.
            var lexRef = m_lexRefRepo.GetObject(hvoRef);

            switch (((ILexRefType)lexRef.Owner).MappingType)
            {
            case (int)LexRefTypeTags.MappingTypes.kmtEntryTree:
            case (int)LexRefTypeTags.MappingTypes.kmtSenseTree:
            case (int)LexRefTypeTags.MappingTypes.kmtEntryOrSenseTree:
                return(!m_excludedItems.Contains(lexRef.TargetsRS[0].Hvo));
            }
            return(true);
        }
        /// <summary>
        /// Override to filter the specified properties.
        /// </summary>
        public override int get_VecItem(int hvo, int tag, int index)
        {
            ITestItem tester;

            if (!m_filterFlids.TryGetValue(tag, out tester))
            {
                return(base.get_VecItem(hvo, tag, index));
            }
            int chvoReal = BaseSda.get_VecSize(hvo, tag);

            using (ArrayPtr arrayPtr = MarshalEx.ArrayToNative(chvoReal, typeof(int)))
            {
                BaseSda.VecProp(hvo, tag, chvoReal, out chvoReal, arrayPtr);
                int[] candidates = (int[])MarshalEx.NativeToArray(arrayPtr, chvoReal, typeof(int));
                int   iresult    = 0;
                for (int icandidate = 0; icandidate < candidates.Length; icandidate++)
                {
                    if (tester.Test(candidates[icandidate], BaseSda, m_validHvos))
                    {
                        if (iresult == index)
                        {
                            return(candidates[icandidate]);
                        }
                        iresult++;
                    }
                }
                throw new IndexOutOfRangeException("filtered vector does not contain that many items (wanted " + index +
                                                   " but have only " + iresult + ")");
            }
        }
        /// <summary>
        /// Override to filter the specified properties.
        /// </summary>
        public override void VecProp(int hvo, int tag, int chvoMax, out int chvo, ArrayPtr rghvo)
        {
            ITestItem tester;

            if (!m_filterFlids.TryGetValue(tag, out tester))
            {
                base.VecProp(hvo, tag, chvoMax, out chvo, rghvo);
                return;
            }
            int chvoReal = BaseSda.get_VecSize(hvo, tag);

            using (ArrayPtr arrayPtr = MarshalEx.ArrayToNative(chvoReal, typeof(int)))
            {
                BaseSda.VecProp(hvo, tag, chvoReal, out chvoReal, arrayPtr);
                int[] candidates = (int[])MarshalEx.NativeToArray(arrayPtr, chvoReal, typeof(int));
                int[] results    = new int[chvoMax];
                int   iresult    = 0;
                for (int icandidate = 0; icandidate < candidates.Length; icandidate++)
                {
                    if (tester.Test(candidates[icandidate], BaseSda, m_validHvos))
                    {
                        results[iresult++] = candidates[icandidate];
                    }
                }
                chvo = iresult;
                MarshalEx.ArrayToNative(rghvo, chvoMax, results);
            }
        }
        /// <summary>
        /// Override to filter the specified properties.
        /// </summary>
        public override int get_VecSize(int hvo, int tag)
        {
            ITestItem tester;

            if (!m_filterFlids.TryGetValue(tag, out tester))
            {
                return(base.get_VecSize(hvo, tag));
            }
            int chvoReal = BaseSda.get_VecSize(hvo, tag);

            using (ArrayPtr arrayPtr = MarshalEx.ArrayToNative(chvoReal, typeof(int)))
            {
                BaseSda.VecProp(hvo, tag, chvoReal, out chvoReal, arrayPtr);
                int[] candidates = (int[])MarshalEx.NativeToArray(arrayPtr, chvoReal, typeof(int));
                int   iresult    = 0;
                for (int icandidate = 0; icandidate < candidates.Length; icandidate++)
                {
                    if (tester.Test(candidates[icandidate], BaseSda, m_validHvos))
                    {
                        iresult++;
                    }
                }
                return(iresult);
            }
        }
        /// <summary>
        /// Make one that wraps the specified cache and passes items in the specified property of the specified root object.
        /// </summary>
        public FilterSdaDecorator(ISilDataAccess domainDataByFlid, int mainFlid, int hvoRoot)
            : base(domainDataByFlid)
        {
            m_mainFlid = mainFlid;
            m_hvoRoot  = hvoRoot;
            int chvoReal = BaseSda.get_VecSize(m_hvoRoot, m_mainFlid);

            using (ArrayPtr arrayPtr = MarshalEx.ArrayToNative(chvoReal, typeof(int)))
            {
                BaseSda.VecProp(m_hvoRoot, m_mainFlid, chvoReal, out chvoReal, arrayPtr);
                m_validHvos = new Set <int>((int[])MarshalEx.NativeToArray(arrayPtr, chvoReal, typeof(int)));
            }
        }