1 40 package org.dspace.app.itemimport; 41 42 import java.io.BufferedInputStream ; 43 import java.io.BufferedReader ; 44 import java.io.File ; 45 import java.io.FileInputStream ; 46 import java.io.FileReader ; 47 import java.io.FileWriter ; 48 import java.io.FilenameFilter ; 49 import java.io.IOException ; 50 import java.io.PrintWriter ; 51 import java.sql.SQLException ; 52 import java.util.ArrayList ; 53 import java.util.HashMap ; 54 import java.util.Iterator ; 55 import java.util.Map ; 56 import java.util.StringTokenizer ; 57 58 import javax.xml.parsers.DocumentBuilder ; 59 import javax.xml.parsers.DocumentBuilderFactory ; 60 import javax.xml.parsers.ParserConfigurationException ; 61 import javax.xml.transform.TransformerException ; 62 63 import org.apache.commons.cli.CommandLine; 64 import org.apache.commons.cli.CommandLineParser; 65 import org.apache.commons.cli.HelpFormatter; 66 import org.apache.commons.cli.Options; 67 import org.apache.commons.cli.PosixParser; 68 import org.apache.xpath.XPathAPI; 69 import org.dspace.authorize.AuthorizeException; 70 import org.dspace.content.Bitstream; 71 import org.dspace.content.BitstreamFormat; 72 import org.dspace.content.Bundle; 73 import org.dspace.content.Collection; 74 import org.dspace.content.FormatIdentifier; 75 import org.dspace.content.InstallItem; 76 import org.dspace.content.Item; 77 import org.dspace.content.MetadataSchema; 78 import org.dspace.content.WorkspaceItem; 79 import org.dspace.core.ConfigurationManager; 80 import org.dspace.core.Constants; 81 import org.dspace.core.Context; 82 import org.dspace.eperson.EPerson; 83 import org.dspace.handle.HandleManager; 84 import org.dspace.workflow.WorkflowManager; 85 import org.w3c.dom.Document ; 86 import org.w3c.dom.NamedNodeMap ; 87 import org.w3c.dom.Node ; 88 import org.w3c.dom.NodeList ; 89 import org.xml.sax.SAXException ; 90 91 107 public class ItemImport 108 { 109 static boolean useWorkflow = false; 110 111 static boolean isTest = false; 112 113 static boolean isResume = false; 114 115 static PrintWriter mapOut = null; 116 117 static FilenameFilter metadataFileFilter = new FilenameFilter () 119 { 120 public boolean accept(File dir, String n) 121 { 122 return n.startsWith("metadata_"); 123 } 124 }; 125 126 static FilenameFilter directoryFilter = new FilenameFilter () 128 { 129 public boolean accept(File dir, String n) 130 { 131 File item = new File (dir.getAbsolutePath() + File.separatorChar + n); 132 return item.isDirectory(); 133 } 134 }; 135 136 137 public static void main(String [] argv) throws Exception 138 { 139 CommandLineParser parser = new PosixParser(); 141 142 Options options = new Options(); 143 144 options.addOption("a", "add", false, "add items to DSpace"); 145 options.addOption("r", "replace", false, "replace items in mapfile"); 146 options.addOption("d", "delete", false, 147 "delete items listed in mapfile"); 148 options.addOption("s", "source", true, "source of items (directory)"); 149 options.addOption("c", "collection", true, 150 "destination collection(s) Handle or database ID"); 151 options.addOption("m", "mapfile", true, "mapfile items in mapfile"); 152 options.addOption("e", "eperson", true, 153 "email of eperson doing importing"); 154 options.addOption("w", "workflow", false, 155 "send submission through collection's workflow"); 156 options.addOption("t", "test", false, 157 "test run - do not actually import items"); 158 options.addOption("R", "resume", false, 159 "resume a failed import (add only)"); 160 161 options.addOption("h", "help", false, "help"); 162 163 CommandLine line = parser.parse(options, argv); 164 165 String command = null; String sourcedir = null; 167 String mapfile = null; 168 String eperson = null; String [] collections = null; 171 if (line.hasOption('h')) 172 { 173 HelpFormatter myhelp = new HelpFormatter(); 174 myhelp.printHelp("ItemImport\n", options); 175 System.out 176 .println("\nadding items: ItemImport -a -e eperson -c collection -s sourcedir -m mapfile"); 177 System.out 178 .println("replacing items: ItemImport -r -e eperson -c collection -s sourcedir -m mapfile"); 179 System.out 180 .println("deleting items: ItemImport -d -e eperson -m mapfile"); 181 System.out 182 .println("If multiple collections are specified, the first collection will be the one that owns the item."); 183 184 System.exit(0); 185 } 186 187 if (line.hasOption('a')) 188 { 189 command = "add"; 190 } 191 192 if (line.hasOption('r')) 193 { 194 command = "replace"; 195 } 196 197 if (line.hasOption('d')) 198 { 199 command = "delete"; 200 } 201 202 if (line.hasOption('w')) 203 { 204 useWorkflow = true; 205 } 206 207 if (line.hasOption('t')) 208 { 209 isTest = true; 210 System.out.println("**Test Run** - not actually importing items."); 211 } 212 213 if (line.hasOption('s')) { 215 sourcedir = line.getOptionValue('s'); 216 } 217 218 if (line.hasOption('m')) { 220 mapfile = line.getOptionValue('m'); 221 } 222 223 if (line.hasOption('e')) { 225 eperson = line.getOptionValue('e'); 226 } 227 228 if (line.hasOption('c')) { 230 collections = line.getOptionValues('c'); 231 } 232 233 if (line.hasOption('R')) 234 { 235 isResume = true; 236 System.out 237 .println("**Resume import** - attempting to import items not already imported"); 238 } 239 240 if (command == null) 243 { 244 System.out 245 .println("Error - must run with either add, replace, or remove (run with -h flag for details)"); 246 System.exit(1); 247 } 248 else if (command.equals("add") || command.equals("replace")) 249 { 250 if (sourcedir == null) 251 { 252 System.out 253 .println("Error - a source directory containing items must be set"); 254 System.out.println(" (run with -h flag for details)"); 255 System.exit(1); 256 } 257 258 if (mapfile == null) 259 { 260 System.out 261 .println("Error - a map file to hold importing results must be specified"); 262 System.out.println(" (run with -h flag for details)"); 263 System.exit(1); 264 } 265 266 if (eperson == null) 267 { 268 System.out 269 .println("Error - an eperson to do the importing must be specified"); 270 System.out.println(" (run with -h flag for details)"); 271 System.exit(1); 272 } 273 274 if (collections == null) 275 { 276 System.out 277 .println("Error - at least one destination collection must be specified"); 278 System.out.println(" (run with -h flag for details)"); 279 System.exit(1); 280 } 281 } 282 else if (command.equals("delete")) 283 { 284 if (eperson == null) 285 { 286 System.out 287 .println("Error - an eperson to do the importing must be specified"); 288 System.exit(1); 289 } 290 291 if (mapfile == null) 292 { 293 System.out.println("Error - a map file must be specified"); 294 System.exit(1); 295 } 296 } 297 298 if (isResume && !command.equals("add")) 300 { 301 System.out 302 .println("Error - resume option only works with --add command"); 303 System.exit(1); 304 } 305 306 File myFile = new File (mapfile); 309 310 if (myFile.exists() && command.equals("add") && !isResume) 311 { 312 System.out.println("Error - the mapfile " + mapfile 313 + " already exists."); 314 System.out 315 .println("Either delete it or use --resume if attempting to resume an aborted import."); 316 System.exit(1); 317 } 318 319 ItemImport myloader = new ItemImport(); 320 321 Context c = new Context(); 323 324 EPerson myEPerson = null; 326 327 if (eperson.indexOf('@') != -1) 328 { 329 myEPerson = EPerson.findByEmail(c, eperson); 331 } 332 else 333 { 334 myEPerson = EPerson.find(c, Integer.parseInt(eperson)); 335 } 336 337 if (myEPerson == null) 338 { 339 System.out.println("Error, eperson cannot be found: " + eperson); 340 System.exit(1); 341 } 342 343 c.setCurrentUser(myEPerson); 344 345 Collection[] mycollections = null; 347 348 if (!command.equals("delete")) 350 { 351 System.out.println("Destination collections:"); 352 353 mycollections = new Collection[collections.length]; 354 355 for (int i = 0; i < collections.length; i++) 357 { 358 if (collections[i].indexOf('/') != -1) 360 { 361 mycollections[i] = (Collection) HandleManager 364 .resolveToObject(c, collections[i]); 365 366 if ((mycollections[i] == null) 368 || (mycollections[i].getType() != Constants.COLLECTION)) 369 { 370 mycollections[i] = null; 371 } 372 } 373 else if (collections[i] != null) 376 { 377 mycollections[i] = Collection.find(c, Integer 378 .parseInt(collections[i])); 379 } 380 381 if (mycollections[i] == null) 383 { 384 throw new IllegalArgumentException ("Cannot resolve " 385 + collections[i] + " to collection"); 386 } 387 388 String owningPrefix = ""; 390 391 if (i == 0) 392 { 393 owningPrefix = "Owning "; 394 } 395 396 System.out.println(owningPrefix + " Collection: " 397 + mycollections[i].getMetadata("name")); 398 } 399 } 401 try 402 { 403 c.setIgnoreAuthorization(true); 404 405 if (command.equals("add")) 406 { 407 myloader.addItems(c, mycollections, sourcedir, mapfile); 408 } 409 else if (command.equals("replace")) 410 { 411 myloader.replaceItems(c, mycollections, sourcedir, mapfile); 412 } 413 else if (command.equals("delete")) 414 { 415 myloader.deleteItems(c, mapfile); 416 } 417 418 c.complete(); 420 } 421 catch (Exception e) 422 { 423 if (mapOut != null) 425 { 426 mapOut.close(); 427 } 428 429 mapOut = null; 430 431 c.abort(); 432 e.printStackTrace(); 433 System.out.println(e); 434 } 435 436 if (mapOut != null) 437 { 438 mapOut.close(); 439 } 440 441 if (isTest) 442 { 443 System.out.println("***End of Test Run***"); 444 } 445 } 446 447 private void addItems(Context c, Collection[] mycollections, 448 String sourceDir, String mapFile) throws Exception 449 { 450 Map skipItems = new HashMap (); 453 System.out.println("Adding items from directory: " + sourceDir); 454 System.out.println("Generating mapfile: " + mapFile); 455 456 File outFile = null; 458 459 if (!isTest) 460 { 461 if (isResume) 464 { 465 skipItems = readMapFile(mapFile); 466 } 467 468 outFile = new File (mapFile); 470 mapOut = new PrintWriter (new FileWriter (outFile, isResume)); 471 472 if (mapOut == null) 473 { 474 throw new Exception ("can't open mapfile: " + mapFile); 475 } 476 } 477 478 File d = new java.io.File (sourceDir); 480 481 if (d == null) 482 { 483 System.out.println("Error, cannot open source directory " 484 + sourceDir); 485 System.exit(1); 486 } 487 488 String [] dircontents = d.list(directoryFilter); 489 490 for (int i = 0; i < dircontents.length; i++) 491 { 492 if (skipItems.containsKey(dircontents[i])) 493 { 494 System.out.println("Skipping import of " + dircontents[i]); 495 } 496 else 497 { 498 addItem(c, mycollections, sourceDir, dircontents[i], mapOut); 499 System.out.println(i + " " + dircontents[i]); 500 } 501 } 502 } 503 504 private void replaceItems(Context c, Collection[] mycollections, 505 String sourceDir, String mapFile) throws Exception 506 { 507 File d = new java.io.File (sourceDir); 509 510 if (d == null) 511 { 512 System.out.println("Error, cannot open source directory " 513 + sourceDir); 514 System.exit(1); 515 } 516 517 Map myhash = readMapFile(mapFile); 519 520 Iterator i = myhash.keySet().iterator(); 523 ArrayList itemsToDelete = new ArrayList (); 524 525 while (i.hasNext()) 526 { 527 String newItemName = (String ) i.next(); 529 String oldHandle = (String ) myhash.get(newItemName); 530 531 Item oldItem = null; 532 Item newItem = null; 533 534 if (oldHandle.indexOf('/') != -1) 535 { 536 System.out.println("\tReplacing: " + oldHandle); 537 538 oldItem = (Item) HandleManager.resolveToObject(c, oldHandle); 540 } 541 else 542 { 543 oldItem = Item.find(c, Integer.parseInt(oldHandle)); 544 } 545 546 556 File handleFile = new File (sourceDir + File.separatorChar + newItemName + File.separatorChar + "handle"); 557 PrintWriter handleOut = new PrintWriter (new FileWriter (handleFile, true)); 558 559 if (handleOut == null) 560 { 561 throw new Exception ("can't open handle file: " + handleFile.getCanonicalPath()); 562 } 563 564 handleOut.println(oldHandle); 565 handleOut.close(); 566 567 deleteItem(c, oldItem); 568 569 newItem = addItem(c, mycollections, sourceDir, newItemName, null); 570 } 571 } 572 573 private void deleteItems(Context c, String mapFile) throws Exception 574 { 575 System.out.println("Deleting items listed in mapfile: " + mapFile); 576 577 Map myhash = readMapFile(mapFile); 579 580 Iterator i = myhash.keySet().iterator(); 582 583 while (i.hasNext()) 584 { 585 String itemID = (String ) myhash.get(i.next()); 586 587 if (itemID.indexOf('/') != -1) 588 { 589 String myhandle = itemID; 590 System.out.println("Deleting item " + myhandle); 591 deleteItem(c, myhandle); 592 } 593 else 594 { 595 Item myitem = Item.find(c, Integer.parseInt(itemID)); 597 System.out.println("Deleting item " + itemID); 598 deleteItem(c, myitem); 599 } 600 } 601 } 602 603 608 private Item addItem(Context c, Collection[] mycollections, String path, 609 String itemname, PrintWriter mapOut) throws Exception 610 { 611 String mapOutput = null; 612 613 System.out.println("Adding item from directory " + itemname); 614 615 Item myitem = null; 617 WorkspaceItem wi = null; 618 619 if (!isTest) 620 { 621 wi = WorkspaceItem.create(c, mycollections[0], false); 622 myitem = wi.getItem(); 623 } 624 625 loadMetadata(c, myitem, path + File.separatorChar + itemname 627 + File.separatorChar); 628 629 processContentsFile(c, myitem, path + File.separatorChar + itemname, 632 "contents"); 633 634 if (useWorkflow) 635 { 636 if (!isTest) 639 { 640 WorkflowManager.startWithoutNotify(c, wi); 641 642 mapOutput = itemname + " " + myitem.getID(); 644 } 645 } 646 else 647 { 648 String myhandle = processHandleFile(c, myitem, path 650 + File.separatorChar + itemname, "handle"); 651 652 if (!isTest) 654 { 655 InstallItem.installItem(c, wi, myhandle); 656 657 myhandle = HandleManager.findHandle(c, myitem); 659 660 mapOutput = itemname + " " + myhandle; 661 } 662 } 663 664 if (mycollections.length > 1) 666 { 667 for (int i = 1; i < mycollections.length; i++) 668 { 669 if (!isTest) 670 { 671 mycollections[i].addItem(myitem); 672 } 673 } 674 } 675 676 if (mapOut != null) 678 { 679 mapOut.println(mapOutput); 680 } 681 682 c.commit(); 683 684 return myitem; 685 } 686 687 private void deleteItem(Context c, Item myitem) throws Exception 689 { 690 if (!isTest) 691 { 692 Collection[] collections = myitem.getCollections(); 693 694 for (int i = 0; i < collections.length; i++) 696 { 697 collections[i].removeItem(myitem); 698 } 699 } 700 } 701 702 private void deleteItem(Context c, String myhandle) throws Exception 704 { 705 Item myitem = (Item) HandleManager.resolveToObject(c, myhandle); 708 709 if (myitem == null) 710 { 711 System.out.println("Error - cannot locate item - already deleted?"); 712 } 713 else 714 { 715 deleteItem(c, myitem); 716 } 717 } 718 719 private Map readMapFile(String filename) throws Exception 724 { 725 Map myhash = new HashMap (); 726 727 BufferedReader is = null; 728 try 729 { 730 is = new BufferedReader (new FileReader (filename)); 731 732 String line; 733 734 while ((line = is.readLine()) != null) 735 { 736 String myfile; 737 String myhandle; 738 739 StringTokenizer st = new StringTokenizer (line); 741 742 if (st.hasMoreTokens()) 743 { 744 myfile = st.nextToken(); 745 } 746 else 747 { 748 throw new Exception ("Bad mapfile line:\n" + line); 749 } 750 751 if (st.hasMoreTokens()) 752 { 753 myhandle = st.nextToken(); 754 } 755 else 756 { 757 throw new Exception ("Bad mapfile line:\n" + line); 758 } 759 760 myhash.put(myfile, myhandle); 761 } 762 } 763 finally 764 { 765 if (is != null) 766 { 767 is.close(); 768 } 769 } 770 771 return myhash; 772 } 773 774 private void loadMetadata(Context c, Item myitem, String path) 776 throws SQLException , IOException , ParserConfigurationException , 777 SAXException , TransformerException 778 { 779 loadDublinCore(c, myitem, path + "dublin_core.xml"); 781 782 File folder = new File (path); 784 File file[] = folder.listFiles(metadataFileFilter); 785 for (int i = 0; i < file.length; i++) 786 { 787 loadDublinCore(c, myitem, file[i].getAbsolutePath()); 788 } 789 } 790 791 private void loadDublinCore(Context c, Item myitem, String filename) 792 throws SQLException , IOException , ParserConfigurationException , 793 SAXException , TransformerException { 795 Document document = loadXML(filename); 796 797 String schema; 801 NodeList metadata = XPathAPI.selectNodeList(document, "/dublin_core"); 802 Node schemaAttr = metadata.item(0).getAttributes().getNamedItem( 803 "schema"); 804 if (schemaAttr == null) 805 { 806 schema = MetadataSchema.DC_SCHEMA; 807 } 808 else 809 { 810 schema = schemaAttr.getNodeValue(); 811 } 812 813 NodeList dcNodes = XPathAPI.selectNodeList(document, 815 "/dublin_core/dcvalue"); 816 817 System.out.println("\tLoading dublin core from " + filename); 818 819 for (int i = 0; i < dcNodes.getLength(); i++) 821 { 822 Node n = dcNodes.item(i); 823 addDCValue(myitem, schema, n); 824 } 825 } 826 827 private void addDCValue(Item i, String schema, Node n) throws TransformerException 828 { 829 String value = getStringValue(n); if (value == null) 832 value = ""; 833 String element = getAttributeValue(n, "element"); 835 String qualifier = getAttributeValue(n, "qualifier"); String language = getAttributeValue(n, "language"); 839 840 System.out.println("\tSchema: " + schema + " Element: " + element + " Qualifier: " + qualifier 841 + " Value: " + value); 842 843 if (qualifier.equals("none") || "".equals(qualifier)) 844 { 845 qualifier = null; 846 } 847 848 if (language.equals("")) 850 { 851 language = ConfigurationManager.getProperty("default.language"); 852 } 853 854 if (language == null) 856 { 857 language = "en"; 858 } 859 860 if (!isTest) 861 { 862 i.addMetadata(schema, element, qualifier, language, value); 863 } 864 } 865 866 869 public String getStringValue(Node node) 870 { 871 String value = node.getNodeValue(); 872 873 if (node.hasChildNodes()) 874 { 875 Node first = node.getFirstChild(); 876 877 if (first.getNodeType() == Node.TEXT_NODE) 878 { 879 return first.getNodeValue(); 880 } 881 } 882 883 return value; 884 } 885 886 889 private String processHandleFile(Context c, Item i, String path, 890 String filename) 891 { 892 String filePath = path + File.separatorChar + filename; 893 String line = ""; 894 String result = null; 895 896 System.out.println("Processing handle file: " + filename); 897 BufferedReader is = null; 898 try 899 { 900 is = new BufferedReader (new FileReader (filePath)); 901 902 result = is.readLine(); 904 905 System.out.println("read handle: '" + result + "'"); 906 907 } 908 catch (Exception e) 909 { 910 System.out 912 .println("It appears there is no handle file -- generating one"); 913 } 914 finally 915 { 916 if (is != null) 917 { 918 try 919 { 920 is.close(); 921 } 922 catch (IOException e1) 923 { 924 System.err 925 .println("Non-critical problem releasing resources."); 926 } 927 } 928 } 929 930 return result; 931 } 932 933 937 private void processContentsFile(Context c, Item i, String path, 938 String filename) throws SQLException , IOException , 939 AuthorizeException 940 { 941 String contentspath = path + File.separatorChar + filename; 942 String line = ""; 943 944 System.out.println("\tProcessing contents file: " + contentspath); 945 946 BufferedReader is = null; 947 try 948 { 949 is = new BufferedReader (new FileReader (contentspath)); 950 951 while ((line = is.readLine()) != null) 952 { 953 if ("".equals(line.trim())) 954 { 955 continue; 956 } 957 958 if (line.trim().startsWith("-r ")) 961 { 962 String sRegistrationLine = line.trim(); 970 int iAssetstore = -1; 971 String sFilePath = null; 972 String sBundle = null; 973 StringTokenizer tokenizer = 974 new StringTokenizer (sRegistrationLine); 975 while (tokenizer.hasMoreTokens()) 976 { 977 String sToken = tokenizer.nextToken(); 978 if (sToken.equals("-r")) 979 { 980 continue; 981 } 982 else if (sToken.equals("-s") && tokenizer.hasMoreTokens()) 983 { 984 try 985 { 986 iAssetstore = 987 Integer.parseInt(tokenizer.nextToken()); 988 } 989 catch (NumberFormatException e) 990 { 991 } 993 } 994 else if (sToken.equals("-f") && tokenizer.hasMoreTokens()) 995 { 996 sFilePath = tokenizer.nextToken(); 997 998 } 999 else if (sToken.startsWith("bundle:")) 1000 { 1001 sBundle = sToken.substring(7); 1002 } 1003 else 1004 { 1005 } 1007 } if (iAssetstore == -1 || sFilePath == null) 1009 { 1010 System.out.println("\tERROR: invalid contents file line"); 1011 System.out.println("\t\tSkipping line: " 1012 + sRegistrationLine); 1013 continue; 1014 } 1015 registerBitstream(c, i, iAssetstore, sFilePath, sBundle); 1016 System.out.println("\tRegistering Bitstream: " + sFilePath 1017 + "\tAssetstore: " + iAssetstore 1018 + "\tBundle: " + sBundle); 1019 continue; } 1021 1022 String bundleMarker = "\tbundle:"; 1024 1025 int markerIndex = line.indexOf(bundleMarker); 1026 1027 if (markerIndex == -1) 1028 { 1029 processContentFileEntry(c, i, path, line, null); 1031 System.out.println("\tBitstream: " + line); 1032 } 1033 else 1034 { 1035 String bundleName = line.substring(markerIndex 1037 + bundleMarker.length()); 1038 String bitstreamName = line.substring(0, markerIndex); 1039 bitstreamName = bitstreamName.trim(); 1040 1041 processContentFileEntry(c, i, path, bitstreamName, 1042 bundleName); 1043 System.out.println("\tBitstream: " + bitstreamName 1044 + "\tBundle: " + bundleName); 1045 } 1046 } 1047 } 1048 finally 1049 { 1050 if (is != null) 1051 { 1052 is.close(); 1053 } 1054 } 1055 } 1056 1057 public void processContentFileEntry(Context c, Item i, String path, 1059 String fileName, String bundleName) throws SQLException , 1060 IOException , AuthorizeException 1061 { 1062 String fullpath = path + File.separatorChar + fileName; 1063 1064 BufferedInputStream bis = new BufferedInputStream (new FileInputStream ( 1066 fullpath)); 1067 1068 Bitstream bs = null; 1069 String newBundleName = bundleName; 1070 1071 if (bundleName == null) 1072 { 1073 if (fileName.equals("license.txt")) 1075 { 1076 newBundleName = "LICENSE"; 1077 } 1078 else 1079 { 1080 newBundleName = "ORIGINAL"; 1082 } 1083 } 1084 1085 if (!isTest) 1086 { 1087 Bundle[] bundles = i.getBundles(newBundleName); 1089 Bundle targetBundle = null; 1090 1091 if (bundles.length < 1) 1092 { 1093 targetBundle = i.createBundle(newBundleName); 1095 } 1096 else 1097 { 1098 targetBundle = bundles[0]; 1100 } 1101 1102 bs = targetBundle.createBitstream(bis); 1104 1105 bs.setName(fileName); 1106 1107 BitstreamFormat bf = FormatIdentifier.guessFormat(c, bs); 1111 bs.setFormat(bf); 1112 1113 bs.update(); 1114 } 1115 } 1116 1117 1129 public void registerBitstream(Context c, Item i, int assetstore, 1130 String bitstreamPath, String bundleName ) 1131 throws SQLException , IOException , AuthorizeException 1132 { 1133 1136 Bitstream bs = null; 1137 String newBundleName = bundleName; 1138 1139 if (bundleName == null) 1140 { 1141 if (bitstreamPath.endsWith("license.txt")) 1143 { 1144 newBundleName = "LICENSE"; 1145 } 1146 else 1147 { 1148 newBundleName = "ORIGINAL"; 1150 } 1151 } 1152 1153 if(!isTest) 1154 { 1155 Bundle[] bundles = i.getBundles(newBundleName); 1157 Bundle targetBundle = null; 1158 1159 if( bundles.length < 1 ) 1160 { 1161 targetBundle = i.createBundle(newBundleName); 1163 } 1164 else 1165 { 1166 targetBundle = bundles[0]; 1168 } 1169 1170 bs = targetBundle.registerBitstream(assetstore, bitstreamPath); 1172 1173 int iLastSlash = bitstreamPath.lastIndexOf('/'); 1175 bs.setName(bitstreamPath.substring(iLastSlash + 1)); 1176 1177 BitstreamFormat bf = FormatIdentifier.guessFormat(c, bs); 1180 bs.setFormat(bf); 1181 1182 bs.update(); 1183 } 1184 } 1185 1186 public String getAttributeValue(Node n, String myattributename) 1188 { 1189 String myvalue = ""; 1190 1191 NamedNodeMap nm = n.getAttributes(); 1192 1193 for (int i = 0; i < nm.getLength(); i++) 1194 { 1195 Node node = nm.item(i); 1196 String name = node.getNodeName(); 1197 String value = node.getNodeValue(); 1198 1199 if (myattributename.equals(name)) 1200 { 1201 return value; 1202 } 1203 } 1204 1205 return myvalue; 1206 } 1207 1208 1210 1229 private String getElementData(Node parentElement, String childName) 1230 throws TransformerException 1231 { 1232 Node childNode = XPathAPI.selectSingleNode(parentElement, childName); 1234 1235 if (childNode == null) 1236 { 1237 return null; 1239 } 1240 1241 Node dataNode = childNode.getFirstChild(); 1243 1244 if (dataNode == null) 1245 { 1246 return null; 1247 } 1248 1249 String value = dataNode.getNodeValue().trim(); 1251 1252 return value; 1253 } 1254 1255 1263 private static Document loadXML(String filename) throws IOException , 1264 ParserConfigurationException , SAXException 1265 { 1266 DocumentBuilder builder = DocumentBuilderFactory.newInstance() 1267 .newDocumentBuilder(); 1268 1269 return builder.parse(new File (filename)); 1270 } 1271} 1272 | Popular Tags |