use of java.util.regex.PatternSyntaxException in project che by eclipse.
the class PatternConstructor method substituteLinebreak.
/**
* Copied from {@link org.eclipse.jface.text.FindReplaceDocumentAdapter}' to support '\R'
*
* @param findString the string to substitute
* @return the new string
* @throws PatternSyntaxException if "\R" is at an illegal position
*/
private static String substituteLinebreak(String findString) throws PatternSyntaxException {
int length = findString.length();
StringBuffer buf = new StringBuffer(length);
int inCharGroup = 0;
int inBraces = 0;
boolean inQuote = false;
for (int i = 0; i < length; i++) {
char ch = findString.charAt(i);
switch(ch) {
case '[':
buf.append(ch);
if (!inQuote)
inCharGroup++;
break;
case ']':
buf.append(ch);
if (!inQuote)
inCharGroup--;
break;
case '{':
buf.append(ch);
if (!inQuote && inCharGroup == 0)
inBraces++;
break;
case '}':
buf.append(ch);
if (!inQuote && inCharGroup == 0)
inBraces--;
break;
case '\\':
if (i + 1 < length) {
char ch1 = findString.charAt(i + 1);
if (inQuote) {
if (ch1 == 'E')
inQuote = false;
buf.append(ch).append(ch1);
i++;
} else if (ch1 == 'R') {
if (inCharGroup > 0 || inBraces > 0) {
String msg = SearchMessages.PatternConstructor_error_line_delim_position;
throw new PatternSyntaxException(msg, findString, i);
}
//$NON-NLS-1$
buf.append("(?>\\r\\n?|\\n)");
i++;
} else {
if (ch1 == 'Q') {
inQuote = true;
}
buf.append(ch).append(ch1);
i++;
}
} else {
buf.append(ch);
}
break;
default:
buf.append(ch);
break;
}
}
return buf.toString();
}
use of java.util.regex.PatternSyntaxException in project lucida by claritylab.
the class AnswerPatternFilter method loadPatterns.
/**
* Loads the answer patterns from a directory of PROPERTY files. The first
* line of each file is the total number of passages used to assess the
* patterns. It is followed by a list of pattern descriptors, along with
* their number of correct and wrong applications. The format of the
* descriptors is described in the documentation of the class
* <code>AnswerPattern</code>.
*
* @param dir directory of the answer patterns
* @return true, iff the answer patterns were loaded successfully
*/
public static boolean loadPatterns(String dir) {
File[] files = FileUtils.getFiles(dir);
try {
BufferedReader in;
String prop, expr;
int passages, correct, wrong;
HashSet<AnswerPattern> patterns;
for (File file : files) {
MsgPrinter.printStatusMsg(" ...for " + file.getName());
prop = file.getName();
in = new BufferedReader(new FileReader(file));
// total number of passages used to assess the patterns
passages = Integer.parseInt(in.readLine().split(" ")[1]);
nOfPassages.put(prop, passages);
patterns = new HashSet<AnswerPattern>();
while (in.ready()) {
in.readLine();
// pattern descriptor
expr = in.readLine();
// number of correct applications
correct = Integer.parseInt(in.readLine().split(" ")[1]);
// number of wrong applications
wrong = Integer.parseInt(in.readLine().split(" ")[1]);
try {
patterns.add(new AnswerPattern(expr, prop, correct, wrong));
} catch (PatternSyntaxException pse) {
MsgPrinter.printErrorMsg("Problem loading pattern:\n" + prop + " " + expr);
MsgPrinter.printErrorMsg(pse.getMessage());
}
}
props.put(prop, patterns);
in.close();
}
MsgPrinter.printStatusMsg(" ...done");
} catch (IOException e) {
return false;
}
return true;
}
use of java.util.regex.PatternSyntaxException in project lucida by claritylab.
the class QuestionInterpreter method loadPatterns.
/**
* Loads the question patterns from a directory of PROPERTY files. Each file
* contains a list of pattern descriptors. Their format is described in the
* documentation of the class <code>QuestionPattern</code>.
*
* @param dir directory of the question patterns
* @return true, iff the question patterns were loaded successfully
*/
public static boolean loadPatterns(String dir) {
File[] files = FileUtils.getFiles(dir);
try {
BufferedReader in;
String prop, line;
for (File file : files) {
prop = file.getName();
in = new BufferedReader(new FileReader(file));
while (in.ready()) {
line = in.readLine().trim();
if (line.length() == 0 || line.startsWith("//"))
// skip blank lines and comments
continue;
if (line.startsWith("QUESTION_TEMPLATE")) {
// add question template
String[] tokens = line.split("\\s+", 2);
if (tokens.length > 1)
questionTemplates.put(prop, tokens[1]);
} else if (line.startsWith("ANSWER_TEMPLATE")) {
// add answer template
String[] tokens = line.split("\\s+", 2);
if (tokens.length > 1)
answerTemplates.put(prop, tokens[1]);
} else {
try {
// add question pattern
questionPatterns.add(new QuestionPattern(line, prop));
// add keywords to the dictionary for prop
addKeywords(line, prop);
} catch (PatternSyntaxException pse) {
MsgPrinter.printErrorMsg("Problem loading pattern:\n" + prop + " " + line);
MsgPrinter.printErrorMsg(pse.getMessage());
}
}
}
in.close();
}
} catch (IOException e) {
return false;
}
return true;
}
use of java.util.regex.PatternSyntaxException in project jersey by jersey.
the class UriTemplateParser method parseName.
private int parseName(final CharacterIterator ci, int skipGroup) {
char c = consumeWhiteSpace(ci);
// Normal path param unless otherwise stated
char paramType = 'p';
StringBuilder nameBuffer = new StringBuilder();
// Look for query or matrix types
if (c == '?' || c == ';') {
paramType = c;
c = ci.next();
}
if (Character.isLetterOrDigit(c) || c == '_') {
// Template name character
nameBuffer.append(c);
} else {
throw new IllegalArgumentException(LocalizationMessages.ERROR_TEMPLATE_PARSER_ILLEGAL_CHAR_START_NAME(c, ci.pos(), template));
}
String nameRegexString = "";
while (true) {
c = ci.next();
// "\\{(\\w[-\\w\\.]*)
if (Character.isLetterOrDigit(c) || c == '_' || c == '-' || c == '.') {
// Template name character
nameBuffer.append(c);
} else if (c == ',' && paramType != 'p') {
// separator allowed for non-path parameter names
nameBuffer.append(c);
} else if (c == ':' && paramType == 'p') {
nameRegexString = parseRegex(ci);
break;
} else if (c == '}') {
break;
} else if (c == ' ') {
c = consumeWhiteSpace(ci);
if (c == ':') {
nameRegexString = parseRegex(ci);
break;
} else if (c == '}') {
break;
} else {
// Error
throw new IllegalArgumentException(LocalizationMessages.ERROR_TEMPLATE_PARSER_ILLEGAL_CHAR_AFTER_NAME(c, ci.pos(), template));
}
} else {
throw new IllegalArgumentException(LocalizationMessages.ERROR_TEMPLATE_PARSER_ILLEGAL_CHAR_PART_OF_NAME(c, ci.pos(), template));
}
}
String name = nameBuffer.toString();
Pattern namePattern;
try {
if (paramType == '?' || paramType == ';') {
String[] subNames = name.split(",\\s?");
// Build up the regex for each of these properties
StringBuilder regexBuilder = new StringBuilder(paramType == '?' ? "\\?" : ";");
String separator = paramType == '?' ? "\\&" : ";/\\?";
// Start a group because each parameter could repeat
// names.add("__" + (paramType == '?' ? "query" : "matrix"));
boolean first = true;
regexBuilder.append("(");
for (String subName : subNames) {
regexBuilder.append("(&?");
regexBuilder.append(subName);
regexBuilder.append("(=([^");
regexBuilder.append(separator);
regexBuilder.append("]*))?");
regexBuilder.append(")");
if (!first) {
regexBuilder.append("|");
}
names.add(subName);
groupCounts.add(first ? 5 : 3);
first = false;
}
// groupCounts.add(1);
skipGroup = 1;
// Knock of last bar
regexBuilder.append(")*");
namePattern = Pattern.compile(regexBuilder.toString());
// Make sure we display something useful
name = paramType + name;
} else {
names.add(name);
if (!nameRegexString.isEmpty()) {
numOfExplicitRegexes++;
}
namePattern = (nameRegexString.isEmpty()) ? TEMPLATE_VALUE_PATTERN : Pattern.compile(nameRegexString);
if (nameToPattern.containsKey(name)) {
if (!nameToPattern.get(name).equals(namePattern)) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_TEMPLATE_PARSER_NAME_MORE_THAN_ONCE(name, template));
}
} else {
nameToPattern.put(name, namePattern);
}
// Determine group count of pattern
Matcher m = namePattern.matcher("");
int g = m.groupCount();
groupCounts.add(1 + skipGroup);
skipGroup = g;
}
regex.append('(').append(namePattern).append(')');
normalizedTemplate.append('{').append(name).append('}');
} catch (PatternSyntaxException ex) {
throw new IllegalArgumentException(LocalizationMessages.ERROR_TEMPLATE_PARSER_INVALID_SYNTAX(nameRegexString, name, template), ex);
}
// Tell the next time through the loop how many to skip
return skipGroup;
}
use of java.util.regex.PatternSyntaxException in project OpenAM by OpenRock.
the class PWResetUserValidationModelImpl method isUserAttrValueValid.
/**
* Returns <code>true</code> if the entered user attr value
* is comprised of safe characters. It will return false
* and view bean will display an error message if it
* contains invalid characters
*
* @param orgDN organization DN.
* @param userAttrValue User enter data for user validation.
* @return <code>true</code> if entered data is valid.
*/
public boolean isUserAttrValueValid(String orgDN, String userAttrValue) {
String regexExpr = null;
boolean isValid = false;
try {
regexExpr = getAttributeValue(orgDN, PW_RESET_INVALIDCHAR_REGEX);
} catch (SSOException e) {
debug.warning("PWResetUserValidationModelImpl.isUserAttrValueValid", e);
errorMsg = getErrorString(e);
} catch (SMSException e) {
debug.error("PWResetUserValidationModelImpl.isUserAttrValueValid", e);
errorMsg = getErrorString(e);
}
// assume all characters are allowed when regex expression is not defined
if (regexExpr == null || regexExpr.isEmpty()) {
return true;
}
if (debug.messageEnabled()) {
debug.message("PWResetUserValidationModelImpl.isUserAttrValueValid: " + "using regular expression '" + regexExpr + "'");
}
try {
Pattern pattern = Pattern.compile(regexExpr);
Matcher matcher = pattern.matcher(userAttrValue);
boolean found = matcher.find();
if (found) {
errorMsg = getLocalizedString("userNotExists.message");
} else {
isValid = true;
}
} catch (PatternSyntaxException pse) {
debug.error("PWResetUserValidationModelImpl.isUserAttrValueValid: " + "expression's syntax is invalid '" + regexExpr + "'", pse);
errorMsg = getErrorString(pse);
}
return isValid;
}
Aggregations