/** * Creates a new instance of the class. Required by Log4J2. * @param config the configuration * @param options the options * @return a new instance, or {@code null} if the options are invalid */ public static ColorConverter newInstance(Configuration config, String[] options) { if (options.length < 1) { LOGGER.error("Incorrect number of options on style. " + "Expected at least 1, received {}", options.length); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on style"); return null; } PatternParser parser = PatternLayout.createPatternParser(config); List<PatternFormatter> formatters = parser.parse(options[0]); AnsiElement element = (options.length != 1) ? ELEMENTS.get(options[1]) : null; return new ColorConverter(formatters, element); }
/** * Creates a list of PatternFormatter from the given configuration and options or null if no pattern is supplied. * * @param config A configuration. * @param options pattern options. * @return a list of PatternFormatter from the given configuration and options or null if no pattern is supplied. */ private static List<PatternFormatter> toPatternFormatterList(final Configuration config, final String[] options) { if (options.length == 0 || options[0] == null) { LOGGER.error("No pattern supplied on style for config=" + config); return null; } final PatternParser parser = PatternLayout.createPatternParser(config); if (parser == null) { LOGGER.error("No PatternParser created for config=" + config + ", options=" + Arrays.toString(options)); return null; } return parser.parse(options[0]); }
/** * Gets a new instance of the {@link MinecraftFormattingConverter} with the * specified options. * * @param config The current configuration * @param options The pattern options * @return The new instance * @see MinecraftFormattingConverter */ public static MinecraftFormattingConverter newInstance( Configuration config, String[] options ) { if ( options.length < 1 || options.length > 2 ) { LOGGER.error( "Incorrect number of options on minecraftFormatting. Expected at least 1, max 2 received " + options.length ); return null; } if ( options[0] == null ) { LOGGER.error( "No pattern supplied on minecraftFormatting" ); return null; } PatternParser parser = PatternLayout.createPatternParser( config ); List<PatternFormatter> formatters = parser.parse( options[0] ); boolean strip = options.length > 1 && "strip".equals( options[1] ); return new MinecraftFormattingConverter( formatters, strip ); }
/** * Gets an instance of the class. * * @param config * The current Configuration. * @param options * pattern options, may be null. * @return instance of class. */ public static VariablesNotEmptyReplacementConverter newInstance(final Configuration config, final String[] options) { if (options.length != 1) { LOGGER.error("Incorrect number of options on varsNotEmpty. Expected 1 received " + options.length); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on varsNotEmpty"); return null; } final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); return new VariablesNotEmptyReplacementConverter(formatters); }
/** * Gets an instance of the class. * * @param config The current Configuration. * @param options pattern options, may be null. If first element is "short", * only the first line of the throwable will be formatted. * @return instance of class. */ public static RegexReplacementConverter newInstance(final Configuration config, final String[] options) { if (options.length != 3) { LOGGER.error("Incorrect number of options on replace. Expected 3 received " + options.length); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on replace"); return null; } if (options[1] == null) { LOGGER.error("No regular expression supplied on replace"); return null; } if (options[2] == null) { LOGGER.error("No substitution supplied on replace"); return null; } final Pattern p = Pattern.compile(options[1]); final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); return new RegexReplacementConverter(formatters, p, options[2]); }
/** * Gets an instance of the class. * * @param config The current Configuration. * @param options pattern options, may be null. If first element is "short", only the first line of the * throwable will be formatted. * @return instance of class. */ public static HighlightConverter newInstance(final Configuration config, final String[] options) { if (options.length < 1) { LOGGER.error("Incorrect number of options on style. Expected at least 1, received " + options.length); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on style"); return null; } final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); final boolean disableAnsi = Arrays.toString(options).contains(PatternParser.DISABLE_ANSI + "=true"); final boolean noConsoleNoAnsi = Arrays.toString(options).contains(PatternParser.NO_CONSOLE_NO_ANSI + "=true"); final boolean hideAnsi = disableAnsi || (noConsoleNoAnsi && System.console() == null); return new HighlightConverter(formatters, createLevelStyleMap(options), hideAnsi); }
/** * Gets an instance of the class. * * @param config The current Configuration. * @param options pattern options, an array of three elements: pattern, testString, and substitution. * @return instance of class. */ public static EqualsReplacementConverter newInstance(final Configuration config, final String[] options) { if (options.length != 3) { LOGGER.error("Incorrect number of options on equals. Expected 3 received " + options.length); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on equals"); return null; } if (options[1] == null) { LOGGER.error("No test string supplied on equals"); return null; } if (options[2] == null) { LOGGER.error("No substitution supplied on equals"); return null; } final String p = options[1]; final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); return new EqualsReplacementConverter(formatters, p, options[2], parser); }
final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); return new EqualsIgnoreCaseReplacementConverter(formatters, p, options[2], parser);
return null; final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); final String style = AnsiEscape.createSequence(options[1].split(Patterns.COMMA_SEPARATOR));
/** * Gets an instance of the class. * * @param config The current Configuration. * @param options pattern options, an array of two elements: pattern, max length (defaults to 100 on invalid value). * @return instance of class. */ public static MaxLengthConverter newInstance(final Configuration config, final String[] options) { if (options.length != 2) { LOGGER.error("Incorrect number of options on maxLength: expected 2 received {}: {}", options.length, options); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on maxLength"); return null; } if (options[1] == null) { LOGGER.error("No length supplied on maxLength"); return null; } final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); return new MaxLengthConverter(formatters, AbstractAppender.parseInt(options[1], 100)); }
/** * Creates an EncodingPatternConverter using a pattern string and an optional escape format. * * @param config the current Configuration * @param options first option is the nested pattern format; second option is the escape format (optional) * @return instance of pattern converter. */ public static EncodingPatternConverter newInstance(final Configuration config, final String[] options) { if (options.length > 2 || options.length == 0) { LOGGER.error("Incorrect number of options on escape. Expected 1 or 2, but received {}", options.length); return null; } if (options[0] == null) { LOGGER.error("No pattern supplied on escape"); return null; } final EscapeFormat escapeFormat = options.length < 2 ? EscapeFormat.HTML : EnglishEnums.valueOf(EscapeFormat.class, options[1], EscapeFormat.HTML); final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> formatters = parser.parse(options[0]); return new EncodingPatternConverter(formatters, escapeFormat); }
private MarkerPatternSelector(final PatternMatch[] properties, final String defaultPattern, final boolean alwaysWriteExceptions, final boolean disableAnsi, final boolean noConsoleNoAnsi, final Configuration config) { final PatternParser parser = PatternLayout.createPatternParser(config); for (final PatternMatch property : properties) { try { final List<PatternFormatter> list = parser.parse(property.getPattern(), alwaysWriteExceptions, disableAnsi, noConsoleNoAnsi); formatterMap.put(property.getKey(), list.toArray(new PatternFormatter[list.size()])); patternMap.put(property.getKey(), property.getPattern()); } catch (final RuntimeException ex) { throw new IllegalArgumentException("Cannot parse pattern '" + property.getPattern() + "'", ex); } } try { final List<PatternFormatter> list = parser.parse(defaultPattern, alwaysWriteExceptions, disableAnsi, noConsoleNoAnsi); defaultFormatters = list.toArray(new PatternFormatter[list.size()]); this.defaultPattern = defaultPattern; } catch (final RuntimeException ex) { throw new IllegalArgumentException("Cannot parse pattern '" + defaultPattern + "'", ex); } }
@Override public Serializer build() { if (Strings.isEmpty(pattern) && Strings.isEmpty(defaultPattern)) { return null; } if (patternSelector == null) { try { final PatternParser parser = createPatternParser(configuration); final List<PatternFormatter> list = parser.parse(pattern == null ? defaultPattern : pattern, alwaysWriteExceptions, disableAnsi, noConsoleNoAnsi); final PatternFormatter[] formatters = list.toArray(new PatternFormatter[0]); return new PatternSerializer(formatters, replace); } catch (final RuntimeException ex) { throw new IllegalArgumentException("Cannot parse pattern '" + pattern + "'", ex); } } return new PatternSelectorSerializer(patternSelector, replace); }
/** * @deprecated Use {@link #newBuilder()} instead. This will be private in a future version. */ @Deprecated public ScriptPatternSelector(final AbstractScript script, final PatternMatch[] properties, final String defaultPattern, final boolean alwaysWriteExceptions, final boolean disableAnsi, final boolean noConsoleNoAnsi, final Configuration config) { this.script = script; this.configuration = config; if (!(script instanceof ScriptRef)) { config.getScriptManager().addScript(script); } final PatternParser parser = PatternLayout.createPatternParser(config); for (final PatternMatch property : properties) { try { final List<PatternFormatter> list = parser.parse(property.getPattern(), alwaysWriteExceptions, disableAnsi, noConsoleNoAnsi); formatterMap.put(property.getKey(), list.toArray(new PatternFormatter[list.size()])); patternMap.put(property.getKey(), property.getPattern()); } catch (final RuntimeException ex) { throw new IllegalArgumentException("Cannot parse pattern '" + property.getPattern() + "'", ex); } } try { final List<PatternFormatter> list = parser.parse(defaultPattern, alwaysWriteExceptions, disableAnsi, noConsoleNoAnsi); defaultFormatters = list.toArray(new PatternFormatter[list.size()]); this.defaultPattern = defaultPattern; } catch (final RuntimeException ex) { throw new IllegalArgumentException("Cannot parse pattern '" + defaultPattern + "'", ex); } }
final PatternParser parser = PatternLayout.createPatternParser(config); final List<PatternFormatter> parsedFormatters = parser.parse(this.options.getSuffix());