LOG.debug("Scanning document " + name);
LOG.debug("Scanning document " + getName() + '/' + docName);
	private IndexInfo validateInternal(Txn transaction, DBBroker broker, String docName, ValidateBlock doValidate) 			throws EXistException, PermissionDeniedException, TriggerException, SAXException, LockException {		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(docName);			document = new DocumentImpl(broker, docName,	this);					checkPermissions(transaction, broker, docName, oldDoc);			manageDocumentInformation(broker, docName, 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, docName, oldDoc != null),					oldDoc == null ? Trigger.STORE_DOCUMENT_EVENT : Trigger.UPDATE_DOCUMENT_EVENT);			info.prepareTrigger(broker, transaction, getName() + "/" + docName, oldDoc);			LOG.debug("Scanning document " + name);			doValidate.run(info);						document.setMaxDepth(document.getMaxDepth() + 1);			document.calculateTreeLevelStartPoints();			// new document is valid: remove old document 			if (oldDoc != null) {				LOG.debug("removing old document " + oldDoc.getFileName());				oldDoc.getUpdateLock().acquire(Lock.WRITE_LOCK);				oldDocLocked = true;				if (oldDoc.getResourceType() == DocumentImpl.BINARY_FILE)					broker.removeBinaryResource(transaction, (BinaryDocument) oldDoc);				else					broker.removeDocument(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.getNextDocId(transaction, this));			    addDocument(transaction, broker, document);			}			indexer.setValidating(false);			info.postValidateTrigger();			return info;		} finally {		   if (oldDocLocked) oldDoc.getUpdateLock().release(Lock.WRITE_LOCK);			getLock().release();		}	}