📄 syntaxfilters.java~2~
字号:
/*
* 03/08/2004
*
* SyntaxFilters.java - Manages a list of wildcard file filters and what
* syntax highlighting styles they map to.
*/
package org.fife.rtext;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.swing.JOptionPane;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
/**
* Manages a list of wildcard file filters and what syntax highlighting
* styles they map to.
*
* @author Robert Futrell
* @version 0.1
*/
public class SyntaxFilters implements SyntaxConstants {
/**
* One filter set for every file type except plain text.
*/
private ArrayList[] filters;
/**
* Default extensions RText matches to syntax styles.
*/
private static final String[][] DEFAULT_FILTERS = {
/* X86 asm */ { "*.asm" },
/* C */ { "*.c" },
/* C++ */ { "*.cpp", "*.cxx", "*.h" },
/* C# */ { "*.cs" },
/* CSS */ { "*.css" },
/* Fortran */ { "*.f", "*.for", "*.fort", "*.f77", "*.f90" },
/* HTML */ { "*.htm", "*.html" },
/* Java */ { "*.java" },
/* JavaScript */ { "*.js" },
/* Perl */ { "*.perl", "*.pl", "*.pm" },
/* Properties files */ { "*.properties" },
/* Python */ { "*.py" },
/* SAS */ { "*.sas" },
/* SQL */ { "*.sql" },
/* UNIX shell */ { "*.sh", "*.?sh" },
/* Win batch */ { "*.bat", "*.cmd" },
/* XML */ { "*.xml", "*.macro", "*.manifest" },
};
/*****************************************************************************/
/**
* Creates a new <code>SyntaxFilters</code> with default values for
* all filters.
*/
public SyntaxFilters() {
this(null);
}
/*****************************************************************************/
/**
* Creates a new <code>SyntaxFilters</code> from the given string.
*
* @param filterString Must be a <code>String</code> generated from
* <code>SyntaxFilter.toString()</code>. If its
* format is invalid, then default filter strings are
* used for all syntax styles.
*/
public SyntaxFilters(String filterString) {
// One filter set for every file type except plain text.
filters = new ArrayList[MAX_SYNTAX_STYLE_NUMBER];
for (int i=0; i<MAX_SYNTAX_STYLE_NUMBER; i++)
filters[i] = new ArrayList();
restoreDefaultFileFilters();
int i = 1; // Start at 1 to skip 0==NO_SYNTAX_STYLE.
if (filterString!=null) {
int oldCommaPos = 0;
int commaPos = filterString.indexOf(',', 0);
while (commaPos!=-1) {
String token = filterString.substring(oldCommaPos,commaPos);
setFiltersForSyntaxStyle(i++, token);
oldCommaPos = commaPos + 1;
commaPos = filterString.indexOf(',', oldCommaPos);
}
// Get the last one (with no trailing comma).
setFiltersForSyntaxStyle(i, filterString.substring(oldCommaPos));
}
// Use the defaults for all file filters if the file filter string
// was "too short" to cover all syntax styles. This is because we may
// have added a new syntax style somewhere in the middle, and this
// would mess up our filter->style mapping.
if (i!=MAX_SYNTAX_STYLE_NUMBER) {
restoreDefaultFileFilters();
}
}
/*****************************************************************************/
/**
* Adds a file filter for a given syntax style.
*
* @param syntaxStyle The syntax style to add a file filter to. If this
* style is less than 1 or greater than
* <code>MAX_SYNTAX_STYLE_NUMBER</code>, this method
* will do nothing.
* @param filter The filter to add. If <code>null</code>, nothing will
* be done.
*/
public void addFileFilter(final int syntaxStyle, final String filter) {
if (syntaxStyle<1 || syntaxStyle>MAX_SYNTAX_STYLE_NUMBER ||
filter==null)
return;
// -1 because we don't have filters for NO_SYNTAX_STYLE.
filters[syntaxStyle-1].add(filter);
}
/*****************************************************************************/
/**
* Returns a list of all wildcard file filters associated with this
* syntax type, separated by spaces. For example, if the C++ syntax
* style has filters <code>*.cpp</code> and <code>*.h</code> associated
* with it, then <code>getFilterString(CPLUSPLUS_SYNTAX_STYLE)</code>
* would return <code>"*.cpp *.h"</code>.
*
* @param syntaxStyle The syntax style for which to retrieve the
* string. If it is less than 1 or greater than
* <code>SyntaxConstants.MAX_STYLE_NUMBER</code>,
* <code>null</code> is returned.
*/
public String getFilterString(int syntaxStyle) {
if (syntaxStyle<1 ||
syntaxStyle>SyntaxConstants.MAX_SYNTAX_STYLE_NUMBER)
return null;
// -1 because we don't have filters for NO_SYNTAX_STYLE.
Iterator iterator = filters[syntaxStyle-1].iterator();
String filterString = "";
while (iterator.hasNext())
filterString += (String)iterator.next() + " ";
return filterString;
}
/*****************************************************************************/
/**
* Returns the type of syntax highlighting to use for the given text
* file based on its extension and the current filters.
*
* @param fileName The file to syntax highlight.
* @return The syntax highlighting scheme to use, for example,
* <code>JAVA_SYNTAX_STYLE</code> or
* <code>CPLUSPLUS_SYNTAX_STYLE</code>.
* @see org.fife.ui.rsyntaxtextarea.RSyntaxTextArea
* @see org.fife.ui.rsyntaxtextarea.SyntaxConstants
*/
public int getSyntaxStyleForFile(final String fileName) {
if (fileName==null)
return NO_SYNTAX_STYLE;
Iterator iterator;
String regEx;
final String fileNameLowerCase = fileName.toLowerCase();
for (int i=0; i<MAX_SYNTAX_STYLE_NUMBER; i++) {
iterator = filters[i].iterator();
while (iterator.hasNext()) {
regEx = RTextUtilities.getRegularExpressionForFileFilter(
(String)iterator.next(), true);
if (regEx!=null && fileNameLowerCase.matches(regEx))
// +1 because we have no file filters for NO_SYNTAX_STYLE.
return i+1;
}
}
return NO_SYNTAX_STYLE;
}
/*****************************************************************************/
/**
* Returns <code>true</code> if and only if the file filter string passed
* in is "valid". Currently valid file filter strings contain only the
* following characters: A-Z, a-z, 0-9, '*', '?', '.', '-', '_', ' ', '$'.
*
* @param fileFilterString The file filter string to test.
* @return <code>true</code> if the file filter string is valid, false
* otherwise.
*/
public static boolean isValidFileFilterString(String fileFilterString) {
int length = fileFilterString.length();
for (int i=0; i<length; i++) {
char c = fileFilterString.charAt(i);
switch (c) {
case '*':
case '?':
case '.':
case '-':
case '_':
case '$':
case ' ':
continue;
default:
if (!Character.isLetterOrDigit(c))
return false;
}
}
return true;
}
/*****************************************************************************/
/**
* Sets default values for syntax filters.
*/
public void restoreDefaultFileFilters() {
// NOTE: We loop through this way instead of hard-coding
// filter-to-syntax-style values because if we ever add new
// syntax styles in the future, this method will still be sound
// (once the new styles are added in, of course).
for (int i=0; i<MAX_SYNTAX_STYLE_NUMBER; i++) {
filters[i].clear();
String[] defaults = DEFAULT_FILTERS[i];
for (int j=0; j<defaults.length; j++)
filters[i].add(defaults[j]);
}
}
/*****************************************************************************/
/**
* Sets all file filters for a given syntax style.
*
* @param syntaxStyle The syntax style for which to set the file filters.
* If this parameter is less than 1 or greater than
* <code>MAX_SYNTAX_STYLE_NUMBER</code> then nothing
* happens.
* @param filterString A string representing the file filters separated
* by spaces. If <code>null</code>, nothing happens.
*/
public void setFiltersForSyntaxStyle(int syntaxStyle, String filterString) {
if (syntaxStyle<1 || syntaxStyle>SyntaxConstants.MAX_SYNTAX_STYLE_NUMBER ||
filterString==null)
return;
//StringTokenizer st = new StringTokenizer(filterString);
ArrayList tempList = filters[syntaxStyle-1]; // -1 since we have no NO_SYNTAX_STYLE filters.
tempList.clear();
int oldSpacePos = 0;
int spacePos = filterString.indexOf(' ', 0);
while (spacePos!=-1) {
String token = filterString.substring(oldSpacePos, spacePos);
if (!token.equals(""))
tempList.add(token);
oldSpacePos = spacePos + 1;
spacePos = filterString.indexOf(' ', oldSpacePos);
}
if (oldSpacePos<filterString.length()-1)
tempList.add(filterString.substring(oldSpacePos));
tempList = null;
}
/*****************************************************************************/
/**
* Returns this object as a string.
*
* @return A string representing this <code>SyntaxFilters</code>.
*/
public String toString() {
String retVal = "";
for (int i=0; i<MAX_SYNTAX_STYLE_NUMBER; i++)
retVal += getFilterString(i+1) + ",";
// Get rid of the last comma.
retVal = retVal.substring(0,retVal.length()-1);
return retVal;
}
/*****************************************************************************/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -