Search in sources :

Example 11 with DateTimeValue

use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.

the class HistoryTrigger method makeCopy.

private void makeCopy(final DBBroker broker, final Txn txn, final DocumentImpl doc) throws TriggerException {
    if (doc == null) {
        return;
    }
    // construct the destination path
    final XmldbURI path = rootPath.append(doc.getURI());
    try {
        // construct the destination document name
        String dtValue = new DateTimeValue(new Date(doc.getLastModified())).getStringValue();
        // multiple ':' are not allowed in URI so use '-'
        dtValue = dtValue.replaceAll(":", "-");
        // as we are using '-' instead of ':' do the same for '.'
        dtValue = dtValue.replaceAll("\\.", "-");
        final XmldbURI name = XmldbURI.create(dtValue);
        // create the destination document
        // TODO : how is the transaction handled ? It holds the locks !
        final Collection destination = broker.getOrCreateCollection(txn, path);
        broker.saveCollection(txn, destination);
        broker.copyResource(txn, doc, destination, name);
    } catch (final XPathException | IOException | PermissionDeniedException | LockException | EXistException xpe) {
        throw new TriggerException(xpe);
    }
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue) XPathException(org.exist.xquery.XPathException) LockException(org.exist.util.LockException) Collection(org.exist.collections.Collection) PermissionDeniedException(org.exist.security.PermissionDeniedException) IOException(java.io.IOException) EXistException(org.exist.EXistException) XmldbURI(org.exist.xmldb.XmldbURI) Date(java.util.Date)

Example 12 with DateTimeValue

use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.

the class FunCurrentDateTime method eval.

public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException {
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().start(this);
        context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
        if (contextSequence != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
        }
        if (contextItem != null) {
            context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
        }
    }
    Sequence result = new DateTimeValue(context.getCalendar());
    if (isCalledAs("current-dateTime")) {
    // do nothing, result already in right form
    } else if (isCalledAs("current-date")) {
        result = result.convertTo(Type.DATE);
    } else if (isCalledAs("current-time")) {
        result = result.convertTo(Type.TIME);
    } else {
        throw new Error("Can't handle function " + getName().getLocalPart());
    }
    if (context.getProfiler().isEnabled()) {
        context.getProfiler().end(this, "", result);
    }
    return result;
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue) Sequence(org.exist.xquery.value.Sequence)

Example 13 with DateTimeValue

use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.

the class PersistentLogin method register.

/**
 * Register the user and generate a first login token which will be valid for the next
 * call to {@link #lookup(String)}.
 *
 * The generated token will have the format base64(series-hash):base64(token-hash).
 *
 * @param user the user name
 * @param password the password
 * @param timeToLive timeout of the token
 * @return a first login token
 * @throws XPathException if a query error occurs
 */
public LoginDetails register(String user, String password, DurationValue timeToLive) throws XPathException {
    DateTimeValue now = new DateTimeValue(new Date());
    DateTimeValue expires = (DateTimeValue) now.plus(timeToLive);
    LoginDetails login = new LoginDetails(user, password, timeToLive, expires.getTimeInMillis());
    seriesMap.put(login.getSeries(), login);
    return login;
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue)

Example 14 with DateTimeValue

use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.

the class DirectoryList 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 baseDir = FileModuleHelper.getFile(inputPath);
    final Sequence patterns = args[1];
    if (logger.isDebugEnabled()) {
        logger.debug("Listing matching files in directory: {}", baseDir);
    }
    context.pushDocumentContext();
    final MemTreeBuilder builder = context.getDocumentBuilder();
    builder.startDocument();
    builder.startElement(new QName("list", NAMESPACE_URI, PREFIX), null);
    builder.addAttribute(new QName("directory", null, null), baseDir.toString());
    try {
        final int patternsLen = patterns.getItemCount();
        final String[] includes = new String[patternsLen];
        for (int i = 0; i < patternsLen; i++) {
            includes[i] = patterns.itemAt(0).getStringValue();
        }
        final DirectoryScanner directoryScanner = new DirectoryScanner();
        directoryScanner.setIncludes(includes);
        directoryScanner.setBasedir(baseDir.toFile());
        directoryScanner.setCaseSensitive(true);
        directoryScanner.scan();
        for (final String includedFile : directoryScanner.getIncludedFiles()) {
            final Path file = baseDir.resolve(includedFile);
            if (logger.isDebugEnabled()) {
                logger.debug("Found: {}", file.toAbsolutePath());
            }
            String relPath = file.toString().substring(baseDir.toString().length() + 1);
            int lastSeparatorPosition = relPath.lastIndexOf(java.io.File.separatorChar);
            String relDir = null;
            if (lastSeparatorPosition >= 0) {
                relDir = relPath.substring(0, lastSeparatorPosition);
                relDir = relDir.replace(java.io.File.separatorChar, '/');
            }
            builder.startElement(new QName("file", NAMESPACE_URI, PREFIX), null);
            builder.addAttribute(new QName("name", null, null), FileUtils.fileName(file));
            Long sizeLong = FileUtils.sizeQuietly(file);
            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(file).toMillis())).getStringValue());
            if (relDir != null && !relDir.isEmpty()) {
                builder.addAttribute(new QName("subdir", null, null), relDir);
            }
            builder.endElement();
        }
        builder.endElement();
        return (NodeValue) builder.getDocument().getDocumentElement();
    } catch (final IOException e) {
        throw new XPathException(this, e.getMessage());
    } finally {
        context.popDocumentContext();
    }
}
Also used : Path(java.nio.file.Path) NodeValue(org.exist.xquery.value.NodeValue) DateTimeValue(org.exist.xquery.value.DateTimeValue) XPathException(org.exist.xquery.XPathException) QName(org.exist.dom.QName) Sequence(org.exist.xquery.value.Sequence) IOException(java.io.IOException) Date(java.util.Date) MemTreeBuilder(org.exist.dom.memtree.MemTreeBuilder) DirectoryScanner(org.apache.tools.ant.DirectoryScanner)

Example 15 with DateTimeValue

use of org.exist.xquery.value.DateTimeValue in project exist by eXist-db.

the class AbstractBackupDescriptor method getDate.

public Date getDate() {
    if (date == null) {
        try {
            final Properties properties = getProperties();
            final String dateStr = properties.getProperty("date");
            if (dateStr != null) {
                final DateTimeValue dtv = new DateTimeValue(dateStr);
                date = dtv.getDate();
            }
        } catch (final IOException | XPathException e) {
        }
        if (date == null) {
            // catch unexpected issues by setting the backup time as early as possible
            date = new Date(0);
        }
    }
    return (date);
}
Also used : DateTimeValue(org.exist.xquery.value.DateTimeValue) XPathException(org.exist.xquery.XPathException) IOException(java.io.IOException) Properties(java.util.Properties) Date(java.util.Date)

Aggregations

DateTimeValue (org.exist.xquery.value.DateTimeValue)19 XPathException (org.exist.xquery.XPathException)12 Date (java.util.Date)9 IOException (java.io.IOException)8 Permission (org.exist.security.Permission)6 PermissionDeniedException (org.exist.security.PermissionDeniedException)5 XmldbURI (org.exist.xmldb.XmldbURI)5 SAXException (org.xml.sax.SAXException)5 Collection (org.exist.collections.Collection)4 QName (org.exist.dom.QName)4 ACLPermission (org.exist.security.ACLPermission)4 Path (java.nio.file.Path)3 MemTreeBuilder (org.exist.dom.memtree.MemTreeBuilder)3 SAXSerializer (org.exist.util.serializer.SAXSerializer)3 Sequence (org.exist.xquery.value.Sequence)3 AttributesImpl (org.xml.sax.helpers.AttributesImpl)3 Tuple (com.evolvedbinary.j8fu.tuple.Tuple.Tuple)2 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)2 URISyntaxException (java.net.URISyntaxException)2 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)2