-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: DOM4J Mode Problem preserving whitespace in strings
PostPosted: Thu Jul 12, 2007 6:02 am 
Newbie

Joined: Mon Sep 04, 2006 9:03 am
Posts: 9
Hibernate version:
3.2.4.sp1

Mapping documents:
Code:
<hibernate-mapping>
    <class name="de.uni_leipzig.lots.common.objects.News" table="news" node="news">         
        <id name="id" column="id" type="long" unsaved-value="null" node="@id">   
            <generator class="increment"/>
        </id>           
        <property name="headline" length="255" not-null="true"/>           
        <property name="message" type="text" not-null="true"/>       
    </class>   
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():
Code:
Session dom4jSession = session.getSession(EntityMode.DOM4J);
Element xmlNews = dom4jSession.get(News.class, id);
StringWriter writer = new StringWriter();
XMLWriter xmlWriter = new XMLWriter(writer, DOM4JHelper.getPrettyPrintOutputFormat());
xmlWriter.write(xmlNews);
xmlWriter.close();


Full stack trace of any exception that occurs:
--

Name and version of the database you are using:
--

The generated SQL (show_sql=true):
--

Debug level Hibernate log excerpt:
--

My problem is, that whitespaces in the message string of the news are not preserved in the XML output. I use DOM4J as backup/restore of all my entities. The last unsolved problem is that whitespaces are not preserved.
With whitespaces I mean line breaks and normal spaces. For some strings of my entities it is important to preserve those.

What do I get as XML?
Code:
<news id="1">
    <headline>Testnews</headline>
    <message>First line Second line Third line</message>
</news>


What do I want?
Code:
<news id="1">
    <headline>Testnews</headline>
    <message><![CDATA[First line
Second line
Third line]]></message>
</news>
or
Code:
<news id="1">
    <headline>Testnews</headline>
    <message xml:space="preserve">First line
Second line
Third line</message>
</news>


Hibernate produces a org.dom4j.tree.DefaultText node for the message text which includes the string "First line\nSecond line\nThird line" which is right. But the XMLWriter from dom4j trims this line breaks, because the text node is no CDATA and the sourrounding <message> node does not have the xml:space="preserve" attribute.

So hibernate should add the xml:space="preserve" attribute to the <message> node or generate a CDATA text node if I want preserve whitespaces for a particular property. So we need a possibility to declare a property to be space preserving in DOM4J mode.

What do you think about this? Should a file a feature request?

Thanks

Alexander Kiel
Univeristy of Leipzig, Germany


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 12, 2007 7:09 am 
Newbie

Joined: Mon Sep 04, 2006 9:03 am
Posts: 9
I did a step-by-step debug run through the Hibernate code. I think org.hibernate.type.Type#setToXMLNode(Node, Object, SessionFactoryImplementor) should do the magic.

In my case I use the type "text" for my message property. The TextType uses the setToXMLNode implementation from NullableType which reads:

Code:
public void setToXMLNode(Node xml, Object value, SessionFactoryImplementor factory)
throws HibernateException {
    xml.setText( toXMLString(value, factory) );
}


I can think about a type which adds first a CDATA node to the xml node and than the string as text to the CDATA node or the type can add a "xml:space="preserve" attribute to the xml node.

Even the implementation of the ClobType does not do anything to preserve whitespaces:

Code:
public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) {
   if (value!=null) {
      Clob clob = (Clob) value;
      try {
         int len = (int) clob.length();
         node.setText( clob.getSubString(0, len) );
      }
      catch (SQLException sqle) {
         throw new HibernateException("could not read XML from Clob", sqle);
      }
   }
}


And (a little bit off topic) the BinaryType did not use Base64 to output to XML. Instead it uses Hex Strings. Base64 should be preferred. And even worse the BlobType throws a UnsupportedOperationException("todo").

So I will try to implement a Type which preserves spaces in XML. It will be a little bit difficult, because it seems I can't do it with a UserType.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.