/** * Creates a new repository for the given file. The file must point to a directory. * * @param file the directory, which should form the root of the repository. * @param mimeRegistry the mime registry to be used. * @throws ContentIOException if an error prevents the repository creation. */ public FileRepository( final File file, final MimeRegistry mimeRegistry ) throws ContentIOException { if ( mimeRegistry == null ) { throw new NullPointerException( "MimeRegistry must be given" ); } if ( file == null ) { throw new NullPointerException( "File must be given" ); } this.mimeRegistry = mimeRegistry; this.root = new FileContentLocation( this, file ); }
/** * Lists all content entities stored in this content-location. This method filters out all files that have an invalid * name (according to the repository rules). * * @return the content entities for this location. * @throws ContentIOException if an repository error occured. */ public ContentEntity[] listContents() throws ContentIOException { final File file = getBackend(); final File[] files = file.listFiles(); final ContentEntity[] entities = new ContentEntity[ files.length ]; for ( int i = 0; i < files.length; i++ ) { final File child = files[ i ]; if ( RepositoryUtilities.isInvalidPathName( child.getName() ) ) { continue; } if ( child.isDirectory() ) { entities[ i ] = new FileContentLocation( this, child ); } else if ( child.isFile() ) { entities[ i ] = new FileContentLocation( this, child ); } } return entities; }
/** * Creates a new content location in the current location. This method must never return null. This method will fail * if an entity with the same name exists in this location. * * @param name the name of the new entity. * @return the newly created entity, never null. * @throws ContentCreationException if the item could not be created. */ public ContentLocation createLocation( final String name ) throws ContentCreationException { if ( RepositoryUtilities.isInvalidPathName( name ) ) { throw new IllegalArgumentException( "The name given is not valid." ); } final File file = getBackend(); final File child = new File( file, name ); if ( child.exists() ) { throw new ContentCreationException( "File already exists." ); } if ( child.mkdir() == false ) { throw new ContentCreationException( "Unable to create the directory" ); } try { return new FileContentLocation( this, child ); } catch ( ContentIOException e ) { throw new ContentCreationException( "Failed to create the content-location", e ); } }
return new FileContentLocation( this, child ); } else if ( child.isFile() ) { return new FileContentItem( this, child );