public final class FileTypeUtil extends java.lang.Object implements SyntaxConstants
Typically, you'll want to inspect the file name before loading the
file into an RSyntaxTextArea or TextEditorPane
instance for best performance. Here's an example of how to do so:
File file = getFileToOpen(); // Open the file for editing TextEditorPane textArea = new TextEditorPane(); textArea.load(FileLocation.create(file)); // Guess the type of code to use for syntax highlighting String style = FileTypeUtil.get().guessContentType(file); textArea.setSyntaxEditingStyle(style);Sometimes you won't be able to identify the type of code in a file or stream; for example, if there is no extension on a shell script, or if you're displaying output read from a stream (say HTML or XML) instead of a flat file. In such a case, you can try to guess the content's file type as follows:
File file = getFileToOpen();
// Open the file for editing
TextEditorPane textArea = new TextEditorPane();
textArea.load(FileLocation.create(file));
// Guess the type of code to use for syntax highlighting
String style = FileTypeUtil.get().guessContentType(file);
if (style == null) {
style = FileTypeUtil.get().guessContentType(textArea);
}
textArea.setSyntaxEditingStyle(style);
This logic primarily looks at the first line of the content and
attempts to identify the following:
SyntaxConstants#SYNTAX_STYLE_NONE, then guess the content
type as shown above.SYNTAX_STYLE_ACTIONSCRIPT, SYNTAX_STYLE_ASSEMBLER_6502, SYNTAX_STYLE_ASSEMBLER_X86, SYNTAX_STYLE_BBCODE, SYNTAX_STYLE_C, SYNTAX_STYLE_CLOJURE, SYNTAX_STYLE_CPLUSPLUS, SYNTAX_STYLE_CSHARP, SYNTAX_STYLE_CSS, SYNTAX_STYLE_CSV, SYNTAX_STYLE_D, SYNTAX_STYLE_DART, SYNTAX_STYLE_DELPHI, SYNTAX_STYLE_DOCKERFILE, SYNTAX_STYLE_DTD, SYNTAX_STYLE_FORTRAN, SYNTAX_STYLE_GO, SYNTAX_STYLE_GROOVY, SYNTAX_STYLE_HANDLEBARS, SYNTAX_STYLE_HOSTS, SYNTAX_STYLE_HTACCESS, SYNTAX_STYLE_HTML, SYNTAX_STYLE_INI, SYNTAX_STYLE_JAVA, SYNTAX_STYLE_JAVASCRIPT, SYNTAX_STYLE_JSON, SYNTAX_STYLE_JSON_WITH_COMMENTS, SYNTAX_STYLE_JSP, SYNTAX_STYLE_KOTLIN, SYNTAX_STYLE_LATEX, SYNTAX_STYLE_LESS, SYNTAX_STYLE_LISP, SYNTAX_STYLE_LUA, SYNTAX_STYLE_MAKEFILE, SYNTAX_STYLE_MARKDOWN, SYNTAX_STYLE_MXML, SYNTAX_STYLE_NONE, SYNTAX_STYLE_NSIS, SYNTAX_STYLE_PERL, SYNTAX_STYLE_PHP, SYNTAX_STYLE_PROPERTIES_FILE, SYNTAX_STYLE_PROTO, SYNTAX_STYLE_PYTHON, SYNTAX_STYLE_RUBY, SYNTAX_STYLE_SAS, SYNTAX_STYLE_SCALA, SYNTAX_STYLE_SQL, SYNTAX_STYLE_TCL, SYNTAX_STYLE_TYPESCRIPT, SYNTAX_STYLE_UNIX_SHELL, SYNTAX_STYLE_VISUAL_BASIC, SYNTAX_STYLE_WINDOWS_BATCH, SYNTAX_STYLE_XML, SYNTAX_STYLE_YAML| Modifier and Type | Method and Description |
|---|---|
static java.util.regex.Pattern |
fileFilterToPattern(java.lang.String fileFilter)
Converts a
String representing a wildcard file filter into
a Pattern containing a regular expression good for
finding files that match the wildcard expression. |
static FileTypeUtil |
get()
Returns the singleton instance of this class.
|
java.util.Map<java.lang.String,java.util.List<java.lang.String>> |
getDefaultContentTypeToFilterMap()
Returns the mapping of content types to lists of extensions used
by this class by default.
|
java.lang.String |
guessContentType(java.io.File file)
Guesses the type of content in a file, based on its name.
|
java.lang.String |
guessContentType(java.io.File file,
boolean ignoreBackupExtensions)
Guesses the type of content in a file, based on its name.
|
java.lang.String |
guessContentType(java.io.File file,
java.util.Map<java.lang.String,java.util.List<java.lang.String>> filters)
Guesses the type of content in a file, based on its name.
|
java.lang.String |
guessContentType(java.io.File file,
java.util.Map<java.lang.String,java.util.List<java.lang.String>> filters,
boolean ignoreBackupExtensions)
Guesses the type of content in a file, based on its name.
|
java.lang.String |
guessContentType(RSyntaxTextArea textArea)
Sets the text area's highlighting style based on its content (e.g.
|
static java.lang.String |
stripBackupExtensions(java.lang.String fileName)
Strips the following extensions from the end of a file name, if they are
there:
.orig
.bak
.old
The idea is that these are typically backup files, and when the
extension can be used to deduce a file's type/content, that
extension should be ignored. |
public static FileTypeUtil get()
public static java.util.regex.Pattern fileFilterToPattern(java.lang.String fileFilter)
String representing a wildcard file filter into
a Pattern containing a regular expression good for
finding files that match the wildcard expression.
Example: For
String regEx = FileTypeUtil.get().fileFilterToPattern("*.c");
the returned pattern will match ^.*\.c$.
Case-sensitivity is taken into account appropriately.
fileFilter - The file filter for which to create equivalent regular
expressions. This filter can currently only contain the
wildcards '*' and '?'.Pattern representing an equivalent regular
expression for the string passed in.java.util.regex.PatternSyntaxException - If the file filter could not be parsed.public java.util.Map<java.lang.String,java.util.List<java.lang.String>> getDefaultContentTypeToFilterMap()
public java.lang.String guessContentType(RSyntaxTextArea textArea)
#!" at the top).textArea - The text area to examine.SyntaxConstants.SYNTAX_STYLE_NONE if nothing can be
determined, but will never be null.SyntaxConstants,
guessContentType(File),
guessContentType(File, boolean)public java.lang.String guessContentType(java.io.File file)
file - The file, which may be null.SyntaxConstants.SYNTAX_STYLE_NONE if nothing can be
determined, but will never be null.SyntaxConstants,
guessContentType(File, boolean),
guessContentType(RSyntaxTextArea)public java.lang.String guessContentType(java.io.File file,
java.util.Map<java.lang.String,java.util.List<java.lang.String>> filters)
Note you'll typically only need to call this overload if your application implements syntax highlighting for additional/custom languages, or supports syntax highlighting files with an extension the default implementation doesn't know about.
file - The file, which may be null.filters - The map of SyntaxConstants values to lists of
wildcard filters. If this is null, a default set of
filters is used.SyntaxConstants.SYNTAX_STYLE_NONE if nothing can be
determined, but will never be null.SyntaxConstants,
guessContentType(File, boolean),
guessContentType(RSyntaxTextArea)public java.lang.String guessContentType(java.io.File file,
boolean ignoreBackupExtensions)
file - The file, which may be null.ignoreBackupExtensions - Whether to ignore backup extensions.SyntaxConstants.SYNTAX_STYLE_NONE if nothing can be
determined, but will never be null.SyntaxConstants,
guessContentType(File),
guessContentType(RSyntaxTextArea)public java.lang.String guessContentType(java.io.File file,
java.util.Map<java.lang.String,java.util.List<java.lang.String>> filters,
boolean ignoreBackupExtensions)
Note you'll typically only need to call this overload if your application implements syntax highlighting for additional/custom languages, or supports syntax highlighting files with an extension the default implementation doesn't know about.
file - The file, which may be null.filters - The map of SyntaxConstants values to lists of
wildcard filters. If this is null, a default set of
filters is used.ignoreBackupExtensions - Whether to ignore backup extensions.SyntaxConstants.SYNTAX_STYLE_NONE if nothing can be
determined, but will never be null.SyntaxConstants,
guessContentType(File),
guessContentType(RSyntaxTextArea)public static java.lang.String stripBackupExtensions(java.lang.String fileName)
.orig.bak.oldfileName - The file name. This may be null.