1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* Copyright (C) 2001-2013 Michael Fuchs
*
* This file is part of herold.
*
* herold is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* herold is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with herold. If not, see <http://www.gnu.org/licenses/>.
*/
package org.dbdoclet.service;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.dbdoclet.comparator.PathTokenCountComparator;
import org.dbdoclet.progress.ProgressEvent;
import org.dbdoclet.progress.ProgressListener;
public class FindServices {
/**
* Die Methode <code>findFileInDirectory</code> durchsucht ein Verzeichnis rekursiv nach
* Unterverzeichnissen und Dateien.
*
* Die gefundenen Dateien werden als <code>java.io.File</code>-Objekte in
* der übergebenen Liste abgelegt.
*
* @param dir Das Verzeichnis
* @param dpattern Der reguläre Ausdruck für die Auswahl der Verzeichnisse.
* @param fpattern Der reguläre Ausdruck für die Auswahl der Dateien.
* @param list Die Liste der gefundenen Dateien als <code>File</code>-Objekte.
*
* @see java.io.File
*/
public static void findFileInDirectory(File dir,
String dpattern,
String fpattern,
List<File> list) {
findFileInDirectory(dir, dpattern, fpattern, null, list);
}
public static void findFileInDirectory(File dir,
String dpattern,
String fpattern,
ProgressListener listener,
List<File> list) {
if (dir == null) {
throw new IllegalArgumentException("The argument dir must not be null!");
}
if (dpattern == null) {
throw new IllegalArgumentException("The argument dpattern must not be null!");
}
if (fpattern == null) {
throw new IllegalArgumentException("The argument fpattern must not be null!");
}
if (list == null) {
throw new IllegalArgumentException("The argument list must not be null!");
}
File[] files1 = dir.listFiles();
File[] files2;
File file1;
File file2;
Pattern red = Pattern.compile(dpattern, Pattern.CASE_INSENSITIVE);
Pattern ref = Pattern.compile(fpattern, Pattern.CASE_INSENSITIVE);
Matcher dirMatcher;
Matcher fileMatcher;
if (files1 == null) {
return;
}
for (int i = 0; i < files1.length; i++) {
file1 = files1[i];
dirMatcher = red.matcher(file1.getName());
if (file1.isDirectory() && dirMatcher.matches()) {
if (listener != null) {
listener.progress(new ProgressEvent().setStage(ProgressEvent.STAGE_PREPARE).setUserObject(file1));
}
files2 = file1.listFiles();
if (files2 != null && files2.length > 0) {
for (int j = 0; j < files2.length; j++) {
file2 = files2[j];
fileMatcher = ref.matcher(file2.getName());
if (file2.isFile() && fileMatcher.matches()) {
list.add(file2);
}
}
}
findFileInDirectory(file1, dpattern, fpattern, listener, list);
continue;
}
if (file1.isDirectory()) {
if (listener != null) {
listener.progress(new ProgressEvent().setStage(ProgressEvent.STAGE_PREPARE).setUserObject(file1));
}
findFileInDirectory(file1, dpattern, fpattern, listener, list);
}
}
}
public static void findFile(String path, ArrayList<File> list) {
if (path == null) {
throw new IllegalArgumentException("The argument path must not be null!");
}
findFile(new File(path), null, list);
}
public static void findFile(File dir, ArrayList<File> list) {
findFile(dir, null, list);
}
public static void findFile(File dir, String fpattern, ArrayList<File> list) {
findFile(dir, fpattern, true, list);
}
/**
* Die Methode <code>findFile</code> sucht nach Dateien im Verzeichnis
* <code>dir</code> die auf den regulären Ausdruck <code>fpattern</code>
* passen.
*
* @param dir Das Verzeichnis.
* @param fpattern Der reguläre Ausdruck.
* @param list Die Liste der gefundenen Dateien
*/
public static void findFile(File dir, String fpattern, boolean recursive, ArrayList<File> list) {
if (dir == null) {
throw new IllegalArgumentException("The argument dir must not be null!");
}
if (list == null) {
throw new IllegalArgumentException("The argument list must not be null!");
}
File[] files = dir.listFiles();
File file;
Pattern ref = null;
Matcher fileMatcher = null;
if (fpattern != null) {
ref = Pattern.compile(fpattern, Pattern.CASE_INSENSITIVE);
}
if (files == null) {
return;
}
for (int i = 0; i < files.length; i++) {
file = files[i];
if (file.isDirectory()) {
if (recursive == true) {
findFile(file, fpattern, list);
}
} else {
if (ref != null) {
fileMatcher = ref.matcher(file.getName());
if (fileMatcher.matches()) {
list.add(file);
}
} else {
list.add(file);
}
}
}
}
/**
* Die Methode <code>findDirectory</code> sucht nach Unterverzeichnissen im
* Verzeichnis <code>dir</code> die auf den regulären Ausdruck
* <code>dpattern</code> passen. Die Liste wird, bevor sie an die aufrufende
* Methode zurückgegeben wird, nach der Anzahl der Pfadelemente
* sortiert. Und zwar so, daß die längsten Pfade am Anfang der Liste
* stehen. Dies ermöglicht eine Verarbeitung, z.B. Umbenennung, der Pfade
* vom Ende her, in der Art, daß möglichst alle Pfade ihre Gültigkeit
* behalten. Würde ein Pfad zuerst am Anfang manipuliert, z.B. durch
* Umbennenung, wären alle Pfadnamen der Unterverzeichnisse auf einen Schlag
* ungültig.
*
* @param dir Das Verzeichnis
* @param dpattern Der reguläre Ausdruck.
* @param list Die Liste der gefundenen Dateien
*/
public static void findDirectory(File dir, String dpattern, ArrayList<File> list) {
if (dir == null) {
throw new IllegalArgumentException("The argument dir must not be null!");
}
if (dpattern == null) {
throw new IllegalArgumentException("The argument dpattern must not be null!");
}
if (list == null) {
throw new IllegalArgumentException("The argument list must not be null!");
}
File[] files = dir.listFiles();
File file;
Pattern red = Pattern.compile(dpattern, Pattern.CASE_INSENSITIVE);
Matcher dirMatcher;
if (files == null) {
return;
}
for (int i = 0; i < files.length; i++) {
file = files[i];
if (file.isDirectory()) {
dirMatcher = red.matcher(file.getName());
if (dirMatcher.matches()) {
list.add(file);
}
findDirectory(file, dpattern, list);
}
}
Collections.sort(list, new PathTokenCountComparator());
}
}
|