Example #1
0
        public virtual int ReplaceKidHint(TaggingHintKey kidHintKey, ICollection <TaggingHintKey> newKidsHintKeys)
        {
            TaggingHintKey parentKey = GetParentHint(kidHintKey);

            if (parentKey == null)
            {
                return(-1);
            }
            if (kidHintKey.IsFinished())
            {
                ILog logger = LogManager.GetLogger(typeof(iText.Layout.Tagging.LayoutTaggingHelper));
                logger.Error(iText.IO.LogMessageConstant.CANNOT_REPLACE_FINISHED_HINT);
                // If kidHintKey is finished you won't be able to add it anywhere after replacing is ended.
                // If kidHintKey might be finished, use moveKidHint instead.
                // replaceKidHint should be used when parent might be finished.
                return(-1);
            }
            int kidIndex = RemoveParentHint(kidHintKey);
            IList <TaggingHintKey> kidsToBeAdded = new List <TaggingHintKey>();

            foreach (TaggingHintKey newKidKey in newKidsHintKeys)
            {
                int i = RemoveParentHint(newKidKey);
                if (i == RETVAL_PARENT_AND_KID_FINISHED || i == RETVAL_NO_PARENT && newKidKey.IsFinished())
                {
                    ILog logger = LogManager.GetLogger(typeof(iText.Layout.Tagging.LayoutTaggingHelper));
                    logger.Error(iText.IO.LogMessageConstant.CANNOT_MOVE_FINISHED_HINT);
                    continue;
                }
                kidsToBeAdded.Add(newKidKey);
            }
            AddKidsHint(parentKey, kidsToBeAdded, kidIndex, true);
            return(kidIndex);
        }
Example #2
0
        private bool CreateSingleTag(TaggingHintKey hintKey, TagTreePointer tagPointer)
        {
            if (hintKey.IsFinished())
            {
                ILog logger = LogManager.GetLogger(typeof(iText.Layout.Tagging.LayoutTaggingHelper));
                logger.Error(iText.IO.LogMessageConstant.ATTEMPT_TO_CREATE_A_TAG_FOR_FINISHED_HINT);
                return(false);
            }
            if (IsNonAccessibleHint(hintKey))
            {
                // try move pointer to the nearest accessible parent in case any direct content will be
                // tagged with this tagPointer
                TaggingHintKey parentTagHint = GetAccessibleParentHint(hintKey);
                context.GetWaitingTagsManager().TryMovePointerToWaitingTag(tagPointer, parentTagHint);
                return(false);
            }
            WaitingTagsManager waitingTagsManager = context.GetWaitingTagsManager();

            if (!waitingTagsManager.TryMovePointerToWaitingTag(tagPointer, hintKey))
            {
                IAccessibleElement modelElement = hintKey.GetAccessibleElement();
                TaggingHintKey     parentHint   = GetAccessibleParentHint(hintKey);
                int ind = -1;
                if (parentHint != null)
                {
                    // if parent tag hasn't been created yet - it's ok, kid tags will be moved on it's creation
                    if (waitingTagsManager.TryMovePointerToWaitingTag(tagPointer, parentHint))
                    {
                        IList <TaggingHintKey> siblingsHint = GetAccessibleKidsHint(parentHint);
                        int i = siblingsHint.IndexOf(hintKey);
                        ind = GetNearestNextSiblingTagIndex(waitingTagsManager, tagPointer, siblingsHint, i);
                    }
                }
                tagPointer.AddTag(ind, modelElement.GetAccessibilityProperties());
                if (hintKey.GetOverriddenRole() != null)
                {
                    tagPointer.SetRole(hintKey.GetOverriddenRole());
                }
                waitingTagsManager.AssignWaitingState(tagPointer, hintKey);
                IList <TaggingHintKey> kidsHint = GetAccessibleKidsHint(hintKey);
                foreach (TaggingHintKey kidKey in kidsHint)
                {
                    MoveKidTagIfCreated(hintKey, kidKey);
                }
                return(true);
            }
            return(false);
        }
Example #3
0
        private int RemoveParentHint(TaggingHintKey hintKey)
        {
            TaggingHintKey parentHint = parentHints.Get(hintKey);

            if (parentHint == null)
            {
                return(RETVAL_NO_PARENT);
            }
            TaggingHintKey accessibleParentHint = GetAccessibleParentHint(hintKey);

            if (hintKey.IsFinished() && parentHint.IsFinished() && (accessibleParentHint == null || accessibleParentHint
                                                                    .IsFinished()))
            {
                return(RETVAL_PARENT_AND_KID_FINISHED);
            }
            return(RemoveParentHint(hintKey, parentHint));
        }
Example #4
0
        private bool IsSomeParentNotFinished(TaggingHintKey parentHint)
        {
            TaggingHintKey hintKey = parentHint;

            while (true)
            {
                if (hintKey == null)
                {
                    return(false);
                }
                if (!hintKey.IsFinished())
                {
                    return(true);
                }
                if (!IsNonAccessibleHint(hintKey))
                {
                    return(false);
                }
                hintKey = GetParentHint(hintKey);
            }
        }
Example #5
0
        public virtual int MoveKidHint(TaggingHintKey hintKeyOfKidToMove, TaggingHintKey newParent, int insertIndex
                                       )
        {
            if (newParent.IsFinished())
            {
                ILog logger = LogManager.GetLogger(typeof(iText.Layout.Tagging.LayoutTaggingHelper));
                logger.Error(iText.IO.LogMessageConstant.CANNOT_MOVE_HINT_TO_FINISHED_PARENT);
                return(-1);
            }
            int removeRes = RemoveParentHint(hintKeyOfKidToMove);

            if (removeRes == RETVAL_PARENT_AND_KID_FINISHED || removeRes == RETVAL_NO_PARENT && hintKeyOfKidToMove.IsFinished
                    ())
            {
                ILog logger = LogManager.GetLogger(typeof(iText.Layout.Tagging.LayoutTaggingHelper));
                logger.Error(iText.IO.LogMessageConstant.CANNOT_MOVE_FINISHED_HINT);
                return(-1);
            }
            AddKidsHint(newParent, JavaCollectionsUtil.SingletonList <TaggingHintKey>(hintKeyOfKidToMove), insertIndex,
                        true);
            return(removeRes);
        }
Example #6
0
        public virtual void FinishTaggingHint(IPropertyContainer hintOwner)
        {
            TaggingHintKey rendererKey = GetHintKey(hintOwner);

            // artifact is always finished
            if (rendererKey == null || rendererKey.IsFinished())
            {
                return;
            }
            if (rendererKey.IsElementBasedFinishingOnly() && !(hintOwner is IElement))
            {
                // avoid auto finishing of hints created based on IElements
                return;
            }
            if (!IsNonAccessibleHint(rendererKey))
            {
                IAccessibleElement modelElement = rendererKey.GetAccessibleElement();
                String             role         = modelElement.GetAccessibilityProperties().GetRole();
                if (rendererKey.GetOverriddenRole() != null)
                {
                    role = rendererKey.GetOverriddenRole();
                }
                IList <ITaggingRule> rules = taggingRules.Get(role);
                bool ruleResult            = true;
                if (rules != null)
                {
                    foreach (ITaggingRule rule in rules)
                    {
                        ruleResult = ruleResult && rule.OnTagFinish(this, rendererKey);
                    }
                }
                if (!ruleResult)
                {
                    return;
                }
            }
            rendererKey.SetFinished();
        }
Example #7
0
        private void AddKidsHint(TaggingHintKey parentKey, ICollection <TaggingHintKey> newKidsKeys, int insertIndex
                                 , bool skipFinishedChecks)
        {
            if (newKidsKeys.IsEmpty())
            {
                return;
            }
            if (parentKey.IsArtifact())
            {
                foreach (TaggingHintKey kid in newKidsKeys)
                {
                    MarkArtifactHint(kid);
                }
                return;
            }
            if (!skipFinishedChecks && parentKey.IsFinished())
            {
                ILog logger = LogManager.GetLogger(typeof(iText.Layout.Tagging.LayoutTaggingHelper));
                logger.Error(iText.IO.LogMessageConstant.CANNOT_ADD_HINTS_TO_FINISHED_PARENT);
                return;
            }
            IList <TaggingHintKey> kidsHint = kidsHints.Get(parentKey);

            if (kidsHint == null)
            {
                kidsHint = new List <TaggingHintKey>();
            }
            TaggingHintKey parentTagHint           = IsNonAccessibleHint(parentKey) ? GetAccessibleParentHint(parentKey) : parentKey;
            bool           parentTagAlreadyCreated = parentTagHint != null && IsTagAlreadyExistsForHint(parentTagHint);

            foreach (TaggingHintKey kidKey in newKidsKeys)
            {
                if (kidKey.IsArtifact())
                {
                    continue;
                }
                TaggingHintKey prevParent = GetParentHint(kidKey);
                if (prevParent != null)
                {
                    // TODO seems to be a legit use case to re-add hints to just ensure that hints are added
                    //                Logger logger = LoggerFactory.getLogger(LayoutTaggingHelper.class);
                    //                logger.error(LogMessageConstant.CANNOT_ADD_KID_HINT_WHICH_IS_ALREADY_ADDED_TO_ANOTHER_PARENT);
                    continue;
                }
                if (!skipFinishedChecks && kidKey.IsFinished())
                {
                    ILog logger = LogManager.GetLogger(typeof(iText.Layout.Tagging.LayoutTaggingHelper));
                    logger.Error(iText.IO.LogMessageConstant.CANNOT_ADD_FINISHED_HINT_AS_A_NEW_KID_HINT);
                    continue;
                }
                if (insertIndex > -1)
                {
                    kidsHint.Add(insertIndex++, kidKey);
                }
                else
                {
                    kidsHint.Add(kidKey);
                }
                parentHints.Put(kidKey, parentKey);
                if (parentTagAlreadyCreated)
                {
                    if (kidKey.GetAccessibleElement() is TaggingDummyElement)
                    {
                        CreateTag(kidKey, new TagTreePointer(document));
                    }
                    if (IsNonAccessibleHint(kidKey))
                    {
                        foreach (TaggingHintKey nestedKid in GetAccessibleKidsHint(kidKey))
                        {
                            if (nestedKid.GetAccessibleElement() is TaggingDummyElement)
                            {
                                CreateTag(nestedKid, new TagTreePointer(document));
                            }
                            MoveKidTagIfCreated(parentTagHint, nestedKid);
                        }
                    }
                    else
                    {
                        MoveKidTagIfCreated(parentTagHint, kidKey);
                    }
                }
            }
            if (!kidsHint.IsEmpty())
            {
                kidsHints.Put(parentKey, kidsHint);
            }
        }