📄 itemimport.java
字号:
if (language.equals("")) { language = ConfigurationManager.getProperty("default.language"); } // a goofy default, but there it is if (language == null) { language = "en"; } if (!isTest) { i.addMetadata(schema, element, qualifier, language, value); } } /** * Return the String value of a Node */ public String getStringValue(Node node) { String value = node.getNodeValue(); if (node.hasChildNodes()) { Node first = node.getFirstChild(); if (first.getNodeType() == Node.TEXT_NODE) { return first.getNodeValue(); } } return value; } /** * Read in the handle file or return null if empty or doesn't exist */ private String processHandleFile(Context c, Item i, String path, String filename) { String filePath = path + File.separatorChar + filename; String line = ""; String result = null; System.out.println("Processing handle file: " + filename); BufferedReader is = null; try { is = new BufferedReader(new FileReader(filePath)); // result gets contents of file, or null result = is.readLine(); System.out.println("read handle: '" + result + "'"); } catch (Exception e) { // probably no handle file, just return null System.out .println("It appears there is no handle file -- generating one"); } finally { if (is != null) { try { is.close(); } catch (IOException e1) { System.err .println("Non-critical problem releasing resources."); } } } return result; } /** * Given a contents file and an item, stuffing it with bitstreams from the * contents file */ private void processContentsFile(Context c, Item i, String path, String filename) throws SQLException, IOException, AuthorizeException { String contentspath = path + File.separatorChar + filename; String line = ""; System.out.println("\tProcessing contents file: " + contentspath); BufferedReader is = null; try { is = new BufferedReader(new FileReader(contentspath)); while ((line = is.readLine()) != null) { if ("".equals(line.trim())) { continue; } // 1) registered into dspace (leading -r) // 2) imported conventionally into dspace (no -r) if (line.trim().startsWith("-r ")) { // line should be one of these two: // -r -s n -f filepath // -r -s n -f filepath\tbundle:bundlename // where // n is the assetstore number // filepath is the path of the file to be registered // bundlename is an optional bundle name String sRegistrationLine = line.trim(); int iAssetstore = -1; String sFilePath = null; String sBundle = null; StringTokenizer tokenizer = new StringTokenizer(sRegistrationLine); while (tokenizer.hasMoreTokens()) { String sToken = tokenizer.nextToken(); if (sToken.equals("-r")) { continue; } else if (sToken.equals("-s") && tokenizer.hasMoreTokens()) { try { iAssetstore = Integer.parseInt(tokenizer.nextToken()); } catch (NumberFormatException e) { // ignore - iAssetstore remains -1 } } else if (sToken.equals("-f") && tokenizer.hasMoreTokens()) { sFilePath = tokenizer.nextToken(); } else if (sToken.startsWith("bundle:")) { sBundle = sToken.substring(7); } else { // unrecognized token - should be no problem } } // while if (iAssetstore == -1 || sFilePath == null) { System.out.println("\tERROR: invalid contents file line"); System.out.println("\t\tSkipping line: " + sRegistrationLine); continue; } registerBitstream(c, i, iAssetstore, sFilePath, sBundle); System.out.println("\tRegistering Bitstream: " + sFilePath + "\tAssetstore: " + iAssetstore + "\tBundle: " + sBundle); continue; // process next line in contents file } // look for a bundle name String bundleMarker = "\tbundle:"; int markerIndex = line.indexOf(bundleMarker); if (markerIndex == -1) { // no bundle found processContentFileEntry(c, i, path, line, null); System.out.println("\tBitstream: " + line); } else { // found bundle String bundleName = line.substring(markerIndex + bundleMarker.length()); String bitstreamName = line.substring(0, markerIndex); bitstreamName = bitstreamName.trim(); processContentFileEntry(c, i, path, bitstreamName, bundleName); System.out.println("\tBitstream: " + bitstreamName + "\tBundle: " + bundleName); } } } finally { if (is != null) { is.close(); } } } // each entry represents a bitstream.... public void processContentFileEntry(Context c, Item i, String path, String fileName, String bundleName) throws SQLException, IOException, AuthorizeException { String fullpath = path + File.separatorChar + fileName; // get an input stream BufferedInputStream bis = new BufferedInputStream(new FileInputStream( fullpath)); Bitstream bs = null; String newBundleName = bundleName; if (bundleName == null) { // is it license.txt? if (fileName.equals("license.txt")) { newBundleName = "LICENSE"; } else { // call it ORIGINAL newBundleName = "ORIGINAL"; } } if (!isTest) { // find the bundle Bundle[] bundles = i.getBundles(newBundleName); Bundle targetBundle = null; if (bundles.length < 1) { // not found, create a new one targetBundle = i.createBundle(newBundleName); } else { // put bitstreams into first bundle targetBundle = bundles[0]; } // now add the bitstream bs = targetBundle.createBitstream(bis); bs.setName(fileName); // Identify the format // FIXME - guessing format guesses license.txt incorrectly as a text // file format! BitstreamFormat bf = FormatIdentifier.guessFormat(c, bs); bs.setFormat(bf); bs.update(); } } /** * Register the bitstream file into DSpace * * @param c * @param i * @param assetstore * @param bitstreamPath the full filepath expressed in the contents file * @param bundleName * @throws SQLException * @throws IOException * @throws AuthorizeException */ public void registerBitstream(Context c, Item i, int assetstore, String bitstreamPath, String bundleName ) throws SQLException, IOException, AuthorizeException { // TODO validate assetstore number // TODO make sure the bitstream is there Bitstream bs = null; String newBundleName = bundleName; if (bundleName == null) { // is it license.txt? if (bitstreamPath.endsWith("license.txt")) { newBundleName = "LICENSE"; } else { // call it ORIGINAL newBundleName = "ORIGINAL"; } } if(!isTest) { // find the bundle Bundle[] bundles = i.getBundles(newBundleName); Bundle targetBundle = null; if( bundles.length < 1 ) { // not found, create a new one targetBundle = i.createBundle(newBundleName); } else { // put bitstreams into first bundle targetBundle = bundles[0]; } // now add the bitstream bs = targetBundle.registerBitstream(assetstore, bitstreamPath); // set the name to just the filename int iLastSlash = bitstreamPath.lastIndexOf('/'); bs.setName(bitstreamPath.substring(iLastSlash + 1)); // Identify the format // FIXME - guessing format guesses license.txt incorrectly as a text file format! BitstreamFormat bf = FormatIdentifier.guessFormat(c, bs); bs.setFormat(bf); bs.update(); } } // XML utility methods public String getAttributeValue(Node n, String myattributename) { String myvalue = ""; NamedNodeMap nm = n.getAttributes(); for (int i = 0; i < nm.getLength(); i++) { Node node = nm.item(i); String name = node.getNodeName(); String value = node.getNodeValue(); if (myattributename.equals(name)) { return value; } } return myvalue; } // XML utility methods stolen from administer. /** * Get the CDATA of a particular element. For example, if the XML document * contains: * <P> * <code> * <foo><mimetype>application/pdf</mimetype></foo> * </code> * passing this the <code>foo</code> node and <code>mimetype</code> will * return <code>application/pdf</code>. * </P> * Why this isn't a core part of the XML API I do not know... * * @param parentElement * the element, whose child element you want the CDATA from * @param childName * the name of the element you want the CDATA from * * @return the CDATA as a <code>String</code> */ private String getElementData(Node parentElement, String childName) throws TransformerException { // Grab the child node Node childNode = XPathAPI.selectSingleNode(parentElement, childName); if (childNode == null) { // No child node, so no values return null; } // Get the #text Node dataNode = childNode.getFirstChild(); if (dataNode == null) { return null; } // Get the data String value = dataNode.getNodeValue().trim(); return value; } /** * Load in the XML from file. * * @param filename * the filename to load from * * @return the DOM representation of the XML file */ private static Document loadXML(String filename) throws IOException, ParserConfigurationException, SAXException { DocumentBuilder builder = DocumentBuilderFactory.newInstance() .newDocumentBuilder(); return builder.parse(new File(filename)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -