Index: src/jaxme/org/apache/ws/jaxme/generator/sg/impl/JAXBGroupSG.java
===================================================================
RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/generator/sg/impl/JAXBGroupSG.java,v
retrieving revision 1.10.2.1
diff -u -r1.10.2.1 JAXBGroupSG.java
--- src/jaxme/org/apache/ws/jaxme/generator/sg/impl/JAXBGroupSG.java	27 Jul 2004 08:40:59 -0000	1.10.2.1
+++ src/jaxme/org/apache/ws/jaxme/generator/sg/impl/JAXBGroupSG.java	20 Aug 2004 20:00:23 -0000
@@ -24,6 +24,7 @@
 import java.util.Set;
 
 import javax.xml.bind.JAXBException;
+import javax.xml.bind.MarshalException;
 import javax.xml.namespace.QName;
 
 import org.apache.ws.jaxme.JMElement;
@@ -243,14 +244,7 @@
       String serializerName = getXMLSerializersFieldName(particle.getPropertySG());
       Context myClassContext = child.getTypeSG().getComplexTypeSG().getClassContext();
       JavaQName serializerClass = myClassContext.getXMLSerializerName();
-      JavaField jf = pSource.newJavaField(serializerName, serializerClass, JavaSource.PRIVATE);
-      JavaQName elementInterface = myClassContext.getXMLInterfaceName();
-      if (child.getTypeSG().isGlobalClass()) {
-        jm.addLine(jf, " = (", serializerClass, ") ", pFactory, ".getJMXmlSerializer(", elementInterface, ".class);");
-      } else {
-        jm.addLine(jf, " = new ", serializerClass, "();");
-      }
-      jm.addLine(jf, ".init(pFactory);");
+      pSource.newJavaField(serializerName, serializerClass, JavaSource.PRIVATE);
     }
     return jm;
   }
@@ -284,7 +278,25 @@
       f.addLine("new ", QName.class, "(", JavaSource.getQuoted(objectSG.getName().getNamespaceURI()),
                 ", ", JavaSource.getQuoted(objectSG.getName().getLocalName()), ");");
       if (objectSG.getTypeSG().isComplex()) {
-        pMethod.addLine(fieldName, ".marshal(", data, ", ", f, ", ", pValue, ");");
+          Context myClassContext =
+              child.getObjectSG().getTypeSG().getComplexTypeSG().getClassContext();
+          JavaQName serializerClass = myClassContext.getXMLSerializerName();
+          JavaQName elementInterface = myClassContext.getXMLInterfaceName();
+          pMethod.addIf(fieldName + " == null");
+          pMethod.addTry();
+          if (child.getObjectSG().getTypeSG().isGlobalClass()) {
+              pMethod.addLine(fieldName + " = ("+ serializerClass+ ") ",
+                      	      "getFactory().getJMXmlSerializer(", elementInterface,
+                      	      ".class);");
+    	  } else {
+    	      pMethod.addLine(fieldName + " = new ", serializerClass, "();");
+              pMethod.addLine(fieldName + ".init(getFactory());");
+    	  }
+          DirectAccessible e = pMethod.addCatch(JAXBException.class);
+          pMethod.addThrowNew(SAXException.class, e);
+          pMethod.addEndTry();
+          pMethod.addEndIf();
+          pMethod.addLine(fieldName, ".marshal(", data, ", ", f, ", ", pValue, ");");
       } else {
         Object v = child.getObjectSG().getTypeSG().getSimpleTypeSG().getCastToString(pMethod, pValue, data);
         pMethod.addLine("marshalAtomicChild(", data, ", ", f, ", ", v, ");");
Index: src/jaxme/org/apache/ws/jaxme/generator/sg/impl/JAXBTypeSG.java
===================================================================
RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/generator/sg/impl/JAXBTypeSG.java,v
retrieving revision 1.7
diff -u -r1.7 JAXBTypeSG.java
--- src/jaxme/org/apache/ws/jaxme/generator/sg/impl/JAXBTypeSG.java	2 Mar 2004 21:15:17 -0000	1.7
+++ src/jaxme/org/apache/ws/jaxme/generator/sg/impl/JAXBTypeSG.java	20 Aug 2004 20:00:27 -0000
@@ -256,33 +256,6 @@
     log.finest(mName, "<-");
   }
 
-  private boolean hasDefaultValue(XSType type) throws SAXException {
-    if (type.getComplexType().hasSimpleContent()) {
-      return false;
-    }
-    XSParticle particle = type.getComplexType().getParticle();
-    if (particle == null) {
-      return false;
-    }
-    XSParticle[] particles = particle.getGroup().getParticles();
-
-    for (int i = 0; i < particles.length; i++) {
-      XSElement subElement = particles[i].getElement();
-      XSType subType = subElement.getType();
-      if (subType.isSimple()) {
-        if (subElement.getDefault() != null) {
-          return true;
-        }
-      }
-      else {
-        if (hasDefaultValue(subType)) {
-          return true;
-        }
-      }
-    }
-    return false;
-  }
-
   public JavaField getXMLField(TypeSG pController, JavaSource pSource, 
                                  String pFieldName, String pDefaultValue) throws SAXException {
     if (pSource.isInterface()) {
@@ -302,12 +275,7 @@
         runtimeType = pController.getRuntimeType();
       } 
       JavaField jf = pSource.newJavaField(pFieldName, runtimeType, JavaSource.PRIVATE);
-      if (pController.isComplex()) {
-        if (hasDefaultValue(xsType)) {
-          jf.addLine("new " + pController.getRuntimeType().getInnerClassName() + "Impl()");
-        }
-      }
-      else {
+      if (!pController.isComplex()) {
         Object o = pController.getSimpleTypeSG().getInitialValue(pSource);
         if (o == null && pDefaultValue != null) {
           if (runtimeType.equals(StringSG.STRING_TYPE)) {
Index: src/jaxme/org/apache/ws/jaxme/impl/JMXmlSerializerImpl.java
===================================================================
RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/impl/JMXmlSerializerImpl.java,v
retrieving revision 1.3.2.1
diff -u -r1.3.2.1 JMXmlSerializerImpl.java
--- src/jaxme/org/apache/ws/jaxme/impl/JMXmlSerializerImpl.java	16 Aug 2004 08:29:17 -0000	1.3.2.1
+++ src/jaxme/org/apache/ws/jaxme/impl/JMXmlSerializerImpl.java	20 Aug 2004 20:00:27 -0000
@@ -80,7 +80,15 @@
 	}
   }
 
-  public void init(JAXBContextImpl pFactory) throws JAXBException {}
+  private JAXBContextImpl factory;
+
+  public void init(JAXBContextImpl pFactory) throws JAXBException {
+      factory = pFactory;
+  }
+
+  public JAXBContextImpl getFactory() {
+      return factory;
+  }
 
   public String getPreferredPrefix(String pURI) {
     return null;
Index: src/jaxme/org/apache/ws/jaxme/junit/DefaultValueTest.java
===================================================================
RCS file: /home/cvs/ws-jaxme/src/jaxme/org/apache/ws/jaxme/junit/DefaultValueTest.java,v
retrieving revision 1.2
diff -u -r1.2 DefaultValueTest.java
--- src/jaxme/org/apache/ws/jaxme/junit/DefaultValueTest.java	16 Feb 2004 23:39:54 -0000	1.2
+++ src/jaxme/org/apache/ws/jaxme/junit/DefaultValueTest.java	20 Aug 2004 20:00:31 -0000
@@ -25,6 +25,7 @@
 import org.apache.ws.jaxme.test.misc.defaults.Persons;
 import org.apache.ws.jaxme.test.misc.defaults.PersonsType.PersonType;
 import org.apache.ws.jaxme.test.misc.defaults.PersonsType.PersonType.NameType;
+import org.apache.ws.jaxme.test.misc.defaults.impl.PersonsTypeImpl.PersonTypeImpl.NameTypeImpl;
 import org.xml.sax.InputSource;
 
 /**
@@ -68,6 +69,9 @@
     i.hasNext();
     person = (PersonType) i.next();
     name = person.getName();
+    if (name == null) {
+        name = new NameTypeImpl();
+    }
     assertEquals("Anonymous", name.getFirst());
     assertEquals(null, name.getLast());
     assertEquals("Cb", person.getAlias());
