Custom plugin: adding attributes in the specified order

Post here questions and problems related to oXygen frameworks/document types.
honyk
Posts: 176
Joined: Wed Apr 29, 2009 4:55 pm

Custom plugin: adding attributes in the specified order

Post by honyk »

Dear All,

in my custom plugin action, I am trying to set several attributes, but all of them are sorted alphabetically in the result XML code. Is there any way to keep the insertion order?
For example, if my 'ulink' element has 'url' attribute and additional 'type' attribute is added, it is placed before the 'url', not after it.

I am adding attributes using this command:

Code: Select all

 authorAccess.getDocumentController().setAttribute("type", new AttrValue("variable"), uLinkNode);
Thanks, Jan
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Custom plugin: adding attributes in the specified order

Post by Radu »

Hi Jan,

The attributes order is not relevant for the semantic meaning of the XML document.
From what I think the Author mode should kind of preserve by default the order in which the attributes were added.
Is it possible that in the Preferences->"Editor / Format / XML" page you have checked the "Sort attributes" checkbox?

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
honyk
Posts: 176
Joined: Wed Apr 29, 2009 4:55 pm

Re: Custom plugin: adding attributes in the specified order

Post by honyk »

That 'Sort attributes' option is disabled. I agree the order is just a cosmetic issue. My action is a kind of shortcut to common manual action. Such side-effect breaks the consistency :-(
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Custom plugin: adding attributes in the specified order

Post by Radu »

Hi,

So I made this operation to experiment a bit:

Code: Select all

  public void doOperation(AuthorAccess authorAccess, ArgumentsMap args)
      throws AuthorOperationException {
    try {
      AuthorElement elem = (AuthorElement) authorAccess.getDocumentController().getNodeAtOffset(authorAccess.getEditorAccess().getCaretOffset());
      authorAccess.getDocumentController().setAttribute("zz", new AttrValue("val1"), elem);
      authorAccess.getDocumentController().setAttribute("yy", new AttrValue("val2"), elem);
      authorAccess.getDocumentController().setAttribute("aa", new AttrValue("val3"), elem);
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
  }
I invoked it on an element in the Author mode and the XML in the Text editing mode looks like this:

Code: Select all

<body zz="val1" yy="val2" aa="val3"/>
So the order in which you add the attributes is obeyed at serialization.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
honyk
Posts: 176
Joined: Wed Apr 29, 2009 4:55 pm

Re: Custom plugin: adding attributes in the specified order

Post by honyk »

You are correct! During my experiments, I haven't switched to Text mode and now I can see it is correct there all the time.
I've finally found a dedicated option for this behavior: Preferences...|Editor|Edit Modes|Author: Sort attributes alphabetically for "Full Tags with Attributes" which is my default mode. When unchecked, I have the correct order also in Author mode.

Thanks a lot!
Radu
Posts: 9059
Joined: Fri Jul 09, 2004 5:18 pm

Re: Custom plugin: adding attributes in the specified order

Post by Radu »

Hi Jan,
Thanks for posting the solution, I somehow forgot about this extra setting.
Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
Post Reply