1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DirectoryHelper.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
34 import java
.io
.FileFilter
;
35 import java
.util
.ArrayList
;
38 * Helper for directory access
40 * @author Lars.Langhans@sun.com
42 public class DirectoryHelper
44 ArrayList m_aFileList
= new ArrayList();
45 boolean m_bRecursiveIsAllowed
= true;
47 void setRecursiveIsAllowed(boolean _bValue
)
49 m_bRecursiveIsAllowed
= _bValue
;
53 * Traverse over a given directory, and filter with a given FileFilter
54 * object and gives back the deep directory as a Object[] list, which
55 * contain a String object for every directory entry.
58 * List directory /bin, filter out all files which ends with '.prn'
60 * FileFilter aFileFilter = new FileFilter()
62 * public boolean accept( File pathname )
64 * if (pathname.getName().endsWith(".prn"))
72 * Object[] aList = DirectoryHelper.traverse("/bin", aFileFilter);
73 * for (int i=0;i<aList.length;i++)
75 * String aEntry = (String)aList[i];
76 * System.out.println(aEntry);
80 public static Object
[] traverse( String _sDirectory
, FileFilter _aFileFilter
, boolean _bRecursiveIsAllowed
)
82 DirectoryHelper a
= new DirectoryHelper();
83 a
.setRecursiveIsAllowed(_bRecursiveIsAllowed
);
84 a
.traverse_impl(_sDirectory
, _aFileFilter
);
85 return a
.m_aFileList
.toArray();
88 public static Object
[] traverse( String _sDirectory
, boolean _bRecursiveIsAllowed
)
90 DirectoryHelper a
= new DirectoryHelper();
91 a
.setRecursiveIsAllowed(_bRecursiveIsAllowed
);
92 a
.traverse_impl(_sDirectory
, null);
93 return a
.m_aFileList
.toArray();
96 void traverse_impl( String afileDirectory
, FileFilter _aFileFilter
)
98 File fileDirectory
= new File(afileDirectory
);
99 // Testing, if the file is a directory, and if so, it throws an exception
100 if ( !fileDirectory
.isDirectory() )
102 throw new IllegalArgumentException( "not a directory: " + fileDirectory
.getName() );
105 // Getting all files and directories in the current directory
107 if (_aFileFilter
!= null)
109 aDirEntries
= fileDirectory
.listFiles(_aFileFilter
);
113 aDirEntries
= fileDirectory
.listFiles();
116 // Iterating for each file and directory
117 for ( int i
= 0; i
< aDirEntries
.length
; ++i
)
119 if ( aDirEntries
[ i
].isDirectory() )
121 if (m_bRecursiveIsAllowed
== true)
123 // Recursive call for the new directory
124 traverse_impl( aDirEntries
[ i
].getAbsolutePath(), _aFileFilter
);
129 // adding file to List
132 // Composing the URL by replacing all backslashs
133 // String stringUrl = "file:///" + aFileEntries[ i ].getAbsolutePath().replace( '\\', '/' );
134 String aStr
= aDirEntries
[ i
].getAbsolutePath();
135 m_aFileList
.add(aStr
);
137 catch( Exception exception
)
139 exception
.printStackTrace();
147 // public static void main(String[] args)
149 // String sDirectory = "/misc/convwatch/gfxcmp/data/doc-pool/demo";
150 // Object[] aDirectoryList = DirectoryHelper.traverse( sDirectory, false );
152 // for (int i=0;i<aDirectoryList.length;i++)
154 // String sEntry = (String)aDirectoryList[i];
155 // System.out.println(sEntry);