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);
66 public static Object
[] traverse( String _sDirectory
, FileFilter _aFileFilter
, boolean _bRecursiveIsAllowed
)
68 DirectoryHelper a
= new DirectoryHelper();
69 a
.setRecursiveIsAllowed(_bRecursiveIsAllowed
);
70 a
.traverse_impl(_sDirectory
, _aFileFilter
);
71 return a
.m_aFileList
.toArray();
74 public static Object
[] traverse( String _sDirectory
, boolean _bRecursiveIsAllowed
)
76 DirectoryHelper a
= new DirectoryHelper();
77 a
.setRecursiveIsAllowed(_bRecursiveIsAllowed
);
78 a
.traverse_impl(_sDirectory
, null);
79 return a
.m_aFileList
.toArray();
82 void traverse_impl( String afileDirectory
, FileFilter _aFileFilter
)
84 File fileDirectory
= new File(afileDirectory
);
85 // Testing, if the file is a directory, and if so, it throws an exception
86 if ( !fileDirectory
.isDirectory() )
88 throw new IllegalArgumentException( "not a directory: " + fileDirectory
.getName() );
91 // Getting all files and directories in the current directory
93 if (_aFileFilter
!= null)
95 aDirEntries
= fileDirectory
.listFiles(_aFileFilter
);
99 aDirEntries
= fileDirectory
.listFiles();
102 // Iterating for each file and directory
103 for ( int i
= 0; i
< aDirEntries
.length
; ++i
)
105 if ( aDirEntries
[ i
].isDirectory() )
107 if (m_bRecursiveIsAllowed
== true)
109 // Recursive call for the new directory
110 traverse_impl( aDirEntries
[ i
].getAbsolutePath(), _aFileFilter
);
115 // adding file to List
118 // Composing the URL by replacing all backslashs
119 String aStr
= aDirEntries
[ i
].getAbsolutePath();
120 m_aFileList
.add(aStr
);
122 catch( Exception exception
)
124 exception
.printStackTrace();