1 /*** 2 * Redistribution and use in source and binary forms, with or without 3 * modification, are permitted provided that the following conditions are 4 * met : 5 * 6 * . Redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer. 8 * 9 * . Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * . The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 * 28 * $Id: Directory.java 420 2006-10-30 09:31:59Z mat007 $ 29 */ 30 31 package palmed.file; 32 33 import java.io.IOException; 34 import java.util.Enumeration; 35 import javax.microedition.io.Connector; 36 import javax.microedition.io.file.FileConnection; 37 38 /*** 39 * This class implements a directory descriptor. 40 * 41 * @author Mathieu Champlon 42 * @version $Revision: 420 $ $Date: 2006-10-30 18:31:59 +0900 (lun., 30 oct. 2006) $ 43 */ 44 public final class Directory implements IDirectory 45 { 46 /*** 47 * The prefix for file connection descriptors. 48 */ 49 private static final String PREFIX = "file:///"; 50 /*** 51 * The upper directory filename. 52 */ 53 private static final String UP_DIRECTORY = ".."; 54 /*** 55 * The file names directory separator string. 56 */ 57 private static final String SEPARATOR = System.getProperty( "file.separator" ); 58 /*** 59 * The descriptor path. 60 */ 61 private final String path_; 62 /*** 63 * The parent directory. 64 */ 65 private final IDirectory parent_; 66 67 /*** 68 * Create a descriptor. 69 * 70 * @param parent the parent descriptor 71 * @param path the descriptor path 72 */ 73 public Directory( final IDirectory parent, final String path ) 74 { 75 if( parent == null ) 76 throw new IllegalArgumentException( "parameter 'parent' is null" ); 77 if( path == null ) 78 throw new IllegalArgumentException( "parameter 'path' is null" ); 79 parent_ = parent; 80 path_ = path; 81 } 82 83 /*** 84 * {@inheritDoc} 85 */ 86 public IDirectory select( final String filename, final IFileBrowserListener listener ) 87 { 88 if( filename.equals( UP_DIRECTORY ) ) 89 return parent_; 90 if( isDirectory( path_ + filename ) ) 91 return new Directory( this, path_ + filename ); 92 if( listener != null ) 93 listener.select( new File( path_ + filename ) ); 94 return this; 95 } 96 97 /*** 98 * {@inheritDoc} 99 */ 100 public void accept( final IDirectoryVisitor visitor ) throws IOException 101 { 102 visitor.visitDirectory( UP_DIRECTORY ); 103 final Enumeration enumeration = list(); 104 while( enumeration.hasMoreElements() ) 105 { 106 final String filename = (String)enumeration.nextElement(); 107 if( isDirectory( filename ) ) 108 visitor.visitDirectory( filename ); 109 else 110 visitor.visitFile( filename ); 111 } 112 } 113 114 private Enumeration list() throws IOException 115 { 116 final FileConnection connection = (FileConnection)Connector.open( PREFIX + path_, Connector.READ ); 117 final Enumeration enumeration = connection.list(); 118 connection.close(); 119 return enumeration; 120 } 121 122 private boolean isDirectory( final String filename ) 123 { 124 return filename.endsWith( SEPARATOR ); 125 } 126 127 /*** 128 * {@inheritDoc} 129 */ 130 public String toString() 131 { 132 return path_; 133 } 134 135 /*** 136 * {@inheritDoc} 137 */ 138 public boolean isWritable() 139 { 140 return true; 141 } 142 143 /*** 144 * {@inheritDoc} 145 */ 146 public void create( final String name ) throws IOException 147 { 148 final FileConnection connection = (FileConnection)Connector.open( PREFIX + path_ + name, Connector.WRITE ); 149 connection.mkdir(); 150 connection.close(); 151 } 152 153 /*** 154 * {@inheritDoc} 155 */ 156 public boolean isInPath( final String name ) 157 { 158 return path_.equals( name ) || parent_.isInPath( name ); 159 } 160 }