Program: 189739
Project: 189739
Buggy source:
/** 
 * Serializes the result of a RESTXQ Service
 * @param result The result of the RESTXQ Service's Resource Function invokation
 * @param serializationAnnotations Serialization Annotations which were present on the Resource Function
 * @param response The HTTP Response to Serialize the result to
 * @throws RestXqServiceException If an error occurs during serialization
 */
@Override public void serialize(final Sequence result,final Set<SerializationAnnotation> serializationAnnotations,final HttpResponse response) throws RestXqServiceException {
  if (response.isCommitted()) {
    return;
  }
  final Iterator<TypedValue> itResult=result.iterator();
  if (itResult.hasNext()) {
    final TypedValue firstResultPart=itResult.next();
    Element elem=null;
    if (firstResultPart.getType().equals(Type.DOCUMENT)) {
      elem=((Document)firstResultPart.getValue()).getDocumentElement();
    }
 else     if (firstResultPart.getType().equals(Type.ELEMENT)) {
      elem=(Element)firstResultPart.getValue();
    }
    final Map<SerializationProperty,String> serializationProperties=new EnumMap<SerializationProperty,String>(SerializationProperty.class);
    serializationProperties.putAll(getDefaultSerializationProperties());
    if (elem != null && new QName(elem.getNamespaceURI(),elem.getLocalName()).equals(RestResponseHandler.REST_RESPONSE_ELEMENT_NAME)) {
      processSerializationAnnotations(serializationAnnotations,serializationProperties);
      new RestResponseHandler().process(elem,serializationProperties,response);
      if (itResult.hasNext()) {
        final TypedValue secondResultPart=itResult.next();
        serializeBody(secondResultPart,response,serializationProperties);
      }
    }
 else {
      processSerializationAnnotations(serializationAnnotations,serializationProperties);
      serializeBody(firstResultPart,response,serializationProperties);
    }
  }
}

Fixed version (ground truth):
/** 
 * Serializes the result of a RESTXQ Service
 * @param result The result of the RESTXQ Service's Resource Function invokation
 * @param serializationAnnotations Serialization Annotations which were present on the Resource Function
 * @param response The HTTP Response to Serialize the result to
 * @throws RestXqServiceException If an error occurs during serialization
 */
@Override public void serialize(final Sequence result,final Set<SerializationAnnotation> serializationAnnotations,final HttpResponse response) throws RestXqServiceException {
  if (response.isCommitted()) {
    return;
  }
  final Iterator<TypedValue> itResult=result.iterator();
  if (itResult.hasNext()) {
    final TypedValue firstResultPart=itResult.next();
    Element elem=null;
    if (firstResultPart.getType().equals(Type.DOCUMENT)) {
      elem=((Document)firstResultPart.getValue()).getDocumentElement();
    }
 else     if (firstResultPart.getType().equals(Type.ELEMENT)) {
      elem=(Element)firstResultPart.getValue();
    }
    final Map<SerializationProperty,String> serializationProperties=new EnumMap<SerializationProperty,String>(SerializationProperty.class);
    serializationProperties.putAll(getDefaultSerializationProperties());
    if (elem != null && new QName(elem.getNamespaceURI(),elem.getLocalName()).equals(RestResponseHandler.REST_RESPONSE_ELEMENT_NAME)) {
      processSerializationAnnotations(serializationAnnotations,serializationProperties);
      new RestResponseHandler().process(elem,serializationProperties,response);
      if (itResult.hasNext()) {
        final Sequence seqBody=result.tail();
        serializeBody(seqBody,response,serializationProperties);
      }
    }
 else {
      processSerializationAnnotations(serializationAnnotations,serializationProperties);
      serializeBody(result,response,serializationProperties);
    }
  }
}