1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 package convwatch; 29 30 import java.io.File; 31 import java.io.FileFilter; 32 import java.util.ArrayList; 33 34 /** 35 * Helper for directory access 36 * 37 * @author Lars.Langhans@sun.com 38 */ 39 public class DirectoryHelper 40 { 41 ArrayList m_aFileList = new ArrayList(); 42 boolean m_bRecursiveIsAllowed = true; 43 44 void setRecursiveIsAllowed(boolean _bValue) 45 { 46 m_bRecursiveIsAllowed = _bValue; 47 } 48 49 /** 50 * Traverse over a given directory, and filter with a given FileFilter 51 * object and gives back the deep directory as a Object[] list, which 52 * contain a String object for every directory entry. 53 * 54 * <B>Example</B> 55 * List directory /bin, filter out all files which ends with '.prn' 56 * 57 * FileFilter aFileFilter = new FileFilter() 58 * { 59 * public boolean accept( File pathname ) 60 * { 61 * if (pathname.getName().endsWith(".prn")) 62 * { 63 * return false; 64 * } 65 * return true; 66 * } 67 * }; 68 * 69 * Object[] aList = DirectoryHelper.traverse("/bin", aFileFilter); 70 * for (int i=0;i<aList.length;i++) 71 * { 72 * String aEntry = (String)aList[i]; 73 * System.out.println(aEntry); 74 * } 75 * 76 */ 77 public static Object[] traverse( String _sDirectory, FileFilter _aFileFilter, boolean _bRecursiveIsAllowed ) 78 { 79 DirectoryHelper a = new DirectoryHelper(); 80 a.setRecursiveIsAllowed(_bRecursiveIsAllowed); 81 a.traverse_impl(_sDirectory, _aFileFilter); 82 return a.m_aFileList.toArray(); 83 } 84 85 public static Object[] traverse( String _sDirectory, boolean _bRecursiveIsAllowed ) 86 { 87 DirectoryHelper a = new DirectoryHelper(); 88 a.setRecursiveIsAllowed(_bRecursiveIsAllowed); 89 a.traverse_impl(_sDirectory, null); 90 return a.m_aFileList.toArray(); 91 } 92 93 void traverse_impl( String afileDirectory, FileFilter _aFileFilter ) 94 { 95 File fileDirectory = new File(afileDirectory); 96 // Testing, if the file is a directory, and if so, it throws an exception 97 if ( !fileDirectory.isDirectory() ) 98 { 99 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() ); 100 } 101 102 // Getting all files and directories in the current directory 103 File[] aDirEntries; 104 if (_aFileFilter != null) 105 { 106 aDirEntries = fileDirectory.listFiles(_aFileFilter); 107 } 108 else 109 { 110 aDirEntries = fileDirectory.listFiles(); 111 } 112 113 // Iterating for each file and directory 114 for ( int i = 0; i < aDirEntries.length; ++i ) 115 { 116 if ( aDirEntries[ i ].isDirectory() ) 117 { 118 if (m_bRecursiveIsAllowed == true) 119 { 120 // Recursive call for the new directory 121 traverse_impl( aDirEntries[ i ].getAbsolutePath(), _aFileFilter ); 122 } 123 } 124 else 125 { 126 // adding file to List 127 try 128 { 129 // Composing the URL by replacing all backslashs 130 // String stringUrl = "file:///" + aFileEntries[ i ].getAbsolutePath().replace( '\\', '/' ); 131 String aStr = aDirEntries[ i ].getAbsolutePath(); 132 m_aFileList.add(aStr); 133 } 134 catch( Exception exception ) 135 { 136 exception.printStackTrace(); 137 break; 138 } 139 } 140 } 141 } 142 143 // tests 144 // public static void main(String[] args) 145 // { 146 // String sDirectory = "/misc/convwatch/gfxcmp/data/doc-pool/demo"; 147 // Object[] aDirectoryList = DirectoryHelper.traverse( sDirectory, false ); 148 // 149 // for (int i=0;i<aDirectoryList.length;i++) 150 // { 151 // String sEntry = (String)aDirectoryList[i]; 152 // System.out.println(sEntry); 153 // } 154 // } 155 } 156 157