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.modules.XMLResource; |
27 |
| |
28 |
| import java.io.File; |
29 |
| import java.io.FileOutputStream; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| public class RetrieveDocument extends Command { |
38 |
| |
39 |
0
| public boolean execute(XMLTools.Config table) throws Exception {
|
40 |
| |
41 |
0
| if (table.getString(XMLTools.COLLECTION) == null) {
|
42 |
0
| System.out.println("ERROR : Collection name and switch required");
|
43 |
0
| return false;
|
44 |
| } |
45 |
| |
46 |
0
| if (table.getString(XMLTools.NAME_OF) == null) {
|
47 |
0
| System.out.println("ERROR : Document Key and switch required");
|
48 |
0
| return false;
|
49 |
| } |
50 |
| |
51 |
0
| Collection col = null;
|
52 |
0
| try {
|
53 |
| |
54 |
| |
55 |
0
| String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
|
56 |
| table.getBoolean(XMLTools.LOCAL)); |
57 |
0
| col = DatabaseManager.getCollection(colstring);
|
58 |
| |
59 |
0
| if (col == null) {
|
60 |
0
| System.out.println("ERROR : Collection not found!");
|
61 |
0
| return false;
|
62 |
| } |
63 |
| |
64 |
0
| String docname = table.getString(XMLTools.NAME_OF);
|
65 |
0
| XMLResource resource = (XMLResource) col.getResource(docname);
|
66 |
| |
67 |
0
| if (resource == null) {
|
68 |
0
| System.out.println("ERROR : Document not found!");
|
69 |
0
| return false;
|
70 |
| } |
71 |
| |
72 |
0
| String documentstr = (String) resource.getContent();
|
73 |
0
| String path = table.getString(XMLTools.FILE_PATH);
|
74 |
0
| if (documentstr != null && path != null && !"".equals(path)) {
|
75 |
| |
76 |
0
| FileOutputStream out = null;
|
77 |
0
| try {
|
78 |
0
| File file = new File(table.getString(XMLTools.FILE_PATH));
|
79 |
| |
80 |
0
| if (file.getParentFile() != null) {
|
81 |
0
| file.getParentFile().mkdirs();
|
82 |
| } |
83 |
| |
84 |
0
| out = new FileOutputStream(file);
|
85 |
| |
86 |
0
| System.out.println("Writing...");
|
87 |
| |
88 |
0
| out.write(documentstr.getBytes("utf-8"));
|
89 |
0
| out.close();
|
90 |
| |
91 |
0
| System.out.println("Wrote file " + table.getString(XMLTools.FILE_PATH));
|
92 |
| } finally { |
93 |
0
| if (out != null) {
|
94 |
0
| out.close();
|
95 |
| } |
96 |
| } |
97 |
| } else { |
98 |
| |
99 |
0
| System.out.println(documentstr);
|
100 |
| } |
101 |
| } finally { |
102 |
| |
103 |
0
| if (col != null) {
|
104 |
0
| col.close();
|
105 |
| } |
106 |
| } |
107 |
| |
108 |
0
| return true;
|
109 |
| } |
110 |
| |
111 |
0
| public void usage() {
|
112 |
0
| System.out.println("Format: xindice rd -c <context> [-l [-d <path>]] [-v] [parameters...]");
|
113 |
0
| System.out.println();
|
114 |
0
| System.out.println("Retrieves a document from a specific collection");
|
115 |
0
| System.out.println();
|
116 |
0
| System.out.println("Command-specific switches:");
|
117 |
0
| System.out.println(" -f|--filepath <file>");
|
118 |
0
| System.out.println(" File path where document will be stored, if omitted");
|
119 |
0
| System.out.println(" document will printed to standard output");
|
120 |
0
| System.out.println(" -n|--nameOf <name>");
|
121 |
0
| System.out.println(" Document key");
|
122 |
0
| System.out.println();
|
123 |
0
| System.out.println("Examples:");
|
124 |
0
| System.out.println(" xindice rd -c /db/test/ocs -f a:\\file.xml -n file.xml");
|
125 |
0
| System.out.println();
|
126 |
| } |
127 |
| } |
128 |
| |