1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
31 import java
.io
.FileFilter
;
32 import java
.util
.ArrayList
;
35 * Helper for directory access
37 * @author lla@openoffice.org
39 public class DirectoryHelper
41 ArrayList
<String
> m_aFileList
= new ArrayList
<String
>();
42 boolean m_bRecursiveIsAllowed
= true;
44 void setRecursiveIsAllowed(boolean _bValue
)
46 m_bRecursiveIsAllowed
= _bValue
;
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.
55 * List directory /bin, filter out all files which ends with '.prn'
57 * FileFilter aFileFilter = new FileFilter()
59 * public boolean accept( File pathname )
61 * if (pathname.getName().endsWith(".prn"))
69 * Object[] aList = DirectoryHelper.traverse("/bin", aFileFilter);
70 * for (int i=0;i<aList.length;i++)
72 * String aEntry = (String)aList[i];
73 * System.out.println(aEntry);
78 * @param _bRecursiveIsAllowed
79 * @return list of directories
81 public static Object
[] traverse( String _sDirectory
, FileFilter _aFileFilter
, boolean _bRecursiveIsAllowed
)
83 DirectoryHelper a
= new DirectoryHelper();
84 a
.setRecursiveIsAllowed(_bRecursiveIsAllowed
);
85 a
.traverse_impl(_sDirectory
, _aFileFilter
);
86 return a
.m_aFileList
.toArray();
89 public static Object
[] traverse( String _sDirectory
, boolean _bRecursiveIsAllowed
)
91 DirectoryHelper a
= new DirectoryHelper();
92 a
.setRecursiveIsAllowed(_bRecursiveIsAllowed
);
93 a
.traverse_impl(_sDirectory
, null);
94 return a
.m_aFileList
.toArray();
97 void traverse_impl( String afileDirectory
, FileFilter _aFileFilter
)
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() )
103 throw new IllegalArgumentException( "not a directory: " + fileDirectory
.getName() );
106 // Getting all files and directories in the current directory
108 if (_aFileFilter
!= null)
110 aDirEntries
= fileDirectory
.listFiles(_aFileFilter
);
114 aDirEntries
= fileDirectory
.listFiles();
117 // Iterating for each file and directory
118 for ( int i
= 0; i
< aDirEntries
.length
; ++i
)
120 if ( aDirEntries
[ i
].isDirectory() )
122 if (m_bRecursiveIsAllowed
== true)
124 // Recursive call for the new directory
125 traverse_impl( aDirEntries
[ i
].getAbsolutePath(), _aFileFilter
);
130 // adding file to List
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
);
138 catch( Exception exception
)
140 exception
.printStackTrace();
148 // public static void main(String[] args)
150 // String sDirectory = "/misc/convwatch/gfxcmp/data/doc-pool/demo";
151 // Object[] aDirectoryList = DirectoryHelper.traverse( sDirectory, false );
153 // for (int i=0;i<aDirectoryList.length;i++)
155 // String sEntry = (String)aDirectoryList[i];
156 // System.out.println(sEntry);