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.XUpdateQueryService; |
27 |
| |
28 |
| import java.io.BufferedReader; |
29 |
| import java.io.File; |
30 |
| import java.io.FileInputStream; |
31 |
| import java.io.InputStreamReader; |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| |
40 |
| public class XUpdate extends Command { |
41 |
| |
42 |
0
| public boolean execute(XMLTools.Config table) throws Exception {
|
43 |
| |
44 |
0
| if (table.getString(XMLTools.COLLECTION) == null) {
|
45 |
0
| System.out.println("ERROR : Collection name and switch required");
|
46 |
0
| return false;
|
47 |
| } |
48 |
| |
49 |
0
| if ("".equals(table.getString(XMLTools.FILE_PATH))) {
|
50 |
0
| System.out.println("ERROR : Path to file containing XUpdate to execute required");
|
51 |
0
| return false;
|
52 |
| } |
53 |
| |
54 |
0
| Collection col = null;
|
55 |
0
| try {
|
56 |
| |
57 |
0
| String name = table.getString(XMLTools.NAME_OF);
|
58 |
| |
59 |
| |
60 |
0
| String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
|
61 |
| table.getBoolean(XMLTools.LOCAL)); |
62 |
| |
63 |
| |
64 |
0
| StringBuffer commands = new StringBuffer();
|
65 |
0
| File file = new File(table.getString(XMLTools.FILE_PATH));
|
66 |
| |
67 |
0
| BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
|
68 |
0
| String line;
|
69 |
0
| while ((line = reader.readLine()) != null) {
|
70 |
0
| commands.append(line);
|
71 |
| } |
72 |
| |
73 |
0
| reader.close();
|
74 |
| |
75 |
| |
76 |
0
| col = DatabaseManager.getCollection(colstring);
|
77 |
0
| if (col == null) {
|
78 |
0
| System.out.println("ERROR : Collection not found!");
|
79 |
0
| return false;
|
80 |
| } |
81 |
| |
82 |
| |
83 |
0
| XUpdateQueryService service = (XUpdateQueryService) col.getService("XUpdateQueryService", "1.0");
|
84 |
| |
85 |
0
| long result;
|
86 |
| |
87 |
0
| if (name == null) {
|
88 |
0
| result = service.update(commands.toString());
|
89 |
| } |
90 |
| else { |
91 |
0
| result = service.updateResource(name, commands.toString());
|
92 |
| } |
93 |
| |
94 |
0
| System.out.println(result + " documents updated");
|
95 |
| } finally { |
96 |
0
| if (col != null) {
|
97 |
0
| col.close();
|
98 |
| } |
99 |
| } |
100 |
| |
101 |
0
| return true;
|
102 |
| } |
103 |
| |
104 |
0
| public void usage() {
|
105 |
0
| System.out.println("Format: xindice xupdate -c <context> [-l [-d <path>]] [-v] [parameters...]");
|
106 |
0
| System.out.println();
|
107 |
0
| System.out.println("Runs XUpdate query stored in a file against a specified collection or");
|
108 |
0
| System.out.println("document");
|
109 |
0
| System.out.println();
|
110 |
0
| System.out.println("Command-specific switches:");
|
111 |
0
| System.out.println(" -f|--filepath <file>");
|
112 |
0
| System.out.println(" Name of the file that holds XUpdate query");
|
113 |
0
| System.out.println(" -n|--nameOf <name>");
|
114 |
0
| System.out.println(" Document name, if query to be run against single document");
|
115 |
0
| System.out.println();
|
116 |
0
| System.out.println("Examples:");
|
117 |
0
| System.out.println(" xindice xupdate -c /db/test -f /path/to/xupdate.xml");
|
118 |
0
| System.out.println(" xindice xupdate -c /db/test -n document-to-update.xml -f /path/to/xupdate.xml");
|
119 |
0
| System.out.println();
|
120 |
| } |
121 |
| } |