-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[inputoutput] Add file filters and extend existing ones.
see #113 Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
9deb2e4
commit a488b51
Showing
84 changed files
with
2,854 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
197 changes: 197 additions & 0 deletions
197
core/inputoutput/src/main/java/org/arakhne/afc/inputoutput/filefilter/CSVFileFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.