use of org.apache.oro.text.regex.PatternMatcher in project tdi-studio-se by Talend.
the class MapperComponent method hasOrRenameData.
/**
*
* DOC amaumont Comment method "hasOrRenameData".
*
* @param oldName
* @param newName can be null if <code>renameAction</code> is false
* @param renameAction true to rename in all expressions, false to get boolean if present in one of the expressions
* @return
*/
@Override
protected boolean hasOrRenameData(String oldName, String newName, boolean renameAction) {
if (oldName == null || newName == null && renameAction) {
throw new NullPointerException();
}
if (externalData != null) {
List<ExternalMapperTable> tables = new ArrayList<ExternalMapperTable>(externalData.getInputTables());
tables.addAll(externalData.getOutputTables());
if (externalData.getVarsTables() != null) {
tables.addAll(externalData.getVarsTables());
}
for (ExternalMapperTable table : tables) {
if (table.getExpressionFilter() != null) {
Pattern pattern = getRenamePattern(oldName);
if (pattern != null) {
PatternMatcher matcher = new Perl5Matcher();
((Perl5Matcher) matcher).setMultiline(true);
if (renameAction) {
Perl5Substitution substitution = getRenameSubstitution(newName);
String expression = renameDataIntoExpression(pattern, matcher, substitution, table.getExpressionFilter());
table.setExpressionFilter(expression);
} else {
if (hasDataIntoExpression(pattern, matcher, table.getExpressionFilter())) {
return true;
}
}
}
}
List<ExternalMapperTableEntry> metadataTableEntries = table.getMetadataTableEntries();
if (metadataTableEntries != null) {
// loop on all entries of current table
for (ExternalMapperTableEntry entry : metadataTableEntries) {
if (hasOrRenameEntry(entry, oldName, newName, renameAction)) {
// existed
return true;
}
}
// for (ExternalMapperTableEntry entry : metadataTableEntries) {
}
if (table.getConstraintTableEntries() != null) {
for (ExternalMapperTableEntry entry : table.getConstraintTableEntries()) {
if (hasOrRenameEntry(entry, oldName, newName, renameAction)) {
// existed
return true;
}
}
}
}
}
return false;
}
use of org.apache.oro.text.regex.PatternMatcher in project tdi-studio-se by Talend.
the class NodeQueryCheckUtil method apacheRegexMatch.
/**
* See bug 5836. java.util.regex works too slow here. Use apache oro regex library instead.
* <p>
* DOC xye Comment method "apacheRegexMatch".
*
* @param patternString
* @param flag
* @param input
* @return
*/
private static boolean apacheRegexMatch(final String patternString, final int flag, final String input) {
PatternCompiler pc = new Perl5Compiler();
org.apache.oro.text.regex.Pattern pattern = null;
try {
pattern = pc.compile(patternString, flag);
PatternMatcher columnMatcher = new Perl5Matcher();
return columnMatcher.matches(input, pattern);
} catch (MalformedPatternException e) {
return false;
}
}
use of org.apache.oro.text.regex.PatternMatcher in project jmeter by apache.
the class RegexExtractor method initTemplate.
private void initTemplate() {
if (template != null) {
return;
}
// Contains Strings and Integers
List<Object> combined = new ArrayList<>();
String rawTemplate = getTemplate();
PatternMatcher matcher = JMeterUtils.getMatcher();
Pattern templatePattern = // $NON-NLS-1$
JMeterUtils.getPatternCache().getPattern(// $NON-NLS-1$
"\\$(\\d+)\\$", Perl5Compiler.READ_ONLY_MASK & Perl5Compiler.SINGLELINE_MASK);
if (log.isDebugEnabled()) {
log.debug("Pattern = '{}', template = '{}'", templatePattern.getPattern(), rawTemplate);
}
int beginOffset = 0;
MatchResult currentResult;
PatternMatcherInput pinput = new PatternMatcherInput(rawTemplate);
while (matcher.contains(pinput, templatePattern)) {
currentResult = matcher.getMatch();
final int beginMatch = currentResult.beginOffset(0);
if (beginMatch > beginOffset) {
// string is not empty
combined.add(rawTemplate.substring(beginOffset, beginMatch));
}
// add match as Integer
combined.add(Integer.valueOf(currentResult.group(1)));
beginOffset = currentResult.endOffset(0);
}
if (beginOffset < rawTemplate.length()) {
// trailing string is not empty
combined.add(rawTemplate.substring(beginOffset, rawTemplate.length()));
}
if (log.isDebugEnabled()) {
log.debug("Template item count: {}", combined.size());
int i = 0;
for (Object o : combined) {
log.debug("Template item-{}: {} '{}'", i++, o.getClass(), o);
}
}
template = combined;
}
use of org.apache.oro.text.regex.PatternMatcher in project jmeter by apache.
the class RegexFunction method generateTemplate.
private Object[] generateTemplate(String rawTemplate) {
List<String> pieces = new ArrayList<>();
// String or Integer
List<Object> combined = new LinkedList<>();
PatternMatcher matcher = JMeterUtils.getMatcher();
Util.split(pieces, matcher, templatePattern, rawTemplate);
PatternMatcherInput input = new PatternMatcherInput(rawTemplate);
boolean startsWith = isFirstElementGroup(rawTemplate);
if (startsWith) {
// Remove initial empty entry
pieces.remove(0);
}
Iterator<String> iter = pieces.iterator();
while (iter.hasNext()) {
boolean matchExists = matcher.contains(input, templatePattern);
if (startsWith) {
if (matchExists) {
combined.add(Integer.valueOf(matcher.getMatch().group(1)));
}
combined.add(iter.next());
} else {
combined.add(iter.next());
if (matchExists) {
combined.add(Integer.valueOf(matcher.getMatch().group(1)));
}
}
}
if (matcher.contains(input, templatePattern)) {
combined.add(Integer.valueOf(matcher.getMatch().group(1)));
}
return combined.toArray();
}
use of org.apache.oro.text.regex.PatternMatcher in project jmeter by apache.
the class RegexFunction method execute.
/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
//$NON-NLS-1$
String valueIndex = "";
//$NON-NLS-1$
String defaultValue = "";
//$NON-NLS-1$
String between = "";
//$NON-NLS-1$
String name = "";
//$NON-NLS-1$
String inputVariable = "";
Pattern searchPattern;
Object[] tmplt;
try {
searchPattern = JMeterUtils.getPatternCache().getPattern(((CompoundVariable) values[0]).execute(), Perl5Compiler.READ_ONLY_MASK);
tmplt = generateTemplate(((CompoundVariable) values[1]).execute());
if (values.length > 2) {
valueIndex = ((CompoundVariable) values[2]).execute();
}
if (valueIndex.length() == 0) {
//$NON-NLS-1$
valueIndex = "1";
}
if (values.length > 3) {
between = ((CompoundVariable) values[3]).execute();
}
if (values.length > 4) {
String dv = ((CompoundVariable) values[4]).execute();
if (dv.length() != 0) {
defaultValue = dv;
}
}
if (values.length > 5) {
name = ((CompoundVariable) values[5]).execute();
}
if (values.length > 6) {
inputVariable = ((CompoundVariable) values[6]).execute();
}
} catch (MalformedCachePatternException e) {
log.error("Malformed cache pattern:" + values[0], e);
throw new InvalidVariableException("Malformed cache pattern:" + values[0], e);
}
// Relatively expensive operation, so do it once
JMeterVariables vars = getVariables();
if (vars == null) {
// Can happen if called during test closedown
return defaultValue;
}
if (name.length() > 0) {
vars.put(name, defaultValue);
}
String textToMatch = null;
if (inputVariable.length() > 0) {
textToMatch = vars.get(inputVariable);
} else if (previousResult != null) {
textToMatch = previousResult.getResponseDataAsString();
}
if (textToMatch == null || textToMatch.length() == 0) {
return defaultValue;
}
List<MatchResult> collectAllMatches = new ArrayList<>();
try {
PatternMatcher matcher = JMeterUtils.getMatcher();
PatternMatcherInput input = new PatternMatcherInput(textToMatch);
while (matcher.contains(input, searchPattern)) {
MatchResult match = matcher.getMatch();
if (match != null) {
collectAllMatches.add(match);
}
}
} finally {
if (name.length() > 0) {
//$NON-NLS-1$
vars.put(name + "_matchNr", Integer.toString(collectAllMatches.size()));
}
}
if (collectAllMatches.isEmpty()) {
return defaultValue;
}
if (valueIndex.equals(ALL)) {
StringBuilder value = new StringBuilder();
Iterator<MatchResult> it = collectAllMatches.iterator();
boolean first = true;
while (it.hasNext()) {
if (!first) {
value.append(between);
} else {
first = false;
}
value.append(generateResult(it.next(), name, tmplt, vars));
}
return value.toString();
} else if (valueIndex.equals(RAND)) {
MatchResult result = collectAllMatches.get(ThreadLocalRandom.current().nextInt(collectAllMatches.size()));
return generateResult(result, name, tmplt, vars);
} else {
try {
int index = Integer.parseInt(valueIndex) - 1;
if (index >= collectAllMatches.size()) {
return defaultValue;
}
MatchResult result = collectAllMatches.get(index);
return generateResult(result, name, tmplt, vars);
} catch (NumberFormatException e) {
float ratio = Float.parseFloat(valueIndex);
MatchResult result = collectAllMatches.get((int) (collectAllMatches.size() * ratio + .5) - 1);
return generateResult(result, name, tmplt, vars);
}
}
}
Aggregations