Page 1 of 1

how to the list of which elements can be wrapped a element

Posted: Wed Nov 29, 2023 12:57 pm
by shikhar_472
Hi Team,

Is there any inbuild method or way to get the list of element which can wrapped on selection.

USE CASE - suppose i am selecting a node and i want to change that selected node with the possible wrap element i need the list of element which should allow on that position while replacing the tag with another one.
<disclaimer>
<para><paratext></paratext></para>
</disclaimer>

here i want to select the disclaimer and want to change the disclaimer with the possible allowed elements which should be wrapper outside of para i want that element of list

Re: how to the list of which elements can be wrapped a element

Posted: Wed Nov 29, 2023 6:47 pm
by cosminef
Hello,
Try to use this code, and optionally add more logging if you encounter errors to investigate them:

Code: Select all

public void doOperation(AuthorAccess authorAccess, ArgumentsMap args)
      throws AuthorOperationException {
    AuthorElement nodeToRename = ...;
    AuthorSchemaManager schemaManager = authorAccess.getDocumentController().getAuthorSchemaManager();
    Map<QName, Collection<QName>> elementToParentsMap = schemaManager.getElementToParentsMap(nodeToRename);
    try {
      WhatElementsCanGoHereContext context = schemaManager.createWhatElementsCanGoHereContext(nodeToRename.getStartOffset());
      List<CIElement> allPossibleElementsWhichCanBeInsertedInsteadOfNodeToRename = schemaManager.getChildrenElements(context);
      //Now we need to filter all possible elements and leave only the ones which can contain the selected content.
      List<AuthorNode> childrenOfNodeToRename = nodeToRename.getContentNodes();
      if(childrenOfNodeToRename != null && ! childrenOfNodeToRename.isEmpty()) {
        for (AuthorNode childInsideSelection : childrenOfNodeToRename) {
          Collection<QName> possibleParentsForChild = elementToParentsMap.get(new QName(childInsideSelection.getName()));
          if(possibleParentsForChild != null) {
            Iterator<CIElement> iter = allPossibleElementsWhichCanBeInsertedInsteadOfNodeToRename.iterator();
            while(iter.hasNext()) {
              CIElement possibleReplacement = iter.next();
              if(! possibleParentsForChild.contains(new QName(possibleReplacement.getName()))) {
                iter.remove();
              }
            }
          }
        }
      }
    } catch (BadLocationException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
Best Regards,
Cosmin

Re: how to the list of which elements can be wrapped a element

Posted: Fri Dec 01, 2023 9:52 am
by shikhar_472
Hi Cosmin,

Thank you very much for the solution i have one more doubt if child element is not present in that scenarion i wanted the filtered list for sure I can get all PossibleElements when child is not present but what if some tags does now allow text in that list how to differentiate that

Re: how to the list of which elements can be wrapped a element

Posted: Fri Dec 01, 2023 11:34 am
by mihaela
Hello,

Can you please give us a specific example to better understand what you are trying to obtain?

Best Regards,
Mihaela