Dynamic href and keyref!

mu258770
Posts: 111

Dynamic href and keyref!

Mon Nov 21, 2016 2:32 pm

Hi,

In oXygen, we have two paste as link options in which one is for inserting any internal links as "href" (paste as link) and the other for inserting links as "keyref" (paste as link(keyref)).

We would like to combine these two options and make only one option which should work in a way that, if the user has selected any element in the same topic and click on paste as link option, it should create an "href" link only. If the selected element is not from the same topic, then "keyref" should be inserted. Is there any we we can make this dynamic link insertion possible (by using some xpaths or so)?

We are using oXygen author 18.0 eclipse plugin version.

Regards,
Shabeer
alex_jitianu
Posts: 649

Re: Dynamic href and keyref!

Tue Nov 22, 2016 5:42 pm

Hi Shabeer,

What you could do is to create you own action that will always delegate to the default "Paste as link(keyref)" action. It can do that by calling:

Code: Select all

DITAAccess.pasteAsReference(authorAccess, PasteInfo.PASTE_AS_KEYREF);

if no exception was thrown and no error message was returned then you can assume the operation was successful. The next step is to inspect what was introduced and decide if you should replace the @keyref with a plain @href:

Code: Select all

    int caretOffset = authorAccess.getEditorAccess().getCaretOffset();
    AuthorNode nodeAtOffset = authorAccess.getDocumentController().getNodeAtOffset(caretOffset);
    // TODO Extract the keyname
    String keyName = getKeyName(nodeAtOffset);
    LinkedHashMap<String, KeyInfo> keys = DITAAccess.getKeys(authorAccess.getEditorAccess().getEditorLocation());
    // TODO Check if the key maps to the current file and, if true, replace the @keyref with a local @href on the nodeAtOffset


Best regards,
Alex
mu258770
Posts: 111

Re: Dynamic href and keyref!

Mon Nov 28, 2016 9:48 am

Hi Alex,

Thank you for your reply!

We have thought about one other alternative to make this dynamic paste as href and keyref possible.

1) Enable both paste as link, and paste as link (keyref) options in the context menu.
2) Paste as link should be enabled only when the element copied is from the same topic
3) Same way paste as link (keyref) should be enabled only when copied content is from other topic.

We would like to know whether this is possible using any Xpath expression.

We know that in oXygen we can enable and disable submenus in the document type association window by using Xpath expression. Please let us know whether that will work in this case.

Thank you!

Regards,
Shabeer
alex_jitianu
Posts: 649

Re: Dynamic href and keyref!

Mon Nov 28, 2016 2:04 pm

Hi Shabeer,

You can go in this direction too. I suggest adding a context menu customizer. Since you are in an Eclipse environment the easiest way would be to use the ActionBarContributorCustomizer extension point. On the customizeAuthorPageExtensionMenu() callback I recommend searching for the two actions (you can use the IMenuManager parameter for that) and replace them with an action of your own (a wrapper that will delegate to one of the previous actions) . This action of yours will look inside the clipboard using ro.sync.ecss.extensions.api.AuthorAccess.getAuthorObjectFromClipboard() and based on the origin of the copied content will delegate to either "paste as link" or to "paste as link (keyref)".

Best regards,
Alex
mu258770
Posts: 111

Re: Dynamic href and keyref!

Wed Nov 30, 2016 1:07 pm

Hi Alex,

Thanks for your reply!

We are able to create our own action which delegates to Paste as Keyref. It works same as Paste as Link (Keyref).

But we are unable to find whether the copied content in the clipboard is from the same topic or from the different. We have tried with the code which you have provided but not able to proceed further. Is there any existing API for finding the root id of the copied content and to find the root id of the current topic (where we would paste the link).

Could you please help us further.

Regards,
Shabeer
Radu
Posts: 5757

Re: Dynamic href and keyref!

Thu Dec 01, 2016 10:20 am

Hi Shabeer,

Alex took a few days off so I will try to help you further.
So after you use this API ro.sync.ecss.extensions.api.AuthorAccess.getAuthorObjectFromClipboard(), the clipboard object contains an array of fragments (usually it's only one fragment if you did not copy multiple selection intervals) ro.sync.ecss.component.AuthorClipboardObject.getFragments().
An AuthorDocumentFragmentClipboardObject has a content descriptor AuthorDocumentFragmentClipboardObject.getContentDescription() which keeps the location from which the fragment was copied ro.sync.ecss.strictediting.ContentDescription.getSystemID() so you could compare this location with the one of the current edited XML document AuthorAccess.getEditorAccess().getEditorLocation().

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
mu258770
Posts: 111

Re: Dynamic href and keyref!

Thu Dec 01, 2016 3:35 pm

Hi Radu,

Thank you for your reply!

I am able to get the root id of the current topic. But am not getting the root id of the topic from which we copied the content.

Also I don't know how I can overwrite the Paste as keyref insertion if it is from same topic.

Please see the code below.

Code: Select all

public void doOperation(AuthorAccess authorAccess, ArgumentsMap args)
    throws IllegalArgumentException, AuthorOperationException
  {
       DITAAccess.pasteAsReference(authorAccess, PasteInfo.PASTE_AS_KEYREF);
            int caretOffset = authorAccess.getEditorAccess().getCaretOffset();
     //Using the below line we can get the current topic id.
     AuthorNode nodeAtOffset = authorAccess.getDocumentController().getNodeAtOffset(caretOffset);
    
     //The below line should have helped in getting the root id of the copied content, but not getting!!!!!
     AuthorClipboardObject nodeAtOffset1=authorAccess.getAuthorObjectFromClipboard();
    
     //Once we get the root id, how we can overwrite the paste as keyref if it is from same topic????
    
  }   


Please help in proceeding.

Regards,
Shabeer
Radu
Posts: 5757

Re: Dynamic href and keyref!

Thu Dec 01, 2016 4:34 pm

Hi Shabeer,

You should invoke this regular paste as keyref:

Code: Select all

DITAAccess.pasteAsReference(authorAccess, PasteInfo.PASTE_AS_KEYREF);


only if you are pasting a target to another topic. So you need to execute the line above based on a condition, right now you are always executing it.
I gave you some hints in the previous post about what methods to call for the AuthorClipboardObject object in order to find out the information that you need.
If you decide that the pasted content is from the same topic, you can use our API AuthorAccess.getDocumentController().insertXMLFragment(xmlFragment, offset); to insert an XML fragment that you compose at the caret offset.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
mu258770
Posts: 111

Re: Dynamic href and keyref!

Thu Dec 01, 2016 5:54 pm

Hi Radu,

We are now able to find whether the link should be inserted as href or keyref. The pasting as "keyref" is working properly.

But for inserting the paste as "href", we do not know how we can get the value of the href to be inserted. Could you please help in that. We just need to get the value from clipboard. But with whatever we have used, we cannot get the exact link value from those.

Code: Select all

if(!clipboard_topic.equals(current_topic)) {

                      DITAAccess.pasteAsReference(authorAccess, PasteInfo.PASTE_AS_KEYREF);

}
else{         
            
            String xrefFragment =
                            "<xref href='" + "id from the clipboard???" + "' />";
            authorAccess.getDocumentController().insertXMLFragment(xrefFragment,caretOffset);
}


Regards,
Shabeer
Radu
Posts: 5757

Re: Dynamic href and keyref!

Fri Dec 02, 2016 10:32 am

Hi Shabeer,

The clipboard object should have enough information to compute both the element ID and the topic ID.
Something like:

Code: Select all

    AuthorDocumentFragmentClipboardObject[] frags = clipboardObject.getFragments();
    if(frags != null && frags.length > 0){
      AuthorDocumentFragment frag = frags[0].getFragment();
      List<AuthorNode> contentNodes = frag.getContentNodes();
      if(contentNodes != null && !contentNodes.isEmpty()){
        AuthorNode firstNode = contentNodes.get(0);
        if(firstNode instanceof AuthorElement){
          AttrValue idAttrValue = ((AuthorElement)firstNode).getAttribute("id");
          if(idAttrValue != null){
            String elementID = idAttrValue.getValue();
            //And we also need the topic ID...
            ContentDescription descr = frags[0].getContentDescription();
            List<ContentDescriptionItem> ancestorElements = descr.getAncestors();
            if(ancestorElements != null && ! ancestorElements.isEmpty()){
              ContentDescriptionItem rootElementDescription = ancestorElements.get(0);
              String topicID = rootElementDescription.getAttributes().get("id");
            }
          }
        }
      }
    }


Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
mu258770
Posts: 111

Re: Dynamic href and keyref!

Fri Dec 02, 2016 4:14 pm

Hi Radu

Thanks a lot for you reply!

The provided solution works:). But if you can give us the exact operation action for "Paste as link" that would help us doing this better way.

We have the paste as keyref action as,

Code: Select all

DITAAccess.pasteAsReference(authorAccess, PasteInfo.PASTE_AS_KEYREF);


Could you please let us know the action for Paste as link simiarly.

Regards,
Shabeer
Radu
Posts: 5757

Re: Dynamic href and keyref!

Fri Dec 02, 2016 4:26 pm

Hi Shabeer,

The default paste as reference operation does something like:

Code: Select all

DITAAccess.pasteAsReference(authorAccess, PasteInfo.PASTE_AS_REFERENCE);


Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com

Return to “DITA (Editing and Publishing DITA Content)”

Who is online

Users browsing this forum: Exabot [Bot] and 3 guests