|
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.xml.sax.InputSource; |
|
25 |
| import org.xml.sax.XMLReader; |
|
26 |
| import org.xmldb.api.DatabaseManager; |
|
27 |
| import org.xmldb.api.base.Collection; |
|
28 |
| import org.xmldb.api.base.Resource; |
|
29 |
| |
|
30 |
| import javax.xml.parsers.SAXParserFactory; |
|
31 |
| import java.io.File; |
|
32 |
| import java.io.FileInputStream; |
|
33 |
| import java.io.InputStream; |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| public class AddDocument extends Command { |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
0
| public boolean execute(XMLTools.Config table) throws Exception {
|
|
48 |
| |
|
49 |
0
| if (table.getString(XMLTools.COLLECTION) == null) {
|
|
50 |
0
| System.out.println("ERROR : Collection and switch required");
|
|
51 |
0
| return false;
|
|
52 |
| } |
|
53 |
| |
|
54 |
0
| if ("".equals(table.getString(XMLTools.FILE_PATH))) {
|
|
55 |
0
| System.out.println("ERROR : File path required");
|
|
56 |
0
| return false;
|
|
57 |
| } |
|
58 |
| |
|
59 |
0
| Collection col = null;
|
|
60 |
0
| InputStream fis = null;
|
|
61 |
0
| try {
|
|
62 |
| |
|
63 |
0
| String colstring = normalizeCollectionURI(table.getString(XMLTools.COLLECTION),
|
|
64 |
| table.getBoolean(XMLTools.LOCAL)); |
|
65 |
0
| col = DatabaseManager.getCollection(colstring);
|
|
66 |
0
| if (col == null) {
|
|
67 |
0
| System.out.println("ERROR : Collection not found!");
|
|
68 |
0
| return false;
|
|
69 |
| } |
|
70 |
| |
|
71 |
0
| File file = new File(table.getString(XMLTools.FILE_PATH));
|
|
72 |
0
| fis = new FileInputStream(file);
|
|
73 |
| |
|
74 |
| |
|
75 |
0
| SAXParserFactory spf = javax.xml.parsers.SAXParserFactory.newInstance();
|
|
76 |
0
| spf.setNamespaceAware(true);
|
|
77 |
| |
|
78 |
0
| XMLReader reader = spf.newSAXParser().getXMLReader();
|
|
79 |
0
| StringSerializer ser = new StringSerializer();
|
|
80 |
0
| reader.setContentHandler(ser);
|
|
81 |
0
| reader.setProperty("http://xml.org/sax/properties/lexical-handler", ser);
|
|
82 |
0
| reader.parse(new InputSource(fis));
|
|
83 |
| |
|
84 |
| |
|
85 |
0
| Resource resource = col.createResource(table.getString(XMLTools.NAME_OF), "XMLResource");
|
|
86 |
0
| resource.setContent(ser.toString());
|
|
87 |
0
| col.storeResource(resource);
|
|
88 |
| |
|
89 |
0
| System.out.println("Added document " + table.getString(XMLTools.COLLECTION) + "/" + resource.getId());
|
|
90 |
| } finally { |
|
91 |
0
| if (col != null) {
|
|
92 |
0
| col.close();
|
|
93 |
| } |
|
94 |
| |
|
95 |
0
| if (fis != null) {
|
|
96 |
0
| fis.close();
|
|
97 |
| } |
|
98 |
| } |
|
99 |
| |
|
100 |
0
| return true;
|
|
101 |
| } |
|
102 |
| |
|
103 |
0
| public void usage() {
|
|
104 |
0
| System.out.println("Format: xindice ad -c <context> [-l [-d <path>]] [-v] [parameters...]");
|
|
105 |
0
| System.out.println();
|
|
106 |
0
| System.out.println("Adds a single document to an existing collection");
|
|
107 |
0
| System.out.println();
|
|
108 |
0
| System.out.println("Command-specific switches:");
|
|
109 |
0
| System.out.println(" -f|--filepath <file>");
|
|
110 |
0
| System.out.println(" File path for the document being added to the collection");
|
|
111 |
0
| System.out.println(" -n|--nameOf <name>");
|
|
112 |
0
| System.out.println(" Document key. If this parameter is omitted, database will");
|
|
113 |
0
| System.out.println(" generate an unique key");
|
|
114 |
0
| System.out.println();
|
|
115 |
0
| System.out.println("Examples:");
|
|
116 |
0
| System.out.println(" xindice ad -c /db/test -f /tmp/xmldocument -n myxmldocument");
|
|
117 |
0
| System.out.println(" xindice ad -c /db/test/nested -f /tmp/data.xml");
|
|
118 |
0
| System.out.println();
|
|
119 |
| } |
|
120 |
| } |