|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.tools.command; |
|
21 |
| |
|
22 |
| import org.apache.xindice.tools.XMLTools; |
|
23 |
| |
|
24 |
| import org.xmldb.api.DatabaseManager; |
|
25 |
| import org.xmldb.api.base.Collection; |
|
26 |
| import org.xmldb.api.base.Resource; |
|
27 |
| |
|
28 |
| import java.io.File; |
|
29 |
| import java.io.FileInputStream; |
|
30 |
| import java.io.InputStream; |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| public class AddResource extends Command { |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
0
| public boolean execute(XMLTools.Config table) throws Exception {
|
|
45 |
| |
|
46 |
0
| if (table.getString(XMLTools.COLLECTION) == null) {
|
|
47 |
0
| System.out.println("ERROR : Collection and switch required");
|
|
48 |
0
| return false;
|
|
49 |
| } |
|
50 |
| |
|
51 |
0
| if ("".equals(table.getString(XMLTools.FILE_PATH))) {
|
|
52 |
0
| System.out.println("ERROR : File path required");
|
|
53 |
0
| return false;
|
|
54 |
| } |
|
55 |
| |
|
56 |
0
| Collection col = null;
|
|
57 |
0
| InputStream fis = null;
|
|
58 |
0
| try {
|
|
59 |
| |
|
60 |
0
| String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
|
|
61 |
| table.getBoolean(XMLTools.LOCAL)); |
|
62 |
0
| col = DatabaseManager.getCollection(colstring);
|
|
63 |
0
| if (col == null) {
|
|
64 |
0
| System.out.println("ERROR : Collection not found!");
|
|
65 |
0
| return false;
|
|
66 |
| } |
|
67 |
| |
|
68 |
| |
|
69 |
0
| File file = new File(table.getString(XMLTools.FILE_PATH));
|
|
70 |
0
| fis = new FileInputStream(file);
|
|
71 |
0
| byte[] data = new byte[fis.available()];
|
|
72 |
0
| fis.read(data);
|
|
73 |
| |
|
74 |
| |
|
75 |
0
| Resource resource = col.createResource(table.getString(XMLTools.NAME_OF), "BinaryResource");
|
|
76 |
0
| resource.setContent(data);
|
|
77 |
0
| col.storeResource(resource);
|
|
78 |
| |
|
79 |
0
| System.out.println("Added resource " + table.getString(XMLTools.COLLECTION) + "/" + resource.getId());
|
|
80 |
| } finally { |
|
81 |
0
| if (col != null) {
|
|
82 |
0
| col.close();
|
|
83 |
| } |
|
84 |
| |
|
85 |
0
| if (fis != null) {
|
|
86 |
0
| fis.close();
|
|
87 |
| } |
|
88 |
| } |
|
89 |
| |
|
90 |
0
| return true;
|
|
91 |
| } |
|
92 |
| |
|
93 |
0
| public void usage() {
|
|
94 |
0
| System.out.println("Format: xindice ar -c <context> [-l [-d <path>]] [-v] [parameters...]");
|
|
95 |
0
| System.out.println();
|
|
96 |
0
| System.out.println("Adds a single binary resourcet to an existing collection");
|
|
97 |
0
| System.out.println();
|
|
98 |
0
| System.out.println("Command-specific switches:");
|
|
99 |
0
| System.out.println(" -f|--filepath <file>");
|
|
100 |
0
| System.out.println(" File path for the resource being added to the collection");
|
|
101 |
0
| System.out.println(" -n|--nameOf <name>");
|
|
102 |
0
| System.out.println(" Resource key. If this parameter is omitted, database will");
|
|
103 |
0
| System.out.println(" generate an unique key");
|
|
104 |
0
| System.out.println();
|
|
105 |
| } |
|
106 |
| } |