final MimeTable mimeTable = MimeTable.getInstance();
final boolean isXML = mimeTable.isXMLContent(newName.toString());
final MimeType mimeType = mimeTable.getContentTypeFor(newName);
if ( mimeType != null && !mimeType.getName().equals( doc.getMetadata().getMimeType()) ) {
doc.getMetadata().setMimeType(mimeType.getName());
broker.storeXMLResource(txn, doc);
doc = source.getDocument(broker, uri.lastSegment());
}
if (isXML) {
if (doc.getResourceType() == DocumentImpl.XML_FILE) {
//XML to XML
//move to same type as it
broker.moveResource(txn, doc, destination, newName);
} else {
//convert BINARY to XML
final File file = broker.getBinaryFile((BinaryDocument) doc);
FileInputSource is = new FileInputSource(file);
final IndexInfo info = destination.validateXMLResource(txn, broker, newName, is);
info.getDocument().getMetadata().setMimeType(mimeType.getName());
is = new FileInputSource(file);
destination.store(txn, broker, info, is, false);
source.removeBinaryResource(txn, broker, doc);
}
} else {
if (doc.getResourceType() == DocumentImpl.BINARY_FILE) {
//BINARY to BINARY
//move to same type as it
broker.moveResource(txn, doc, destination, newName);
} else {
//convert XML to BINARY
// xml file
final Serializer serializer = broker.getSerializer();
serializer.setUser(broker.getSubject());
serializer.setProperties(XML_OUTPUT_PROPERTIES);
File tempFile = null;
FileInputStream is = null;
try {
tempFile = File.createTempFile("eXist-resource-", ".xml");
tempFile.deleteOnExit();
final Writer w = new OutputStreamWriter(new FileOutputStream(tempFile), "UTF-8");
serializer.serialize(doc, w);
w.flush();
w.close();
is = new FileInputStream(tempFile);
final DocumentMetadata meta = doc.getMetadata();
final Date created = new Date(meta.getCreated());
final Date lastModified = new Date(meta.getLastModified());
BinaryDocument binary = destination.validateBinaryResource(txn, broker, newName, is, mimeType.getName(), -1, created, lastModified);
binary = destination.addBinaryResource(txn, broker, binary, is, mimeType.getName(), -1, created, lastModified);
source.removeXMLResource(txn, broker, doc.getFileURI());
} finally {
if (is != null)