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