Program: 64495
Project: 64495
Buggy source:
/** 
 * Convert the given object to XML and update the given marshal record with that XML Document.
 * @param object the object to marshal
 * @param marshalRecord the marshalRecord to marshal the object to
 * @param descriptor the XMLDescriptor for the object being marshalled
 */
private void marshal(Object object,MarshalRecord marshalRecord,AbstractSession session,XMLDescriptor descriptor,boolean isXMLRoot){
  if (null != schema) {
    marshalRecord=new ValidatingMarshalRecord(marshalRecord,this);
  }
  if (this.attachmentMarshaller != null) {
    marshalRecord.setXOPPackage(this.attachmentMarshaller.isXOPPackage());
  }
  marshalRecord.setMarshaller(this);
  if (this.mapper == null) {
    addDescriptorNamespacesToXMLRecord(descriptor,marshalRecord);
  }
 else   if (this.mapper != null) {
    if (descriptor == null) {
      marshalRecord.setNamespaceResolver(new PrefixMapperNamespaceResolver(mapper,null));
    }
 else {
      marshalRecord.setNamespaceResolver(new PrefixMapperNamespaceResolver(mapper,descriptor.getNamespaceResolver()));
    }
    marshalRecord.setCustomNamespaceMapper(true);
  }
  NamespaceResolver nr=marshalRecord.getNamespaceResolver();
  XMLRoot root=null;
  if (isXMLRoot) {
    root=(XMLRoot)object;
  }
  marshalRecord.beforeContainmentMarshal(object);
  if (!isFragment()) {
    String encoding=getEncoding();
    String version=DEFAULT_XML_VERSION;
    if (!isXMLRoot && descriptor != null) {
      marshalRecord.setLeafElementType(descriptor.getDefaultRootElementType());
    }
 else {
      if (root.getEncoding() != null) {
        encoding=root.getEncoding();
      }
      if (root.getXMLVersion() != null) {
        version=root.getXMLVersion();
      }
    }
    marshalRecord.startDocument(encoding,version);
  }
  if (getXmlHeader() != null) {
    marshalRecord.writeHeader();
  }
  if (isXMLRoot) {
    if (root.getObject() instanceof Node) {
      marshalRecord.node((Node)root.getObject(),new NamespaceResolver(),root.getNamespaceURI(),root.getLocalName());
      marshalRecord.endDocument();
      return;
    }
  }
  XPathFragment rootFragment=buildRootFragment(object,descriptor,isXMLRoot,marshalRecord);
  String schemaLocation=getSchemaLocation();
  String noNsSchemaLocation=getNoNamespaceSchemaLocation();
  boolean isNil=false;
  if (isXMLRoot) {
    object=root.getObject();
    if (root.getSchemaLocation() != null) {
      schemaLocation=root.getSchemaLocation();
    }
    if (root.getNoNamespaceSchemaLocation() != null) {
      noNsSchemaLocation=root.getNoNamespaceSchemaLocation();
    }
    marshalRecord.setLeafElementType(root.getSchemaType());
    isNil=root.isNil();
  }
  String xsiPrefix=null;
  if ((null != getSchemaLocation()) || (null != getNoNamespaceSchemaLocation()) || (isNil)) {
    xsiPrefix=nr.resolveNamespaceURI(XMLConstants.SCHEMA_INSTANCE_URL);
    if (null == xsiPrefix) {
      xsiPrefix=XMLConstants.SCHEMA_INSTANCE_PREFIX;
      nr.put(XMLConstants.SCHEMA_INSTANCE_PREFIX,XMLConstants.SCHEMA_INSTANCE_URL);
    }
  }
  TreeObjectBuilder treeObjectBuilder=null;
  if (descriptor != null) {
    treeObjectBuilder=(TreeObjectBuilder)descriptor.getObjectBuilder();
  }
  if (session == null) {
    session=(AbstractSession)xmlContext.getSession(0);
  }
  marshalRecord.setSession(session);
  if (null != rootFragment && !(rootFragment.getLocalName().equals(XMLConstants.EMPTY_STRING))) {
    marshalRecord.startPrefixMappings(nr);
    if (!isXMLRoot && descriptor != null && descriptor.getNamespaceResolver() == null && rootFragment.hasNamespace()) {
      throw XMLMarshalException.namespaceResolverNotSpecified(rootFragment.getShortName());
    }
    if (isIncludeRoot()) {
      marshalRecord.openStartElement(rootFragment,nr);
    }
    if (null != schemaLocation) {
      marshalRecord.attributeWithoutQName(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.SCHEMA_LOCATION,xsiPrefix,schemaLocation);
    }
    if (null != noNsSchemaLocation) {
      marshalRecord.attributeWithoutQName(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.NO_NS_SCHEMA_LOCATION,xsiPrefix,noNsSchemaLocation);
    }
    if (isNil) {
      marshalRecord.nilSimple(nr);
    }
    marshalRecord.namespaceDeclarations(nr);
    if (descriptor != null && !isNil) {
      treeObjectBuilder.addXsiTypeAndClassIndicatorIfRequired(marshalRecord,descriptor,null,descriptor.getDefaultRootElementField(),root,object,isXMLRoot,true);
      treeObjectBuilder.marshalAttributes(marshalRecord,object,session);
    }
    if (isIncludeRoot()) {
      marshalRecord.closeStartElement();
    }
  }
 else {
    marshalRecord.marshalWithoutRootElement(treeObjectBuilder,object,descriptor,root,isXMLRoot);
  }
  if (treeObjectBuilder != null && !isNil) {
    treeObjectBuilder.buildRow(marshalRecord,object,session,this,rootFragment,WriteType.UNDEFINED);
  }
 else   if (isXMLRoot) {
    if (object != null && !isNil) {
      if (root.getDeclaredType() != null && root.getObject() != null && root.getDeclaredType() != root.getObject().getClass()) {
        QName type=(QName)XMLConversionManager.getDefaultJavaTypes().get(object.getClass());
        if (type != null) {
          xsiPrefix=nr.resolveNamespaceURI(XMLConstants.SCHEMA_INSTANCE_URL);
          if (null == xsiPrefix) {
            xsiPrefix=XMLConstants.SCHEMA_INSTANCE_PREFIX;
            marshalRecord.namespaceDeclaration(xsiPrefix,XMLConstants.SCHEMA_INSTANCE_URL);
          }
          marshalRecord.namespaceDeclaration(XMLConstants.SCHEMA_PREFIX,XMLConstants.SCHEMA_URL);
          String typeValue=type.getLocalPart();
          if (marshalRecord.isNamespaceAware()) {
            typeValue=XMLConstants.SCHEMA_PREFIX + marshalRecord.getNamespaceSeparator() + typeValue;
          }
          marshalRecord.attribute(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.SCHEMA_TYPE_ATTRIBUTE,xsiPrefix + XMLConstants.COLON + XMLConstants.SCHEMA_TYPE_ATTRIBUTE,typeValue);
        }
      }
      marshalRecord.characters(root.getSchemaType(),object,null,false);
    }
  }
  if (null != rootFragment && !(rootFragment.getLocalName().equals(XMLConstants.EMPTY_STRING)) && isIncludeRoot()) {
    marshalRecord.endElement(rootFragment,nr);
    marshalRecord.endPrefixMappings(nr);
  }
  if (!isFragment()) {
    marshalRecord.endDocument();
  }
  marshalRecord.afterContainmentMarshal(null,object);
}

Fixed version (ground truth):
/** 
 * Convert the given object to XML and update the given marshal record with that XML Document.
 * @param object the object to marshal
 * @param marshalRecord the marshalRecord to marshal the object to
 * @param descriptor the XMLDescriptor for the object being marshalled
 */
private void marshal(Object object,MarshalRecord marshalRecord,AbstractSession session,XMLDescriptor descriptor,boolean isXMLRoot){
  if (null != schema) {
    marshalRecord=new ValidatingMarshalRecord(marshalRecord,this);
  }
  if (this.attachmentMarshaller != null) {
    marshalRecord.setXOPPackage(this.attachmentMarshaller.isXOPPackage());
  }
  marshalRecord.setMarshaller(this);
  if (this.mapper == null) {
    addDescriptorNamespacesToXMLRecord(descriptor,marshalRecord);
  }
 else   if (this.mapper != null) {
    if (descriptor == null) {
      marshalRecord.setNamespaceResolver(new PrefixMapperNamespaceResolver(mapper,null));
    }
 else {
      marshalRecord.setNamespaceResolver(new PrefixMapperNamespaceResolver(mapper,descriptor.getNamespaceResolver()));
    }
    marshalRecord.setCustomNamespaceMapper(true);
  }
  NamespaceResolver nr=marshalRecord.getNamespaceResolver();
  XMLRoot root=null;
  if (isXMLRoot) {
    root=(XMLRoot)object;
  }
  marshalRecord.beforeContainmentMarshal(object);
  if (!isFragment()) {
    String encoding=getEncoding();
    String version=DEFAULT_XML_VERSION;
    if (!isXMLRoot && descriptor != null) {
      marshalRecord.setLeafElementType(descriptor.getDefaultRootElementType());
    }
 else {
      if (root.getEncoding() != null) {
        encoding=root.getEncoding();
      }
      if (root.getXMLVersion() != null) {
        version=root.getXMLVersion();
      }
    }
    marshalRecord.startDocument(encoding,version);
  }
  if (getXmlHeader() != null) {
    marshalRecord.writeHeader();
  }
  if (isXMLRoot) {
    if (root.getObject() instanceof Node) {
      marshalRecord.node((Node)root.getObject(),new NamespaceResolver(),root.getNamespaceURI(),root.getLocalName());
      marshalRecord.endDocument();
      return;
    }
  }
  XPathFragment rootFragment=buildRootFragment(object,descriptor,isXMLRoot,marshalRecord);
  String schemaLocation=getSchemaLocation();
  String noNsSchemaLocation=getNoNamespaceSchemaLocation();
  boolean isNil=false;
  if (isXMLRoot) {
    object=root.getObject();
    if (root.getSchemaLocation() != null) {
      schemaLocation=root.getSchemaLocation();
    }
    if (root.getNoNamespaceSchemaLocation() != null) {
      noNsSchemaLocation=root.getNoNamespaceSchemaLocation();
    }
    marshalRecord.setLeafElementType(root.getSchemaType());
    isNil=root.isNil();
  }
  String xsiPrefix=null;
  if ((null != getSchemaLocation()) || (null != getNoNamespaceSchemaLocation()) || (isNil)) {
    xsiPrefix=nr.resolveNamespaceURI(XMLConstants.SCHEMA_INSTANCE_URL);
    if (null == xsiPrefix) {
      xsiPrefix=XMLConstants.SCHEMA_INSTANCE_PREFIX;
      nr.put(XMLConstants.SCHEMA_INSTANCE_PREFIX,XMLConstants.SCHEMA_INSTANCE_URL);
    }
  }
  TreeObjectBuilder treeObjectBuilder=null;
  if (descriptor != null) {
    treeObjectBuilder=(TreeObjectBuilder)descriptor.getObjectBuilder();
  }
  if (session == null) {
    session=(AbstractSession)xmlContext.getSession(0);
  }
  marshalRecord.setSession(session);
  if (null != rootFragment && !(rootFragment.getLocalName().equals(XMLConstants.EMPTY_STRING))) {
    marshalRecord.startPrefixMappings(nr);
    if (!isXMLRoot && descriptor != null && descriptor.getNamespaceResolver() == null && rootFragment.hasNamespace()) {
      throw XMLMarshalException.namespaceResolverNotSpecified(rootFragment.getShortName());
    }
    if (isIncludeRoot()) {
      marshalRecord.openStartElement(rootFragment,nr);
    }
    if (null != schemaLocation) {
      marshalRecord.attributeWithoutQName(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.SCHEMA_LOCATION,xsiPrefix,schemaLocation);
    }
    if (null != noNsSchemaLocation) {
      marshalRecord.attributeWithoutQName(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.NO_NS_SCHEMA_LOCATION,xsiPrefix,noNsSchemaLocation);
    }
    if (isNil) {
      marshalRecord.nilSimple(nr);
    }
    marshalRecord.namespaceDeclarations(nr);
    if (descriptor != null && !isNil) {
      treeObjectBuilder.addXsiTypeAndClassIndicatorIfRequired(marshalRecord,descriptor,null,descriptor.getDefaultRootElementField(),root,object,isXMLRoot,true);
      treeObjectBuilder.marshalAttributes(marshalRecord,object,session);
    }
    if (isIncludeRoot()) {
      marshalRecord.closeStartElement();
    }
  }
 else {
    marshalRecord.marshalWithoutRootElement(treeObjectBuilder,object,descriptor,root,isXMLRoot);
  }
  if (treeObjectBuilder != null && !isNil) {
    treeObjectBuilder.buildRow(marshalRecord,object,session,this,rootFragment,WriteType.UNDEFINED);
  }
 else   if (isXMLRoot) {
    if (object != null && !isNil) {
      if (root.getDeclaredType() != null && root.getObject() != null && root.getDeclaredType() != root.getObject().getClass()) {
        QName type=(QName)XMLConversionManager.getDefaultJavaTypes().get(object.getClass());
        if (type != null) {
          xsiPrefix=nr.resolveNamespaceURI(XMLConstants.SCHEMA_INSTANCE_URL);
          if (null == xsiPrefix) {
            xsiPrefix=XMLConstants.SCHEMA_INSTANCE_PREFIX;
            marshalRecord.namespaceDeclaration(xsiPrefix,XMLConstants.SCHEMA_INSTANCE_URL);
          }
          marshalRecord.namespaceDeclaration(XMLConstants.SCHEMA_PREFIX,XMLConstants.SCHEMA_URL);
          String typeValue=type.getLocalPart();
          if (marshalRecord.isNamespaceAware()) {
            typeValue=XMLConstants.SCHEMA_PREFIX + marshalRecord.getNamespaceSeparator() + typeValue;
          }
          marshalRecord.attribute(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.SCHEMA_TYPE_ATTRIBUTE,xsiPrefix + XMLConstants.COLON + XMLConstants.SCHEMA_TYPE_ATTRIBUTE,typeValue);
        }
      }
      marshalRecord.characters(root.getSchemaType(),object,null,false);
    }
  }
  if (null != rootFragment && !(rootFragment.getLocalName().equals(XMLConstants.EMPTY_STRING)) && isIncludeRoot()) {
    marshalRecord.endElement(rootFragment,nr);
    marshalRecord.endPrefixMappings(nr);
  }
  if (!isFragment()) {
    marshalRecord.endDocument();
  }
  marshalRecord.afterContainmentMarshal(null,isXMLRoot ? root : object);
}