Formatting XML StructuredDocuments [message #677501] |
Wed, 08 June 2011 13:12  |
Eclipse User |
|
|
|
I am having some difficulties formatting an xml structured document, and was looking for some advice on how to resolve it. I am using Indigo RC4.
In a nutshell, I am programatically adding a new element to a document, formatting that element, and then performing a save.
Below is a fairly close representation of the code:
...
IStructuredModel model= null;
try {
...
model = StructuredModelManager.getModelManager().getModelForEdit(file);
model.beginRecording(this, "Add Some Element");
Element elem = doCreateElement();
((IDOMModel) model).getDocument().getDocumentElement()
.appendChild(reg);
FormatProcessorXML formatter = new FormatProcessorXML();
formatter.formatNode(elem);
model.endRecording(this);
...
} finally {
...
model.save();
}
The above works fine if the model is open in an xml editor. However when it is not I will usually get at an NPE in the formatter.
java.lang.NullPointerException
at org.eclipse.wst.xml.core.internal.provisional.format.NodeFormatter.getLineDelimiter(NodeFormatter.java:215)
at org.eclipse.wst.xml.core.internal.provisional.format.NodeFormatter.formatIndentationBeforeNode(NodeFormatter.java:161)
...
For some reason the IStructuredDocument is null for the newly created element:
Line 160: IStructuredDocument doc = node.getModel().getStructuredDocument();
Am I missing a step?
Thanks,
Gerry
|
|
|
|
|
|
Re: Formatting XML StructuredDocuments [message #683484 is a reply to message #681857] |
Mon, 13 June 2011 16:52   |
Eclipse User |
|
|
|
Hey Gerry,
Glad it's working!
I can't say with certainty what's causing it to be fine with the XML editor open. I've tried with the XML editor open, and I'm seeing a BadLocationException being thrown followed by an NPE in a different spot. Was the element being formatted properly when the editor was open too?
But, the text gets persisted to the structured document because once the element is added as a child, the model updater of the parent--which is different from the new element's model--is notified and knows that it needs to update the structured document's source with the generated source of the new tag. This structured document differs from the structured document associated with the ownerDocument of the new element. It looks like the flatNode associated with the new element is based on the location it is in the parent node's source, but the structured document of the new element is completely different from the parent. So location problems are likely to occur.
For example, if I created an element from an empty, unmanaged model, element.getStructuredDocument() would return an empty string. If I tried to do something like
element.getStructuredDocument().getLineInformationOfOffset(element.getStartOffset());
We'd get a bad location exception since getStartOffset() would be based off the offset of the flatNode in the populated document.
[Updated on: Mon, 13 June 2011 16:56] by Moderator
|
|
|
Re: Formatting XML StructuredDocuments [message #684507 is a reply to message #683484] |
Wed, 15 June 2011 13:22  |
Eclipse User |
|
|
|
Thanks for the explanation. I think I understand.
Nick Sandonato wrote on Mon, 13 June 2011 16:52I can't say with certainty what's causing it to be fine with the XML editor open. I've tried with the XML editor open, and I'm seeing a BadLocationException being thrown followed by an NPE in a different spot. Was the element being formatted properly when the editor was open too?
Yes. The XML was perfectly formatted and persisted when the XML editor was open. From your explanation I suspect that the model updater must have been invoked at the time of the element add, which isn't the case when the editor is closed.
|
|
|
Powered by
FUDForum. Page generated in 0.30041 seconds