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