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 |
| |
27 |
| import java.io.File; |
28 |
| import java.io.FilenameFilter; |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public class AddMultipleDocuments extends Command { |
40 |
| |
41 |
0
| public boolean execute(XMLTools.Config table) throws Exception {
|
42 |
| |
43 |
| |
44 |
0
| if (table.getString(XMLTools.COLLECTION) == null) {
|
45 |
0
| System.out.println("ERROR : Collection and switch required");
|
46 |
0
| return false;
|
47 |
| } |
48 |
| |
49 |
0
| if ("".equals(table.getString(XMLTools.FILE_PATH))) {
|
50 |
0
| System.out.println("ERROR : Directory name and switch required");
|
51 |
0
| return false;
|
52 |
| } |
53 |
| |
54 |
0
| Collection col = null;
|
55 |
0
| try {
|
56 |
| |
57 |
| |
58 |
0
| String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
|
59 |
| table.getBoolean(XMLTools.LOCAL)); |
60 |
0
| col = DatabaseManager.getCollection(colstring);
|
61 |
0
| if (col == null) {
|
62 |
0
| System.out.println("ERROR : Collection not found!");
|
63 |
0
| return false;
|
64 |
| } |
65 |
| |
66 |
| |
67 |
0
| File dir = new File(table.getString(XMLTools.FILE_PATH));
|
68 |
0
| if (dir.isDirectory()) {
|
69 |
| |
70 |
0
| String[] children = new String[]{};
|
71 |
0
| final String ext = table.getString(XMLTools.EXTENSION);
|
72 |
| |
73 |
| |
74 |
0
| if (!ext.equals("")) {
|
75 |
0
| children = dir.list(new FilenameFilter() {
|
76 |
0
| public boolean accept(File none, String name) {
|
77 |
0
| return name.endsWith("." + ext);
|
78 |
| } |
79 |
| }); |
80 |
0
| } else if (ext.equals("")) {
|
81 |
0
| children = dir.list();
|
82 |
| } |
83 |
| |
84 |
0
| System.out.println("Reading files from: " + dir.getCanonicalPath());
|
85 |
| |
86 |
| |
87 |
0
| final Command cmd = new AddDocument();
|
88 |
| |
89 |
| |
90 |
0
| XMLTools.Config localtable = new XMLTools.Config();
|
91 |
0
| localtable.setString(XMLTools.COLLECTION, table.getString(XMLTools.COLLECTION));
|
92 |
0
| if (table.getBoolean(XMLTools.LOCAL)) {
|
93 |
0
| localtable.setBoolean(XMLTools.LOCAL, true);
|
94 |
| } |
95 |
| |
96 |
| |
97 |
0
| for (int i = 0; i < children.length; i++) {
|
98 |
| |
99 |
0
| File file = new File(dir, children[i]);
|
100 |
0
| if (file.isFile()) {
|
101 |
0
| try {
|
102 |
| |
103 |
0
| localtable.setString(XMLTools.NAME_OF, file.getName());
|
104 |
0
| localtable.setString(XMLTools.FILE_PATH, file.getPath());
|
105 |
| |
106 |
| |
107 |
0
| cmd.execute(localtable);
|
108 |
| } catch (Exception e) { |
109 |
0
| System.out.println("Error Adding File: " + file.getName());
|
110 |
0
| if (table.getBoolean(XMLTools.VERBOSE)) {
|
111 |
0
| e.printStackTrace();
|
112 |
| } |
113 |
| } |
114 |
| } |
115 |
| } |
116 |
| } |
117 |
| |
118 |
| } finally { |
119 |
| |
120 |
0
| if (col != null) {
|
121 |
0
| col.close();
|
122 |
| } |
123 |
| } |
124 |
| |
125 |
0
| return true;
|
126 |
| } |
127 |
| |
128 |
0
| public void usage() {
|
129 |
0
| System.out.println("Format: xindice addmultiple -c <context> [-l [-d <path>]] [-v] [parameters...]");
|
130 |
0
| System.out.println();
|
131 |
0
| System.out.println("Add several documents to an existing collection at one time. Documents are");
|
132 |
0
| System.out.println("added with an ID the same as the file name");
|
133 |
0
| System.out.println();
|
134 |
0
| System.out.println("Command-specific switches:");
|
135 |
0
| System.out.println(" -f|--filepath <path>");
|
136 |
0
| System.out.println(" Directory name for the files being added to a collection");
|
137 |
0
| System.out.println(" -e|--extension <extention>");
|
138 |
0
| System.out.println(" File extension for documents to use in the filter");
|
139 |
0
| System.out.println();
|
140 |
| } |
141 |
| } |