下面列出了java.util.regex.PatternSyntaxException#getIndex ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Parse a literal value and put into the pattern cache if it does not exist.
*
* @param node
*/
public void parseAndPutPattern(JexlNode node) {
// Catch the situation where a user might enter FIELD1 !~ VALUE1
Object literalValue = JexlASTHelper.getLiteralValue(node);
if (literalValue != null && String.class.equals(literalValue.getClass())) {
String literalString = (String) literalValue;
try {
if (patternCache.containsKey(literalString)) {
return;
}
patternCache.put(literalString, Pattern.compile(literalString));
} catch (PatternSyntaxException e) {
String builtNode = JexlStringBuildingVisitor.buildQueryWithoutParse(node);
String errMsg = "Invalid pattern found in filter function '" + builtNode + "'";
throw new PatternSyntaxException(errMsg, e.getPattern(), e.getIndex());
}
}
}
private void mark(NSMutableAttributedString text, PatternSyntaxException e) {
if(null == e) {
text.removeAttributeInRange(
NSAttributedString.ForegroundColorAttributeName,
NSRange.NSMakeRange(new NSUInteger(0), text.length()));
return;
}
int index = e.getIndex(); //The approximate index in the pattern of the error
NSRange range = null;
if(-1 == index) {
range = NSRange.NSMakeRange(new NSUInteger(0), text.length());
}
if(index < text.length().intValue()) {
//Initializes the NSRange with the range elements of location and length;
range = NSRange.NSMakeRange(new NSUInteger(index), new NSUInteger(1));
}
text.addAttributesInRange(RED_FONT, range);
}
@Override
public void performApply(IProgressMonitor monitor) throws CoreException {
// normally should be handled by LanguageSettingsProviderTab
final String text = pattern.getText();
try {
Pattern.compile(text);
} catch (PatternSyntaxException ex) {
// BUG in CDT: core exceptions thrown here are not visible to users. CDT-WTF
// IStatus status = new Status(IStatus.ERROR, Plugin.PLUGIN_ID,
// IStatus.OK,
// "invalid suffix pattern in CMAKE_EXPORT_COMPILE_COMMANDS Parser", ex);
// throw new CoreException(status);
throw new PatternSyntaxException(
"Invalid suffix pattern in CMAKE_EXPORT_COMPILE_COMMANDS Parser:\n" + ex.getDescription(), ex.getPattern(),
ex.getIndex());
}
}
public Object stringToValue(String string) throws ParseException {
try {
return Pattern.compile(string);
} catch (final PatternSyntaxException e) {
throw new ParseException(e.getMessage(), e.getIndex());
}
}
/**
* Used the knowledge built up whilst processing since the last path element to determine what kind of path
* element to create.
* @return the new path element
*/
private PathElement createPathElement() {
if (this.insideVariableCapture) {
throw new PatternParseException(this.pos, this.pathPatternData, PatternMessage.MISSING_CLOSE_CAPTURE);
}
PathElement newPE = null;
if (this.variableCaptureCount > 0) {
if (this.variableCaptureCount == 1 && this.pathElementStart == this.variableCaptureStart &&
this.pathPatternData[this.pos - 1] == '}') {
if (this.isCaptureTheRestVariable) {
// It is {*....}
newPE = new CaptureTheRestPathElement(
this.pathElementStart, getPathElementText(), this.parser.getSeparator());
}
else {
// It is a full capture of this element (possibly with constraint), for example: /foo/{abc}/
try {
newPE = new CaptureVariablePathElement(this.pathElementStart, getPathElementText(),
this.parser.isCaseSensitive(), this.parser.getSeparator());
}
catch (PatternSyntaxException pse) {
throw new PatternParseException(pse,
findRegexStart(this.pathPatternData, this.pathElementStart) + pse.getIndex(),
this.pathPatternData, PatternMessage.REGEX_PATTERN_SYNTAX_EXCEPTION);
}
recordCapturedVariable(this.pathElementStart,
((CaptureVariablePathElement) newPE).getVariableName());
}
}
else {
if (this.isCaptureTheRestVariable) {
throw new PatternParseException(this.pathElementStart, this.pathPatternData,
PatternMessage.CAPTURE_ALL_IS_STANDALONE_CONSTRUCT);
}
RegexPathElement newRegexSection = new RegexPathElement(this.pathElementStart,
getPathElementText(), this.parser.isCaseSensitive(),
this.pathPatternData, this.parser.getSeparator());
for (String variableName : newRegexSection.getVariableNames()) {
recordCapturedVariable(this.pathElementStart, variableName);
}
newPE = newRegexSection;
}
}
else {
if (this.wildcard) {
if (this.pos - 1 == this.pathElementStart) {
newPE = new WildcardPathElement(this.pathElementStart, this.parser.getSeparator());
}
else {
newPE = new RegexPathElement(this.pathElementStart, getPathElementText(),
this.parser.isCaseSensitive(), this.pathPatternData, this.parser.getSeparator());
}
}
else if (this.singleCharWildcardCount != 0) {
newPE = new SingleCharWildcardedPathElement(this.pathElementStart, getPathElementText(),
this.singleCharWildcardCount, this.parser.isCaseSensitive(), this.parser.getSeparator());
}
else {
newPE = new LiteralPathElement(this.pathElementStart, getPathElementText(),
this.parser.isCaseSensitive(), this.parser.getSeparator());
}
}
return newPE;
}
/**
* Used the knowledge built up whilst processing since the last path element to determine what kind of path
* element to create.
* @return the new path element
*/
private PathElement createPathElement() {
if (this.insideVariableCapture) {
throw new PatternParseException(this.pos, this.pathPatternData, PatternMessage.MISSING_CLOSE_CAPTURE);
}
PathElement newPE = null;
if (this.variableCaptureCount > 0) {
if (this.variableCaptureCount == 1 && this.pathElementStart == this.variableCaptureStart &&
this.pathPatternData[this.pos - 1] == '}') {
if (this.isCaptureTheRestVariable) {
// It is {*....}
newPE = new CaptureTheRestPathElement(
this.pathElementStart, getPathElementText(), this.parser.getSeparator());
}
else {
// It is a full capture of this element (possibly with constraint), for example: /foo/{abc}/
try {
newPE = new CaptureVariablePathElement(this.pathElementStart, getPathElementText(),
this.parser.isCaseSensitive(), this.parser.getSeparator());
}
catch (PatternSyntaxException pse) {
throw new PatternParseException(pse,
findRegexStart(this.pathPatternData, this.pathElementStart) + pse.getIndex(),
this.pathPatternData, PatternMessage.REGEX_PATTERN_SYNTAX_EXCEPTION);
}
recordCapturedVariable(this.pathElementStart,
((CaptureVariablePathElement) newPE).getVariableName());
}
}
else {
if (this.isCaptureTheRestVariable) {
throw new PatternParseException(this.pathElementStart, this.pathPatternData,
PatternMessage.CAPTURE_ALL_IS_STANDALONE_CONSTRUCT);
}
RegexPathElement newRegexSection = new RegexPathElement(this.pathElementStart,
getPathElementText(), this.parser.isCaseSensitive(),
this.pathPatternData, this.parser.getSeparator());
for (String variableName : newRegexSection.getVariableNames()) {
recordCapturedVariable(this.pathElementStart, variableName);
}
newPE = newRegexSection;
}
}
else {
if (this.wildcard) {
if (this.pos - 1 == this.pathElementStart) {
newPE = new WildcardPathElement(this.pathElementStart, this.parser.getSeparator());
}
else {
newPE = new RegexPathElement(this.pathElementStart, getPathElementText(),
this.parser.isCaseSensitive(), this.pathPatternData, this.parser.getSeparator());
}
}
else if (this.singleCharWildcardCount != 0) {
newPE = new SingleCharWildcardedPathElement(this.pathElementStart, getPathElementText(),
this.singleCharWildcardCount, this.parser.isCaseSensitive(), this.parser.getSeparator());
}
else {
newPE = new LiteralPathElement(this.pathElementStart, getPathElementText(),
this.parser.isCaseSensitive(), this.parser.getSeparator());
}
}
return newPE;
}