name = file.getName();
path = file.getParent();
Collection target = (path==null) ? root : XMLDBAbstractCollectionManipulator.createCollection(root, path);
MimeType mime = MimeTable.getInstance().getContentTypeFor(name);
try{
NodeValue content = ModuleUtils.streamToXML(context, new ByteArrayInputStream(baos.toByteArray()));
resource = target.createResource(name, "XMLResource");
ContentHandler handler = ((XMLResource)resource).setContentAsSAX();
handler.startDocument();
content.toSAX(context.getBroker(), handler, null);
handler.endDocument();
} catch(SAXException e){
resource = target.createResource(name, "BinaryResource");
resource.setContent(baos.toByteArray());
}
if (resource != null){
if (mime != null){
((EXistResource)resource).setMimeType(mime.getName());
}
target.storeResource(resource);
}
}