use of org.apache.oro.text.regex.Perl5Compiler in project tdi-studio-se by Talend.
the class JavaRoutineSynchronizer method renamePigudfClass.
/*
* (non-Javadoc)
*
* @see org.talend.designer.codegen.AbstractRoutineSynchronizer#renamePigudfClass(org.talend.core.model.properties.
* RoutineItem)
*/
@Override
public void renamePigudfClass(PigudfItem pigudfItem, String oldLabel) {
if (pigudfItem == null) {
return;
}
String routineContent = new String(pigudfItem.getContent().getInnerContent());
String label = pigudfItem.getProperty().getLabel();
//
Perl5Matcher matcher = new Perl5Matcher();
Perl5Compiler compiler = new Perl5Compiler();
PatternMatcherInput patternMatcherInput = new PatternMatcherInput(routineContent);
//$NON-NLS-1$//$NON-NLS-2$
String regx = "public(\\s)+class(\\s)+" + oldLabel + "(\\s)(.+)\\{";
String extendsText = "";
try {
org.apache.oro.text.regex.Pattern pattern = compiler.compile(regx);
boolean contains = matcher.contains(patternMatcherInput, pattern);
if (contains) {
org.apache.oro.text.regex.MatchResult matchResult = matcher.getMatch();
extendsText = matchResult.group(matchResult.groups() - 1);
}
//$NON-NLS-1$
String regexp = "public(\\s)+class(\\s)+\\w+(\\s)\\{";
if (extendsText != null) {
extendsText = extendsText.trim();
//$NON-NLS-1$//$NON-NLS-2$
regexp = "public(\\s)+class(\\s)+\\w+(\\s)+" + extendsText + "(\\s)*\\{";
}
// rename class name
//$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
routineContent = routineContent.replaceFirst(regexp, "public class " + label + " " + extendsText + " {");
// rename constructor
String constructorRegx = "(\\s+)" + oldLabel + "(\\((.*)\\))";
String toReplace = "$1" + label + "$1$2";
pattern = compiler.compile(constructorRegx);
Perl5Substitution substitution = new Perl5Substitution(toReplace, Perl5Substitution.INTERPOLATE_ALL);
routineContent = Util.substitute(matcher, pattern, substitution, routineContent, Util.SUBSTITUTE_ALL);
} catch (MalformedPatternException e) {
ExceptionHandler.process(new Exception("Rename pigudf failed"));
}
pigudfItem.getContent().setInnerContent(routineContent.getBytes());
}
use of org.apache.oro.text.regex.Perl5Compiler in project tdi-studio-se by Talend.
the class MapDataDelegateHelper method matchExpression.
private boolean matchExpression(String regex, String expression) {
PatternCompiler compiler = new Perl5Compiler();
try {
//$NON-NLS-1$ //$NON-NLS-2$
Pattern pattern = compiler.compile("\\b(" + UpdateContextVariablesHelper.replaceSpecialChar(regex) + ")(\\b|\\_)");
PatternMatcher matcher = new Perl5Matcher();
((Perl5Matcher) matcher).setMultiline(true);
if (matcher.contains(expression, pattern)) {
return true;
}
} catch (MalformedPatternException e) {
//
}
return false;
}
use of org.apache.oro.text.regex.Perl5Compiler in project nutch by apache.
the class OutlinkExtractor method getOutlinks.
/**
* Extracts <code>Outlink</code> from given plain text and adds anchor to the
* extracted <code>Outlink</code>s
*
* @param plainText
* the plain text from wich URLs should be extracted.
* @param anchor
* the anchor of the url
*
* @return Array of <code>Outlink</code>s within found in plainText
*/
public static Outlink[] getOutlinks(final String plainText, String anchor, Configuration conf) {
long start = System.currentTimeMillis();
final List<Outlink> outlinks = new ArrayList<>();
try {
final PatternCompiler cp = new Perl5Compiler();
final Pattern pattern = cp.compile(URL_PATTERN, Perl5Compiler.CASE_INSENSITIVE_MASK | Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.MULTILINE_MASK);
final PatternMatcher matcher = new Perl5Matcher();
final PatternMatcherInput input = new PatternMatcherInput(plainText);
MatchResult result;
String url;
// loop the matches
while (matcher.contains(input, pattern)) {
// do not unnecessarily hit this limit.)
if (System.currentTimeMillis() - start >= 60000L) {
if (LOG.isWarnEnabled()) {
LOG.warn("Time limit exceeded for getOutLinks");
}
break;
}
result = matcher.getMatch();
url = result.group(0);
try {
outlinks.add(new Outlink(url, anchor));
} catch (MalformedURLException mue) {
LOG.warn("Invalid url: '" + url + "', skipping.");
}
}
} catch (Exception ex) {
// on
if (LOG.isErrorEnabled()) {
LOG.error("getOutlinks", ex);
}
}
final Outlink[] retval;
// create array of the Outlinks
if (outlinks != null && outlinks.size() > 0) {
retval = outlinks.toArray(new Outlink[0]);
} else {
retval = new Outlink[0];
}
return retval;
}
use of org.apache.oro.text.regex.Perl5Compiler in project jmeter by apache.
the class ReplaceFunctionsWithStrings method transformValue.
@Override
public JMeterProperty transformValue(JMeterProperty prop) throws InvalidVariableException {
PatternMatcher pm = JMeterUtils.getMatcher();
PatternCompiler compiler = new Perl5Compiler();
String input = prop.getStringValue();
if (input == null) {
return prop;
}
for (Map.Entry<String, String> entry : getVariables().entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
if (regexMatch) {
try {
Pattern pattern = compiler.compile(constructPattern(value));
input = Util.substitute(pm, pattern, new StringSubstitution(FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX), input, Util.SUBSTITUTE_ALL);
} catch (MalformedPatternException e) {
log.warn("Malformed pattern: {}", value);
}
} else {
input = StringUtilities.substitute(input, value, FUNCTION_REF_PREFIX + key + FUNCTION_REF_SUFFIX);
}
}
return new StringProperty(prop.getName(), input);
}
use of org.apache.oro.text.regex.Perl5Compiler in project otter by alibaba.
the class ConfigHelperTest method testWildCard.
@Test
public void testWildCard() {
PatternMatcher matcher = new Perl5Matcher();
Pattern pattern = null;
PatternCompiler pc = new Perl5Compiler();
try {
pattern = pc.compile("havana_us_.*", Perl5Compiler.DEFAULT_MASK);
} catch (MalformedPatternException e) {
throw new ConfigException(e);
}
boolean ismatch = matcher.matches("havana_us_0001", pattern);
System.out.println(ismatch);
}
Aggregations