oldDoc.copyOf(document); indexer.setDocumentObject(oldDoc); oldDocLocked = false; document = oldDoc;
oldDoc.copyOf(document); indexer.setDocumentObject(oldDoc); oldDocLocked = false; document = oldDoc; }
    private IndexInfo validateXMLResourceInternal(Txn transaction, DBBroker broker, XmldbURI docUri, ValidateBlock doValidate)    throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {                checkConfiguration(transaction, broker, docUri);        if (broker.isReadOnly()) throw new PermissionDeniedException("Database is read-only");        DocumentImpl document, oldDoc = null;        boolean oldDocLocked = false;        try {            getLock().acquire(Lock.WRITE_LOCK);            oldDoc = (DocumentImpl) documents.get(docUri.getRawCollectionPath());            document = new DocumentImpl(broker, this, docUri);                        if (oldDoc == null) {                CollectionConfiguration config = getConfiguration(broker);                if (config != null) {                    document.setPermissions(config.getDefResPermissions());                }            } else                document.setPermissions(oldDoc.getPermissions().getPermissions());                        checkPermissions(transaction, broker, oldDoc);            manageDocumentInformation(broker, oldDoc, document );                        Indexer indexer = new Indexer(broker, transaction);            IndexInfo info = new IndexInfo(indexer);            indexer.setDocument(document);            addObserversToIndexer(broker, indexer);            indexer.setValidating(true);                        // if !triggersEnabled, setupTriggers will return null anyway, so no need to check            info.setTrigger(                    setupTriggers(broker, docUri, oldDoc != null),                    oldDoc == null ? Trigger.STORE_DOCUMENT_EVENT : Trigger.UPDATE_DOCUMENT_EVENT);                         info.prepareTrigger(broker, transaction, getURI().append(docUri), oldDoc);                        LOG.debug("Scanning document " + getURI().append(docUri));            doValidate.run(info);                        // new document is valid: remove old document            if (oldDoc != null) {                LOG.debug("removing old document " + oldDoc.getFileURI());                oldDoc.getUpdateLock().acquire(Lock.WRITE_LOCK);                oldDocLocked = true;                if (oldDoc.getResourceType() == DocumentImpl.BINARY_FILE)                    broker.removeBinaryResource(transaction, (BinaryDocument) oldDoc);                else                    broker.removeXMLResource(transaction, oldDoc, false);                oldDoc.copyOf(document);                indexer.setDocumentObject(oldDoc);                oldDocLocked = false;		// old has become new at this point                document = oldDoc;            } else {                document.getUpdateLock().acquire(Lock.WRITE_LOCK);                document.setDocId(broker.getNextResourceId(transaction, this));                addDocument(transaction, broker, document);            }                        indexer.setValidating(false);            info.postValidateTrigger();            return info;        } finally {            if (oldDocLocked) oldDoc.getUpdateLock().release(Lock.WRITE_LOCK);            getLock().release();        }    }