Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » How to best open a schema-backed XML model for an external file without UI?(XML model access)
How to best open a schema-backed XML model for an external file without UI? [message #520836] Mon, 15 March 2010 14:37 Go to next message
Walter Brunauer is currently offline Walter Brunauer
Messages: 6
Registered: July 2009
Junior Member
Hi there,

please ignore my ignorance, but I can't find an obvious way to open a schema-backed XML model for an external file without any UI plug-ins necessarily being available (this IMO removes the choice for the IStructuredDocument API, because its provided by StructuredTextEditor).

My best choice currently is IModelManager.getModelForEdit(String, InputStream, URIResolver), but at least the last parameter is obsoleted already.

Is this really the best match, assuming this API is going to last? Or are there better choices? Note, that I would need to get the same model instance as StructuredTextEditor gets for the external file via the IStructuredDocument API, as I need to be in sync with it.

Any help highly appreciated!

Thanks a lot in advance,

Walter Brunauer
Re: How to best open a schema-backed XML model for an external file without UI? [message #520978 is a reply to message #520836] Tue, 16 March 2010 02:43 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2314
Registered: July 2009
Senior Member
On 3/15/2010 10:37 AM, Walter Brunauer wrote:
> please ignore my ignorance, but I can't find an obvious way to open a
> schema-backed XML model for an external file without any UI plug-ins
> necessarily being available (this IMO removes the choice for the
> IStructuredDocument API, because its provided by StructuredTextEditor).
>
> My best choice currently is IModelManager.getModelForEdit(String,
> InputStream, URIResolver), but at least the last parameter is obsoleted
> already.
>
> Is this really the best match, assuming this API is going to last? Or
> are there better choices? Note, that I would need to get the same model
> instance as StructuredTextEditor gets for the external file via the
> IStructuredDocument API, as I need to be in sync with it.
>
> Any help highly appreciated!
>
> Thanks a lot in advance,

It's not ignorance nor surprising considering that interface is nearing
a decade of age and predates the platform's headless means of doing
this: File Buffers (in the org.eclipse.core.filebuffers bundle). The
SSE Model Manager was updated shortly before WTP came into existence to
make use of File Buffers, as that mechanism supports files in and
outside of the workspace, hence the deprecation of many of the access
methods. The correct flow is to connect to a text file buffer using
org.eclipse.core.filebuffers.IFileBufferManager#connect(IPat h,
LocationKind.LOCATION, IProgressMonitor), get the ITextFileBuffer for
the same path and location kind using
org.eclipse.core.filebuffers.IFileBufferManager#getFileBuffe r(IPath,
LocationKind), and then use the text document with
org.eclipse.wst.sse.core.internal.provisional.IModelManager# getModelFor[Read|Edit](IStructuredDocument)--type
casting is required in this case. This is actually what the
StructuredTextEditor does (in
org.eclipse.wst.sse.ui.StructuredTextEditor#doSetInput(IEdit orInput) if
that's useful) except the file buffer is handled by the
TextFileDocumentProvider. The underlying infrastructure supports the
same for files in the workspace, but you can still use the older
shortcuts on IModelManager that deal with IFiles directly. Don't forget
to release the model when you're done with it and disconnect from the
file buffer after that. This is as permanent an interface as we can
make it, short of moving it into a correctly public package and removing
everything that's deprecated--which would cause massive binary breakage,
so far stopping us from doing so even though we begin almost every
release with the intention of finally cleaning it up. That and I
haven't come up with a way to let you cleanly share models created from
a stream through a consistent API while having only the first client
supply the input stream. It's a bit of a conundrum.

--

Nitin Dahyabhai
Eclipse WTP Source Editing
IBM Rational


---
Nitin Dahyabhai
Eclipse WTP, IBM
Re: How to best open a schema-backed XML model for an external file without UI? [message #521488 is a reply to message #520978] Wed, 17 March 2010 18:24 Go to previous messageGo to next message
Walter Brunauer is currently offline Walter Brunauer
Messages: 6
Registered: July 2009
Junior Member
Thank you for your quick response. I guess I understand the underlying concept much better now.

Ok, so I did as you told me, it works fine and I get the very same model instance as the StructuredTextEditor, as long as the path is representing an IFile. See the simplified code snippet below how I get my model:

IDOMModel getModel(IPath path, IFile file) {
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
LocationKind kind = LocationKind.LOCATION;
if(file != null) {
kind = LocationKind.IFILE;
}
bufferManager.connect(path, kind, new NullProgressMonitor());
ITextFileBuffer buffer = bufferManager.getTextFileBuffer(path, kind);
IDocument document = buffer.getDocument();
IModelManager modelManager = StructuredModelManager.getModelManager();
IStructuredModel model = modelManager.getModelForEdit((IStructuredDocument)document);
return (IDOMModel)model;
}

But if the path is representing an external location (file == null), the StructuredTextEditor gets the following backtrace on trying to do auto-completion:

java.lang.IllegalStateException: problem getting model
at org.eclipse.wst.xml.ui.internal.contentassist.AbstractConten tAssistProcessor.computeCompletionProposals(AbstractContentA ssistProcessor.java:1388)
at org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssi stProcessor.computeCompletionProposals(XMLContentAssistProce ssor.java:120)
at org.eclipse.wst.sse.ui.internal.contentassist.CompoundConten tAssistProcessor.computeCompletionProposals(CompoundContentA ssistProcessor.java:303)
<snip>

This IMHO is because the AbstractContentAssistProcessor always uses IModelManager.getExistingModelForRead(IDocument) directly, which assumes the document really needs to be the very same instance, and not only one fulfilling the equal() clauses.

Once again, I cannot use any UI classes (like the StructuredTextEditor itself does with TextFileDocumentProvider.getDocument(Object)) to get the same document, as I need the model also in a headless application.

Any idea how to overcome this?

Thanks again,

Walter
Re: How to best open a schema-backed XML model for an external file without UI? [message #521815 is a reply to message #521488] Thu, 18 March 2010 19:20 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2314
Registered: July 2009
Senior Member
#getExistingModelForRead() doesn't cause a model to be created around the structured document, it only returns a model when one already exists (think of it as a performance tweak in a sense). The #getModelForEdit() call that the editor class makes brings the model into existence for the lifetime of the editor (or rather it's use of that input), so the content assist succeeds. A #.equals() really isn't appropriate for this scenario since we want the model holding the exact document object.

So if this NPE is happening, who made sure that the model was created beforehand? And how is it that with a headless requirement you're running into an NPE from a UI class?


---
Nitin Dahyabhai
Eclipse WTP, IBM
Re: How to best open a schema-backed XML model for an external file without UI? [message #521957 is a reply to message #521815] Fri, 19 March 2010 12:54 Go to previous message
Walter Brunauer is currently offline Walter Brunauer
Messages: 6
Registered: July 2009
Junior Member
Hm, sorry for the confusion - I guess my follow-up question was not clear enough. In my code, I need to open models of external files independent of (also for non-UI applications) and other times before StructuredTextEditor does. And my point was, that TextFileDocumentProvider.getDocument(Object) initialized its cache with its own mechanism in TextFileDocumentProvider.createFileInfo(Object).

Having said this, I got it working now with the following change in my implementation, and thought it might be interesting for others to know as well:

IDOMModel getModel(IPath path, IFile file) {
ITextFileBufferManager bufferManager = FileBuffers.getTextFileBufferManager();
ITextFileBuffer buffer = null;
if(file != null) {
LocationKind kind = LocationKind.IFILE;
bufferManager.connect(path, kind, new NullProgressMonitor());
buffer = bufferManager.getTextFileBuffer(path, kind);
} else {
IFileStore fileStore = EFS.getStore(path.toFile().toURI());
bufferManager.connectFileStore(fileStore, new NullProgressMonitor());
buffer = bufferManager.getFileStoreTextFileBuffer(fileStore);
}
IDocument document = buffer.getDocument();
IModelManager modelManager = StructuredModelManager.getModelManager();
IStructuredModel model = modelManager.getModelForEdit((IStructuredDocument)document);
return (IDOMModel)model;
}

Note, that I now handle external files the same way as TextFileDocumentProvider does, and use TextFileBufferManager.connectFileStore(IFileStore, IProgressMonitor) to register the external file.

Anyway, thanks for your help,

Smile

Walter
Previous Topic:How to deploy files as well as folders from WebContent directory?
Next Topic:Multiple webapps from one Dynamic Web Project ?
Goto Forum:
  


Current Time: Fri Oct 31 05:11:07 GMT 2014

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

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