📄 itemexport.java
字号:
} private static void exportItem(Context c, ItemIterator i, String destDirName, int seqStart) throws Exception { int mySequenceNumber = seqStart; System.out.println("Beginning export"); while (i.hasNext()) { System.out.println("Exporting item to " + mySequenceNumber); exportItem(c, i.next(), destDirName, mySequenceNumber); mySequenceNumber++; } } private static void exportItem(Context c, Item myItem, String destDirName, int seqStart) throws Exception { File destDir = new File(destDirName); if (destDir.exists()) { // now create a subdirectory File itemDir = new File(destDir + "/" + seqStart); System.out.println("Exporting Item " + myItem.getID() + " to " + itemDir); if (itemDir.exists()) { throw new Exception("Directory " + destDir + "/" + seqStart + " already exists!"); } if (itemDir.mkdir()) { // make it this far, now start exporting writeMetadata(c, myItem, itemDir); writeBitstreams(c, myItem, itemDir); writeHandle(c, myItem, itemDir); } else { throw new Exception("Error, can't make dir " + itemDir); } } else { throw new Exception("Error, directory " + destDirName + " doesn't exist!"); } } /** * Discover the different schemas in use and output a seperate metadata * XML file for each schema. * * @param c * @param i * @param destDir * @throws Exception */ private static void writeMetadata(Context c, Item i, File destDir) throws Exception { // Build a list of schemas for the item HashMap map = new HashMap(); DCValue[] dcorevalues = i.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY); for (int ii = 0; ii < dcorevalues.length; ii++) { map.put(dcorevalues[ii].schema, null); } // Save each of the schemas into it's own metadata file Iterator iterator = map.keySet().iterator(); while (iterator.hasNext()) { String schema = (String) iterator.next(); writeMetadata(c, schema, i, destDir); } } // output the item's dublin core into the item directory private static void writeMetadata(Context c, String schema, Item i, File destDir) throws Exception { String filename; if (schema.equals(MetadataSchema.DC_SCHEMA)) { filename = "dublin_core.xml"; } else { filename = "metadata_" + schema + ".xml"; } File outFile = new File(destDir, filename); System.out.println("Attempting to create file " + outFile); if (outFile.createNewFile()) { BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(outFile)); DCValue[] dcorevalues = i.getMetadata(schema, Item.ANY, Item.ANY, Item.ANY); // XML preamble byte[] utf8 = "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>\n" .getBytes("UTF-8"); out.write(utf8, 0, utf8.length); String dcTag = "<dublin_core schema=\""+schema+"\">\n"; utf8 = dcTag.getBytes("UTF-8"); out.write(utf8, 0, utf8.length); for (int j = 0; j < dcorevalues.length; j++) { DCValue dcv = dcorevalues[j]; String qualifier = dcv.qualifier; if (qualifier == null) { qualifier = "none"; } utf8 = (" <dcvalue element=\"" + dcv.element + "\" " + "qualifier=\"" + qualifier + "\">" + Utils.addEntities(dcv.value) + "</dcvalue>\n").getBytes("UTF-8"); out.write(utf8, 0, utf8.length); } utf8 = "</dublin_core>\n".getBytes("UTF-8"); out.write(utf8, 0, utf8.length); out.close(); } else { throw new Exception("Cannot create dublin_core.xml in " + destDir); } } // create the file 'handle' which contains the handle assigned to the item private static void writeHandle(Context c, Item i, File destDir) throws Exception { String filename = "handle"; File outFile = new File(destDir, filename); if (outFile.createNewFile()) { PrintWriter out = new PrintWriter(new FileWriter(outFile)); out.println(i.getHandle()); // close the contents file out.close(); } else { throw new Exception("Cannot create file " + filename + " in " + destDir); } } /** * Create both the bitstreams and the contents file. Any bitstreams that * were originally registered will be marked in the contents file as such. * However, the export directory will contain actual copies of the content * files being exported. * * @param c the DSpace context * @param i the item being exported * @param destDir the item's export directory * @throws Exception if there is any problem writing to the export * directory */ private static void writeBitstreams(Context c, Item i, File destDir) throws Exception { File outFile = new File(destDir, "contents"); if (outFile.createNewFile()) { PrintWriter out = new PrintWriter(new FileWriter(outFile)); Bundle[] bundles = i.getBundles(); for (int j = 0; j < bundles.length; j++) { // bundles can have multiple bitstreams now... Bitstream[] bitstreams = bundles[j].getBitstreams(); String bundleName = bundles[j].getName(); for (int k = 0; k < bitstreams.length; k++) { Bitstream b = bitstreams[k]; String myName = b.getName(); String oldName = myName; int myPrefix = 1; // only used with name conflict InputStream is = b.retrieve(); boolean isDone = false; // done when bitstream is finally // written while (!isDone) { File fout = new File(destDir, myName); if (fout.createNewFile()) { FileOutputStream fos = new FileOutputStream(fout); Utils.bufferedCopy(is, fos); // close streams is.close(); fos.close(); // write the manifest file entry if (b.isRegisteredBitstream()) { out.println("-r -s " + b.getStoreNumber() + " -f " + myName + "\tbundle:" + bundleName); } else { out.println(myName + "\tbundle:" + bundleName); } isDone = true; } else { myName = myPrefix + "_" + oldName; // keep appending // numbers to the // filename until // unique myPrefix++; } } } } // close the contents file out.close(); } else { throw new Exception("Cannot create contents in " + destDir); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -