[jboss-svn-commits] JBoss Common SVN: r2417 - in jbossxb/trunk/src: main/java/org/jboss/xb/binding/sunday/unmarshalling and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Thu Jun 14 06:44:32 EDT 2007
Author: alex.loubyansky at jboss.com
Date: 2007-06-14 06:44:31 -0400 (Thu, 14 Jun 2007)
New Revision: 2417
Added:
jbossxb/trunk/src/test/java/org/jboss/test/xml/IgnorableWhitespaceUnitTestCase.java
jbossxb/trunk/src/test/resources/org/jboss/test/xml/IgnorableWhitespaceContent.xml
Modified:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/sax/SaxJBossXBParser.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaBinding.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java
Log:
JBXB-103
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/sax/SaxJBossXBParser.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/sax/SaxJBossXBParser.java 2007-06-14 00:27:11 UTC (rev 2416)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/parser/sax/SaxJBossXBParser.java 2007-06-14 10:44:31 UTC (rev 2417)
@@ -310,15 +310,15 @@
{
// todo look at this later
// do not notify content handler if these are just whitespaces
- int i = start;
- while(i < start + length)
- {
- if(!Character.isWhitespace(ch[i++]))
- {
+ //int i = start;
+ //while(i < start + length)
+ //{
+ // if(!Character.isWhitespace(ch[i++]))
+ // {
contentHandler.characters(ch, start, length);
- break;
- }
- }
+ // break;
+ // }
+ //}
}
public void ignorableWhitespace(char ch[], int start, int length)
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaBinding.java 2007-06-14 00:27:11 UTC (rev 2416)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SchemaBinding.java 2007-06-14 10:44:31 UTC (rev 2417)
@@ -85,6 +85,11 @@
private boolean useNoArgCtorIfFound;
/** The default property name to use for simple content bindings */
private String simpleContentProperty = "value";
+
+ /** if all the characters in the mixed content are whitespaces
+ * should they be considered indentation and ignored?
+ * the default is true for the backwards compatibility */
+ private boolean ignoreWhitespacesInMixedContent = true;
/** default XOP unmarshaller */
private XOPUnmarshaller xopUnmarshaller;
@@ -445,6 +450,16 @@
&& wildcard.getUnresolvedMarshaller() instanceof DomLocalMarshaller;
}
+ public boolean isIgnoreWhitespacesInMixedContent()
+ {
+ return ignoreWhitespacesInMixedContent;
+ }
+
+ public void setIgnoreWhitespacesInMixedContent(boolean value)
+ {
+ this.ignoreWhitespacesInMixedContent = value;
+ }
+
void addElementParticle(ParticleBinding particle)
{
ElementBinding element = (ElementBinding)particle.getTerm();
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java 2007-06-14 00:27:11 UTC (rev 2416)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java 2007-06-14 10:44:31 UTC (rev 2417)
@@ -89,10 +89,96 @@
public void characters(char[] ch, int start, int length)
{
StackItem stackItem = stack.peek();
- if(stackItem.cursor == null)
+ if(stackItem.cursor != null)
{
- if(stackItem.textContent == null)
+ return;
+ }
+
+ ElementBinding e = (ElementBinding) stackItem.particle.getTerm();
+/* if(!stackItem.ended && e.getType().isTextContentAllowed())
+ {
+ int i = start;
+ while (i < start + length)
{
+ if(ch[i] == 0x0a)
+ {
+ stackItem.indentation = true;
+ }
+ else
+ {
+ if (ch[i] == ' ' || ch[i] == 0x0d)
+ {
+ }
+ else
+ {
+ stackItem.indentation = false;
+ break;
+ }
+ }
+ ++i;
+ }
+
+ if(!stackItem.indentation)
+ {
+ if (stackItem.textContent == null)
+ {
+ stackItem.textContent = new StringBuffer();
+ }
+ stackItem.textContent.append(ch, start, length);
+ }
+ }
+*/
+ // if current is ended the characters belong to its parent
+ if(stackItem.ended)
+ {
+ int i = 0;
+ do
+ {
+ stackItem = stack.peek(++i);
+ }
+ while(stackItem.cursor != null && i < stack.size());
+
+ e = (ElementBinding) stackItem.particle.getTerm();
+ }
+
+ // collect characters only if they are allowed content
+ if(e.getType().isTextContentAllowed())
+ {
+ if(stackItem.indentation != Boolean.FALSE)
+ {
+ if(e.getType().isSimple())
+ {
+ // simple content is not analyzed
+ stackItem.indentation = Boolean.FALSE;
+ stackItem.ignorableCharacters = false;
+ }
+ else if(e.getSchema() != null && !e.getSchema().isIgnoreWhitespacesInMixedContent())
+ {
+ stackItem.indentation = Boolean.FALSE;
+ stackItem.ignorableCharacters = false;
+ }
+ else
+ {
+ // the indentation is currently defined as whitespaces with next line characters
+ // this should probably be externalized in the form of a filter or something
+ for (int i = start; i < start + length; ++i)
+ {
+ if(ch[i] == 0x0a)
+ {
+ stackItem.indentation = Boolean.TRUE;
+ }
+ else if (!Character.isWhitespace(ch[i]))
+ {
+ stackItem.indentation = Boolean.FALSE;
+ stackItem.ignorableCharacters = false;
+ break;
+ }
+ }
+ }
+ }
+
+ if (stackItem.textContent == null)
+ {
stackItem.textContent = new StringBuffer();
}
stackItem.textContent.append(ch, start, length);
@@ -261,7 +347,8 @@
ElementBinding parentElement = (ElementBinding) item.particle.getTerm();
parentElement.setXopUnmarshaller(schema.getXopUnmarshaller());
-
+
+ flushIgnorableCharacters();
item.handler = DefaultHandlers.XOP_HANDLER;
item.ignoreCharacters = true;
item.o = item.handler.startParticle(stack.peek().o, startName, stack.peek().particle, null, nsRegistry);
@@ -287,6 +374,8 @@
}
else
{
+ flushIgnorableCharacters();
+
Object o = item.o;
// push all except the last one
for(int i = newCursors.size() - 1; i >= 0; --i)
@@ -801,6 +890,26 @@
// Private
+ private void flushIgnorableCharacters()
+ {
+ StackItem stackItem = stack.peek();
+ if(stackItem.cursor != null || stackItem.textContent == null)
+ {
+ return;
+ }
+
+ if(stackItem.indentation == Boolean.TRUE || stackItem.ignorableCharacters)
+ {
+ if(log.isTraceEnabled())
+ {
+ log.trace("ignored characters: " + ((ElementBinding) stackItem.particle.getTerm()).getQName() + " '"
+ + stackItem.textContent + "'");
+ }
+ stackItem.textContent = null;
+ stackItem.indentation = null;
+ }
+ }
+
private ParticleBinding getParentParticle()
{
ListIterator iter = stack.prevIterator();
@@ -839,6 +948,8 @@
// characters
//
+ flushIgnorableCharacters();
+
TypeBinding charType = type.getSimpleType();
if(charType == null)
{
@@ -1191,6 +1302,8 @@
Object o;
ValueList repeatableParticleValue;
StringBuffer textContent;
+ Boolean indentation;
+ boolean ignorableCharacters = true;
boolean ended;
public StackItem(ModelGroupBinding.Cursor cursor)
@@ -1226,6 +1339,9 @@
{
textContent.delete(0, textContent.length());
}
+
+ indentation = null;
+ ignorableCharacters = true;
}
}
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java 2007-06-14 00:27:11 UTC (rev 2416)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java 2007-06-14 10:44:31 UTC (rev 2417)
@@ -416,6 +416,11 @@
this.simple = simple ? Boolean.TRUE : Boolean.FALSE;
}
+ public boolean isTextContentAllowed()
+ {
+ return simpleType != null || isSimple();
+ }
+
public ClassMetaData getClassMetaData()
{
return classMetaData;
Added: jbossxb/trunk/src/test/java/org/jboss/test/xml/IgnorableWhitespaceUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/IgnorableWhitespaceUnitTestCase.java (rev 0)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/IgnorableWhitespaceUnitTestCase.java 2007-06-14 10:44:31 UTC (rev 2417)
@@ -0,0 +1,104 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, Red Hat Middleware LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.test.xml;
+
+import java.io.StringReader;
+import java.util.List;
+
+import javax.xml.namespace.QName;
+
+import junit.framework.TestSuite;
+
+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;
+
+
+/**
+ * IgnorableWhitespaceUnitTestCase.
+ *
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision$
+ */
+public class IgnorableWhitespaceUnitTestCase extends AbstractJBossXBTest
+{
+ private static final String NS = "http://www.jboss.org/test/xml/simpleContent";
+
+ private static final String XSD =
+ "<?xml version='1.0' encoding='UTF-8'?>" +
+ "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'" +
+ " targetNamespace='http://www.jboss.org/test/xml/simpleContent'" +
+ " xmlns='http://www.jboss.org/test/xml/simpleContent'" +
+ " elementFormDefault='qualified'" +
+ " attributeFormDefault='unqualified'" +
+ " version='1.0'>" +
+ " <xsd:element name='top'>" +
+ " <xsd:complexType>" +
+ " <xsd:sequence>" +
+ " <xsd:element name='string' type='myString' minOccurs='0' maxOccurs='unbounded'/>" +
+ " </xsd:sequence>" +
+ " </xsd:complexType>" +
+ " </xsd:element>" +
+ " <xsd:complexType name='myString'>" +
+ " <xsd:simpleContent>" +
+ " <xsd:extension base='xsd:string'>" +
+ " <xsd:attribute name='id' type='xsd:ID'/>" +
+ " </xsd:extension>" +
+ " </xsd:simpleContent>" +
+ " </xsd:complexType>" +
+ "</xsd:schema>";
+
+ public static final TestSuite suite()
+ {
+ return new TestSuite(IgnorableWhitespaceUnitTestCase.class);
+ }
+
+ public IgnorableWhitespaceUnitTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testCollectionOverrideProperty() throws Exception
+ {
+ SchemaBinding schema = XsdBinder.bind(new StringReader(XSD), null);
+
+ schema.setIgnoreUnresolvedFieldOrClass(false);
+ schema.setIgnoreWhitespacesInMixedContent(false);
+
+ ClassMetaData classMetaData = new ClassMetaData();
+ classMetaData.setImpl(Top.class.getName());
+ ElementBinding element = schema.getElement(new QName(NS, "top"));
+ assertNotNull(element);
+ element.setClassMetaData(classMetaData);
+
+ Top top = (Top) unmarshal("IgnorableWhitespaceContent.xml", schema, Top.class);
+ assertNotNull(top.string);
+ assertEquals(1, top.string.size());
+ assertEquals(" ", top.string.get(0));
+ }
+
+ public static class Top
+ {
+ public List string;
+ }
+}
Added: jbossxb/trunk/src/test/resources/org/jboss/test/xml/IgnorableWhitespaceContent.xml
===================================================================
--- jbossxb/trunk/src/test/resources/org/jboss/test/xml/IgnorableWhitespaceContent.xml (rev 0)
+++ jbossxb/trunk/src/test/resources/org/jboss/test/xml/IgnorableWhitespaceContent.xml 2007-06-14 10:44:31 UTC (rev 2417)
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<top xmlns='http://www.jboss.org/test/xml/simpleContent'>
+ <string> </string>
+</top>
More information about the jboss-svn-commits
mailing list