use of org.apache.oro.text.regex.Pattern in project ofbiz-framework by apache.
the class SeoConfigUtil method init.
/**
* Initialize url regular express configuration.
*/
public static void init() {
FileInputStream configFileIS = null;
String result = "success";
seoPatterns = new HashMap<String, Pattern>();
seoReplacements = new HashMap<String, String>();
forwardReplacements = new HashMap<String, String>();
forwardResponseCodes = new HashMap<String, Integer>();
userExceptionPatterns = new LinkedList<Pattern>();
specialProductIds = new HashMap<String, String>();
charFilters = new HashMap<String, String>();
try {
URL seoConfigFilename = UtilURL.fromResource(SEO_CONFIG_FILENAME);
Document configDoc = UtilXml.readXmlDocument(seoConfigFilename, false);
Element rootElement = configDoc.getDocumentElement();
String regexIfMatch = UtilXml.childElementValue(rootElement, ELEMENT_REGEXPIFMATCH, DEFAULT_REGEXP);
Debug.logInfo("Parsing " + regexIfMatch, module);
try {
regexpIfMatch = perlCompiler.compile(regexIfMatch, Perl5Compiler.READ_ONLY_MASK);
} catch (MalformedPatternException e1) {
Debug.logWarning(e1, "Error while parsing " + regexIfMatch, module);
}
// parse category-url element
try {
Element categoryUrlElement = UtilXml.firstChildElement(rootElement, ELEMENT_CATEGORY_URL);
Debug.logInfo("Parsing " + ELEMENT_CATEGORY_URL + " [" + (categoryUrlElement != null) + "]:", module);
if (categoryUrlElement != null) {
String enableCategoryUrlValue = UtilXml.childElementValue(categoryUrlElement, ELEMENT_VALUE, DEFAULT_CATEGORY_URL_VALUE);
if (DEFAULT_CATEGORY_URL_VALUE.equalsIgnoreCase(enableCategoryUrlValue)) {
categoryUrlEnabled = true;
} else {
categoryUrlEnabled = false;
}
if (categoryUrlEnabled) {
String allowedContextValue = UtilXml.childElementValue(categoryUrlElement, ELEMENT_ALLOWED_CONTEXT_PATHS, null);
allowedContextPaths = new HashSet<String>();
if (UtilValidate.isNotEmpty(allowedContextValue)) {
List<String> allowedContextPathList = StringUtil.split(allowedContextValue, ALLOWED_CONTEXT_PATHS_SEPERATOR);
for (String path : allowedContextPathList) {
if (UtilValidate.isNotEmpty(path)) {
path = path.trim();
if (!allowedContextPaths.contains(path)) {
allowedContextPaths.add(path);
Debug.logInfo(" " + ELEMENT_ALLOWED_CONTEXT_PATHS + ": " + path, module);
}
}
}
}
String categoryNameValue = UtilXml.childElementValue(categoryUrlElement, ELEMENT_CATEGORY_NAME, DEFAULT_CATEGORY_NAME_VALUE);
if (DEFAULT_CATEGORY_NAME_VALUE.equalsIgnoreCase(categoryNameValue)) {
categoryNameEnabled = false;
} else {
categoryNameEnabled = true;
}
Debug.logInfo(" " + ELEMENT_CATEGORY_NAME + ": " + categoryNameEnabled, module);
categoryUrlSuffix = UtilXml.childElementValue(categoryUrlElement, ELEMENT_CATEGORY_URL_SUFFIX, null);
if (UtilValidate.isNotEmpty(categoryUrlSuffix)) {
categoryUrlSuffix = categoryUrlSuffix.trim();
if (categoryUrlSuffix.contains("/")) {
categoryUrlSuffix = null;
}
}
Debug.logInfo(" " + ELEMENT_CATEGORY_URL_SUFFIX + ": " + categoryUrlSuffix, module);
}
}
} catch (NullPointerException e) {
// no "category-url" element
Debug.logWarning("No category-url element found in " + seoConfigFilename.toString(), module);
}
// parse jsessionid element
try {
Element jSessionId = UtilXml.firstChildElement(rootElement, ELEMENT_JSESSIONID);
Debug.logInfo("Parsing " + ELEMENT_JSESSIONID + " [" + (jSessionId != null) + "]:", module);
if (jSessionId != null) {
Element anonymous = UtilXml.firstChildElement(jSessionId, ELEMENT_ANONYMOUS);
if (anonymous != null) {
String anonymousValue = UtilXml.childElementValue(anonymous, ELEMENT_VALUE, DEFAULT_ANONYMOUS_VALUE);
if (DEFAULT_ANONYMOUS_VALUE.equalsIgnoreCase(anonymousValue)) {
jSessionIdAnonEnabled = false;
} else {
jSessionIdAnonEnabled = true;
}
} else {
jSessionIdAnonEnabled = Boolean.valueOf(DEFAULT_ANONYMOUS_VALUE).booleanValue();
}
Debug.logInfo(" " + ELEMENT_ANONYMOUS + ": " + jSessionIdAnonEnabled, module);
Element user = UtilXml.firstChildElement(jSessionId, ELEMENT_USER);
if (user != null) {
String userValue = UtilXml.childElementValue(user, ELEMENT_VALUE, DEFAULT_USER_VALUE);
if (DEFAULT_USER_VALUE.equalsIgnoreCase(userValue)) {
jSessionIdUserEnabled = false;
} else {
jSessionIdUserEnabled = true;
}
Element exceptions = UtilXml.firstChildElement(user, ELEMENT_EXCEPTIONS);
if (exceptions != null) {
Debug.logInfo(" " + ELEMENT_EXCEPTIONS + ": ", module);
List<? extends Element> exceptionUrlPatterns = UtilXml.childElementList(exceptions, ELEMENT_URLPATTERN);
for (int i = 0; i < exceptionUrlPatterns.size(); i++) {
Element element = exceptionUrlPatterns.get(i);
String urlpattern = element.getTextContent();
if (UtilValidate.isNotEmpty(urlpattern)) {
try {
Pattern pattern = perlCompiler.compile(urlpattern, Perl5Compiler.READ_ONLY_MASK);
userExceptionPatterns.add(pattern);
Debug.logInfo(" " + ELEMENT_URLPATTERN + ": " + urlpattern, module);
} catch (MalformedPatternException e) {
Debug.logWarning("Can NOT parse " + urlpattern + " in element " + ELEMENT_URLPATTERN + " of " + ELEMENT_EXCEPTIONS + ". Error: " + e.getMessage(), module);
}
}
}
}
} else {
jSessionIdUserEnabled = Boolean.valueOf(DEFAULT_USER_VALUE).booleanValue();
}
Debug.logInfo(" " + ELEMENT_USER + ": " + jSessionIdUserEnabled, module);
}
} catch (NullPointerException e) {
Debug.logWarning("No jsessionid element found in " + seoConfigFilename.toString(), module);
}
// parse url-config elements
try {
NodeList configs = rootElement.getElementsByTagName(ELEMENT_URL_CONFIG);
Debug.logInfo("Parsing " + ELEMENT_URL_CONFIG, module);
int length = configs.getLength();
for (int j = 0; j < length; j++) {
Element config = (Element) configs.item(j);
String urlpattern = UtilXml.childElementValue(config, ELEMENT_URLPATTERN, null);
if (UtilValidate.isEmpty(urlpattern)) {
continue;
}
Debug.logInfo(" " + ELEMENT_URLPATTERN + ": " + urlpattern, module);
Pattern pattern;
try {
pattern = perlCompiler.compile(urlpattern, Perl5Compiler.READ_ONLY_MASK);
seoPatterns.put(urlpattern, pattern);
} catch (MalformedPatternException e) {
Debug.logWarning("Error while creating parttern for seo url-pattern: " + urlpattern, module);
continue;
}
// construct seo patterns
Element seo = UtilXml.firstChildElement(config, ELEMENT_SEO);
if (UtilValidate.isNotEmpty(seo)) {
String replacement = UtilXml.childElementValue(seo, ELEMENT_REPLACEMENT, null);
if (UtilValidate.isNotEmpty(replacement)) {
seoReplacements.put(urlpattern, replacement);
Debug.logInfo(" " + ELEMENT_SEO + " " + ELEMENT_REPLACEMENT + ": " + replacement, module);
}
}
// construct forward patterns
Element forward = UtilXml.firstChildElement(config, ELEMENT_FORWARD);
if (UtilValidate.isNotEmpty(forward)) {
String replacement = UtilXml.childElementValue(forward, ELEMENT_REPLACEMENT, null);
String responseCode = UtilXml.childElementValue(forward, ELEMENT_RESPONSECODE, String.valueOf(DEFAULT_RESPONSECODE));
if (UtilValidate.isNotEmpty(replacement)) {
forwardReplacements.put(urlpattern, replacement);
Debug.logInfo(" " + ELEMENT_FORWARD + " " + ELEMENT_REPLACEMENT + ": " + replacement, module);
if (UtilValidate.isNotEmpty(responseCode)) {
Integer responseCodeInt = DEFAULT_RESPONSECODE;
try {
responseCodeInt = Integer.valueOf(responseCode);
} catch (NumberFormatException nfe) {
Debug.logWarning(nfe, "Error while parsing response code number: " + responseCode, module);
}
forwardResponseCodes.put(urlpattern, responseCodeInt);
Debug.logInfo(" " + ELEMENT_FORWARD + " " + ELEMENT_RESPONSECODE + ": " + responseCodeInt, module);
}
}
}
}
} catch (NullPointerException e) {
// no "url-config" element
Debug.logWarning("No " + ELEMENT_URL_CONFIG + " element found in " + seoConfigFilename.toString(), module);
}
// parse char-filters elements
try {
NodeList nameFilterNodes = rootElement.getElementsByTagName(ELEMENT_CHAR_FILTER);
Debug.logInfo("Parsing " + ELEMENT_CHAR_FILTER + ": ", module);
int length = nameFilterNodes.getLength();
for (int i = 0; i < length; i++) {
Element element = (Element) nameFilterNodes.item(i);
String charaterPattern = UtilXml.childElementValue(element, ELEMENT_CHARACTER_PATTERN, null);
String replacement = UtilXml.childElementValue(element, ELEMENT_REPLACEMENT, null);
if (UtilValidate.isNotEmpty(charaterPattern) && UtilValidate.isNotEmpty(replacement)) {
try {
perlCompiler.compile(charaterPattern, Perl5Compiler.READ_ONLY_MASK);
charFilters.put(charaterPattern, replacement);
Debug.logInfo(" " + ELEMENT_CHARACTER_PATTERN + ": " + charaterPattern, module);
Debug.logInfo(" " + ELEMENT_REPLACEMENT + ": " + replacement, module);
} catch (MalformedPatternException e) {
// skip this filter (character-pattern replacement) if any error happened
Debug.logWarning(e, "Error while parsing " + ELEMENT_CHARACTER_PATTERN + ": " + charaterPattern, module);
}
}
}
} catch (NullPointerException e) {
// no "char-filters" element
Debug.logWarning("No " + ELEMENT_CHAR_FILTER + " element found in " + seoConfigFilename.toString(), module);
}
} catch (SAXException e) {
result = "error";
Debug.logError(e, module);
} catch (ParserConfigurationException e) {
result = "error";
Debug.logError(e, module);
} catch (IOException e) {
result = "error";
Debug.logError(e, module);
} finally {
if (configFileIS != null) {
try {
configFileIS.close();
} catch (IOException e) {
result = "error";
Debug.logError(e, module);
}
}
}
if (seoReplacements.keySet().isEmpty()) {
useUrlRegexp = false;
} else {
useUrlRegexp = true;
}
if ("success".equals(result)) {
isInitialed = true;
}
}
use of org.apache.oro.text.regex.Pattern in project evosuite by EvoSuite.
the class Perl5Matcher_Matches method executeFunction.
@Override
public Object executeFunction() {
// Perl5Matcher conc_matcher = (Perl5Matcher) this.getConcReceiver();
// NonNullReference symb_matcher = (NonNullReference) this
// .getSymbReceiver();
boolean res = this.getConcBooleanRetVal();
ReferenceConstant symb_string_ref = (ReferenceConstant) this.getSymbArgument(0);
// Reference symb_pattern_ref = this.getSymbArgument(1);
String conc_string = (String) this.getConcArgument(0);
Pattern conc_pattern = (Pattern) this.getConcArgument(1);
StringValue symb_string_value = env.heap.getField(org.evosuite.symbolic.vm.regex.Types.JAVA_LANG_STRING, SymbolicHeap.$STRING_VALUE, conc_string, symb_string_ref, conc_string);
if (symb_string_value != null && symb_string_value.containsSymbolicVariable()) {
int concrete_value = res ? 1 : 0;
String pattern_str = conc_pattern.getPattern();
StringConstant symb_pattern_value = ExpressionFactory.buildNewStringConstant(pattern_str);
StringBinaryComparison strComp = new StringBinaryComparison(symb_pattern_value, Operator.APACHE_ORO_PATTERN_MATCHES, symb_string_value, (long) concrete_value);
return strComp;
} else {
return this.getSymbIntegerRetVal();
}
}
use of org.apache.oro.text.regex.Pattern in project evosuite by EvoSuite.
the class ExpressionExecutor method visit.
@Override
public Object visit(StringBinaryComparison n, Void arg) {
String first = (String) n.getLeftOperand().accept(this, null);
String second = (String) n.getRightOperand().accept(this, null);
Operator op = n.getOperator();
switch(op) {
case EQUALSIGNORECASE:
return first.equalsIgnoreCase(second) ? TRUE_VALUE : FALSE_VALUE;
case EQUALS:
return first.equals(second) ? TRUE_VALUE : FALSE_VALUE;
case ENDSWITH:
return first.endsWith(second) ? TRUE_VALUE : FALSE_VALUE;
case CONTAINS:
return first.contains(second) ? TRUE_VALUE : FALSE_VALUE;
case PATTERNMATCHES:
return second.matches(first) ? TRUE_VALUE : FALSE_VALUE;
case APACHE_ORO_PATTERN_MATCHES:
{
Perl5Matcher matcher = new Perl5Matcher();
Perl5Compiler compiler = new Perl5Compiler();
Pattern pattern;
try {
pattern = compiler.compile(first);
} catch (MalformedPatternException e) {
throw new RuntimeException(e);
}
return matcher.matches(second, pattern) ? TRUE_VALUE : FALSE_VALUE;
}
default:
log.warn("StringComparison: unimplemented operator!" + op);
return null;
}
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class SessionFilter method getIpAddress.
protected String getIpAddress(String logLine) {
Pattern incIp = JMeterUtils.getPatternCache().getPattern("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", Perl5Compiler.READ_ONLY_MASK | Perl5Compiler.SINGLELINE_MASK);
Perl5Matcher matcher = JMeterUtils.getMatcher();
matcher.contains(logLine, incIp);
return matcher.getMatch().group(0);
}
use of org.apache.oro.text.regex.Pattern in project jmeter by apache.
the class RegexExtractor method process.
/**
* Parses the response data using regular expressions and saving the results
* into variables for use later in the test.
*
* @see org.apache.jmeter.processor.PostProcessor#process()
*/
@Override
public void process() {
initTemplate();
JMeterContext context = getThreadContext();
SampleResult previousResult = context.getPreviousResult();
if (previousResult == null) {
return;
}
log.debug("RegexExtractor processing result");
// Fetch some variables
JMeterVariables vars = context.getVariables();
String refName = getRefName();
int matchNumber = getMatchNumber();
final String defaultValue = getDefaultValue();
if (defaultValue.length() > 0 || isEmptyDefaultValue()) {
// Only replace default if it is provided or empty default value is explicitly requested
vars.put(refName, defaultValue);
}
Perl5Matcher matcher = JMeterUtils.getMatcher();
String regex = getRegex();
Pattern pattern = null;
try {
pattern = JMeterUtils.getPatternCache().getPattern(regex, Perl5Compiler.READ_ONLY_MASK);
List<MatchResult> matches = processMatches(pattern, regex, previousResult, matchNumber, vars);
int prevCount = 0;
String prevString = vars.get(refName + REF_MATCH_NR);
if (prevString != null) {
// ensure old value is not left defined
vars.remove(refName + REF_MATCH_NR);
try {
prevCount = Integer.parseInt(prevString);
} catch (NumberFormatException nfe) {
log.warn("Could not parse number: '{}'", prevString);
}
}
// Number of refName_n variable sets to keep
int matchCount = 0;
try {
MatchResult match;
if (matchNumber >= 0) {
// Original match behaviour
match = getCorrectMatch(matches, matchNumber);
if (match != null) {
vars.put(refName, generateResult(match));
saveGroups(vars, refName, match);
} else {
// refname has already been set to the default (if present)
removeGroups(vars, refName);
}
} else // < 0 means we save all the matches
{
// remove any single matches
removeGroups(vars, refName);
matchCount = matches.size();
// Save the count
vars.put(refName + REF_MATCH_NR, Integer.toString(matchCount));
for (int i = 1; i <= matchCount; i++) {
match = getCorrectMatch(matches, i);
if (match != null) {
final String refName_n = refName + UNDERSCORE + i;
vars.put(refName_n, generateResult(match));
saveGroups(vars, refName_n, match);
}
}
}
// Remove any left-over variables
for (int i = matchCount + 1; i <= prevCount; i++) {
final String refName_n = refName + UNDERSCORE + i;
vars.remove(refName_n);
removeGroups(vars, refName_n);
}
} catch (RuntimeException e) {
log.warn("Error while generating result");
}
} catch (MalformedCachePatternException e) {
log.error("Error in pattern: '{}'", regex);
} finally {
JMeterUtils.clearMatcherMemory(matcher, pattern);
}
}
Aggregations