Java Code Examples for java.util.regex.PatternSyntaxException#getIndex()

The following examples show how to use java.util.regex.PatternSyntaxException#getIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidPatternVisitor.java    From datawave with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
        }
    }
}
 
Example 2
Source File: PreferencesController.java    From cyberduck with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: CompileCommandsJsonParserOptionPage.java    From cmake4eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@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());
  }
}
 
Example 4
Source File: RegExFormatter.java    From consulo with Apache License 2.0 5 votes vote down vote up
public Object stringToValue(String string) throws ParseException {
    try {
        return Pattern.compile(string);
    } catch (final PatternSyntaxException e) {
        throw new ParseException(e.getMessage(), e.getIndex());
    }
}
 
Example 5
Source File: InternalPathPatternParser.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * 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;
}
 
Example 6
Source File: InternalPathPatternParser.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * 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;
}