String resourceType = "XMLResource";
if( srcFile != null ) {
log( "Storing " + srcFile.getName() );
MimeType mime = getMimeTable().getContentTypeFor( srcFile.getName() );
String baseMimeType;
if( forceMimeType != null ) {
baseMimeType = forceMimeType;
} else if( mime != null ) {
baseMimeType = mime.getName();
} else {
baseMimeType = defaultMimeType;
}
if( type != null ) {
if( "xml".equals(type) ) {
mime = ( baseMimeType != null ) ? ( new MimeType( baseMimeType, MimeType.XML ) ) : MimeType.XML_TYPE;
} else if( "binary".equals(type) ) {
mime = ( baseMimeType != null ) ? ( new MimeType( baseMimeType, MimeType.BINARY ) ) : MimeType.BINARY_TYPE;
}
}
// single file
if( mime == null ) {
final String msg = "Cannot guess mime-type kind for " + srcFile.getName() + ". Treating it as a binary.";
log( msg, Project.MSG_ERR );
mime = ( baseMimeType != null ) ? ( new MimeType( baseMimeType, MimeType.BINARY ) ) : MimeType.BINARY_TYPE;
}
resourceType = mime.isXMLType() ? "XMLResource" : "BinaryResource";
if( targetFile == null ) {
targetFile = srcFile.getName();
}
try {
log( "Creating resource " + targetFile + " in collection " + col.getName() + " of type " + resourceType + " with mime-type: " + mime.getName(), Project.MSG_DEBUG );
res = col.createResource( targetFile, resourceType );
if( srcFile.length() == 0 ) {
// note: solves bug id 2429889 when this task hits empty files
} else {
res.setContent( srcFile );
( (EXistResource)res ).setMimeType( mime.getName() );
col.storeResource( res );
}
if( permissions != null ) {
setPermissions( res );
}
}
catch( final XMLDBException e ) {
final String msg = "XMLDB exception caught: " + e.getMessage();
if( failonerror ) {
throw( new BuildException( msg, e ) );
} else {
log( msg, e, Project.MSG_ERR );
}
}
} else {
for( final FileSet fileSet : fileSetList ) {
log( "Storing fileset", Project.MSG_DEBUG );
// using fileset
final DirectoryScanner scanner = fileSet.getDirectoryScanner( getProject() );
scanner.scan();
final String[] includedFiles = scanner.getIncludedFiles();
final String[] includedDirs = scanner.getIncludedDirectories();
log( "Found " + includedDirs.length + " directories and " + includedFiles.length + " files.\n" );
final File baseDir = scanner.getBasedir();
if( includeEmptyDirs && createSubcollections ) {
for( final String included : includedDirs ) {
try {
file = new File( baseDir, included );
log( "Creating " + included + " ...\n" );
//TODO : use dedicated function in XmldbURI
// check whether the relative file path contains file seps
p = included.lastIndexOf( File.separatorChar );
if( p != Constants.STRING_NOT_FOUND ) {
relDir = included.substring( 0, p );
// It's necessary to do this translation on Windows, and possibly MacOS:
relDir = relDir.replace( File.separatorChar, '/' );
if( createSubcollections && ( ( prevDir == null ) || ( !relDir.equals( prevDir ) ) ) ) {
//TODO : use dedicated function in XmldbURI
col = mkcol( root, baseURI, XmldbURI.ROOT_COLLECTION + path, relDir );
prevDir = relDir;
}
} else {
col = mkcol( root, baseURI, XmldbURI.ROOT_COLLECTION + path, included );
}
}
catch( final XMLDBException e ) {
final String msg = "XMLDB exception caught: " + e.getMessage();
if( failonerror ) {
throw( new BuildException( msg, e ) );
} else {
log( msg, e, Project.MSG_ERR );
}
}
}
}
for( final String included : includedFiles ) {
try {
file = new File( baseDir, included );
log( "Storing " + included + " ...\n" );
//TODO : use dedicated function in XmldbURI
// check whether the relative file path contains file seps
p = included.lastIndexOf( File.separatorChar );
if( p != Constants.STRING_NOT_FOUND ) {
relDir = included.substring( 0, p );
// It's necessary to do this translation on Windows, and possibly MacOS:
relDir = relDir.replace( File.separatorChar, '/' );
if( createSubcollections && ( ( prevDir == null ) || ( !relDir.equals( prevDir ) ) ) ) {
//TODO : use dedicated function in XmldbURI
col = mkcol( root, baseURI, XmldbURI.ROOT_COLLECTION + path, relDir );
prevDir = relDir;
}
} else {
// No file separator found in resource name, reset col to the root collection
col = root;
}
MimeType currentMime = getMimeTable().getContentTypeFor( file.getName() );
String currentBaseMimeType;
if( forceMimeType != null ) {
currentBaseMimeType = forceMimeType;
} else if( currentMime != null ) {
currentBaseMimeType = currentMime.getName();
} else {
currentBaseMimeType = defaultMimeType;
}
if( type != null ) {
if( "xml".equals(type) ) {
currentMime = ( currentBaseMimeType != null ) ? ( new MimeType( currentBaseMimeType, MimeType.XML ) ) : MimeType.XML_TYPE;
} else if( "binary".equals(type) ) {
currentMime = ( currentBaseMimeType != null ) ? ( new MimeType( currentBaseMimeType, MimeType.BINARY ) ) : MimeType.BINARY_TYPE;
}
}
if( currentMime == null ) {
final String msg = "Cannot find mime-type kind for " + file.getName() + ". Treating it as a binary.";
log( msg, Project.MSG_ERR );
currentMime = ( currentBaseMimeType != null ) ? ( new MimeType( currentBaseMimeType, MimeType.BINARY ) ) : MimeType.BINARY_TYPE;
}
resourceType = currentMime.isXMLType() ? "XMLResource" : "BinaryResource";
log( "Creating resource " + file.getName() + " in collection " + col.getName() + " of type " + resourceType + " with mime-type: " + currentMime.getName(), Project.MSG_DEBUG );
res = col.createResource( file.getName(), resourceType );
res.setContent( file );
( (EXistResource)res ).setMimeType( currentMime.getName() );
col.storeResource( res );
if( permissions != null ) {
setPermissions( res );
}