Programmaticlly call XML Formatter [message #215151] |
Thu, 12 June 2008 07:43  |
Eclipse User |
|
|
|
Is there any way of - through another plug-in, programmatically - doing
what WST does when you right-click an xml-file and choose:
"Source"->"Format"?
I found someone who had a similar problem quite a while ago, but since the
thread is a monologue, it isn't very helpful.
http://dev.eclipse.org/newslists/news.eclipse.webtools/msg12 104.html
I've been trying to use:
org.eclipse.wst.xml.core.internal.provisional.format.FormatP rocessorXML
I've found no other (less ...provisional) Class for this.
This does work, despite the access being discouraged, but so far only for
String input. When I run the 'formatDocument(IDocument)' method, I get a
NPE on line 138 in
org.eclipse.wst.sse.core.internal.format.AbstractStructuredF ormatProcessor
(R_2_0_2), because the structuredModel variable is null. The IDocument I
supply is from a JET transformation (and isn't null). Is there something
special expected of the supplied IDocument, or is there something I've
forgotten to init before calling the method?
Thanks in advance for any pointers whatsoever,
Anders
|
|
|
|
|
Re: Programmaticlly call XML Formatter [message #217673 is a reply to message #217408] |
Wed, 16 July 2008 00:14  |
Eclipse User |
|
|
|
Anders Olsson wrote:
> Hello Nitin, and thank you for you reply.
>
> I'm sorry for my rather late response. I did not have time to work on
> this until recently.
>
> I've tried your suggestion and now my code looks as follows:
>
> IModelManager modelManager =
> org.eclipse.wst.sse.core.StructuredModelManager.getModelMana ger();
> IStructuredModel model = modelManager.getExistingModelForEdit(xmlFile);
> IStructuredDocument document = model.getStructuredDocument();
> FormatProcessorXML fp = new FormatProcessorXML();
> fp.formatDocument(document);
>
> I have gotten this to work, but only under special conditions. It seems
> like I at least once have to have had the file opened in an editor. If I
> try to format it directly when the workbench is started, 'model' is
> null. After opening the editor it doesn't matter if I keep it open or
> not. From my tests it does not seem like the Action in "Source>Format"
> has this limitation. Is there some IStructuredModel defined, which the
> WST Action uses? In that case, can I find and use that one too?
You just need to use the same method without the "existing" part,
getModelForEdit(IFile).
--
---
Nitin Dahyabhai
Eclipse WTP Source Editing
IBM Rational
|
|
|
Powered by
FUDForum. Page generated in 0.05217 seconds