use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class LocalXMLResource method getContentAsSAX.
@Override
public void getContentAsSAX(final ContentHandler handler) throws XMLDBException {
// case 1: content is an external DOM node
if (root != null && !(root instanceof NodeValue)) {
try {
final String option = collection.getProperty(Serializer.GENERATE_DOC_EVENTS, "false");
final DOMStreamer streamer = (DOMStreamer) SerializerPool.getInstance().borrowObject(DOMStreamer.class);
try {
streamer.setContentHandler(handler);
streamer.setLexicalHandler(lexicalHandler);
streamer.serialize(root, option.equalsIgnoreCase("true"));
} finally {
SerializerPool.getInstance().returnObject(streamer);
}
} catch (final Exception e) {
throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, e.getMessage(), e);
}
} else {
withDb((broker, transaction) -> {
try {
// case 2: content is an atomic value
if (value != null) {
value.toSAX(broker, handler, getProperties());
// case 3: content is an internal node or a document
} else {
final Serializer serializer = broker.borrowSerializer();
try {
serializer.setUser(user);
serializer.setProperties(getProperties());
serializer.setSAXHandlers(handler, lexicalHandler);
if (root != null) {
serializer.toSAX((NodeValue) root);
} else if (proxy != null) {
serializer.toSAX(proxy);
} else {
read(broker, transaction).apply((document, broker1, transaction1) -> {
try {
serializer.toSAX(document);
return null;
} catch (final SAXException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
});
}
} finally {
broker.returnSerializer(serializer);
}
}
return null;
} catch (final SAXException e) {
throw new XMLDBException(ErrorCodes.VENDOR_ERROR, e.getMessage(), e);
}
});
}
}
use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class GetRunningJobs method eval.
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
if (!context.getSubject().hasDbaRole()) {
throw (new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to get the list of running jobs"));
}
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("jobs", NAMESPACE_URI, PREFIX), null);
final BrokerPool brokerPool = context.getBroker().getBrokerPool();
final ProcessMonitor monitor = brokerPool.getProcessMonitor();
final ProcessMonitor.JobInfo[] jobs = monitor.runningJobs();
for (ProcessMonitor.JobInfo job : jobs) {
final Thread process = job.getThread();
final Date startDate = new Date(job.getStartTime());
builder.startElement(new QName("job", NAMESPACE_URI, PREFIX), null);
builder.addAttribute(new QName("id", null, null), process.getName());
builder.addAttribute(new QName("action", null, null), job.getAction());
builder.addAttribute(new QName("start", null, null), new DateTimeValue(startDate).getStringValue());
builder.addAttribute(new QName("info", null, null), job.getAddInfo().toString());
builder.endElement();
}
builder.endElement();
builder.endDocument();
return (NodeValue) builder.getDocument().getDocumentElement();
} finally {
context.popDocumentContext();
}
}
use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class EXIUtils method getInputStream.
protected static InputStream getInputStream(Item item, XQueryContext context) throws XPathException, MalformedURLException, IOException {
switch(item.getType()) {
case Type.ANY_URI:
LOG.debug("Streaming xs:anyURI");
// anyURI provided
String url = item.getStringValue();
// Fix URL
if (url.startsWith("/")) {
url = "xmldb:exist://" + url;
}
return new URL(url).openStream();
case Type.ELEMENT:
case Type.DOCUMENT:
LOG.debug("Streaming element or document node");
/*
if (item instanceof NodeProxy) {
NodeProxy np = (NodeProxy) item;
String url = "xmldb:exist://" + np.getDocument().getBaseURI();
LOG.debug("Document detected, adding URL " + url);
streamSource.setSystemId(url);
}
*/
// Node provided
final ConsumerE<ConsumerE<Serializer, IOException>, IOException> withSerializerFn = fn -> {
final Serializer serializer = context.getBroker().borrowSerializer();
try {
fn.accept(serializer);
} finally {
context.getBroker().returnSerializer(serializer);
}
};
NodeValue node = (NodeValue) item;
return new NodeInputStream(context.getBroker().getBrokerPool(), withSerializerFn, node);
default:
LOG.error("Wrong item type {}", Type.getTypeName(item.getType()));
throw new XPathException("wrong item type " + Type.getTypeName(item.getType()));
}
}
use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class Directory method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
if (!context.getSubject().hasDbaRole()) {
XPathException xPathException = new XPathException(this, "Permission denied, calling user '" + context.getSubject().getName() + "' must be a DBA to call this function.");
logger.error("Invalid user", xPathException);
throw xPathException;
}
final String inputPath = args[0].getStringValue();
final Path directoryPath = FileModuleHelper.getFile(inputPath);
if (logger.isDebugEnabled()) {
logger.debug("Listing matching files in directory: {}", directoryPath.toAbsolutePath().toString());
}
if (!Files.isDirectory(directoryPath)) {
throw new XPathException(this, "'" + inputPath + "' does not point to a valid directory.");
}
// Get list of files, null if baseDir does not point to a directory
context.pushDocumentContext();
try (final Stream<Path> scannedFiles = Files.list(directoryPath)) {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("list", null, null), null);
scannedFiles.forEach(entry -> {
if (logger.isDebugEnabled()) {
logger.debug("Found: {}", entry.toAbsolutePath().toString());
}
String entryType = "unknown";
if (Files.isRegularFile(entry)) {
entryType = "file";
} else if (Files.isDirectory(entry)) {
entryType = "directory";
}
builder.startElement(new QName(entryType, NAMESPACE_URI, PREFIX), null);
builder.addAttribute(new QName("name", null, null), FileUtils.fileName(entry));
try {
if (Files.isRegularFile(entry)) {
final Long sizeLong = Files.size(entry);
String sizeString = Long.toString(sizeLong);
String humanSize = getHumanSize(sizeLong, sizeString);
builder.addAttribute(new QName("size", null, null), sizeString);
builder.addAttribute(new QName("human-size", null, null), humanSize);
}
builder.addAttribute(new QName("modified", null, null), new DateTimeValue(new Date(Files.getLastModifiedTime(entry).toMillis())).getStringValue());
builder.addAttribute(new QName("hidden", null, null), new BooleanValue(Files.isHidden(entry)).getStringValue());
builder.addAttribute(new QName("canRead", null, null), new BooleanValue(Files.isReadable(entry)).getStringValue());
builder.addAttribute(new QName("canWrite", null, null), new BooleanValue(Files.isWritable(entry)).getStringValue());
} catch (final IOException | XPathException ioe) {
LOG.warn(ioe);
}
builder.endElement();
});
builder.endElement();
return (NodeValue) builder.getDocument().getDocumentElement();
} catch (final IOException ioe) {
throw new XPathException(this, ioe);
} finally {
context.popDocumentContext();
}
}
use of org.exist.xquery.value.NodeValue in project exist by eXist-db.
the class FunAnalyzeString method eval.
@Override
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
context.pushDocumentContext();
try {
final MemTreeBuilder builder = context.getDocumentBuilder();
builder.startDocument();
builder.startElement(new QName("analyze-string-result", Function.BUILTIN_FUNCTION_NS), null);
String input = "";
if (!args[0].isEmpty()) {
input = args[0].itemAt(0).getStringValue();
}
if (input != null && !input.isEmpty()) {
final String pattern = args[1].itemAt(0).getStringValue();
String flags = "";
if (args.length == 3) {
flags = args[2].itemAt(0).getStringValue();
}
analyzeString(builder, input, pattern, flags);
}
builder.endElement();
builder.endDocument();
return (NodeValue) builder.getDocument().getDocumentElement();
} finally {
context.popDocumentContext();
}
}
Aggregations