[jboss-svn-commits] JBoss Common SVN: r2419 - in jbossxb/trunk/src: test/java/org/jboss/test/xml and 1 other directory.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Jun 19 08:42:59 EDT 2007
Author: alex.loubyansky at jboss.com
Date: 2007-06-19 08:42:59 -0400 (Tue, 19 Jun 2007)
New Revision: 2419
Modified:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/ObjectModelBuilder.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallingContext.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/IgnorableWhitespaceUnitTestCase.java
Log:
JBXB-103
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/ObjectModelBuilder.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/ObjectModelBuilder.java 2007-06-14 14:38:00 UTC (rev 2418)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/ObjectModelBuilder.java 2007-06-19 12:42:59 UTC (rev 2419)
@@ -96,8 +96,9 @@
*/
private final NamespaceRegistry nsRegistry = new NamespaceRegistry();
- private XSTypeDefinition currentType;
-
+ // whether text content should be trimmed before it is set
+ private boolean trimTextContent = true; // for backwards compatibility
+
private boolean trace = log.isTraceEnabled();
// Public
@@ -164,6 +165,16 @@
return nsRegistry;
}
+ public boolean isTrimTextContent()
+ {
+ return trimTextContent;
+ }
+
+ public void setTrimTextContent(boolean trimTextContent)
+ {
+ this.trimTextContent = trimTextContent;
+ }
+
/**
* Construct a QName from a value
*
@@ -196,7 +207,7 @@
public XSTypeDefinition getType()
{
- return currentType;
+ return null;
}
// Public
@@ -330,9 +341,6 @@
{
Object parent = accepted.isEmpty() ? root : peekAccepted();
- // todo currentType assignment
- currentType = type;
-
Object element;
if(!namespaceURI.equals(curNsSwitchingFactory))
{
@@ -385,11 +393,18 @@
Object acceptedElement = peekAccepted();
if(element.characters != null && element.characters.length() > 0)
{
- String characters = element.characters.toString().trim();
- if(characters.length() > 0)
+ if(trimTextContent)
{
- curFactory.setValue(acceptedElement, this, namespaceURI, localName, characters);
+ String characters = element.characters.toString().trim();
+ if (characters.length() > 0)
+ {
+ curFactory.setValue(acceptedElement, this, namespaceURI, localName, characters);
+ }
}
+ else
+ {
+ curFactory.setValue(acceptedElement, this, namespaceURI, localName, element.characters.toString());
+ }
}
}
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallingContext.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallingContext.java 2007-06-14 14:38:00 UTC (rev 2418)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/UnmarshallingContext.java 2007-06-19 12:42:59 UTC (rev 2419)
@@ -51,7 +51,19 @@
*/
NamespaceContext getNamespaceContext();
+ /**
+ * @return true if the text content passed to the setValue(...) method
+ * is automatically trimmed (the default).
+ */
+ boolean isTrimTextContent();
+
/**
+ * Should the text content be automatically trimmed before setValue(...) is called.
+ * @param trimTextContent
+ */
+ void setTrimTextContent(boolean trimTextContent);
+
+ /**
* Returns child's content.
* todo consider deprecating this method
* @param namespaceURI
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/IgnorableWhitespaceUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/IgnorableWhitespaceUnitTestCase.java 2007-06-14 14:38:00 UTC (rev 2418)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/IgnorableWhitespaceUnitTestCase.java 2007-06-19 12:42:59 UTC (rev 2419)
@@ -22,16 +22,22 @@
package org.jboss.test.xml;
import java.io.StringReader;
+import java.util.ArrayList;
import java.util.List;
import javax.xml.namespace.QName;
import junit.framework.TestSuite;
+import org.jboss.xb.binding.ObjectModelFactory;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.UnmarshallerFactory;
+import org.jboss.xb.binding.UnmarshallingContext;
import org.jboss.xb.binding.metadata.ClassMetaData;
import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
+import org.xml.sax.Attributes;
/**
@@ -95,10 +101,53 @@
assertNotNull(top.string);
assertEquals(2, top.string.size());
assertEquals(" ", top.string.get(0));
+ assertEquals("\n newline, 6 spaces, newline, 3 spaces\n ", top.string.get(1));
+ }
+ public void testObjectModelFactory() throws Exception
+ {
+ String url = findXML("IgnorableWhitespaceContent.xml");
+
+ ObjectModelFactory omf = new OMF();
+
+ Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
+ Object o = unmarshaller.unmarshal(url, omf, null);
+
+ assertNotNull(o);
+ assertTrue(o instanceof Top);
+ Top top = (Top) o;
+ assertEquals(2, top.string.size());
+ assertEquals(" ", top.string.get(0));
assertEquals("\n newline, 6 spaces, newline, 3 spaces\n ", top.string.get(1));
+
}
+ public static final class OMF implements ObjectModelFactory
+ {
+ public Object completeRoot(Object root, UnmarshallingContext ctx, String namespaceURI, String localName)
+ {
+ return root;
+ }
+
+ public Object newRoot(Object root, UnmarshallingContext ctx, String namespaceURI, String localName, Attributes attrs)
+ {
+ ctx.setTrimTextContent(false);
+ return new Top();
+ }
+
+ public void setValue(Top top, UnmarshallingContext ctx, String ns, String name, String value)
+ {
+ if(name.equals("string"))
+ {
+ if(top.string == null)
+ {
+ top.string = new ArrayList();
+ }
+ top.string.add(value);
+ }
+ }
+ }
+
public static class Top
{
public List string;
More information about the jboss-svn-commits
mailing list