[jboss-svn-commits] JBoss Common SVN: r3756 - in jbossxb/trunk/src: main/java/org/jboss/xb/builder and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Wed Nov 18 10:13:53 EST 2009
Author: alex.loubyansky at jboss.com
Date: 2009-11-18 10:13:53 -0500 (Wed, 18 Nov 2009)
New Revision: 3756
Added:
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/NoopParticleHandler.java
jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/RegisteredAttributesHandler.java
Modified:
jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/BeanHandler.java
jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValueUnitTestCase.java
Log:
JBXB-233
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/NoopParticleHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/NoopParticleHandler.java (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/NoopParticleHandler.java 2009-11-18 15:13:53 UTC (rev 3756)
@@ -0,0 +1,57 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+
+import org.xml.sax.Attributes;
+
+/**
+ * A NoopParticleHandler.
+ *
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class NoopParticleHandler implements ParticleHandler
+{
+ public static final NoopParticleHandler INSTANCE = new NoopParticleHandler();
+
+ @Override
+ public Object endParticle(Object o, QName elementName, ParticleBinding particle)
+ {
+ return o;
+ }
+
+ @Override
+ public void setParent(Object parent, Object o, QName elementName, ParticleBinding particle,
+ ParticleBinding parentParticle)
+ {
+ }
+
+ @Override
+ public Object startParticle(Object parent, QName elementName, ParticleBinding particle, Attributes attrs,
+ NamespaceContext nsCtx)
+ {
+ return parent;
+ }
+}
Added: jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/RegisteredAttributesHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/RegisteredAttributesHandler.java (rev 0)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/binding/sunday/unmarshalling/RegisteredAttributesHandler.java 2009-11-18 15:13:53 UTC (rev 3756)
@@ -0,0 +1,116 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.NamespaceContext;
+import javax.xml.namespace.QName;
+
+import org.xml.sax.Attributes;
+
+/**
+ * A RegisteredAttributesHandler.
+ *
+ * @author <a href="alex at jboss.com">Alexey Loubyansky</a>
+ * @version $Revision: 1.1 $
+ */
+public class RegisteredAttributesHandler extends AttributesHandler
+{
+ private Map<QName, AttributeBinding> registered;
+ private AnyAttributeBinding any;
+ private Map<QName, AttributeBinding> defaultAttrs;
+
+ public void attributes(Object o, QName elementName, TypeBinding type, Attributes attrs, NamespaceContext nsCtx)
+ {
+ if(registered == null)
+ return;
+
+ Map<QName, AttributeBinding> notSetDefaultAttrs = null;
+ // note: this is never used in the builder impl
+ // but there is a test for this in SchemaDefaultAttributeValueUnitTestCase
+ if(defaultAttrs != null)
+ notSetDefaultAttrs = new HashMap<QName, AttributeBinding>(defaultAttrs);
+
+ for(int i = 0; i < attrs.getLength(); ++i)
+ {
+ QName qName = new QName(attrs.getURI(i), attrs.getLocalName(i));
+ AttributeBinding binding = registered.get(qName);
+ if(binding != null)
+ {
+ AttributeHandler handler = binding.getHandler();
+ Object value = handler.unmarshal(elementName, qName, binding, nsCtx, attrs.getValue(i));
+ handler.attribute(elementName, qName, binding, o, value);
+
+ if(notSetDefaultAttrs != null && binding.getDefaultConstraint() != null)
+ notSetDefaultAttrs.remove(qName);
+ }
+ else if (any != null)
+ {
+ AnyAttributeHandler handler = any.getHandler();
+ Object value = handler.unmarshal(elementName, qName, any, nsCtx, attrs.getValue(i));
+ handler.attribute(elementName, qName, any, o, value);
+ }
+ }
+
+ if(notSetDefaultAttrs != null && !notSetDefaultAttrs.isEmpty())
+ {
+ for(AttributeBinding binding : notSetDefaultAttrs.values())
+ {
+ AttributeHandler handler = binding.getHandler();
+ Object value = handler.unmarshal(elementName, binding.getQName(), binding, nsCtx, binding.getDefaultConstraint());
+ handler.attribute(elementName, binding.getQName(), binding, o, value);
+ }
+ }
+ }
+
+ public void addAttribute(AttributeBinding attr)
+ {
+ if(registered == null)
+ registered = Collections.singletonMap(attr.getQName(), attr);
+ else
+ {
+ if(registered.size() == 1)
+ registered = new HashMap<QName, AttributeBinding>(registered);
+ registered.put(attr.getQName(), attr);
+ }
+
+ if(attr.getDefaultConstraint() != null)
+ {
+ if(defaultAttrs == null)
+ defaultAttrs = Collections.singletonMap(attr.getQName(), attr);
+ else
+ {
+ if(defaultAttrs.size() == 1)
+ defaultAttrs = new HashMap<QName, AttributeBinding>(defaultAttrs);
+ defaultAttrs.put(attr.getQName(), attr);
+ }
+ }
+ }
+
+ public void setAnyAttribute(AnyAttributeBinding any)
+ {
+ this.any = any;
+ }
+}
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java 2009-11-17 09:30:17 UTC (rev 3755)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/builder/JBossXBNoSchemaBuilder.java 2009-11-18 15:13:53 UTC (rev 3756)
@@ -106,6 +106,7 @@
import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementInterceptor;
import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
import org.jboss.xb.binding.sunday.unmarshalling.ModelGroupBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.NoopParticleHandler;
import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
import org.jboss.xb.binding.sunday.unmarshalling.ParticleHandler;
import org.jboss.xb.binding.sunday.unmarshalling.RepeatableParticleHandler;
@@ -970,6 +971,8 @@
attribute.setNormalizeSpace(preserveSpace.preserve() ? false : true);
if (trace)
log.trace("Bound attribute " + qName + " type=" + beanInfo.getName() + " property=" + property.getName() + " propertyType=" + attributeTypeInfo + ", normalizeSpace=" + attribute.isNormalizeSpace() + ", typeBinding=" + typeBinding.getQName());
+
+ handler.getAttributesHandler().addAttribute(attribute);
}
// Is this any attribute
@@ -989,6 +992,8 @@
anyAttribute.setNormalizeSpace(preserveSpace.preserve() ? false : true);
if (trace)
log.trace("Bound any attribute type=" + beanInfo.getName() + " property=" + property.getName() + ", normalizeSpace=" + anyAttribute.isNormalizeSpace());
+
+ handler.getAttributesHandler().setAnyAttribute(anyAttribute);
}
// Are we determining the property order?
@@ -1085,19 +1090,6 @@
}
}
- // No value property, see if we have a default one
- //if (valueProperty == null)
- //{
- // try
- // {
- // valueProperty = beanInfo.getProperty("value");
- // }
- // catch (Exception ignored)
- // {
- // Nope.
- // }
- //}
-
// Bind the value
if (valueProperty != null)
{
@@ -1529,19 +1521,25 @@
{
BeanInfo wrapperInfo = JBossXBBuilder.configuration.getBeanInfo(groupText.wrapper());
TypeBinding wrapperTypeBinding = resolveTypeBinding(wrapperInfo.getClassInfo());
+
+ ParticleHandler particleHandler = wrapperTypeBinding.getHandler();
+ if (particleHandler instanceof BeanHandler == false)
+ throw new IllegalStateException("Cannot wrap " + wrapperInfo.getName() + " not a bean type " + particleHandler);
+ BeanHandler beanHandler = (BeanHandler) particleHandler;
+ WrapperBeanAdapterFactory wrapperFactory = new WrapperBeanAdapterFactory(beanHandler.getBeanAdapterFactory(), propertyType.getType());
+ BeanHandler wrapperHandler = new BeanHandler(wrapperInfo.getName(), wrapperFactory);
+
// Steal the attributes
Collection<AttributeBinding> otherAttributes = wrapperTypeBinding.getAttributes();
if (otherAttributes != null)
{
for (AttributeBinding other : otherAttributes)
+ {
elementTypeBinding.addAttribute(other);
+ wrapperHandler.getAttributesHandler().addAttribute(other);
+ }
}
- ParticleHandler particleHandler = wrapperTypeBinding.getHandler();
- if (particleHandler instanceof BeanHandler == false)
- throw new IllegalStateException("Cannot wrap " + wrapperInfo.getName() + " not a bean type " + particleHandler);
- BeanHandler beanHandler = (BeanHandler) particleHandler;
- WrapperBeanAdapterFactory wrapperFactory = new WrapperBeanAdapterFactory(beanHandler.getBeanAdapterFactory(), propertyType.getType());
- elementTypeBinding.setHandler(new BeanHandler(wrapperInfo.getName(), wrapperFactory));
+ elementTypeBinding.setHandler(wrapperHandler);
elementTypeBinding.setSimpleType(wrapperTypeBinding.getSimpleType());
}
else
@@ -1947,7 +1945,7 @@
seq.setHandler(BuilderParticleHandler.INSTANCE);
ParticleBinding particle = new ParticleBinding(seq);
wrapperType.setParticle(particle);
- wrapperType.setHandler(new DefaultElementHandler());
+ wrapperType.setHandler(NoopParticleHandler.INSTANCE);
ElementBinding wrapperElement = createElementBinding(propertyType, wrapperType, wrapperQName, false);
wrapperElement.setNillable(annotation.nillable());
@@ -2204,6 +2202,7 @@
AttributeBinding keyBinding = new AttributeBinding(schemaBinding, attrQName, attributeType, attributeHandler);
keyBinding.setRequired(true);
entryType.addAttribute(keyBinding);
+ entryHandler.getAttributesHandler().addAttribute(keyBinding);
}
if(valueAttribute != null)
@@ -2214,6 +2213,7 @@
AttributeBinding valueBinding = new AttributeBinding(schemaBinding, attrQName, attributeType, attributeHandler);
valueBinding.setRequired(true);
entryType.addAttribute(valueBinding);
+ entryHandler.getAttributesHandler().addAttribute(valueBinding);
}
else if(valueElement == null)
{
Modified: jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/BeanHandler.java
===================================================================
--- jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/BeanHandler.java 2009-11-17 09:30:17 UTC (rev 3755)
+++ jbossxb/trunk/src/main/java/org/jboss/xb/builder/runtime/BeanHandler.java 2009-11-18 15:13:53 UTC (rev 3756)
@@ -25,10 +25,12 @@
import javax.xml.namespace.QName;
import org.jboss.logging.Logger;
-import org.jboss.xb.binding.sunday.unmarshalling.DefaultElementHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.AttributesHandler;
import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
import org.jboss.xb.binding.sunday.unmarshalling.ModelGroupBinding;
import org.jboss.xb.binding.sunday.unmarshalling.ParticleBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ParticleHandler;
+import org.jboss.xb.binding.sunday.unmarshalling.RegisteredAttributesHandler;
import org.jboss.xb.binding.sunday.unmarshalling.TermBinding;
import org.jboss.xb.binding.sunday.unmarshalling.ValueAdapter;
import org.jboss.xb.spi.BeanAdapter;
@@ -41,7 +43,7 @@
* @author <a href="adrian at jboss.com">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
-public class BeanHandler extends DefaultElementHandler
+public class BeanHandler /*extends DefaultElementHandler*/ implements ParticleHandler
{
/** The log */
private final Logger log = Logger.getLogger(getClass());
@@ -55,6 +57,8 @@
/** The BeanAdapter */
private BeanAdapterFactory beanAdapterFactory;
+ private RegisteredAttributesHandler attrsHandler = new RegisteredAttributesHandler();
+
/**
* Create a new bean info element handler
*
@@ -102,11 +106,11 @@
throw new RuntimeException("QName " + elementName + " error invoking beanAdapterFactory.newInstance() for bean=" + name, t);
}
- if (o != null && particle.getTerm().isElement())
+ TermBinding term = particle.getTerm();
+ if (o != null && term.isElement())
{
- ElementBinding element = (ElementBinding) particle.getTerm();
- attrs = element.getType().expandWithDefaultAttributes(attrs);
- attributes(o, elementName, element, attrs, nsCtx);
+ ElementBinding element = (ElementBinding) term;
+ attrsHandler.attributes(o, elementName, element.getType(), attrs, nsCtx);
}
return o;
}
@@ -159,4 +163,9 @@
BeanAdapter beanAdapter = (BeanAdapter) o;
return beanAdapter.getValue();
}
+
+ public RegisteredAttributesHandler getAttributesHandler()
+ {
+ return attrsHandler;
+ }
}
Modified: jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValueUnitTestCase.java
===================================================================
--- jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValueUnitTestCase.java 2009-11-17 09:30:17 UTC (rev 3755)
+++ jbossxb/trunk/src/test/java/org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValueUnitTestCase.java 2009-11-18 15:13:53 UTC (rev 3756)
@@ -22,12 +22,20 @@
package org.jboss.test.xb.builder.object.attribute.test;
+import javax.xml.namespace.QName;
+
import org.jboss.test.xb.builder.AbstractBuilderTest;
import org.jboss.test.xb.builder.object.attribute.support.DefaultAttribute;
import org.jboss.util.xml.JBossEntityResolver;
import org.jboss.xb.binding.Unmarshaller;
import org.jboss.xb.binding.UnmarshallerFactory;
import org.jboss.xb.binding.resolver.MultiClassSchemaResolver;
+import org.jboss.xb.binding.sunday.unmarshalling.AttributeBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.ElementBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
+import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingInitializer;
+import org.jboss.xb.binding.sunday.unmarshalling.TypeBinding;
+import org.jboss.xb.builder.runtime.BeanHandler;
/**
* A SchemaDefaultAttributeValueUnitTestCase.
@@ -83,4 +91,54 @@
DefaultAttribute da = (DefaultAttribute) result;
assertEquals(new Integer(123), da.getAttribute());
}
+
+ /**
+ * This test demonstrates unmarshalling with default attribute values not added by the sax parser
+ *
+ * @throws Exception
+ */
+ public void testValidationDisabled() throws Exception
+ {
+ UnmarshallerFactory unmarshallerFactory = UnmarshallerFactory.newInstance();
+ Unmarshaller unmarshaller = unmarshallerFactory.newUnmarshaller();
+
+ // this is to make the SAX parser parse the XSD as well and include default attribute values in the startElement
+ //unmarshaller.setSchemaValidation(true);
+
+ JBossEntityResolver xmlResolver = new JBossEntityResolver();
+ xmlResolver.registerLocalEntity("http://www.hostame.org/SchemaDefaultAttributeValue.xsd", "org/jboss/test/xb/builder/object/attribute/test/SchemaDefaultAttributeValue.xsd");
+ unmarshaller.setEntityResolver(xmlResolver);
+
+ MultiClassSchemaResolver schemaBindingResolver = new MultiClassSchemaResolver();
+ schemaBindingResolver.mapURIToClass("xb:test:default-attribute", DefaultAttribute.class);
+
+ // add the default constraint to the attribute
+ schemaBindingResolver.mapSchemaInitializer("xb:test:default-attribute",
+ new SchemaBindingInitializer()
+ {
+ @Override
+ public SchemaBinding init(SchemaBinding schema)
+ {
+ ElementBinding element = schema.getElement(new QName("xb:test:default-attribute", "default-attribute"));
+ assertNotNull(element);
+ TypeBinding type = element.getType();
+ AttributeBinding attribute = type.getAttribute(new QName("attribute"));
+ assertNotNull(attribute);
+ attribute.setDefaultConstraint("123");
+ // with this kind attributes handler this has to be done...
+ BeanHandler handler = (BeanHandler) type.getHandler();
+ handler.getAttributesHandler().addAttribute(attribute);
+ return schema;
+ }
+ }
+ );
+
+ String xml = findXML("SchemaDefaultAttributeValue.xml");
+ Object result = unmarshaller.unmarshal(xml, schemaBindingResolver);
+
+ assertNotNull(result);
+ assertTrue(result instanceof DefaultAttribute);
+ DefaultAttribute da = (DefaultAttribute) result;
+ assertEquals(new Integer(123), da.getAttribute());
+ }
}
More information about the jboss-svn-commits
mailing list