Program: 3577
Project: 3577
Buggy source:
/** 
 * Write out a METS manifest. Mostly lifted from Rob Tansley's METS exporter.
 */
private void writeManifest(Context context,Item item,PackageParameters params,OutputStream out) throws PackageValidationException, CrosswalkException, AuthorizeException, SQLException, IOException {
  try {
    Mets mets=new Mets();
    mets.setID(gensym("String_Node_Str"));
    mets.setOBJID("String_Node_Str" + item.getHandle());
    mets.setLABEL("String_Node_Str");
    mets.setPROFILE(getProfile());
    MetsHdr metsHdr=new MetsHdr();
    metsHdr.setCREATEDATE(new Date());
    Agent agent=new Agent();
    agent.setROLE(Role.CUSTODIAN);
    agent.setTYPE(Type.ORGANIZATION);
    Name name=new Name();
    name.getContent().add(new PCData(ConfigurationManager.getProperty("String_Node_Str")));
    agent.getContent().add(name);
    metsHdr.getContent().add(agent);
    mets.getContent().add(metsHdr);
    String dmdTypes[]=getDmdTypes(params);
    String dmdGroup=gensym("String_Node_Str");
    String dmdId[]=new String[dmdTypes.length];
    for (int i=0; i < dmdTypes.length; ++i) {
      dmdId[i]=gensym("String_Node_Str");
      XmlData xmlData=new XmlData();
      String xwalkName, metsName;
      String parts[]=dmdTypes[i].split("String_Node_Str",2);
      if (parts.length > 1) {
        metsName=parts[0];
        xwalkName=parts[1];
      }
 else       xwalkName=metsName=dmdTypes[i];
      DisseminationCrosswalk xwalk=(DisseminationCrosswalk)PluginManager.getNamedPlugin(DisseminationCrosswalk.class,xwalkName);
      if (xwalk == null)       throw new PackageValidationException("String_Node_Str" + dmdTypes[i] + "String_Node_Str");
 else       crosswalkToMets(xwalk,item,xmlData);
      DmdSec dmdSec=new DmdSec();
      dmdSec.setID(dmdId[i]);
      dmdSec.setGROUPID(dmdGroup);
      MdWrap mdWrap=new MdWrap();
      setMdType(mdWrap,metsName);
      mdWrap.getContent().add(xmlData);
      dmdSec.getContent().add(mdWrap);
      mets.getContent().add(dmdSec);
    }
    String licenseID=null;
    AmdSec amdSec=new AmdSec();
    addRightsMd(context,item,amdSec);
    if (amdSec.getContent().size() > 0) {
      licenseID=gensym("String_Node_Str");
      amdSec.setID(licenseID);
      mets.getContent().add(amdSec);
    }
    FileSec fileSec=new FileSec();
    String techMdType=getTechMdType(params);
    String parts[]=techMdType.split("String_Node_Str",2);
    String xwalkName, metsName;
    if (parts.length > 1) {
      metsName=parts[0];
      xwalkName=parts[1];
    }
 else     xwalkName=metsName=techMdType;
    DisseminationCrosswalk xwalk=(DisseminationCrosswalk)PluginManager.getNamedPlugin(DisseminationCrosswalk.class,xwalkName);
    if (xwalk == null)     throw new PackageValidationException("String_Node_Str" + xwalkName + "String_Node_Str");
    String primaryBitstreamFileID=null;
    List contentDivs=new ArrayList();
    Bundle[] bundles=item.getBundles();
    for (int i=0; i < bundles.length; i++) {
      if (PackageUtils.isMetaInfoBundle(bundles[i]))       continue;
      Bitstream[] bitstreams=bundles[i].getBitstreams();
      FileGrp fileGrp=new FileGrp();
      String bName=bundles[i].getName();
      if ((bName != null) && !bName.equals("String_Node_Str"))       fileGrp.setUSE(bundleToFileGrp(bName));
      int primaryBitstreamID=-1;
      boolean isContentBundle=false;
      if ((bName != null) && bName.equals("String_Node_Str")) {
        isContentBundle=true;
        primaryBitstreamID=bundles[i].getPrimaryBitstreamID();
      }
      for (int bits=0; bits < bitstreams.length; bits++) {
        String sid=String.valueOf(bitstreams[bits].getSequenceID());
        edu.harvard.hul.ois.mets.File file=new edu.harvard.hul.ois.mets.File();
        String xmlIDstart="String_Node_Str";
        String fileID=xmlIDstart + sid;
        file.setID(fileID);
        if (bitstreams[bits].getID() == primaryBitstreamID)         primaryBitstreamFileID=fileID;
        if (isContentBundle) {
          Div div=new Div();
          div.setID(gensym("String_Node_Str"));
          div.setTYPE("String_Node_Str");
          Fptr fptr=new Fptr();
          fptr.setFILEID(fileID);
          div.getContent().add(fptr);
          contentDivs.add(div);
        }
        file.setSEQ(bitstreams[bits].getSequenceID());
        String groupID="String_Node_Str" + xmlIDstart + sid;
        if ((bundles[i].getName() != null) && (bundles[i].getName().equals("String_Node_Str") || bundles[i].getName().startsWith("String_Node_Str"))) {
          Bitstream original=findOriginalBitstream(item,bitstreams[bits]);
          if (original != null) {
            groupID="String_Node_Str" + xmlIDstart + original.getSequenceID();
          }
        }
        file.setGROUPID(groupID);
        file.setMIMETYPE(bitstreams[bits].getFormat().getMIMEType());
        file.setSIZE(bitstreams[bits].getSize());
        String csType=bitstreams[bits].getChecksumAlgorithm();
        String cs=bitstreams[bits].getChecksum();
        if (cs != null && csType != null) {
          try {
            file.setCHECKSUMTYPE(Checksumtype.parse(csType));
            file.setCHECKSUM(cs);
          }
 catch (          MetsException e) {
            log.warn("String_Node_Str" + csType + "String_Node_Str");
          }
        }
        FLocat flocat=new FLocat();
        flocat.setLOCTYPE(Loctype.URL);
        flocat.setXlinkHref(makeBitstreamName(bitstreams[bits]));
        String techID="String_Node_Str" + bitstreams[bits].getSequenceID();
        AmdSec fAmdSec=new AmdSec();
        fAmdSec.setID(techID);
        TechMD techMd=new TechMD();
        techMd.setID(gensym("String_Node_Str"));
        MdWrap mdWrap=new MdWrap();
        setMdType(mdWrap,metsName);
        XmlData xmlData=new XmlData();
        mdWrap.getContent().add(xmlData);
        techMd.getContent().add(mdWrap);
        fAmdSec.getContent().add(techMd);
        mets.getContent().add(fAmdSec);
        crosswalkToMets(xwalk,bitstreams[bits],xmlData);
        file.setADMID(techID);
        file.getContent().add(flocat);
        fileGrp.getContent().add(file);
      }
      fileSec.getContent().add(fileGrp);
    }
    mets.getContent().add(fileSec);
    StringBuffer dmdIds=new StringBuffer();
    for (int i=0; i < dmdId.length; ++i)     dmdIds.append("String_Node_Str" + dmdId[i]);
    StructMap structMap=new StructMap();
    structMap.setID(gensym("String_Node_Str"));
    structMap.setTYPE("String_Node_Str");
    structMap.setLABEL("String_Node_Str");
    Div div0=new Div();
    div0.setID(gensym("String_Node_Str"));
    div0.setTYPE("String_Node_Str");
    div0.setDMDID(dmdIds.substring(1));
    if (licenseID != null)     div0.setADMID(licenseID);
    if (primaryBitstreamFileID != null) {
      Fptr fptr=new Fptr();
      fptr.setFILEID(primaryBitstreamFileID);
      div0.getContent().add(fptr);
    }
    div0.getContent().addAll(contentDivs);
    structMap.getContent().add(div0);
    addStructMap(context,item,params,mets);
    mets.getContent().add(structMap);
    mets.validate(new MetsValidator());
    mets.write(new MetsWriter(out));
  }
 catch (  MetsException e) {
    throw new PackageValidationException(e);
  }
}

Fixed version (ground truth):
/** 
 * Write out a METS manifest. Mostly lifted from Rob Tansley's METS exporter.
 */
private void writeManifest(Context context,Item item,PackageParameters params,OutputStream out) throws PackageValidationException, CrosswalkException, AuthorizeException, SQLException, IOException {
  try {
    Mets mets=new Mets();
    mets.setID(gensym("String_Node_Str"));
    mets.setOBJID("String_Node_Str" + item.getHandle());
    mets.setLABEL("String_Node_Str");
    mets.setPROFILE(getProfile());
    MetsHdr metsHdr=new MetsHdr();
    metsHdr.setCREATEDATE(new Date());
    Agent agent=new Agent();
    agent.setROLE(Role.CUSTODIAN);
    agent.setTYPE(Type.ORGANIZATION);
    Name name=new Name();
    name.getContent().add(new PCData(ConfigurationManager.getProperty("String_Node_Str")));
    agent.getContent().add(name);
    metsHdr.getContent().add(agent);
    mets.getContent().add(metsHdr);
    String dmdTypes[]=getDmdTypes(params);
    String dmdGroup=gensym("String_Node_Str");
    String dmdId[]=new String[dmdTypes.length];
    for (int i=0; i < dmdTypes.length; ++i) {
      dmdId[i]=gensym("String_Node_Str");
      XmlData xmlData=new XmlData();
      String xwalkName, metsName;
      String parts[]=dmdTypes[i].split("String_Node_Str",2);
      if (parts.length > 1) {
        metsName=parts[0];
        xwalkName=parts[1];
      }
 else       xwalkName=metsName=dmdTypes[i];
      DisseminationCrosswalk xwalk=(DisseminationCrosswalk)PluginManager.getNamedPlugin(DisseminationCrosswalk.class,xwalkName);
      if (xwalk == null)       throw new PackageValidationException("String_Node_Str" + dmdTypes[i] + "String_Node_Str");
 else       crosswalkToMets(xwalk,item,xmlData);
      DmdSec dmdSec=new DmdSec();
      dmdSec.setID(dmdId[i]);
      dmdSec.setGROUPID(dmdGroup);
      MdWrap mdWrap=new MdWrap();
      setMdType(mdWrap,metsName);
      mdWrap.getContent().add(xmlData);
      dmdSec.getContent().add(mdWrap);
      mets.getContent().add(dmdSec);
    }
    String licenseID=null;
    try {
      AmdSec amdSec=new AmdSec();
      addRightsMd(context,item,amdSec);
      if (amdSec.getContent().size() > 0) {
        licenseID=gensym("String_Node_Str");
        amdSec.setID(licenseID);
        mets.getContent().add(amdSec);
      }
    }
 catch (    AuthorizeException e) {
      String unauth=(params == null) ? null : params.getProperty("String_Node_Str");
      if (!(unauth != null && unauth.equalsIgnoreCase("String_Node_Str")))       throw e;
 else       log.warn("String_Node_Str" + e.toString());
    }
    FileSec fileSec=new FileSec();
    String techMdType=getTechMdType(params);
    String parts[]=techMdType.split("String_Node_Str",2);
    String xwalkName, metsName;
    if (parts.length > 1) {
      metsName=parts[0];
      xwalkName=parts[1];
    }
 else     xwalkName=metsName=techMdType;
    DisseminationCrosswalk xwalk=(DisseminationCrosswalk)PluginManager.getNamedPlugin(DisseminationCrosswalk.class,xwalkName);
    if (xwalk == null)     throw new PackageValidationException("String_Node_Str" + xwalkName + "String_Node_Str");
    String primaryBitstreamFileID=null;
    List contentDivs=new ArrayList();
    String unauth=(params == null) ? null : params.getProperty("String_Node_Str");
    Bundle[] bundles=item.getBundles();
    for (int i=0; i < bundles.length; i++) {
      if (PackageUtils.isMetaInfoBundle(bundles[i]))       continue;
      if (!AuthorizeManager.authorizeActionBoolean(context,bundles[i],Constants.READ)) {
        if (unauth != null && (unauth.equalsIgnoreCase("String_Node_Str")))         continue;
 else         throw new AuthorizeException("String_Node_Str" + bundles[i].getName() + "String_Node_Str");
      }
      Bitstream[] bitstreams=bundles[i].getBitstreams();
      FileGrp fileGrp=new FileGrp();
      String bName=bundles[i].getName();
      if ((bName != null) && !bName.equals("String_Node_Str"))       fileGrp.setUSE(bundleToFileGrp(bName));
      int primaryBitstreamID=-1;
      boolean isContentBundle=false;
      if ((bName != null) && bName.equals("String_Node_Str")) {
        isContentBundle=true;
        primaryBitstreamID=bundles[i].getPrimaryBitstreamID();
      }
      for (int bits=0; bits < bitstreams.length; bits++) {
        boolean auth=AuthorizeManager.authorizeActionBoolean(context,bitstreams[bits],Constants.READ);
        if (!auth) {
          if (unauth != null && unauth.equalsIgnoreCase("String_Node_Str"))           continue;
 else           if (!(unauth != null && unauth.equalsIgnoreCase("String_Node_Str")))           throw new AuthorizeException("String_Node_Str" + String.valueOf(bitstreams[bits].getSequenceID()));
        }
        String sid=String.valueOf(bitstreams[bits].getSequenceID());
        edu.harvard.hul.ois.mets.File file=new edu.harvard.hul.ois.mets.File();
        String xmlIDstart="String_Node_Str";
        String fileID=xmlIDstart + sid;
        file.setID(fileID);
        if (bitstreams[bits].getID() == primaryBitstreamID)         primaryBitstreamFileID=fileID;
        if (isContentBundle) {
          Div div=new Div();
          div.setID(gensym("String_Node_Str"));
          div.setTYPE("String_Node_Str");
          Fptr fptr=new Fptr();
          fptr.setFILEID(fileID);
          div.getContent().add(fptr);
          contentDivs.add(div);
        }
        file.setSEQ(bitstreams[bits].getSequenceID());
        String groupID="String_Node_Str" + xmlIDstart + sid;
        if ((bundles[i].getName() != null) && (bundles[i].getName().equals("String_Node_Str") || bundles[i].getName().startsWith("String_Node_Str"))) {
          Bitstream original=findOriginalBitstream(item,bitstreams[bits]);
          if (original != null) {
            groupID="String_Node_Str" + xmlIDstart + original.getSequenceID();
          }
        }
        file.setGROUPID(groupID);
        file.setMIMETYPE(bitstreams[bits].getFormat().getMIMEType());
        file.setSIZE(auth ? bitstreams[bits].getSize() : 0);
        String csType=bitstreams[bits].getChecksumAlgorithm();
        String cs=bitstreams[bits].getChecksum();
        if (auth && cs != null && csType != null) {
          try {
            file.setCHECKSUMTYPE(Checksumtype.parse(csType));
            file.setCHECKSUM(cs);
          }
 catch (          MetsException e) {
            log.warn("String_Node_Str" + csType + "String_Node_Str");
          }
        }
        FLocat flocat=new FLocat();
        flocat.setLOCTYPE(Loctype.URL);
        flocat.setXlinkHref(makeBitstreamName(bitstreams[bits]));
        String techID="String_Node_Str" + bitstreams[bits].getSequenceID();
        AmdSec fAmdSec=new AmdSec();
        fAmdSec.setID(techID);
        TechMD techMd=new TechMD();
        techMd.setID(gensym("String_Node_Str"));
        MdWrap mdWrap=new MdWrap();
        setMdType(mdWrap,metsName);
        XmlData xmlData=new XmlData();
        mdWrap.getContent().add(xmlData);
        techMd.getContent().add(mdWrap);
        fAmdSec.getContent().add(techMd);
        mets.getContent().add(fAmdSec);
        crosswalkToMets(xwalk,bitstreams[bits],xmlData);
        file.setADMID(techID);
        file.getContent().add(flocat);
        fileGrp.getContent().add(file);
      }
      fileSec.getContent().add(fileGrp);
    }
    mets.getContent().add(fileSec);
    StringBuffer dmdIds=new StringBuffer();
    for (int i=0; i < dmdId.length; ++i)     dmdIds.append("String_Node_Str" + dmdId[i]);
    StructMap structMap=new StructMap();
    structMap.setID(gensym("String_Node_Str"));
    structMap.setTYPE("String_Node_Str");
    structMap.setLABEL("String_Node_Str");
    Div div0=new Div();
    div0.setID(gensym("String_Node_Str"));
    div0.setTYPE("String_Node_Str");
    div0.setDMDID(dmdIds.substring(1));
    if (licenseID != null)     div0.setADMID(licenseID);
    if (primaryBitstreamFileID != null) {
      Fptr fptr=new Fptr();
      fptr.setFILEID(primaryBitstreamFileID);
      div0.getContent().add(fptr);
    }
    div0.getContent().addAll(contentDivs);
    structMap.getContent().add(div0);
    addStructMap(context,item,params,mets);
    mets.getContent().add(structMap);
    mets.validate(new MetsValidator());
    mets.write(new MetsWriter(out));
  }
 catch (  MetsException e) {
    throw new PackageValidationException(e);
  }
}