1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 package convwatch; 25 26 import java.io.File; 27 import java.io.FileFilter; 28 import java.util.ArrayList; 29 30 /** 31 * Helper for directory access 32 * 33 * @author Lars.Langhans@sun.com 34 */ 35 public class DirectoryHelper 36 { 37 ArrayList m_aFileList = new ArrayList(); 38 boolean m_bRecursiveIsAllowed = true; 39 setRecursiveIsAllowed(boolean _bValue)40 void setRecursiveIsAllowed(boolean _bValue) 41 { 42 m_bRecursiveIsAllowed = _bValue; 43 } 44 45 /** 46 * Traverse over a given directory, and filter with a given FileFilter 47 * object and gives back the deep directory as a Object[] list, which 48 * contain a String object for every directory entry. 49 * 50 * <B>Example</B> 51 * List directory /bin, filter out all files which ends with '.prn' 52 * 53 * FileFilter aFileFilter = new FileFilter() 54 * { 55 * public boolean accept( File pathname ) 56 * { 57 * if (pathname.getName().endsWith(".prn")) 58 * { 59 * return false; 60 * } 61 * return true; 62 * } 63 * }; 64 * 65 * Object[] aList = DirectoryHelper.traverse("/bin", aFileFilter); 66 * for (int i=0;i<aList.length;i++) 67 * { 68 * String aEntry = (String)aList[i]; 69 * System.out.println(aEntry); 70 * } 71 * 72 */ traverse( String _sDirectory, FileFilter _aFileFilter, boolean _bRecursiveIsAllowed )73 public static Object[] traverse( String _sDirectory, FileFilter _aFileFilter, boolean _bRecursiveIsAllowed ) 74 { 75 DirectoryHelper a = new DirectoryHelper(); 76 a.setRecursiveIsAllowed(_bRecursiveIsAllowed); 77 a.traverse_impl(_sDirectory, _aFileFilter); 78 return a.m_aFileList.toArray(); 79 } 80 traverse( String _sDirectory, boolean _bRecursiveIsAllowed )81 public static Object[] traverse( String _sDirectory, boolean _bRecursiveIsAllowed ) 82 { 83 DirectoryHelper a = new DirectoryHelper(); 84 a.setRecursiveIsAllowed(_bRecursiveIsAllowed); 85 a.traverse_impl(_sDirectory, null); 86 return a.m_aFileList.toArray(); 87 } 88 traverse_impl( String afileDirectory, FileFilter _aFileFilter )89 void traverse_impl( String afileDirectory, FileFilter _aFileFilter ) 90 { 91 File fileDirectory = new File(afileDirectory); 92 // Testing, if the file is a directory, and if so, it throws an exception 93 if ( !fileDirectory.isDirectory() ) 94 { 95 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() ); 96 } 97 98 // Getting all files and directories in the current directory 99 File[] aDirEntries; 100 if (_aFileFilter != null) 101 { 102 aDirEntries = fileDirectory.listFiles(_aFileFilter); 103 } 104 else 105 { 106 aDirEntries = fileDirectory.listFiles(); 107 } 108 109 // Iterating for each file and directory 110 for ( int i = 0; i < aDirEntries.length; ++i ) 111 { 112 if ( aDirEntries[ i ].isDirectory() ) 113 { 114 if (m_bRecursiveIsAllowed == true) 115 { 116 // Recursive call for the new directory 117 traverse_impl( aDirEntries[ i ].getAbsolutePath(), _aFileFilter ); 118 } 119 } 120 else 121 { 122 // adding file to List 123 try 124 { 125 // Composing the URL by replacing all backslashs 126 // String stringUrl = "file:///" + aFileEntries[ i ].getAbsolutePath().replace( '\\', '/' ); 127 String aStr = aDirEntries[ i ].getAbsolutePath(); 128 m_aFileList.add(aStr); 129 } 130 catch( Exception exception ) 131 { 132 exception.printStackTrace(); 133 break; 134 } 135 } 136 } 137 } 138 139 // tests 140 // public static void main(String[] args) 141 // { 142 // String sDirectory = "/misc/convwatch/gfxcmp/data/doc-pool/demo"; 143 // Object[] aDirectoryList = DirectoryHelper.traverse( sDirectory, false ); 144 // 145 // for (int i=0;i<aDirectoryList.length;i++) 146 // { 147 // String sEntry = (String)aDirectoryList[i]; 148 // System.out.println(sEntry); 149 // } 150 // } 151 } 152 153