Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to control indent in the progress of resource serialization?
How to control indent in the progress of resource serialization? [message #857155] Thu, 26 April 2012 05:34 Go to next message
David BY Chan is currently offline David BY Chan
Messages: 40
Registered: July 2009
Member
Hi, guru, I create a EMF model from XSD and serialize instance to formal
XML document, it works perfectly. EMF is powerful in this way.

Now, I have a new problem, the saved XML document is often read by
Programmers, so good readability is important to this tool. I will give
an example to demonstrate this indent problem.

Assume there's an EMF class called Scheduler, it will run a SQL
statement under some conditions.

public interface Scheduler extends EObject{
...
public String getSQL();
public void setSQL(String value);
....
}

and a serialized document maybe like below:

<configuration>
<scheduler>
<sql>select 1 from dual</sql>
</scheduler>
</configuration>

If the SQL is longer than screen width, we need wrap it or split it to
several lines for good readability.

<configuration>
<scheduler>
<sql>
select 1 from dual
where a=1 and b=2 and c in(
select foo.bar from Kitty)
</sql>
</scheduler>
</configuration>

I can't simply add \n or space to SQL string, because I didn't know the
indent of node <sql>, this node is generated by EML XMLSaveImpl/XMLString.

<configuration>
XXXX<scheduler>
XXXXXXXX<sql>
XXXXXXXXXXXXselect 1 from dual
XXXXXXXXXXXXXXXXwhere a=1 and b=2 and c in(
XXXXXXXXXXXXXXXXXXXXselect foo.bar from Kitty)
XXXXXXXX</sql>
XXXX</scheduler>
</configuration>

I use "XX" to mark indent, as you see, if I know the indent of <sql>,
just plus extra SQL statement indent.

Thank for you great help!!
Re: How to control indent in the progress of resource serialization? [message #857173 is a reply to message #857155] Thu, 26 April 2012 05:54 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 25999
Registered: July 2009
Senior Member
David,

Comments below.


On 26/04/2012 11:34 AM, David BY Chan wrote:
> Hi, guru, I create a EMF model from XSD and serialize instance to
> formal XML document, it works perfectly. EMF is powerful in this way.
>
> Now, I have a new problem, the saved XML document is often read by
> Programmers, so good readability is important to this tool. I will
> give an example to demonstrate this indent problem.
>
> Assume there's an EMF class called Scheduler, it will run a SQL
> statement under some conditions.
>
> public interface Scheduler extends EObject{
> ...
> public String getSQL();
> public void setSQL(String value);
> ...
> }
>
> and a serialized document maybe like below:
>
> <configuration>
> <scheduler>
> <sql>select 1 from dual</sql>
> </scheduler>
> </configuration>
>
> If the SQL is longer than screen width, we need wrap it or split it to
> several lines for good readability.
>
> <configuration>
> <scheduler>
> <sql>
> select 1 from dual
> where a=1 and b=2 and c in(
> select foo.bar from Kitty)
> </sql>
> </scheduler>
> </configuration>
>
> I can't simply add \n or space to SQL string, because I didn't know
> the indent of node <sql>, this node is generated by EML
> XMLSaveImpl/XMLString.
I suppose you could specialize something like
XMLSaveImpl.saveDatTypeElementSingle for this specific feature. There
you have access to "doc", i.e., the XMLString, but even that doesn't
provide much in the way of public API for producing indents (which are
mostly protected things used internally for indenting the elements and
the attributes when they're broken across lines).

Probably much easier is if you assume there will be two spaces of
indentation for every level of containment nesting. So you could
format the value by counting the number of eContainer() calls it takes
to get from your Scheduler instance to the root, 2 in this example, and
add one level for the nesting implied for the element-value to end up
with 3, so you need 2*3 spaces before each line and can break them
accordingly. Of course when you read in values, the formatting is
preserved, so you'd want to do this type of thing only with values the
user edits.


>
> <configuration>
> XXXX<scheduler>
> XXXXXXXX<sql>
> XXXXXXXXXXXXselect 1 from dual
> XXXXXXXXXXXXXXXXwhere a=1 and b=2 and c in(
> XXXXXXXXXXXXXXXXXXXXselect foo.bar from Kitty)
> XXXXXXXX</sql>
> XXXX</scheduler>
> </configuration>
>
> I use "XX" to mark indent, as you see, if I know the indent of <sql>,
> just plus extra SQL statement indent.
>
> Thank for you great help!!
>
Re: How to control indent in the progress of resource serialization? [message #1020125 is a reply to message #857173] Sun, 17 March 2013 05:37 Go to previous messageGo to next message
Ravikanth Somayaji is currently offline Ravikanth Somayaji
Messages: 39
Registered: July 2009
Location: Oxford
Member
Hi Ed,

Which is the best place to check this and set the indentation?

Thanks,
Ravi

>>Probably much easier is if you assume there will be two spaces of
indentation for every level of containment nesting. So you could
format the value by counting the number of eContainer() calls it takes
to get from your Scheduler instance to the root, 2 in this example, and
add one level for the nesting implied for the element-value to end up
with 3, so you need 2*3 spaces before each line and can break them
accordingly. Of course when you read in values, the formatting is
preserved, so you'd want to do this type of thing only with values the
user edits.
Re: How to control indent in the progress of resource serialization? [message #1020130 is a reply to message #1020125] Sun, 17 March 2013 05:51 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 25999
Registered: July 2009
Senior Member
Ravikanth,

Where are the values you want to format coming from?


On 17/03/2013 10:37 AM, Ravikanth Somayaji wrote:
> Hi Ed,
>
> Which is the best place to check this and set the indentation?
>
> Thanks,
> Ravi
>
>>> Probably much easier is if you assume there will be two spaces of
> indentation for every level of containment nesting. So you could
> format the value by counting the number of eContainer() calls it takes
> to get from your Scheduler instance to the root, 2 in this example,
> and add one level for the nesting implied for the element-value to end
> up with 3, so you need 2*3 spaces before each line and can break them
> accordingly. Of course when you read in values, the formatting is
> preserved, so you'd want to do this type of thing only with values the
> user edits.
>
Re: How to control indent in the progress of resource serialization? [message #1020136 is a reply to message #1020130] Sun, 17 March 2013 06:18 Go to previous messageGo to next message
Ravikanth Somayaji is currently offline Ravikanth Somayaji
Messages: 39
Registered: July 2009
Location: Oxford
Member
Hi Ed,

I have an XML which has been created by another application like
<DocumentRoot>
<Element1>
</Element1>
</DocumentRoot>

However, when I de-serialise and serialise using XMLSaveImpl (EMF) I end up with
<DocumentRoot>
  <Element1>
  </Element1>
</DocumentRoot>


As you mentioned in your earlier comment, I'm hoping I can find the depth to the root and set these elements to indentation 0

Kindly advice

[Updated on: Sun, 17 March 2013 06:21]

Report message to a moderator

Re: How to control indent in the progress of resource serialization? [message #1020142 is a reply to message #1020136] Sun, 17 March 2013 06:32 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 25999
Registered: July 2009
Senior Member
Ravikanth,

It doesn't sound like the information about indentation is stored in the
model at all. You've have to specialize the serializer itself, e.g., in
org.eclipse.emf.ecore.xmi.impl.XMLString.getElementIndent(int).

On 17/03/2013 11:18 AM, Ravikanth Somayaji wrote:
> Hi Ed,
>
> I have an XML which has been created by another application like
>
> <DocumentRoot>
> <Element1>
> </Element1>
> </DocumentRoot>
>
> However, when I de-serialise and serialise i end up with
> <DocumentRoot>
> <Element1>
> </Element1>
> </DocumentRoot>
>
>
> As you mentioned in your earlier comment, I'm hoping I can find the
> depth to the root and set these elements to indentation 0
>
> Kindly advice
>
Re: How to control indent in the progress of resource serialization? [message #1020145 is a reply to message #1020142] Sun, 17 March 2013 06:44 Go to previous message
Ravikanth Somayaji is currently offline Ravikanth Somayaji
Messages: 39
Registered: July 2009
Location: Oxford
Member
Ed,

That's right, there is no information in the model.

I suppose XMLString is quite deep in the hierarchy. Is there any entry point XMLSave or XMLHelper where I can introspect the depth from the root, and set a flag or maybe refine the XMLString for serialisation.

Cheers,
Ravi
Previous Topic:[CDO] Text Resources are Awesome
Next Topic:createResource() returns null, what am I missing?
Goto Forum:
  


Current Time: Wed Aug 20 09:00:45 EDT 2014

Powered by FUDForum. Page generated in 0.01928 seconds