Dynamic href and keyref!

Post here questions and problems related to editing and publishing DITA content.
mu258770
Posts: 157
Joined: Mon Aug 18, 2014 4:11 pm

Dynamic href and keyref!

Post 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
alex_jitianu
Posts: 1008
Joined: Wed Nov 16, 2005 11:11 am

Re: Dynamic href and keyref!

Post 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
mu258770
Posts: 157
Joined: Mon Aug 18, 2014 4:11 pm

Re: Dynamic href and keyref!

Post 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
alex_jitianu
Posts: 1008
Joined: Wed Nov 16, 2005 11:11 am

Re: Dynamic href and keyref!

Post 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
mu258770
Posts: 157
Joined: Mon Aug 18, 2014 4:11 pm

Re: Dynamic href and keyref!

Post 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
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

Re: Dynamic href and keyref!

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
mu258770
Posts: 157
Joined: Mon Aug 18, 2014 4:11 pm

Re: Dynamic href and keyref!

Post 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
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

Re: Dynamic href and keyref!

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
mu258770
Posts: 157
Joined: Mon Aug 18, 2014 4:11 pm

Re: Dynamic href and keyref!

Post 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
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

Re: Dynamic href and keyref!

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
mu258770
Posts: 157
Joined: Mon Aug 18, 2014 4:11 pm

Re: Dynamic href and keyref!

Post 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
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

Re: Dynamic href and keyref!

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Srinarayan
Posts: 42
Joined: Tue Jun 08, 2021 3:27 pm

Re: Dynamic href and keyref!

Post 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
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

Re: Dynamic href and keyref!

Post 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
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply