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 09:34 |
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 09:54 |
Ed Merks Messages: 33108 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!!
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| |
Re: How to control indent in the progress of resource serialization? [message #1020130 is a reply to message #1020125] |
Sun, 17 March 2013 09:51 |
Ed Merks Messages: 33108 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.
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
| | | |
Goto Forum:
Current Time: Tue Mar 19 13:08:57 GMT 2024
Powered by FUDForum. Page generated in 0.02365 seconds
|