SCHEMA.setIgnoreUnresolvedFieldOrClass(false);
TypeBinding stringType = SCHEMA.getType(Constants.QNAME_STRING);
// stringType
stringType.setBeforeMarshallingCallback(new TermBeforeMarshallingCallback()
{
public Object beforeMarshalling(Object o, MarshallingContext ctx)
{
if(o != null)
{
ParticleBinding particle = ctx.getParticleBinding();
assertNotNull(particle);
TermBinding term = particle.getTerm();
assertTrue(term.isElement());
ElementBinding element = (ElementBinding) term;
String localPart = element.getQName().getLocalPart();
assertTrue("stringType".endsWith(localPart) || "item".equals(localPart));
TypeBinding type = element.getType();
assertEquals(Constants.QNAME_STRING, type.getQName());
o = ((GlobalElement.StringType)o).data;
}
return o;
}
});
stringType.setBeforeSetParentCallback(new TermBeforeSetParentCallback()
{
public Object beforeSetParent(Object o, UnmarshallingContext ctx)
{
ParticleBinding particle = ctx.getParticle();
assertNotNull(particle);
assertTrue(particle.getTerm().isElement());
ElementBinding element = (ElementBinding) particle.getTerm();
String eName = element.getQName().getLocalPart();
if(eName.equals("stringType"))
{
assertTrue(ctx.getParentValue() instanceof GlobalElement);
TermBinding parentTerm = ctx.getParentParticle().getTerm();
assertTrue(parentTerm.isElement());
assertEquals("global", ((ElementBinding)parentTerm).getQName().getLocalPart());
assertEquals("stringType", ctx.resolvePropertyName());
assertEquals(StringType.class, ctx.resolvePropertyType());
}
else if(eName.equals("item"))
{
assertTrue(ctx.getParentValue() instanceof GlobalElement.Sequence);
TermBinding parentTerm = ctx.getParentParticle().getTerm();
assertTrue(parentTerm instanceof SequenceBinding);
assertEquals("item", ctx.resolvePropertyName());
assertEquals(StringType.class, ctx.resolvePropertyType());
}
else
{
fail("Expected stringType or item but got " + eName);
}
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.setBeforeMarshallingCallback(new TermBeforeMarshallingCallback()
{
public Object beforeMarshalling(Object o, MarshallingContext ctx)
{
ParticleBinding particle = ctx.getParticleBinding();
assertNotNull(particle);
assertTrue(particle.getTerm().isElement());
ElementBinding element = (ElementBinding) particle.getTerm();
assertEquals(new QName("http://jboss.org/ns/test", "stringElement"), element.getQName());
TypeBinding type = element.getType();
assertEquals(Constants.QNAME_STRING, type.getQName());
return o == null ? null : (String)o;
}
});
stringElement.setBeforeSetParentCallback(new TermBeforeSetParentCallback()
{
public Object beforeSetParent(Object o, UnmarshallingContext ctx)
{
ParticleBinding particle = ctx.getParticle();
assertNotNull(particle);
assertTrue(particle.getTerm().isElement());
ElementBinding element = (ElementBinding) particle.getTerm();
assertEquals(new QName("http://jboss.org/ns/test", "stringElement"), element.getQName());
assertTrue(ctx.getParentValue() instanceof GlobalElement);
TermBinding parentTerm = ctx.getParentParticle().getTerm();
assertTrue(parentTerm.isElement());
assertEquals("global", ((ElementBinding)parentTerm).getQName().getLocalPart());
assertEquals(String.class, ctx.resolvePropertyType());
return o == null ? null : (String)o;
}
});
// sequence
sequence = (SequenceBinding) ((ParticleBinding)iter.next()).getTerm();
sequence.setBeforeMarshallingCallback(new TermBeforeMarshallingCallback()
{
public Object beforeMarshalling(Object o, MarshallingContext ctx)
{
ParticleBinding particle = ctx.getParticleBinding();
assertNotNull(particle);