else
else {
	private DocumentImpl determineTreeStructure(DBBroker broker, String name, DocumentImpl document, DocumentImpl oldDoc, XMLReader reader, InputSource source) throws LockException, EXistException, SAXException, PermissionDeniedException, TriggerException {		try {			checkPermissions(broker, name, oldDoc);						manageDocumentInformation(broker, name, oldDoc, document );						Trigger trigger = setupTriggers(broker, name, oldDoc);			Indexer indexer = new Indexer(broker);			indexer.setDocument(document);			addObserversToIndexer(broker, indexer);			prepareSAXParser(broker, name, oldDoc, trigger, indexer, reader );			// first pass: parse the document to determine tree structure			LOG.debug("validating document " + name);			try {				reader.parse(source);			} catch (IOException e) {				throw new EXistException(e);			}			document.setMaxDepth(document.getMaxDepth() + 1);			document.calculateTreeLevelStartPoints();			// new document is valid: remove old document						if (oldDoc != null) {				LOG.debug("removing old document " + oldDoc.getFileName());				if (oldDoc.getResourceType() == DocumentImpl.BINARY_FILE)					broker.removeBinaryResource((BinaryDocument) oldDoc);				else					broker.removeDocument(getName() + '/' + oldDoc.getFileName(), false);				// we continue to use the old document object and just replace its contents				oldDoc.copyOf(document);				indexer.setDocumentObject(oldDoc);				document = oldDoc;			} else {			    document.getUpdateLock().acquire(Lock.WRITE_LOCK);			    document.setDocId(broker.getNextDocId(this));			}			indexer.setValidating(false);			if (trigger != null)				trigger.setValidating(false);		} catch(EXistException e) {		    if(oldDoc != null) oldDoc.getUpdateLock().release(Lock.WRITE_LOCK);		    throw e;		} catch(SAXException e) {		    if(oldDoc != null) oldDoc.getUpdateLock().release(Lock.WRITE_LOCK);		    throw e;		} catch(PermissionDeniedException e) {		    if(oldDoc != null) oldDoc.getUpdateLock().release(Lock.WRITE_LOCK);		    throw e;		} catch(TriggerException e) {		    if(oldDoc != null) oldDoc.getUpdateLock().release(Lock.WRITE_LOCK);		    throw e;		} finally {			lock.release();		}		return document;	}