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.FileFilter; |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public class ImportTree extends Command { |
40 |
| |
41 |
| Command addDocument = null; |
42 |
| Command addCollection = null; |
43 |
| |
44 |
0
| public boolean execute(XMLTools.Config table) throws Exception {
|
45 |
| |
46 |
| |
47 |
0
| if (table.getString(XMLTools.FILE_PATH) == null) {
|
48 |
0
| System.out.println("ERROR : Directory name and switch required");
|
49 |
0
| return false;
|
50 |
| } |
51 |
| |
52 |
0
| if (table.getString(XMLTools.COLLECTION) == null) {
|
53 |
0
| System.out.println("ERROR : Collection name and switch required");
|
54 |
0
| return false;
|
55 |
| } |
56 |
| |
57 |
0
| Collection col = null;
|
58 |
0
| try {
|
59 |
| |
60 |
| |
61 |
0
| addDocument =
|
62 |
| (Command) Class.forName("org.apache.xindice.tools.command.AddDocument").newInstance(); |
63 |
| |
64 |
0
| addCollection =
|
65 |
| (Command) Class.forName("org.apache.xindice.tools.command.AddCollection").newInstance(); |
66 |
| |
67 |
0
| String startcollection = table.getString(XMLTools.COLLECTION);
|
68 |
| |
69 |
| |
70 |
0
| String colstring = normalizeCollectionURI(startcollection, table.getBoolean(XMLTools.LOCAL));
|
71 |
0
| if ((col = DatabaseManager.getCollection(colstring)) == null) {
|
72 |
0
| System.out.println("ERROR : Collection not found!");
|
73 |
0
| return false;
|
74 |
| } |
75 |
| |
76 |
0
| File startdir = new File(table.getString(XMLTools.FILE_PATH));
|
77 |
| |
78 |
| |
79 |
0
| process(startdir, colstring, table);
|
80 |
| |
81 |
| } finally { |
82 |
| |
83 |
0
| if (col != null) {
|
84 |
0
| col.close();
|
85 |
| } |
86 |
| } |
87 |
| |
88 |
0
| return true;
|
89 |
| } |
90 |
| |
91 |
| |
92 |
0
| void process(File directory, String baseCollection, XMLTools.Config table) {
|
93 |
0
| try {
|
94 |
| |
95 |
0
| final String ext = table.getString(XMLTools.EXTENSION);
|
96 |
| |
97 |
| |
98 |
| |
99 |
0
| if (!directory.isDirectory()) {
|
100 |
0
| System.out.println("ERROR: The specified import path is not a directory " +
|
101 |
| directory.getPath()); |
102 |
0
| return;
|
103 |
| } |
104 |
| |
105 |
| |
106 |
0
| baseCollection = createCollection(baseCollection, directory.getName());
|
107 |
| |
108 |
| |
109 |
0
| File[] subdirs = directory.listFiles(new ExtensionFileFilter(ext));
|
110 |
| |
111 |
0
| for (int i = 0; i < subdirs.length; i++) {
|
112 |
| |
113 |
0
| if (subdirs[i].isDirectory()) {
|
114 |
0
| process(subdirs[i], baseCollection, table);
|
115 |
| } else { |
116 |
| |
117 |
0
| importFile(baseCollection, subdirs[i].getAbsolutePath(), subdirs[i].getName());
|
118 |
| } |
119 |
| } |
120 |
| } catch (Exception e) { |
121 |
0
| System.out.println("ERROR : " + e.getMessage());
|
122 |
0
| if (table.getBoolean(XMLTools.VERBOSE)) {
|
123 |
0
| e.printStackTrace(System.err);
|
124 |
| } |
125 |
| } |
126 |
| } |
127 |
| |
128 |
0
| protected void importFile(String baseCollection, String path, String name)
|
129 |
| throws Exception { |
130 |
0
| XMLTools.Config table = new XMLTools.Config();
|
131 |
| |
132 |
0
| table.setString(XMLTools.COLLECTION, baseCollection);
|
133 |
0
| table.setString(XMLTools.FILE_PATH, path);
|
134 |
0
| table.setString(XMLTools.NAME_OF, name);
|
135 |
| |
136 |
0
| addDocument.execute(table);
|
137 |
| } |
138 |
| |
139 |
0
| protected String createCollection(String baseCollection, String newCollection)
|
140 |
| throws Exception { |
141 |
0
| if (!newCollection.equals(".")) {
|
142 |
0
| XMLTools.Config table = new XMLTools.Config();
|
143 |
0
| table.setString(XMLTools.COLLECTION, baseCollection);
|
144 |
0
| table.setString(XMLTools.NAME_OF, newCollection);
|
145 |
| |
146 |
0
| addCollection.execute(table);
|
147 |
| } else { |
148 |
0
| newCollection = "";
|
149 |
| } |
150 |
| |
151 |
0
| return baseCollection + "/" + newCollection;
|
152 |
| } |
153 |
| |
154 |
| |
155 |
| |
156 |
| |
157 |
| private static class ExtensionFileFilter implements FileFilter { |
158 |
| |
159 |
| private String extension = ""; |
160 |
| |
161 |
| |
162 |
| |
163 |
| |
164 |
| |
165 |
| |
166 |
| |
167 |
0
| public ExtensionFileFilter(String extension) {
|
168 |
0
| this.extension = extension;
|
169 |
| } |
170 |
| |
171 |
| |
172 |
| |
173 |
| |
174 |
| |
175 |
| |
176 |
| |
177 |
| |
178 |
| |
179 |
0
| public boolean accept(File pathname) {
|
180 |
0
| if (this.extension.length() > 0) {
|
181 |
0
| return pathname.getName().endsWith("." + this.extension) || pathname.isDirectory();
|
182 |
| } else { |
183 |
0
| return true;
|
184 |
| } |
185 |
| } |
186 |
| } |
187 |
| |
188 |
0
| public void usage() {
|
189 |
0
| System.out.println("Format: xindice import -c <context> [-l [-d <path>]] [-v] [parameters...]");
|
190 |
0
| System.out.println();
|
191 |
0
| System.out.println("Takes a directory path and creates a collection from it's name. The files");
|
192 |
0
| System.out.println("within the directory are converted into documents and then stored into the");
|
193 |
0
| System.out.println("newly created collection with their filename as a document key.");
|
194 |
0
| System.out.println();
|
195 |
0
| System.out.println("Command-specific switches:");
|
196 |
0
| System.out.println(" -f|--filepath <file>");
|
197 |
0
| System.out.println(" Directory tree");
|
198 |
0
| System.out.println(" -e|--extension <extention>");
|
199 |
0
| System.out.println(" File extension for documents to use in the filter");
|
200 |
0
| System.out.println();
|
201 |
| } |
202 |
| } |