}
}
if (cOpt.optionGet != null) {
try {
final Resource res = retrieve(cOpt.optionGet);
if (res != null) {
// String data;
if ("XMLResource".equals(res.getResourceType())) {
if (cOpt.optionOutputFile != null) {
writeOutputFile(cOpt.optionOutputFile, res.getContent());
} else {
System.out.println(res.getContent().toString());
}
} else {
if (cOpt.optionOutputFile != null) {
((ExtendedResource)res).getContentIntoAFile(new File(cOpt.optionOutputFile));
((EXistResource)res).freeResources();
} else {
((ExtendedResource)res).getContentIntoAStream(System.out);
System.out.println();
}
}
}
} catch (final XMLDBException e) {
System.err.println("XMLDBException while trying to retrieve document: " + getExceptionMessage(e));
e.printStackTrace();
return false;
}
} else if (cOpt.optionRemove != null) {
if (!cOpt.foundCollection) {
System.err.println("Please specify target collection with --collection");
} else {
try {
remove(cOpt.optionRemove);
} catch (final XMLDBException e) {
System.err.println("XMLDBException during parse: " + getExceptionMessage(e));
e.printStackTrace();
return false;
}
}
} else if (cOpt.doStore) {
if (!cOpt.foundCollection) {
System.err.println("Please specify target collection with --collection");
} else {
for (final String arg : cOpt.optionalArgs) {
try {
parse(arg);
} catch (final XMLDBException e) {
System.err.println("XMLDBException during parse: " + getExceptionMessage(e));
e.printStackTrace();
return false;
}
}
}
} else if (cOpt.optionXpath != null || cOpt.optionQueryFile != null) {
if (cOpt.optionQueryFile != null) {
final BufferedReader reader = new BufferedReader(new FileReader(cOpt.optionQueryFile));
try {
final StringBuilder buf = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append(EOL);
}
cOpt.optionXpath = buf.toString();
} finally {
reader.close();
}
}
// if no argument has been found, read query from stdin
if ("stdin".equals(cOpt.optionXpath)) {
try {
final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
final StringBuilder buf = new StringBuilder();
String line;
while ((line = stdin.readLine()) != null) {
buf.append(line);
buf.append(EOL);
}
cOpt.optionXpath = buf.toString();
} catch (final IOException e) {
System.err.println("failed to read query from stdin");
cOpt.optionXpath = null;
return false;
}
}
if (cOpt.optionXpath != null) {
try {
final ResourceSet result = find(cOpt.optionXpath);
if (maxResults <= 0) {
maxResults = (int) result.getSize();
}
if (cOpt.optionOutputFile == null) {
for (int i = 0; i < maxResults && i < result.getSize(); i++) {
final Resource res=result.getResource(i);
if(res instanceof ExtendedResource) {
((ExtendedResource)res).getContentIntoAStream(System.out);
} else {
System.out.println(res.getContent());
}
}
} else {
final FileOutputStream fos = new FileOutputStream(cOpt.optionOutputFile);
final BufferedOutputStream bos = new BufferedOutputStream(fos);
final PrintStream ps = new PrintStream(bos);
for (int i = 0; i < maxResults && i < result.getSize(); i++) {
final Resource res = result.getResource(i);
if(res instanceof ExtendedResource) {
((ExtendedResource)res).getContentIntoAStream(ps);
} else {
ps.print(res.getContent().toString());
}
}
ps.close();
bos.close();
fos.close();