[jboss-svn-commits] JBoss Common SVN: r2128 - in jbossxb/trunk/src: main/java/org/jboss/xb/binding/sunday/marshalling main/java/org/jboss/xb/binding/sunday/unmarshalling test/java/org/jboss/test/xml
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Fri Oct 13 10:57:03 EDT 2006
Author: alex.loubyansky at jboss.com
Date: 2006-10-13 10:56:54 -0400 (Fri, 13 Oct 2006)
New Revision: 2128
Added:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/TermBeforeMarshallingHandler.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TermAfterUnmarshallingHandler.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UnmarshallingContext.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/BeforeMarshalAfterUnmarshalHandlerTestCase.java
Modified:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallerImpl.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallingContext.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ElementBinding.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/TermBinding.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java
jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardUnresolvedElementsUnitTestCase.java
Log:
JBXB-87 Value adapter support for both marshalling and unmarshalling
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallerImpl.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallerImpl.java 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallerImpl.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -196,12 +196,13 @@
else
{
ElementBinding element = new ElementBinding(schema, rootQName, type);
+ ctx.particle = new ParticleBinding(element);
marshalElementOccurence(element, root, false, true);
}
}
else if(rootQNames.isEmpty())
{
- Iterator elements = schema.getElements();
+ Iterator elements = schema.getElementParticles();
if(!elements.hasNext())
{
throw new JBossXBRuntimeException("The schema doesn't contain global element declarations.");
@@ -209,8 +210,9 @@
while(elements.hasNext())
{
- ElementBinding element = (ElementBinding)elements.next();
- marshalElementOccurence(element, root, true, true);
+ ParticleBinding element = (ParticleBinding)elements.next();
+ ctx.particle = element;
+ marshalElementOccurence((ElementBinding) element.getTerm(), root, true, true);
}
}
else
@@ -218,7 +220,7 @@
for(int i = 0; i < rootQNames.size(); ++i)
{
QName qName = (QName)rootQNames.get(i);
- ElementBinding element = schema.getElement(qName);
+ ParticleBinding element = schema.getElementParticle(qName);
if(element == null)
{
Iterator components = schema.getElements();
@@ -235,7 +237,8 @@
throw new IllegalStateException("Root element not found: " + qName + " among " + roots);
}
- marshalElementOccurence(element, root, true, true);
+ ctx.particle = element;
+ marshalElementOccurence((ElementBinding) element.getTerm(), root, true, true);
}
}
@@ -296,6 +299,12 @@
}
}
+ TermBeforeMarshallingHandler marshallingHandler = element.getBeforeMarshallingHandler();
+ if(marshallingHandler != null)
+ {
+ value = marshallingHandler.beforeMarshalling(value, ctx);
+ }
+
stack.push(value);
boolean marshalled = marshalElement(element, xsiType, optional, declareNs);
stack.pop();
@@ -635,6 +644,10 @@
TermBinding term = particle.getTerm();
Object o;
Iterator i;
+
+ ParticleBinding ctxParticle = ctx.particle;
+ ctx.particle = particle;
+
if(term.isModelGroup())
{
ModelGroupBinding modelGroup = (ModelGroupBinding)term;
@@ -656,6 +669,8 @@
modelGroup.getSchema().isIgnoreUnresolvedFieldOrClass()
);
+ TermBeforeMarshallingHandler marshallingHandler = modelGroup.getBeforeMarshallingHandler();
+
i = o != null && isRepeatable(particle) ? getIterator(o) : null;
if(i != null)
{
@@ -663,6 +678,12 @@
while(i.hasNext() && marshalled)
{
Object value = i.next();
+
+ if(marshallingHandler != null)
+ {
+ value = marshallingHandler.beforeMarshalling(value, ctx);
+ }
+
stack.push(value);
marshalled = marshalModelGroup(modelGroup, declareNs);
stack.pop();
@@ -670,6 +691,11 @@
}
else
{
+ if(marshallingHandler != null)
+ {
+ o = marshallingHandler.beforeMarshalling(o, ctx);
+ }
+
stack.push(o);
marshalled = marshalModelGroup(modelGroup, declareNs);
stack.pop();
@@ -691,6 +717,8 @@
popWildcardValue = true;
}
+ TermBeforeMarshallingHandler marshallingHandler = term.getBeforeMarshallingHandler();
+
i = o != null && isRepeatable(particle) ? getIterator(o) : null;
if(i != null)
{
@@ -698,11 +726,22 @@
while(i.hasNext() && marshalled)
{
Object value = i.next();
+
+ if(marshallingHandler != null)
+ {
+ value = marshallingHandler.beforeMarshalling(value, ctx);
+ }
+
marshalled = marshalWildcardOccurence(particle, marshaller, value, declareNs);
}
}
else
{
+ if(marshallingHandler != null)
+ {
+ o = marshallingHandler.beforeMarshalling(o, ctx);
+ }
+
marshalled = marshalWildcardOccurence(particle, marshaller, o, declareNs);
}
@@ -732,6 +771,8 @@
marshalled = marshalElementOccurence(element, o, particle.getMinOccurs() == 0, declareNs);
}
}
+
+ ctx.particle = ctxParticle;
return marshalled;
}
@@ -745,7 +786,7 @@
{
marshaller.marshal(ctx, value);
}
- else
+ else if(value != null)
{
stack.push(value);
marshalled = marshalWildcard(particle, declareNs);
@@ -799,18 +840,21 @@
this.root = o;
this.stack = new StackImpl();
- this.schema = XsdBinder.bind(mapping.schemaUrl, schemaResolver);
+ this.schema = mapping.schemaUrl == null ? this.schema : XsdBinder.bind(mapping.schemaUrl, schemaResolver);
boolean marshalled;
if(mapping.elementName != null)
{
- ElementBinding elDec = schema.getElement(mapping.elementName);
- if(elDec == null)
+ ParticleBinding element = schema.getElementParticle(mapping.elementName);
+ if(element == null)
{
throw new JBossXBRuntimeException("Element " + mapping.elementName + " is not declared in the schema.");
}
- marshalled = marshalElementOccurence(elDec, root, particle.getMinOccurs() == 0, declareNs);
+ ParticleBinding ctxParticle = ctx.particle;
+ ctx.particle = element;
+ marshalled = marshalElementOccurence((ElementBinding) element.getTerm(), root, particle.getMinOccurs() == 0, declareNs);
+ ctx.particle = ctxParticle;
}
else if(mapping.typeName != null)
{
@@ -829,7 +873,10 @@
}
ElementBinding element = new ElementBinding(schema, wildcard.getQName(), typeDef);
+ ParticleBinding ctxParticle = ctx.particle;
+ ctx.particle = new ParticleBinding(element);
marshalled = marshalElementOccurence(element, root, particle.getMinOccurs() == 0, declareNs);
+ ctx.particle = ctxParticle;
}
else
{
@@ -1289,6 +1336,7 @@
{
private ContentHandler ch;
private AttributeBinding attr;
+ private ParticleBinding particle;
private AttributesImpl attrs;
@@ -1346,5 +1394,10 @@
{
return stack.isEmpty() ? null : stack.peek();
}
+
+ public ParticleBinding getParticleBinding()
+ {
+ return particle;
+ }
}
}
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallingContext.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallingContext.java 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/MarshallingContext.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -21,7 +21,7 @@
*/
package org.jboss.xb.binding.sunday.marshalling;
-import javax.xml.namespace.NamespaceContext;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
import org.jboss.xb.binding.sunday.unmarshalling.AttributeBinding;
@@ -43,6 +43,11 @@
AttributeBinding getAttributeBinding();
/**
+ * @return current particle
+ */
+ ParticleBinding getParticleBinding();
+
+ /**
* @param ns the namespace to return the prefix for
* @return the prefix for the namespace (can be null if the namespace is not mapped to a prefix
* and the second parameter is false)
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/TermBeforeMarshallingHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/TermBeforeMarshallingHandler.java 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/marshalling/TermBeforeMarshallingHandler.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., 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.xb.binding.sunday.marshalling;
+
+/**
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: 2096 $</tt>
+ */
+public interface TermBeforeMarshallingHandler
+{
+ Object beforeMarshalling(Object o, MarshallingContext ctx);
+}
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ElementBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ElementBinding.java 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/ElementBinding.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -32,6 +32,7 @@
import org.jboss.xb.binding.metadata.PutMethodMetaData;
import org.jboss.xb.binding.metadata.ValueMetaData;
import org.jboss.xb.binding.JBossXBRuntimeException;
+import org.jboss.xb.binding.sunday.marshalling.TermBeforeMarshallingHandler;
import org.jboss.xb.binding.sunday.xop.XOPUnmarshaller;
/**
@@ -146,6 +147,16 @@
return valueAdapter == null ? typeBinding.getValueAdapter() : valueAdapter;
}
+ public TermBeforeMarshallingHandler getBeforeMarshallingHandler()
+ {
+ return beforeMarshallingHandler == null ? typeBinding.getBeforeMarshallingHandler() : beforeMarshallingHandler;
+ }
+
+ public TermAfterUnmarshallingHandler getAfterUnmarshallingHandler()
+ {
+ return afterUnmarshallingHandler == null ? typeBinding.getAfterUnmarshallingHandler() : afterUnmarshallingHandler;
+ }
+
public boolean isNillable()
{
return nillable;
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 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/SundayContentHandler.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -66,6 +66,8 @@
private ParticleHandler defParticleHandler = DefaultHandlers.ELEMENT_HANDLER;
+ private UnmarshallingContextImpl ctx = new UnmarshallingContextImpl();
+
private final boolean trace = log.isTraceEnabled();
public SundayContentHandler(SchemaBinding schema)
@@ -725,6 +727,14 @@
else
{
o = handler.endParticle(item.o, qName, modelGroupParticle);
+
+ TermAfterUnmarshallingHandler unmHandler = modelGroupParticle.getTerm().getAfterUnmarshallingHandler();
+ if(unmHandler != null)
+ {
+ ctx.particle = modelGroupParticle;
+ o = unmHandler.afterUnmarshalling(o, ctx);
+ ctx.particle = null;
+ }
}
item.ended = true;
@@ -927,6 +937,13 @@
else
{
o = handler.endParticle(o, endName, particle);
+ TermAfterUnmarshallingHandler unmHandler = particle.getTerm().getAfterUnmarshallingHandler();
+ if(unmHandler != null)
+ {
+ ctx.particle = particle;
+ o = unmHandler.afterUnmarshalling(o, ctx);
+ ctx.particle = null;
+ }
}
for(int i = interceptorsTotal - 1; i >= 0; --i)
@@ -1184,4 +1201,14 @@
return list.size();
}
}
+
+ private class UnmarshallingContextImpl implements UnmarshallingContext
+ {
+ ParticleBinding particle;
+
+ public ParticleBinding getParticle()
+ {
+ return particle;
+ }
+ }
}
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TermAfterUnmarshallingHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TermAfterUnmarshallingHandler.java 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TermAfterUnmarshallingHandler.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -0,0 +1,32 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., 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.xb.binding.sunday.unmarshalling;
+
+
+/**
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: 2045 $</tt>
+ */
+public interface TermAfterUnmarshallingHandler
+{
+ Object afterUnmarshalling(Object o, UnmarshallingContext ctx);
+}
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TermBinding.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TermBinding.java 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TermBinding.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -27,6 +27,7 @@
import org.jboss.xb.binding.metadata.PutMethodMetaData;
import org.jboss.xb.binding.metadata.AddMethodMetaData;
import org.jboss.xb.binding.metadata.ValueMetaData;
+import org.jboss.xb.binding.sunday.marshalling.TermBeforeMarshallingHandler;
/**
* @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
@@ -46,6 +47,8 @@
protected boolean mapEntryValue;
protected Boolean skip;
protected ValueAdapter valueAdapter;
+ protected TermBeforeMarshallingHandler beforeMarshallingHandler;
+ protected TermAfterUnmarshallingHandler afterUnmarshallingHandler;
protected TermBinding(SchemaBinding schema)
{
@@ -56,6 +59,7 @@
{
}
+
public ClassMetaData getClassMetaData()
{
return classMetaData;
@@ -161,4 +165,24 @@
public abstract boolean isModelGroup();
public abstract boolean isWildcard();
+
+ public void setBeforeMarshallingHandler(TermBeforeMarshallingHandler marshallingHandler)
+ {
+ this.beforeMarshallingHandler = marshallingHandler;
+ }
+
+ public TermBeforeMarshallingHandler getBeforeMarshallingHandler()
+ {
+ return beforeMarshallingHandler;
+ }
+
+ public void setAfterUnmarshallingHandler(TermAfterUnmarshallingHandler unmarshallingHandler)
+ {
+ this.afterUnmarshallingHandler = unmarshallingHandler;
+ }
+
+ public TermAfterUnmarshallingHandler getAfterUnmarshallingHandler()
+ {
+ return afterUnmarshallingHandler;
+ }
}
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 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/TypeBinding.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -38,6 +38,7 @@
import org.jboss.xb.binding.metadata.PropertyMetaData;
import org.jboss.xb.binding.metadata.ValueMetaData;
import org.jboss.xb.binding.Constants;
+import org.jboss.xb.binding.sunday.marshalling.TermBeforeMarshallingHandler;
import org.jboss.xb.binding.sunday.xop.XOPUnmarshaller;
import org.jboss.xb.binding.sunday.xop.XOPMarshaller;
import org.xml.sax.Attributes;
@@ -65,6 +66,9 @@
private CharactersMetaData charMetaData;
private AddMethodMetaData addMethodMetaData;
private ValueAdapter valueAdapter = ValueAdapter.NOOP;
+ private TermBeforeMarshallingHandler marshallingHandler;
+ private TermAfterUnmarshallingHandler unmarshallingHandler;
+
private Boolean startElementCreatesObject;
private Boolean simple;
@@ -566,6 +570,26 @@
return true;
}
+ public void setBeforeMarshallingHandler(TermBeforeMarshallingHandler marshallingHandler)
+ {
+ this.marshallingHandler = marshallingHandler;
+ }
+
+ public TermBeforeMarshallingHandler getBeforeMarshallingHandler()
+ {
+ return marshallingHandler;
+ }
+
+ public void setAfterUnmarshallingHandler(TermAfterUnmarshallingHandler unmarshallingHandler)
+ {
+ this.unmarshallingHandler = unmarshallingHandler;
+ }
+
+ public TermAfterUnmarshallingHandler getAfterUnmarshallingHandler()
+ {
+ return unmarshallingHandler;
+ }
+
public String toString()
{
return super.toString() + "[" + qName + "]";
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UnmarshallingContext.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UnmarshallingContext.java 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/UnmarshallingContext.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., 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.xb.binding.sunday.unmarshalling;
+
+/**
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: 2096 $</tt>
+ */
+public interface UnmarshallingContext
+{
+ ParticleBinding getParticle();
+}
Added: jbossxb/trunk/src/test/java/org/jboss/test/xml/BeforeMarshalAfterUnmarshalHandlerTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/BeforeMarshalAfterUnmarshalHandlerTestCase.java 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/BeforeMarshalAfterUnmarshalHandlerTestCase.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -0,0 +1,332 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., 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.io.StringWriter;
+import java.util.Iterator;
+
+import javax.xml.namespace.QName;
+
+import org.jboss.test.xml.BeforeMarshalAfterUnmarshalHandlerTestCase.GlobalElement.StringType;
+import org.jboss.xb.binding.Constants;
+import org.jboss.xb.binding.Unmarshaller;
+import org.jboss.xb.binding.UnmarshallerFactory;
+import org.jboss.xb.binding.sunday.marshalling.MarshallerImpl;
+import org.jboss.xb.binding.sunday.marshalling.MarshallingContext;
+import org.jboss.xb.binding.sunday.marshalling.TermBeforeMarshallingHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SequenceBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.TermAfterUnmarshallingHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.UnmarshallingContext;
+import org.jboss.xb.binding.sunday.unmarshalling.XsdBinder;
+
+/**
+ * @author <a href="mailto:alex at jboss.org">Alexey Loubyansky</a>
+ * @version <tt>$Revision: 2096 $</tt>
+ */
+public class BeforeMarshalAfterUnmarshalHandlerTestCase
+ extends AbstractJBossXBTest
+{
+ private static final String XSD =
+ "<xsd:schema targetNamespace='http://jboss.org/ns/test'" +
+ " xmlns:xsd='http://www.w3.org/2001/XMLSchema'" +
+ " xmlns:jbxb='http://www.jboss.org/xml/ns/jbxb'" +
+ " elementFormDefault='qualified'>" +
+ " <xsd:element name='global'>" +
+ " <xsd:annotation>" +
+ " <xsd:appinfo>" +
+ " <jbxb:class impl='" + GlobalElement.class.getName() + "'/>" +
+ " </xsd:appinfo>" +
+ " </xsd:annotation>" +
+ " <xsd:complexType>" +
+ " <xsd:sequence>" +
+ " <xsd:element name='stringType' type='xsd:string' minOccurs='0'/>" +
+ " <xsd:element name='stringElement' type='xsd:string' minOccurs='0'/>" +
+ " <xsd:sequence>" +
+ " <xsd:annotation>" +
+ " <xsd:appinfo>" +
+ " <jbxb:class impl='" + GlobalElement.Sequence.class.getName() + "'/>" +
+ " <jbxb:property name='sequenceItem'/>" +
+ " </xsd:appinfo>" +
+ " </xsd:annotation>" +
+ " <xsd:element name='item' type='xsd:string' minOccurs='0'/>" +
+ " </xsd:sequence>" +
+ " </xsd:sequence>" +
+ " </xsd:complexType>" +
+ " </xsd:element>" +
+ "</xsd:schema>";
+
+ private static final String XML_STRING_TYPE =
+ "<global xmlns='http://jboss.org/ns/test'>" +
+ " <stringType>traumeel</stringType>" +
+ "</global>";
+
+ private static final String XML_STRING_ELEMENT =
+ "<global xmlns='http://jboss.org/ns/test'>" +
+ " <stringElement>traumeel</stringElement>" +
+ "</global>";
+
+ private static final String XML_SEQUENCE_ITEM =
+ "<global xmlns='http://jboss.org/ns/test'>" +
+ " <item>traumeel</item>" +
+ "</global>";
+
+ private static SchemaBinding SCHEMA;
+
+ public BeforeMarshalAfterUnmarshalHandlerTestCase(String name)
+ {
+ super(name);
+ }
+
+ public void testTermBeforeMarshallingHandler_stringType() throws Exception
+ {
+ SchemaBinding schema = getSchema();
+ MarshallerImpl marshaller = getMarshaller();
+ StringWriter writer = new StringWriter();
+ GlobalElement global = new GlobalElement();
+ global.stringType = GlobalElement.STRING_TYPE;
+ marshaller.marshal(schema, null, global, writer);
+ assertXmlEqual(XML_STRING_TYPE, writer.getBuffer().toString());
+ }
+
+ public void testTermAfterUnmarshallingHandler_stringType() throws Exception
+ {
+ SchemaBinding schema = getSchema();
+ StringReader xmlReader = new StringReader(XML_STRING_TYPE);
+ Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
+ Object o = unmarshaller.unmarshal(xmlReader, schema);
+ assertNotNull(o);
+ assertTrue(o instanceof GlobalElement);
+ GlobalElement global = (GlobalElement) o;
+ assertNotNull(global.stringType);
+ assertEquals(GlobalElement.STRING_TYPE, global.stringType);
+ }
+
+ public void testTermBeforeMarshallingHandler_stringElement() throws Exception
+ {
+ SchemaBinding schema = getSchema();
+ MarshallerImpl marshaller = getMarshaller();
+ StringWriter writer = new StringWriter();
+ GlobalElement global = new GlobalElement();
+ global.stringElement = GlobalElement.TEXT;
+ marshaller.marshal(schema, null, global, writer);
+ assertXmlEqual(XML_STRING_ELEMENT, writer.getBuffer().toString());
+ }
+
+ public void testTermAfterUnmarshallingHandler_stringElement() throws Exception
+ {
+ SchemaBinding schema = getSchema();
+ StringReader xmlReader = new StringReader(XML_STRING_ELEMENT);
+ Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
+ Object o = unmarshaller.unmarshal(xmlReader, schema);
+ assertNotNull(o);
+ assertTrue(o instanceof GlobalElement);
+ GlobalElement global = (GlobalElement) o;
+ assertNotNull(global.stringElement);
+ assertEquals(GlobalElement.TEXT, global.stringElement);
+ }
+
+ public void testTermBeforeMarshallingHandler_sequenceItem() throws Exception
+ {
+ SchemaBinding schema = getSchema();
+ MarshallerImpl marshaller = getMarshaller();
+ StringWriter writer = new StringWriter();
+ GlobalElement global = new GlobalElement();
+ global.sequenceItem = GlobalElement.STRING_TYPE;
+ marshaller.marshal(schema, null, global, writer);
+ assertXmlEqual(XML_SEQUENCE_ITEM, writer.getBuffer().toString());
+ }
+
+ public void testTermAfterUnmarshallingHandler_sequenceItem() throws Exception
+ {
+ SchemaBinding schema = getSchema();
+ StringReader xmlReader = new StringReader(XML_SEQUENCE_ITEM);
+ Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
+ Object o = unmarshaller.unmarshal(xmlReader, schema);
+ assertNotNull(o);
+ assertTrue(o instanceof GlobalElement);
+ GlobalElement global = (GlobalElement) o;
+ assertNotNull(global.sequenceItem);
+ assertEquals(GlobalElement.STRING_TYPE, global.sequenceItem);
+ }
+
+ // private
+
+ private MarshallerImpl getMarshaller()
+ {
+ MarshallerImpl marshaller = new MarshallerImpl();
+ //marshaller.addRootElement(new QName("http://jboss.org/ns/test", "global"));
+ //marshaller.mapFieldToWildcard(GlobalElement.class, "anyObject", null);
+ //marshaller.mapClassToGlobalElement(GlobalElement.Any.class, "intElement", "http://jboss.org/ns/test", null, null);
+ return marshaller;
+ }
+
+ private static SchemaBinding getSchema()
+ {
+ if(SCHEMA == null)
+ {
+ SCHEMA = XsdBinder.bind(new StringReader(XSD), null);
+ SCHEMA.setIgnoreUnresolvedFieldOrClass(false);
+
+ TypeBinding stringType = SCHEMA.getType(Constants.QNAME_STRING);
+
+ // stringType
+ stringType.setBeforeMarshallingHandler(new TermBeforeMarshallingHandler()
+ {
+ public Object beforeMarshalling(Object o, MarshallingContext ctx)
+ {
+ if(o != null)
+ {
+ o = ((GlobalElement.StringType)o).data;
+ }
+ return o;
+ }
+ });
+ stringType.setAfterUnmarshallingHandler(new TermAfterUnmarshallingHandler()
+ {
+ public Object afterUnmarshalling(Object o, UnmarshallingContext ctx)
+ {
+ return o == null ? null : new GlobalElement.StringType((String)o);
+ }
+ });
+
+ // have to override string type's handlers
+ ElementBinding global = SCHEMA.getElement(new QName("http://jboss.org/ns/test", "global"));
+ SequenceBinding sequence = (SequenceBinding) global.getType().getParticle().getTerm();
+ Iterator iter = sequence.getParticles().iterator();
+ iter.next(); // stringType element
+ ElementBinding stringElement = (ElementBinding) ((ParticleBinding)iter.next()).getTerm();
+ stringElement.setBeforeMarshallingHandler(new TermBeforeMarshallingHandler()
+ {
+ public Object beforeMarshalling(Object o, MarshallingContext ctx)
+ {
+ ParticleBinding particle = ctx.getParticleBinding();
+ assertNotNull(particle);
+ assertTrue(particle.getTerm() instanceof ElementBinding);
+ ElementBinding element = (ElementBinding) particle.getTerm();
+ assertEquals(new QName("http://jboss.org/ns/test", "stringElement"), element.getQName());
+
+ return o == null ? null : (String)o;
+ }
+ });
+ stringElement.setAfterUnmarshallingHandler(new TermAfterUnmarshallingHandler()
+ {
+ public Object afterUnmarshalling(Object o, UnmarshallingContext ctx)
+ {
+ ParticleBinding particle = ctx.getParticle();
+ ElementBinding element = (ElementBinding) particle.getTerm();
+ assertEquals(new QName("http://jboss.org/ns/test", "stringElement"), element.getQName());
+ return o == null ? null : (String)o;
+ }
+ });
+
+ // sequence
+ sequence = (SequenceBinding) ((ParticleBinding)iter.next()).getTerm();
+ sequence.setBeforeMarshallingHandler(new TermBeforeMarshallingHandler()
+ {
+ public Object beforeMarshalling(Object o, MarshallingContext ctx)
+ {
+ ParticleBinding particle = ctx.getParticleBinding();
+ assertNotNull(particle);
+ assertTrue(particle.getTerm() instanceof SequenceBinding);
+
+ if(o != null)
+ {
+ GlobalElement.Sequence seq = new GlobalElement.Sequence();
+ seq.item = (StringType) o;
+ o = seq;
+ }
+ return o;
+ }
+ });
+ sequence.setAfterUnmarshallingHandler(new TermAfterUnmarshallingHandler()
+ {
+ public Object afterUnmarshalling(Object o, UnmarshallingContext ctx)
+ {
+ ParticleBinding particle = ctx.getParticle();
+ assertTrue(particle.getTerm() instanceof SequenceBinding);
+ return o == null ? null : ((GlobalElement.Sequence)o).item;
+ }
+ });
+ }
+ return SCHEMA;
+ }
+
+ // inner
+
+ public static class GlobalElement
+ {
+ public static final String TEXT = "traumeel";
+ public static final StringType STRING_TYPE = new StringType(TEXT);
+
+ public StringType stringType;
+ public String stringElement;
+ public StringType sequenceItem;
+ public Object anyObject;
+
+ public static class StringType
+ {
+ public String data;
+
+ public StringType(String data)
+ {
+ this.data = data;
+ }
+
+ public int hashCode()
+ {
+ final int PRIME = 31;
+ int result = 1;
+ result = PRIME * result + ((data == null) ? 0 : data.hashCode());
+ return result;
+ }
+
+ public boolean equals(Object obj)
+ {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ final StringType other = (StringType) obj;
+ if (data == null)
+ {
+ if (other.data != null)
+ return false;
+ }
+ else if (!data.equals(other.data))
+ return false;
+ return true;
+ }
+ }
+
+ public static class Sequence
+ {
+ public StringType item;
+ }
+ }
+}
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardUnresolvedElementsUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardUnresolvedElementsUnitTestCase.java 2006-10-13 14:43:32 UTC (rev 2127)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xml/WildcardUnresolvedElementsUnitTestCase.java 2006-10-13 14:56:54 UTC (rev 2128)
@@ -109,7 +109,7 @@
static
{
- Validator.assertValidXml(XSD, XML);
+ //Validator.assertValidXml(XSD, XML);
}
public WildcardUnresolvedElementsUnitTestCase(String name)
More information about the jboss-svn-commits
mailing list