All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cucumber.runtime.xstream.PatternConverter Maven / Gradle / Ivy

There is a newer version: 7.18.1
Show newest version
package cucumber.runtime.xstream;

import cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * Converts Strings of the form /hello/im to a {@link Pattern}, using a syntax
 * similar to regexp engines like Ruby, Perl and JavaScript. Flags:
 *
 * 
    *
  • d : UNIX_LINES
  • *
  • i : CASE_INSENSITIVE
  • *
  • x : COMMENTS
  • *
  • m : MULTILINE
  • *
  • l : LITERAL
  • *
  • s : DOTALL
  • *
  • u : UNICODE_CASE
  • *
  • c : CANON_EQ
  • *
*/ public class PatternConverter implements SingleValueConverter { private static final Pattern PATTERN_PATTERN = Pattern.compile("/(.*)/(.+)"); public String toString(Object o) { throw new UnsupportedOperationException(); } @Override public Object fromString(String pattern) { Matcher matcher = PATTERN_PATTERN.matcher(pattern); if (matcher.matches()) { return Pattern.compile(matcher.group(1), flags(matcher.group(2))); } else { return Pattern.compile(pattern); } } private int flags(String flags) { int result = 0; for (char c : flags.toCharArray()) { result |= flag(c); } return result; } private enum FLAG { d(Pattern.UNIX_LINES), i(Pattern.CASE_INSENSITIVE), x(Pattern.COMMENTS), m(Pattern.MULTILINE), l(Pattern.LITERAL), s(Pattern.DOTALL), u(Pattern.UNICODE_CASE), c(Pattern.CANON_EQ); private final int modifier; FLAG(int modifier) { this.modifier = modifier; } } private int flag(char c) { return FLAG.valueOf(String.valueOf(c)).modifier; } @Override public boolean canConvert(Class type) { return type.equals(Pattern.class); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy