Page 1 of 1

Dynamic href and keyref!

Posted: Mon Nov 21, 2016 2:32 pm
by mu258770
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

Re: Dynamic href and keyref!

Posted: Tue Nov 22, 2016 5:42 pm
by alex_jitianu
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

Re: Dynamic href and keyref!

Posted: Mon Nov 28, 2016 9:48 am
by mu258770
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

Re: Dynamic href and keyref!

Posted: Mon Nov 28, 2016 2:04 pm
by alex_jitianu
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

Re: Dynamic href and keyref!

Posted: Wed Nov 30, 2016 1:07 pm
by mu258770
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

Re: Dynamic href and keyref!

Posted: Thu Dec 01, 2016 10:20 am
by Radu
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

Re: Dynamic href and keyref!

Posted: Thu Dec 01, 2016 3:35 pm
by mu258770
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

Re: Dynamic href and keyref!

Posted: Thu Dec 01, 2016 4:34 pm
by Radu
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

Re: Dynamic href and keyref!

Posted: Thu Dec 01, 2016 5:54 pm
by mu258770
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

Re: Dynamic href and keyref!

Posted: Fri Dec 02, 2016 10:32 am
by Radu
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

Re: Dynamic href and keyref!

Posted: Fri Dec 02, 2016 4:14 pm
by mu258770
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

Re: Dynamic href and keyref!

Posted: Fri Dec 02, 2016 4:26 pm
by Radu
Hi Shabeer,

The default paste as reference operation does something like:

Code: Select all

DITAAccess.pasteAsReference(authorAccess, PasteInfo.PASTE_AS_REFERENCE);
Regards,
Radu

Re: Dynamic href and keyref!

Posted: Tue Jun 29, 2021 4:17 pm
by Srinarayan
Hi,

The Paste as Link action (for inserting keyref and href ) used to work fine with Oxygen 20.1.0. Its now throwing error with Oxygen 23.0.0, if the selected element is not from the same topic, at the point :

DITAAccess.pasteAsReference(authorAccess, DITAAccess.PasteInfo.PASTE_AS_KEYREF);

Please note that the paste-as-link action for href (when selected element is in same topic) is still working fine. We are using Oxygen author Eclipse plugin version.

Could you please help us with the solution.

Best Regards,
Srinarayan

Re: Dynamic href and keyref!

Posted: Wed Jun 30, 2021 6:58 am
by Radu
Hi,

I see someone else (possibly also from your team) reported a similar problem with details including the exception stack trace and I answered them here:
post62545.html#p62545

Regards,
Radu