Serializing AuthorNode to String

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

Serializing AuthorNode to String

Post by honyk »

Dear All,
I am trying to convert AuthorNode at caret position to String so it could be, after some tweaking, pasted instead of that original element. But I have difficulties iterating the element children in case of mixed content as 'getContentNodes()' method returns just elements, not text nodes.

Code: Select all

    private static String getXmlFragment(AuthorNode authorNode) throws BadLocationException {

        StringBuilder xmlFragment = new StringBuilder();
        switch (authorNode.getType()) {
            case AuthorNode.NODE_TYPE_TEXT : {
                xmlFragment.append(authorNode.getTextContent());
                break;
            }
            case AuthorNode.NODE_TYPE_ELEMENT: {
                d docbookElement = (d) authorNode;
                xmlFragment.append("<" + docbookElement.getName());
                for (int i = 0; i < docbookElement.getAttributesCount(); i++) {
                    String attr = docbookElement.getAttributeAtIndex(i);
                    if (!attr.startsWith("xmlns")) {
                        xmlFragment.append(" " + attr + "=\"" + docbookElement.getAttribute(attr).getValue() + "\"");
                    }
                }
                xmlFragment.append(">");

                for (AuthorNode contentNode : docbookElement.getContentNodes()) {
                    xmlFragment.append(getXmlFragment(contentNode));
                }
            /*
                ContentIterator ci = docbookElement.getContentIterator();
                while (ci.hasNext()) {
                    xmlFragment.append(ci.next());
                }
            */
                xmlFragment.append("</" + authorNode.getName() + ">");
            }
        }

        return xmlFragment.toString();
    }
Any idea how to convert the content properly?
Thanks, Jan
Radu
Posts: 9049
Joined: Fri Jul 09, 2004 5:18 pm

Re: Serializing AuthorNode to String

Post by Radu »

Hi Jan,

We have specific API for what you need:
https://www.oxygenxml.com/InstData/Edit ... tFragment-

Coming back to your remarks, the Author Nodes structure looks like the one in the image here:
https://www.oxygenxml.com/InstData/Edit ... gment.html
Indeed there are only element nodes, no text nodes. Each node points using the "getStartOffset()" and "getEndOffset()" methods to a large content string containing all text characters and special characters for the node start/end offsets. And there are methods like "AuthorDocumentController.getChars(int, int, Segment)" which allow you to access text between two offsets.

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

Re: Serializing AuthorNode to String

Post by honyk »

Cool, I was not aware of that method. In my case I use AuthorNode as an Input:

Code: Select all

AuthorDocumentFragment uLinkFragment = documentController.createDocumentFragment(uLinkElement, true);
String uLinkXmlFragment = documentController.serializeFragmentToXML(uLinkFragment);
// modifying the fragment 
documentController.deleteNode(uLinkElement);
documentController.insertXMLFragment(uLinkXmlFragment, authorAccess.getEditorAccess().getCaretOffset());
It works, but while the original fragment contains attributes in the desired order, the insertXMLFragment method is causing reordering them alphabetically...
honyk
Posts: 176
Joined: Wed Apr 29, 2009 4:55 pm

Re: Serializing AuthorNode to String

Post by honyk »

That alphabetical sorting is only in the Author mode, not in the XML source. It is caused by the settings, which can be disabled in Preferences...|Editor|Edit Modes|Author: Sort attributes alphabetically for "Full Tags with Attributes".
Post Reply