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 graphical; 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 lla@openoffice.org 38 */ 39 public class DirectoryHelper 40 { 41 ArrayList<String> m_aFileList = new ArrayList<String>(); 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 * @param _sDirectory 77 * @param _aFileFilter 78 * @param _bRecursiveIsAllowed 79 * @return list of directories 80 */ 81 public static Object[] traverse( String _sDirectory, FileFilter _aFileFilter, boolean _bRecursiveIsAllowed ) 82 { 83 DirectoryHelper a = new DirectoryHelper(); 84 a.setRecursiveIsAllowed(_bRecursiveIsAllowed); 85 a.traverse_impl(_sDirectory, _aFileFilter); 86 return a.m_aFileList.toArray(); 87 } 88 89 public static Object[] traverse( String _sDirectory, boolean _bRecursiveIsAllowed ) 90 { 91 DirectoryHelper a = new DirectoryHelper(); 92 a.setRecursiveIsAllowed(_bRecursiveIsAllowed); 93 a.traverse_impl(_sDirectory, null); 94 return a.m_aFileList.toArray(); 95 } 96 97 void traverse_impl( String afileDirectory, FileFilter _aFileFilter ) 98 { 99 File fileDirectory = new File(afileDirectory); 100 // Testing, if the file is a directory, and if so, it throws an exception 101 if ( !fileDirectory.isDirectory() ) 102 { 103 throw new IllegalArgumentException( "not a directory: " + fileDirectory.getName() ); 104 } 105 106 // Getting all files and directories in the current directory 107 File[] aDirEntries; 108 if (_aFileFilter != null) 109 { 110 aDirEntries = fileDirectory.listFiles(_aFileFilter); 111 } 112 else 113 { 114 aDirEntries = fileDirectory.listFiles(); 115 } 116 117 // Iterating for each file and directory 118 for ( int i = 0; i < aDirEntries.length; ++i ) 119 { 120 if ( aDirEntries[ i ].isDirectory() ) 121 { 122 if (m_bRecursiveIsAllowed == true) 123 { 124 // Recursive call for the new directory 125 traverse_impl( aDirEntries[ i ].getAbsolutePath(), _aFileFilter ); 126 } 127 } 128 else 129 { 130 // adding file to List 131 try 132 { 133 // Composing the URL by replacing all backslashs 134 // String stringUrl = "file:///" + aFileEntries[ i ].getAbsolutePath().replace( '\\', '/' ); 135 String aStr = aDirEntries[ i ].getAbsolutePath(); 136 m_aFileList.add(aStr); 137 } 138 catch( Exception exception ) 139 { 140 exception.printStackTrace(); 141 break; 142 } 143 } 144 } 145 } 146 147 // tests 148 // public static void main(String[] args) 149 // { 150 // String sDirectory = "/misc/convwatch/gfxcmp/data/doc-pool/demo"; 151 // Object[] aDirectoryList = DirectoryHelper.traverse( sDirectory, false ); 152 // 153 // for (int i=0;i<aDirectoryList.length;i++) 154 // { 155 // String sEntry = (String)aDirectoryList[i]; 156 // System.out.println(sEntry); 157 // } 158 // } 159 } 160 161