protected <V extends AbstractBasicFileAttributeView> V newView( final AttrHolder holder, final Class<V> type ) { if ( NeedsPreloadedAttrs.class.isAssignableFrom( type ) && holder.getAttrStorage().getContent().size() == 0 ) { readAttributes( (Path) holder ); } try { final Constructor<V> constructor = (Constructor<V>) type.getConstructors()[ 0 ]; final V view = constructor.newInstance( holder ); holder.addAttrView( view ); return view; } catch ( final Exception e ) { } return null; }
@Override public Object getAttribute( final Path path, final String attribute ) throws UnsupportedOperationException, IllegalArgumentException, IOException, SecurityException { checkNotNull( "path", path ); Object value; try { value = Files.getAttribute( path, attribute ); } catch ( UnsupportedOperationException ex ) { value = null; } if ( value == null && path instanceof AttrHolder ) { final AttrHolder holder = ( (AttrHolder) path ); final String[] attr = split( attribute ); if ( holder.getAttrStorage().getContent().isEmpty() ) { loadDotFile( path ); } return holder.getAttrStorage().getAllContent().get( attr[ 1 ] ); } return value; }
@Override public synchronized boolean deleteIfExists( final Path path, final DeleteOption... options ) throws IllegalArgumentException, DirectoryNotEmptyException, IOException, SecurityException { final boolean result = Files.deleteIfExists( path, options ); try { Files.deleteIfExists( dot( path ), options ); } catch ( Exception ex ) { } if ( path instanceof AttrHolder ) { ( (AttrHolder) path ).getAttrStorage().clear(); } return result; }
@Override public synchronized void delete( final Path path, final DeleteOption... options ) throws IllegalArgumentException, NoSuchFileException, DirectoryNotEmptyException, IOException, SecurityException { Files.delete( path, options ); try { Files.deleteIfExists( dot( path ), options ); } catch ( Exception ex ) { } if ( path instanceof AttrHolder ) { ( (AttrHolder) path ).getAttrStorage().clear(); } }
protected void loadDotFile( final Path path ) { final Properties content = new Properties(); content.load( newInputStream( dot( path ) ) ); if ( path instanceof AttrHolder ) { ( (AttrHolder) path ).getAttrStorage().loadContent( content ); } }
@Override public Map<String, Object> readAttributes( final Path path, final String attributes ) throws UnsupportedOperationException, NoSuchFileException, IllegalArgumentException, IOException, SecurityException { checkNotNull( "path", path ); checkNotEmpty( "attributes", attributes ); final Properties original = new Properties( Files.readAttributes( path, attributes ) ); if ( attributes.equals( "*" ) && exists( dot( path ) ) ) { boolean isAttrHolder = path instanceof AttrHolder; if ( isAttrHolder && ( (AttrHolder) path ).getAttrStorage().getContent().size() > 0 ) { return ( (AttrHolder) path ).getAttrStorage().getAllContent(); } final Properties content = new Properties(); content.load( newInputStream( dot( path ) ) ); content.putAll( original ); if ( isAttrHolder ) { ( (AttrHolder) path ).getAttrStorage().loadContent( content ); } return content; } return original; }
public static void buildDotFile( final Path path, final OutputStream out, final FileAttribute<?>... attrs ) { if ( attrs != null && attrs.length > 0 ) { final Properties properties = new Properties(); for ( final FileAttribute<?> attr : attrs ) { if ( attr.value() instanceof Serializable ) { properties.put( attr.name(), attr.value() ); } } try { properties.store( out ); } catch ( final Exception e ) { throw new IOException( e ); } if ( path instanceof AttrHolder ) { ( (AttrHolder) path ).getAttrStorage().loadContent( properties ); } } else { path.getFileSystem().provider().deleteIfExists( dot( path ) ); } }
( (AttrHolder) path ).getAttrStorage().clear();
( (AttrHolder) path ).getAttrStorage().clear();
assertFalse( ( (AttrHolder) file ).getAttrStorage().getContent().isEmpty() ); assertTrue( ( (AttrHolder) file ).getAttrStorage().getContent().isEmpty() );
assertTrue( ioService().exists( dot( file ) ) ); assertFalse( ( (AttrHolder) file ).getAttrStorage().getContent().isEmpty() ); assertTrue( ( (AttrHolder) file ).getAttrStorage().getContent().isEmpty() );
@Test public void testGetAttribute() { final Path file = getFilePath(); ioService().deleteIfExists( file ); ioService().write( file, "ooooo!", Collections.<OpenOption>emptySet(), new FileAttribute<Object>() { @Override public String name() { return "dcore.author"; } @Override public Object value() { return "AuthorName"; } } ); assertNotNull( ioService().getAttribute( file, "dcore:dcore.author" ) ); assertNull( ioService().getAttribute( file, "dcore:dcore.not_here" ) ); assertNotNull( ioService().getAttribute( file, "dcore.author" ) ); assertNull( ioService().getAttribute( file, "something" ) ); ( (AttrHolder) file ).getAttrStorage().clear(); assertNotNull( ioService().getAttribute( file, "dcore:dcore.author" ) ); assertNull( ioService().getAttribute( file, "dcore:dcore.not_here" ) ); assertNotNull( ioService().getAttribute( file, "dcore.author" ) ); assertNull( ioService().getAttribute( file, "something" ) ); }
@Test public void testGetAttributeView() { final Path file = getFilePath(); ioService().deleteIfExists( file ); ioService().write( file, "ooooo!", Collections.<OpenOption>emptySet(), new FileAttribute<Object>() { @Override public String name() { return "dcore.author"; } @Override public Object value() { return "AuthorName"; } } ); assertNotNull( ioService().getFileAttributeView( file, BasicFileAttributeView.class ) ); assertNull( ioService().getFileAttributeView( file, MyAttrsView.class ) ); assertNotNull( ioService().getFileAttributeView( file, XDublinCoreView.class ) ); final DublinCoreAttributes attr = ioService().getFileAttributeView( file, XDublinCoreView.class ).readAttributes(); assertEquals( "AuthorName", attr.getAuthor() ); ( (AttrHolder) file ).getAttrStorage().clear(); assertNotNull( ioService().getFileAttributeView( file, BasicFileAttributeView.class ) ); assertNull( ioService().getFileAttributeView( file, MyAttrsView.class ) ); assertNotNull( ioService().getFileAttributeView( file, XDublinCoreView.class ) ); }