Skip to content

Commit

Permalink
[inputoutput] Add file filters and extend existing ones.
Browse files Browse the repository at this point in the history
see #113

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Jun 13, 2016
1 parent 9deb2e4 commit a488b51
Show file tree
Hide file tree
Showing 84 changed files with 2,854 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

package org.arakhne.afc.inputoutput.filefilter;

import java.io.File;
import java.net.URL;

import org.arakhne.afc.inputoutput.filetype.FileType;
import org.arakhne.afc.inputoutput.mime.MimeName;
import org.arakhne.afc.vmutil.locale.Locale;

/** File filter for the Microsoft/IBM bitmap files.
Expand All @@ -28,14 +33,15 @@
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 14.0
*/
public class BMPFileFilter extends AbstractFileFilter {

/** Default extension for the Microsoft/IBM bitmap files.
*/
public static final String EXTENSION_BMP = "bmp"; //$NON-NLS-1$

/** Default extension for the Microsoft/IBM bitmap files.
/** Default extension for the BMP pictures.
*
* @deprecated since 13.0, see {@link #EXTENSION_BMP}
*/
Expand All @@ -61,4 +67,34 @@ public BMPFileFilter(boolean acceptDirectories) {
EXTENSION_BMP);
}

/** Replies if the specified file contains BMP picture.
*
* @param file is the file to test.
* @return <code>true</code> if the given file contains BMP picture,
* otherwise <code>false</code>
*/
public static boolean isBMPFile(File file) {
return FileType.isContentType(file, MimeName.MIME_BMP.getMimeConstant());
}

/** Replies if the specified file contains BMP picture.
*
* @param file is the file to test.
* @return <code>true</code> if the given file contains BMP picture,
* otherwise <code>false</code>
*/
public static boolean isBMPFile(URL file) {
return FileType.isContentType(file, MimeName.MIME_BMP.getMimeConstant());
}

/** Replies if the specified file contains BMP picture.
*
* @param file is the file to test.
* @return <code>true</code> if the given file contains BMP picture,
* otherwise <code>false</code>
*/
public static boolean isBMPFile(String file) {
return FileType.isContentType(file, MimeName.MIME_BMP.getMimeConstant());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/*
* $Id$
* This file is a part of the Arakhne Foundation Classes, http://www.arakhne.org/afc
*
* Copyright (c) 2000-2012 Stephane GALLAND.
* Copyright (c) 2005-10, Multiagent Team, Laboratoire Systemes et Transports,
* Universite de Technologie de Belfort-Montbeliard.
* Copyright (c) 2013-2016 The original authors, and other authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.arakhne.afc.inputoutput.filefilter;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.regex.Pattern;

import org.arakhne.afc.inputoutput.filetype.FileType;
import org.arakhne.afc.inputoutput.filetype.MagicNumber;
import org.arakhne.afc.inputoutput.filetype.MagicNumberStream;
import org.arakhne.afc.inputoutput.mime.MimeName;
import org.arakhne.afc.vmutil.locale.Locale;

/** File filter for the Comma-separated Values files.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 14.0
*/
public class CSVFileFilter extends AbstractFileFilter {

/** Default extension for the Comma-separated Values files.
*/
public static final String EXTENSION_CSV = "csv"; //$NON-NLS-1$

/** Construct.
*/
public CSVFileFilter() {
this(true);
}

/**
* @param acceptDirectories is <code>true</code> to
* permit to this file filter to accept directories;
* <code>false</code> if the directories should not
* match.
*/
public CSVFileFilter(boolean acceptDirectories) {
super(
acceptDirectories,
Locale.getString(CSVFileFilter.class, "FILE_FILTER_NAME"), //$NON-NLS-1$
EXTENSION_CSV);
}

/** Replies if the specified file contains CSV picture.
*
* @param file is the file to test.
* @return <code>true</code> if the given file contains CSV picture,
* otherwise <code>false</code>
*/
public static boolean isCSVFile(File file) {
return FileType.isContentType(file, MimeName.MIME_CSV.getMimeConstant());
}

/** Replies if the specified file contains CSV picture.
*
* @param file is the file to test.
* @return <code>true</code> if the given file contains CSV picture,
* otherwise <code>false</code>
*/
public static boolean isCSVFile(URL file) {
return FileType.isContentType(file, MimeName.MIME_CSV.getMimeConstant());
}

/** Replies if the specified file contains CSV picture.
*
* @param file is the file to test.
* @return <code>true</code> if the given file contains CSV picture,
* otherwise <code>false</code>
*/
public static boolean isCSVFile(String file) {
return FileType.isContentType(file, MimeName.MIME_CSV.getMimeConstant());
}

static {
//Register MIME file contents
FileType.addContentType(new BinaryCSVMagicNumber());
}

/** This class defines a set of informations that could distinguish
* a file content from another one. It is also known as Magic Number
* on several operating systems.
*
* <p>This magic number supports the text content.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 14.0
*/
private static class BinaryCSVMagicNumber extends MagicNumber {

/** Constructor.
*/
BinaryCSVMagicNumber() {
super(MimeName.MIME_CSV, MimeName.MIME_OCTET_STREAM);
}

/** Replies if the specified stream contains data
* that corresponds to this magic number.
*/
@Override
protected final boolean isContentType(MagicNumberStream stream) {
String string;
boolean found;
int offset = 0;

final String regex = "^[\\x20-\\xFF\t\n\r\n]*$"; //$NON-NLS-1$
final String[] separators = {",", ";", "\t"}; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
final String[] qseparators = {",", ";", "\t", " "}; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$

for (int i = 0; i < 3; ++i) {
found = false;
try {
final byte[] line = stream.readLine(offset);
if (line == null) {
return true;
}
string = new String(line);
offset += line.length;

// Check if text.
if (Pattern.matches(regex, string)) {
for (int j = 0; !found && j < qseparators.length; ++j) {
if (matchSeparator(qseparators[j], true, string)) {
found = true;
}
}
if (!found) {
for (int j = 0; !found && j < separators.length; ++j) {
if (matchSeparator(separators[j], false, string)) {
found = true;
}
}
}
}
} catch (IOException e) {
//
}

if (!found) {
return false;
}
}

return true;
}

/** Replies if the given string is matching the given separators.
*
* @param separator is the separator description
* @param quoted indicates if the quote may be tested.
* @param str is the text to test.
* @return <code>true</code> if matching separator, <code>false</code> otherwise
*/
private static boolean matchSeparator(String separator, boolean quoted, String str) {
final String regex = "^[\n\r\t ]*" //$NON-NLS-1$
+ (quoted ? "\"" : "") //$NON-NLS-1$//$NON-NLS-2$
+ "[^" //$NON-NLS-1$
+ separator + "]*" //$NON-NLS-1$
+ (quoted ? "\"" : "") //$NON-NLS-1$//$NON-NLS-2$
+ "([\n\r\t ]*[" //$NON-NLS-1$
+ separator + "][\n\r\t ]*" //$NON-NLS-1$
+ (quoted ? "\"" : "") //$NON-NLS-1$//$NON-NLS-2$
+ "[^" //$NON-NLS-1$
+ separator + "]*" //$NON-NLS-1$
+ (quoted ? "\"" : "") //$NON-NLS-1$//$NON-NLS-2$
+ ")*"; //$NON-NLS-1$
return Pattern.matches(regex, str);
}

}

}
Loading

0 comments on commit a488b51

Please sign in to comment.