|
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 |
| import org.xmldb.api.DatabaseManager; |
|
24 |
| import org.xmldb.api.base.Collection; |
|
25 |
| import org.xmldb.api.base.ErrorCodes; |
|
26 |
| import org.xmldb.api.base.Resource; |
|
27 |
| import org.xmldb.api.base.XMLDBException; |
|
28 |
| |
|
29 |
| import java.io.File; |
|
30 |
| import java.io.FileOutputStream; |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| public class ExportTree extends Command { |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
0
| public boolean execute(XMLTools.Config table) throws Exception {
|
|
47 |
| |
|
48 |
0
| if (table.getString(XMLTools.COLLECTION) == null) {
|
|
49 |
0
| System.out.println("ERROR : Collection name and switch required");
|
|
50 |
0
| return false;
|
|
51 |
| } |
|
52 |
| |
|
53 |
0
| if (table.getString(XMLTools.FILE_PATH).equals("")) {
|
|
54 |
0
| System.out.println("ERROR: Directory name and switch required");
|
|
55 |
0
| return false;
|
|
56 |
| } |
|
57 |
| |
|
58 |
0
| File fp = new File(table.getString(XMLTools.FILE_PATH));
|
|
59 |
0
| System.out.println();
|
|
60 |
| |
|
61 |
0
| String parent = parentDir(table.getString(XMLTools.COLLECTION));
|
|
62 |
0
| File dir = new File(fp, parent);
|
|
63 |
| |
|
64 |
0
| System.out.println("Creating directory " + dir.getPath());
|
|
65 |
0
| dir.mkdir();
|
|
66 |
| |
|
67 |
0
| process(dir, table);
|
|
68 |
| |
|
69 |
0
| return true;
|
|
70 |
| } |
|
71 |
| |
|
72 |
0
| private String parentDir(String parent) {
|
|
73 |
0
| int idx = parent.lastIndexOf("/");
|
|
74 |
0
| if (idx != -1) {
|
|
75 |
0
| return parent.substring(idx + 1);
|
|
76 |
| } else { |
|
77 |
0
| return parent;
|
|
78 |
| } |
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
0
| boolean process(File directory, XMLTools.Config table) throws Exception {
|
|
85 |
0
| Collection col = null;
|
|
86 |
0
| try {
|
|
87 |
0
| String collection = table.getString(XMLTools.COLLECTION);
|
|
88 |
| |
|
89 |
| |
|
90 |
0
| String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
|
|
91 |
| table.getBoolean(XMLTools.LOCAL)); |
|
92 |
0
| col = DatabaseManager.getCollection(colstring);
|
|
93 |
0
| if (col == null) {
|
|
94 |
0
| System.out.println("ERROR : Collection not found!");
|
|
95 |
0
| return false;
|
|
96 |
| } |
|
97 |
| |
|
98 |
0
| String[] files = null;
|
|
99 |
0
| try {
|
|
100 |
0
| files = col.listResources();
|
|
101 |
| } catch (XMLDBException e) { |
|
102 |
| |
|
103 |
0
| if (e.errorCode != ErrorCodes.VENDOR_ERROR) {
|
|
104 |
0
| throw e;
|
|
105 |
| } |
|
106 |
| } |
|
107 |
| |
|
108 |
0
| if (files != null) {
|
|
109 |
0
| System.out.println("Extracting " + files.length + " files from " + table.getString(XMLTools.COLLECTION));
|
|
110 |
0
| for (int j = 0; j < files.length; j++) {
|
|
111 |
0
| Resource res = col.getResource(files[j]);
|
|
112 |
0
| Object content = res.getContent();
|
|
113 |
0
| FileOutputStream output = new FileOutputStream(new File(directory, files[j]));
|
|
114 |
0
| try {
|
|
115 |
0
| if (content instanceof String) {
|
|
116 |
| |
|
117 |
| |
|
118 |
0
| output.write(((String)content).getBytes("utf-8"));
|
|
119 |
| } else { |
|
120 |
0
| output.write((byte[]) content);
|
|
121 |
| } |
|
122 |
| } finally { |
|
123 |
0
| output.close();
|
|
124 |
| } |
|
125 |
| } |
|
126 |
| } |
|
127 |
| |
|
128 |
0
| String[] list = col.listChildCollections();
|
|
129 |
0
| for (int i = 0; i < list.length; i++) {
|
|
130 |
0
| File file = new File(directory, list[i]);
|
|
131 |
| |
|
132 |
0
| System.out.println("Creating directory " + file.getPath());
|
|
133 |
0
| file.mkdirs();
|
|
134 |
| |
|
135 |
0
| table.setString(XMLTools.COLLECTION, collection + "/" + file.getName());
|
|
136 |
| |
|
137 |
0
| process(new File(directory + "//" + file.getName()), table);
|
|
138 |
| } |
|
139 |
| |
|
140 |
| } finally { |
|
141 |
0
| if (col != null) {
|
|
142 |
0
| col.close();
|
|
143 |
| } |
|
144 |
| } |
|
145 |
0
| return true;
|
|
146 |
| } |
|
147 |
| |
|
148 |
0
| public void usage() {
|
|
149 |
0
| System.out.println("Format: xindice export -c <context> [-l [-d <path>]] [-v] [parameters...]");
|
|
150 |
0
| System.out.println();
|
|
151 |
0
| System.out.println("Takes a collection tree and creates a directory tree from it");
|
|
152 |
0
| System.out.println();
|
|
153 |
0
| System.out.println("Command-specific switches:");
|
|
154 |
0
| System.out.println(" -f|--filepath <path>");
|
|
155 |
0
| System.out.println(" Directory name where tree will be created");
|
|
156 |
0
| System.out.println();
|
|
157 |
| } |
|
158 |
| } |
|
159 |
| |