2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 import java
.io
.FileFilter
;
23 import java
.util
.ArrayList
;
26 * Helper for directory access
28 public class DirectoryHelper
30 ArrayList
<String
> m_aFileList
= new ArrayList
<String
>();
31 boolean m_bRecursiveIsAllowed
= true;
33 void setRecursiveIsAllowed(boolean _bValue
)
35 m_bRecursiveIsAllowed
= _bValue
;
39 * Traverse over a given directory, and filter with a given FileFilter
40 * object and gives back the deep directory as a Object[] list, which
41 * contain a String object for every directory entry.
44 * List directory /bin, filter out all files which ends with '.prn'
46 * FileFilter aFileFilter = new FileFilter()
48 * public boolean accept( File pathname )
50 * if (pathname.getName().endsWith(".prn"))
58 * Object[] aList = DirectoryHelper.traverse("/bin", aFileFilter);
59 * for (int i=0;i<aList.length;i++)
61 * String aEntry = (String)aList[i];
62 * System.out.println(aEntry);
67 * @param _bRecursiveIsAllowed
68 * @return list of directories
70 public static Object
[] traverse( String _sDirectory
, FileFilter _aFileFilter
, boolean _bRecursiveIsAllowed
)
72 DirectoryHelper a
= new DirectoryHelper();
73 a
.setRecursiveIsAllowed(_bRecursiveIsAllowed
);
74 a
.traverse_impl(_sDirectory
, _aFileFilter
);
75 return a
.m_aFileList
.toArray();
78 public static Object
[] traverse( String _sDirectory
, boolean _bRecursiveIsAllowed
)
80 DirectoryHelper a
= new DirectoryHelper();
81 a
.setRecursiveIsAllowed(_bRecursiveIsAllowed
);
82 a
.traverse_impl(_sDirectory
, null);
83 return a
.m_aFileList
.toArray();
86 void traverse_impl( String afileDirectory
, FileFilter _aFileFilter
)
88 File fileDirectory
= new File(afileDirectory
);
89 // Testing, if the file is a directory, and if so, it throws an exception
90 if ( !fileDirectory
.isDirectory() )
92 throw new IllegalArgumentException( "not a directory: " + fileDirectory
.getName() );
95 // Getting all files and directories in the current directory
97 if (_aFileFilter
!= null)
99 aDirEntries
= fileDirectory
.listFiles(_aFileFilter
);
103 aDirEntries
= fileDirectory
.listFiles();
106 // Iterating for each file and directory
107 for ( int i
= 0; i
< aDirEntries
.length
; ++i
)
109 if ( aDirEntries
[ i
].isDirectory() )
111 if (m_bRecursiveIsAllowed
== true)
113 // Recursive call for the new directory
114 traverse_impl( aDirEntries
[ i
].getAbsolutePath(), _aFileFilter
);
119 // adding file to List
122 // Composing the URL by replacing all backslashs
123 String aStr
= aDirEntries
[ i
].getAbsolutePath();
124 m_aFileList
.add(aStr
);
126 catch( Exception exception
)
128 exception
.printStackTrace();