Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » Formatting XML StructuredDocuments(NPE while formatting a newly created element outside of an editor)
Formatting XML StructuredDocuments [message #677501] Wed, 08 June 2011 17:12 Go to next message
Gerry Kessler is currently offline Gerry Kessler
Messages: 125
Registered: July 2009
Senior Member
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 #677509 is a reply to message #677501] Wed, 08 June 2011 17:23 Go to previous messageGo to next message
Gerry Kessler is currently offline Gerry Kessler
Messages: 125
Registered: July 2009
Senior Member
Small change to code snippet:
    ...
    Element elem = doCreateElement();
    ((IDOMModel) model).getDocument().getDocumentElement()
		.appendChild(elem);

    ...

Re: Formatting XML StructuredDocuments [message #681755 is a reply to message #677509] Thu, 09 June 2011 19:29 Go to previous messageGo to next message
Nick Sandonato is currently offline Nick Sandonato
Messages: 126
Registered: July 2009
Senior Member
Hi Gerry,

What's happening in doCreateElement()? Is it creating an Element from a different Document than one that's associated with model? My guess is if that were the case, that the Document associated with elem is different than the one associated with model even after the appendChild, which would also cause the structured document for elem to be different (and potentially null). It looks like we're not throwing the correct DOMException.WRONG_DOCUMENT_ERR if that is the case.

[Updated on: Thu, 09 June 2011 19:41]

Report message to a moderator

Re: Formatting XML StructuredDocuments [message #681857 is a reply to message #681755] Fri, 10 June 2011 00:36 Go to previous messageGo to next message
Gerry Kessler is currently offline Gerry Kessler
Messages: 125
Registered: July 2009
Senior Member
Hi Nick,

Thanks for the clue, and I now have it working. I had simplified my code snippet since the "doCreateElement()" called into some code that was going to be hard to explain. When I looked closely, I noticed that there was a different instance of the document than the one I added it to in the snippet. Maybe it should have thrown the exception, as you say.

If it isn't too much trouble, can you explain to me why all is fine if the XML editor is open at the time of the call, or why the new element when no editor is open, although unformatted, gets persisted ?

Thanks a bunch
Re: Formatting XML StructuredDocuments [message #683484 is a reply to message #681857] Mon, 13 June 2011 20:52 Go to previous messageGo to next message
Nick Sandonato is currently offline Nick Sandonato
Messages: 126
Registered: July 2009
Senior Member
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 20:56]

Report message to a moderator

Re: Formatting XML StructuredDocuments [message #684507 is a reply to message #683484] Wed, 15 June 2011 17:22 Go to previous message
Gerry Kessler is currently offline Gerry Kessler
Messages: 125
Registered: July 2009
Senior Member
Thanks for the explanation. I think I understand.

Nick Sandonato wrote on Mon, 13 June 2011 16:52
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?


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.
Previous Topic:Cannot shut down Apache Tomcat 6.0
Next Topic:Multiple sourceElementParsers extensions per project
Goto Forum:
  


Current Time: Mon Oct 20 09:55:17 GMT 2014

Powered by FUDForum. Page generated in 0.04761 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software