use of org.exist.xmldb.ExtendedResource in project exist by eXist-db.
the class FilterInputStreamCacheMonitorTest method binaryResult.
@Test
public void binaryResult() throws XMLDBException {
final FilterInputStreamCacheMonitor monitor = FilterInputStreamCacheMonitor.getInstance();
// assert no binaries in use yet
int activeCount = monitor.getActive().size();
if (activeCount != 0) {
fail("FilterInputStreamCacheMonitor should have no active binaries, but found: " + activeCount + "." + System.getProperty("line.separator") + monitor.dump());
}
ResourceSet resourceSet = null;
try {
resourceSet = existXmldbEmbeddedServer.executeQuery("util:binary-doc('/db/" + TEST_COLLECTION_NAME + "/icon.png')");
assertEquals(1, resourceSet.getSize());
try (final EXistResource resource = (EXistResource) resourceSet.getResource(0)) {
assertTrue(resource instanceof LocalBinaryResource);
assertTrue(((ExtendedResource) resource).getExtendedContent() instanceof BinaryValue);
// one active binary (as it is in the result set)
assertEquals(1, monitor.getActive().size());
}
// assert no active binaries as we just closed the resource in the try-with-resources
activeCount = monitor.getActive().size();
if (activeCount != 0) {
fail("FilterInputStreamCacheMonitor should again have no active binaries, but found: " + activeCount + "." + System.getProperty("line.separator") + monitor.dump());
}
} finally {
resourceSet.clear();
}
}
use of org.exist.xmldb.ExtendedResource in project exist by eXist-db.
the class XMLDBExtractTask method writeBinaryResource.
/**
* Extract single binary resource.
*
* @param res the resource
* @param dest the destination file
*
* @throws XMLDBException if a database error occurs
* @throws IOException if an I/O error occurs
*/
private void writeBinaryResource(final Resource res, Path dest) throws XMLDBException, IOException {
if (createdirectories) {
final Path parentDir = dest.getParent();
if (Files.notExists(parentDir)) {
Files.createDirectories(parentDir);
}
}
// dest != null && ( !dest.exists() ||
if (dest != null || overwrite) {
if (Files.isDirectory(dest)) {
final String fname = res.getId();
dest = dest.resolve(fname);
}
try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(dest))) {
((ExtendedResource) res).getContentIntoAStream(os);
}
} else {
final String msg = "Dest binary file " + ((dest != null) ? (dest.toAbsolutePath().toString() + " ") : "") + "exists. Use " + "overwrite property to overwrite file.";
if (failonerror) {
throw (new BuildException(msg));
} else {
log(msg, Project.MSG_ERR);
}
}
}
use of org.exist.xmldb.ExtendedResource in project exist by eXist-db.
the class InteractiveClient method processCommandLineActions.
/**
* Process the command line options
*
* @return true if all are successful, otherwise false
* @throws java.lang.Exception
*/
private boolean processCommandLineActions() throws Exception {
final boolean foundCollection = options.setCol.isPresent();
// process command-line actions
if (options.reindex) {
if (!foundCollection) {
System.err.println("Please specify target collection with --collection");
shutdown(false);
return false;
}
try {
reindex();
} catch (final XMLDBException e) {
System.err.println("XMLDBException while reindexing collection: " + getExceptionMessage(e));
e.printStackTrace();
return false;
}
}
if (options.rmCol.isPresent()) {
try {
rmcol(options.rmCol.get());
} catch (final XMLDBException e) {
System.err.println("XMLDBException while removing collection: " + getExceptionMessage(e));
e.printStackTrace();
return false;
}
}
if (options.mkCol.isPresent()) {
try {
mkcol(options.mkCol.get());
} catch (final XMLDBException e) {
System.err.println("XMLDBException during mkcol: " + getExceptionMessage(e));
e.printStackTrace();
return false;
}
}
if (options.getDoc.isPresent()) {
try {
final Resource res = retrieve(options.getDoc.get());
if (res != null) {
// String data;
if ("XMLResource".equals(res.getResourceType())) {
if (options.outputFile.isPresent()) {
writeOutputFile(options.outputFile.get(), res.getContent());
} else {
System.out.println(res.getContent().toString());
}
} else {
if (options.outputFile.isPresent()) {
((ExtendedResource) res).getContentIntoAFile(options.outputFile.get());
((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 (options.rmDoc.isPresent()) {
if (!foundCollection) {
System.err.println("Please specify target collection with --collection");
} else {
try {
remove(options.rmDoc.get());
} catch (final XMLDBException e) {
System.err.println("XMLDBException during parse: " + getExceptionMessage(e));
e.printStackTrace();
return false;
}
}
} else if (!options.parseDocs.isEmpty()) {
if (!foundCollection) {
System.err.println("Please specify target collection with --collection");
} else {
for (final Path path : options.parseDocs) {
try {
parse(path);
} catch (final XMLDBException e) {
System.err.println("XMLDBException during parse: " + getExceptionMessage(e));
e.printStackTrace();
return false;
}
}
}
} else if (options.xpath.isPresent() || !options.queryFiles.isEmpty()) {
String xpath = null;
if (!options.queryFiles.isEmpty()) {
try (final BufferedReader reader = Files.newBufferedReader(options.queryFiles.get(0))) {
final StringBuilder buf = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
buf.append(line);
buf.append(EOL);
}
xpath = buf.toString();
}
}
// if no argument has been found, read query from stdin
if (options.xpath.isPresent()) {
final String xpathStr = options.xpath.get();
if (!xpathStr.equals(CommandlineOptions.XPATH_STDIN)) {
xpath = xpathStr;
} else {
// read from stdin
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);
}
xpath = buf.toString();
} catch (final IOException e) {
System.err.println("failed to read query from stdin");
xpath = null;
return false;
}
}
}
if (xpath != null) {
try {
final ResourceSet result = find(xpath);
final int maxResults = options.howManyResults.filter(n -> n > 0).orElse((int) result.getSize());
if (options.outputFile.isPresent()) {
try (final OutputStream fos = new BufferedOutputStream(Files.newOutputStream(options.outputFile.get()));
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());
}
}
}
} else {
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());
}
}
}
} catch (final XMLDBException e) {
System.err.println("XMLDBException during query: " + getExceptionMessage(e));
e.printStackTrace();
return false;
}
}
} else if (options.xupdateFile.isPresent()) {
try {
xupdate(options.setDoc, options.xupdateFile.get());
} catch (final XMLDBException e) {
System.err.println("XMLDBException during xupdate: " + getExceptionMessage(e));
return false;
} catch (final IOException e) {
System.err.println("IOException during xupdate: " + getExceptionMessage(e));
return false;
}
}
return true;
}
Aggregations