Class FileTypeUtil
- All Implemented Interfaces:
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:
- A shebang, and if so, what file type is being interpreted
- Whether there's an XML processing instruction
- Whether there's an HTML doctype tag
SyntaxConstants#SYNTAX_STYLE_NONE
, then guess the content
type as shown above.- Version:
- 1.0
-
Field Summary
FieldsFields inherited from interface org.fife.ui.rsyntaxtextarea.SyntaxConstants
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_RUST, 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
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Pattern
fileFilterToPattern
(String fileFilter) Converts aString
representing a wildcard file filter into aPattern
containing a regular expression good for finding files that match the wildcard expression.private static String
fileFilterToPatternImpl
(String filter) static FileTypeUtil
get()
Returns the singleton instance of this class.Returns the mapping of content types to lists of extensions used by this class by default.guessContentType
(File file) Guesses the type of content in a file, based on its name.guessContentType
(File file, boolean ignoreBackupExtensions) Guesses the type of content in a file, based on its name.Guesses the type of content in a file, based on its name.Guesses the type of content in a file, based on its name.guessContentType
(RSyntaxTextArea textArea) Sets the text area's highlighting style based on its content (e.g.private static String
Looks for a syntax style for a file name in a given map.private static void
private void
static String
stripBackupExtensions
(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.
-
Field Details
-
map
-
DEFAULT_IGNORE_BACKUP_EXTENSIONS
private static final boolean DEFAULT_IGNORE_BACKUP_EXTENSIONS- See Also:
-
INSTANCE
-
-
Constructor Details
-
FileTypeUtil
private FileTypeUtil()
-
-
Method Details
-
get
Returns the singleton instance of this class.- Returns:
- The singleton instance of this class.
-
fileFilterToPattern
Converts aString
representing a wildcard file filter into aPattern
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.
- Parameters:
fileFilter
- The file filter for which to create equivalent regular expressions. This filter can currently only contain the wildcards'*'
and'?'
.- Returns:
- A
Pattern
representing an equivalent regular expression for the string passed in. - Throws:
PatternSyntaxException
- If the file filter could not be parsed.
-
fileFilterToPatternImpl
-
getDefaultContentTypeToFilterMap
Returns the mapping of content types to lists of extensions used by this class by default.- Returns:
- The mapping.
-
guessContentType
Sets the text area's highlighting style based on its content (e.g. whether it contains "#!
" at the top).- Parameters:
textArea
- The text area to examine.- Returns:
- The guessed content type. This may be
SyntaxConstants.SYNTAX_STYLE_NONE
if nothing can be determined, but will never benull
. - See Also:
-
guessContentType
Guesses the type of content in a file, based on its name. Backup extensions will be ignored.- Parameters:
file
- The file, which may benull
.- Returns:
- The guessed file type. This may be
SyntaxConstants.SYNTAX_STYLE_NONE
if nothing can be determined, but will never benull
. - See Also:
-
guessContentType
Guesses the type of content in a file, based on its name. Backup extensions will be ignored.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.
- Parameters:
file
- The file, which may benull
.filters
- The map ofSyntaxConstants
values to lists of wildcard filters. If this isnull
, a default set of filters is used.- Returns:
- The guessed file type. This may be
SyntaxConstants.SYNTAX_STYLE_NONE
if nothing can be determined, but will never benull
. - See Also:
-
guessContentType
Guesses the type of content in a file, based on its name.- Parameters:
file
- The file, which may benull
.ignoreBackupExtensions
- Whether to ignore backup extensions.- Returns:
- The guessed file type. This may be
SyntaxConstants.SYNTAX_STYLE_NONE
if nothing can be determined, but will never benull
. - See Also:
-
guessContentType
public String guessContentType(File file, Map<String, List<String>> filters, boolean ignoreBackupExtensions) Guesses the type of content in a file, based on its name.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.
- Parameters:
file
- The file, which may benull
.filters
- The map ofSyntaxConstants
values to lists of wildcard filters. If this isnull
, a default set of filters is used.ignoreBackupExtensions
- Whether to ignore backup extensions.- Returns:
- The guessed file type. This may be
SyntaxConstants.SYNTAX_STYLE_NONE
if nothing can be determined, but will never benull
. - See Also:
-
guessContentTypeImpl
Looks for a syntax style for a file name in a given map.- Parameters:
fileName
- The file name, possibly with a backup extension removed.filters
- The map ofSyntaxConstants
values to lists of wildcard filters.- Returns:
- The syntax style for the file, or
null
if nothing could be determined.
-
initFiltersImpl
-
initializeFilters
private void initializeFilters() -
stripBackupExtensions
Strips the following extensions from the end of a file name, if they are there:.orig
.bak
.old
- Parameters:
fileName
- The file name. This may benull
.- Returns:
- The same file name, with any of the above extensions removed.
-