[Schmitzm-commits] r2314 - trunk/schmitzm-core/src/main/java/de/schmitzm/io
scm-commit at wald.intevation.org
scm-commit at wald.intevation.org
Fri May 24 16:39:36 CEST 2013
Author: mojays
Date: 2013-05-24 16:39:36 +0200 (Fri, 24 May 2013)
New Revision: 2314
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileProcessor.java
Log:
new FileProcessor: process a method for all files which satisfy a specification (containing wildcards, e.g. *.tif)
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileProcessor.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileProcessor.java (rev 0)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/io/FileProcessor.java 2013-05-24 14:39:36 UTC (rev 2314)
@@ -0,0 +1,217 @@
+/**
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * This file is part of the SCHMITZM library - a collection of utility
+ * classes based on Java 1.6, focusing (not only) on Java Swing
+ * and the Geotools library.
+ *
+ * The SCHMITZM project is hosted at:
+ * http://wald.intevation.org/projects/schmitzm/
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 Lesser General Public License (license.txt)
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * or try this link: http://www.gnu.org/licenses/lgpl.html
+ *
+ * Contributors:
+ * Martin O. J. Schmitz - initial API and implementation
+ * Stefan A. Tzeggai - additional utility classes
+ */
+package de.schmitzm.io;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.commons.io.FilenameUtils;
+import org.apache.log4j.Logger;
+
+import de.schmitzm.lang.LangUtil;
+
+/**
+ * Calls the {@link #processFile(File, int)} method for all file system files
+ * which satisfy a specification (which may include wildcards), e.g. *.tif.
+ * <br><br>
+ * TODO: currently FileProcessor does not yet support recursive processing through
+ * sub-directories
+ * @author Martin O.J. Schmitz
+ *
+ */
+public abstract class FileProcessor {
+ protected Logger LOGGER = LangUtil.createLogger(this);
+
+ /** File specifications (may include wildcards) given in constructor. */
+ protected List<String> filesSpec;
+
+ /**
+ * Creates new file processor.
+ * @param filesSpec defines the files to process (may include wildcards)
+ */
+ public FileProcessor(String... filesSpec) {
+ this(asList(filesSpec));
+ }
+
+ /**
+ * Creates new file processor.
+ * @param filesSpec defines the files to process (may include wildcards)
+ */
+ public FileProcessor(List<String> filesSpec) {
+ this.filesSpec = filesSpec;
+ }
+
+ /**
+ * Returns the file specifications given in constructor.
+ */
+ public List<String> getFilesSpecifiation() {
+ return this.filesSpec;
+ }
+
+ /**
+ * Converts a dynamic {@link String} array to {@link String} list.
+ * @param filesSpec
+ * @return {@code null} if {@code null} is given as only element
+ */
+ private static List<String> asList(String... filesSpec) {
+ if ( filesSpec.length == 1 && filesSpec[0] == null )
+ return null;
+ return Arrays.asList(filesSpec);
+ }
+
+ /**
+ * Processes a single file.
+ * @param file file to process
+ * @param fileNo number of file (starting with 1)
+ */
+ public abstract void processFile(File file, int fileNo) throws Exception;
+
+ /**
+ * Calls {@link #processFile(File)} for all files defined by
+ * the given files specification which may include wildcards.
+ * @param filesSpec defines the files to process (if {@code null} or empty
+ * array is given, the initial specification {@link #getFilesSpecifiation()}
+ * is used)
+ * @return a list of all processed files
+ * @see FilenameUtils#wildcardMatchOnSystem(String, String)
+ */
+ public List<File> processFiles(boolean cancelOnError, String... filesSpec) throws IOException {
+ return processFiles( cancelOnError, asList(filesSpec) );
+ }
+
+ /**
+ * Calls {@link #processFile(File)} for all files defined by
+ * the given files specification which may include wildcards.
+ * @param filesSpec defines the files to process (if {@code null} or empty
+ * array is given, the initial specification {@link #getFilesSpecifiation()}
+ * is used)
+ * @return a list of all processed files
+ * @see FilenameUtils#wildcardMatchOnSystem(String, String)
+ */
+ public List<File> processFiles(boolean cancelOnError, List<String> filesSpec) throws IOException {
+ if ( filesSpec == null || filesSpec.size() == 0 )
+ filesSpec = getFilesSpecifiation();
+
+ int fileCounter = 0;
+ List<File> processedFiles = new ArrayList<File>();
+ for (String fileSpec : filesSpec) {
+ File file = new File(fileSpec).getAbsoluteFile();
+ File dir = file.getParentFile();
+
+ // Create filter for file specification
+ final String nameFilter = file.getName();
+ FileFilter filter = new FileFilter() {
+ @Override
+ public boolean accept(File file) {
+ if ( file.isDirectory() )
+ return false;
+ return FilenameUtils.wildcardMatchOnSystem(file.getName(), nameFilter);
+ }
+ };
+
+ // Process all files which satisfy the filter
+ File[] files = dir.listFiles(filter);
+ if ( files != null)
+ for (File f : files) {
+ try {
+ processFile(f, ++fileCounter);
+ processedFiles.add(f);
+ } catch (Exception e) {
+ if ( cancelOnError ) {
+ if ( e instanceof IOException )
+ throw (IOException)e;
+ else
+ throw new IOException(e);
+ }
+ LangUtil.logDebugError(LOGGER, e);
+ }
+ }
+ }
+
+ return processedFiles;
+
+ }
+
+ /**
+ * Determines all files which satisfy the given files specification
+ * without doing anything with the files (no call of {@link #processFile(File)})
+ * @param filesSpec defines the files to process (if {@code null} or empty
+ * array is given, the initial specification {@link #getFilesSpecifiation()}
+ * is used)
+ */
+ public List<File> determineFiles(String... filesSpec) {
+ return determineFiles( asList(filesSpec) );
+ }
+
+ /**
+ * Determines all files which satisfy the given files specification
+ * without doing anything with the files (no call of {@link #processFile(File)})
+ * @param filesSpec defines the files to process (if {@code null} or empty
+ * array is given, the initial specification {@link #getFilesSpecifiation()}
+ * is used)
+ */
+ public List<File> determineFiles(List<String> filesSpec) {
+ // Create empty processor to count files
+ FileProcessor proc = new FileProcessor(filesSpec) {
+ @Override
+ public void processFile(File file, int fileNo) {
+ }
+ };
+ try {
+ return proc.processFiles(false);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Counts the number of files which satisfy the given files specification.
+ * @param filesSpec defines the files to process (if {@code null} or empty
+ * array is given, the initial specification {@link #getFilesSpecifiation()}
+ * is used)
+ */
+ public int countFiles(String... filesSpec) {
+ return countFiles( asList(filesSpec) );
+ }
+
+ /**
+ * Counts the number of files which satisfy the given files specification.
+ * @param filesSpec defines the files to process (if {@code null} or empty
+ * array is given, the initial specification {@link #getFilesSpecifiation()}
+ * is used)
+ */
+ public int countFiles(List<String> filesSpec) {
+ return determineFiles(filesSpec).size();
+ }
+}
More information about the Schmitzm-commits
mailing list