Page 1 of 1

Insert Fragment depending of the XML tree

Posted: Wed Jan 25, 2012 8:10 pm
by Vincent
Hi,

That's it, Eclipse is loaded with the sdk and I'm ready to develop ! :D

I use the operation :
ro.sync.ecss.extensions.commons.operations.InsertFragmentOperation
To insert an XML fragment.

The point is, I have different cases depending the place of the caret in the XML tree.

Example :

Code: Select all

<level1>
<level2>
<level3><my_fragment /></level3>
</level2>
</level1>
  • If my caret has a level3 element as parent, I want my_fragment to be inserted inside the current level3.
  • If my caret has a level2 element as parent, I want to create a level3 element (as a last child of the current level2) and insert my_fragment in it.
  • If my caret has a level1 element as parent, I want to create a level2 element (as a last child of the current level1) with a level3 inside and I want to insert my_fragment in this level3.
According to you, what is the best way to proceed ?
Is the operation the good one in this case ?

How can I get the position of my caret in the java class ?

Thank you for your help.

Vincent.

Re: Insert Fragment depending of the XML tree

Posted: Thu Jan 26, 2012 12:51 pm
by mihaela
Hi Vincent,

A solution is to create a single Author action with multiple operations (one operation for each insertion case). You can do this directly from the Document Type Options page, without any other SDK implementation.

To add a new operation use the + button placed in the bottom of the Operations section in the Actions dialog.
For each operation you can define the ro.sync.ecss.extensions.api.AuthorOperation that is invoked (in your case ro.sync.ecss.extensions.commons.operations.InsertFragmentOperation), its arguments and the XPath expression that must be met for this operation to be executed.

Here are the XPath expression and Fragment argument of each operation:
If my caret has a level3 element as parent, I want my_fragment to be inserted inside the current level3.
XPAth:

Code: Select all

self::level3 
Fragment argument:

Code: Select all

<my_fragment/>
If my caret has a level2 element as parent, I want to create a level3 element (as a last child of the current level2) and insert my_fragment in it.
XPAth:

Code: Select all

self::level2
Fragment argument:

Code: Select all

<level3><my_fragment/></level3>
If my caret has a level1 element as parent, I want to create a level2 element (as a last child of the current level1) with a level3 inside and I want to insert my_fragment in this level3.
XPAth:

Code: Select all

self::level1
Fragment argument:

Code: Select all

<level2><level3><my_fragment/></level3></level2>
Another solution (that implies a SDK extension) is to use a single operation for your action that uses your custom implementation of ro.sync.ecss.extensions.api.AuthorOperation. In this AuthorOperation you must detect the current context element and insert the corresponding fragment. Please let me know if you want to approach this solution and need more information.

Best regards,
Mihaela

Re: Insert Fragment depending of the XML tree

Posted: Mon Jan 30, 2012 6:01 pm
by Vincent
Thank you Mihaela,

That's exactly that I wanted to.

Vincent.