Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » Programmaticlly call XML Formatter
Programmaticlly call XML Formatter [message #215151] Thu, 12 June 2008 11:43 Go to next message
Anders Olsson is currently offline Anders Olsson
Messages: 21
Registered: July 2009
Junior Member
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 #215343 is a reply to message #215151] Fri, 13 June 2008 20:22 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2307
Registered: July 2009
Senior Member
You'll want to obtain the SSE Model Manager instance using
org.eclipse.wst.sse.core.StructuredModelManager.getModelMana ger()
and use it to obtain a model for the IFile or stream you're
interested in formatting. The model's structured document
(org.eclipse.wst.sse.core.internal.provisional.IStructuredMo del.getStructuredDocument())
is the IDocument that the processor expects.

--
---
Nitin Dahyabhai
Eclipse WTP Source Editing
IBM Rational


---
Nitin Dahyabhai
Eclipse WTP, IBM
Re: Programmaticlly call XML Formatter [message #217408 is a reply to message #215343] Fri, 11 July 2008 13:53 Go to previous messageGo to next message
Anders Olsson is currently offline Anders Olsson
Messages: 21
Registered: July 2009
Junior Member
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?

Regards,

Anders
Re: Programmaticlly call XML Formatter [message #217673 is a reply to message #217408] Wed, 16 July 2008 04:14 Go to previous message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2307
Registered: July 2009
Senior Member
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


---
Nitin Dahyabhai
Eclipse WTP, IBM
Previous Topic:Import web site into eclipse
Next Topic:WTP Tests souce code
Goto Forum:
  


Current Time: Mon Oct 20 04:40:20 GMT 2014

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

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