Skip to content

Commit

Permalink
m-m-m/base#8: scanner API changes for unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Dec 25, 2024
1 parent e670761 commit e27c323
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -1,120 +1,120 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.nls.formatter.impl;

import io.github.mmm.base.filter.CharFilter;
import io.github.mmm.base.filter.ListCharFilter;
import io.github.mmm.nls.formatter.NlsFormatter;
import io.github.mmm.nls.formatter.NlsFormatterManager;
import io.github.mmm.nls.formatter.NlsFormatterPlugin;
import io.github.mmm.nls.formatter.impl.plugin.AbstractNlsFormatterPlugin;
import io.github.mmm.nls.formatter.impl.plugin.NlsFormatterPluginChoice;
import io.github.mmm.nls.formatter.impl.plugin.NlsFormatterPluginDatePattern;
import io.github.mmm.nls.formatter.impl.plugin.NlsFormatterPluginNumberPattern;
import io.github.mmm.nls.variable.NlsVariableParser;
import io.github.mmm.scanner.CharSequenceScanner;

/**
* Implementation of {@link NlsFormatterManager}.
*/
public class NlsFormatterManagerImpl implements NlsFormatterManager {

/** The singleton instance. */
public static final NlsFormatterManagerImpl INSTANCE = new NlsFormatterManagerImpl();

/** A char filter that accepts everything except ',' and '}'. */
protected static final CharFilter NO_COMMA_OR_END_EXPRESSION = new ListCharFilter(NlsVariableParser.FORMAT_SEPARATOR,
NlsVariableParser.END_EXPRESSION).negate();

/** A char filter that accepts everything except ',' and '}'. */
protected static final CharFilter NO_EXPRESSION = new ListCharFilter(NlsVariableParser.START_EXPRESSION,
NlsVariableParser.END_EXPRESSION).negate();

private final NlsFormatterMap formatterMap;

/**
* The constructor.
*/
public NlsFormatterManagerImpl() {

this(new ConfiguredNlsFormatterMap());
}

/**
* The constructor.
*
* @param formatterMap is the map with the registered formatters.
*/
public NlsFormatterManagerImpl(NlsFormatterMap formatterMap) {

super();
this.formatterMap = formatterMap;
}

/**
* This method is like {@link #getFormatter(String, String)} but reads the
* {@link AbstractNlsFormatterPlugin#getStyle() style} from the given scanner.
*
* @param formatType is the type to be formatted.
* @param scanner is the current {@link CharSequenceScanner} for parsing the style defining details of formatting.
* @return the according {@link NlsFormatter}.
*/
@Override
public NlsFormatterPlugin getFormatter(String formatType, CharSequenceScanner scanner) {

if (TYPE_CHOICE.equals(formatType)) {
return new NlsFormatterPluginChoice(scanner);
}
String formatStyle = scanner.readWhile(NO_EXPRESSION);
return getFormatter(formatType, formatStyle);
}

/**
* This method creates the {@link NlsFormatter} for the given {@code formatType} and the custom {@code subformat}. It
* is called if no formatter is {@link NlsFormatterMap#registerFormatter(NlsFormatterPlugin, String, String)
* registered} for the given arguments. <br>
*
* @param formatType is the type to be formatted.
* @param subformat is the custom formatStyle for which no static formatter is registered.
* @return the according custom formatter or {@code null} if no such formatter is could be created.
*/
protected NlsFormatterPlugin getSubFormatter(String formatType, String subformat) {

if (TYPE_NUMBER.equals(formatType)) {
return new NlsFormatterPluginNumberPattern(subformat);
} else if ((TYPE_DATE.equals(formatType)) || (TYPE_TIME.equals(formatType)) || (TYPE_DATETIME.equals(formatType))) {
return new NlsFormatterPluginDatePattern(subformat);
}
return null;
}

@Override
public NlsFormatterPlugin getFormatter(String formatType, String formatStyle) {

NlsFormatterPlugin result = null;
result = this.formatterMap.getFormatter(formatType, formatStyle);
if (result == null) {
if (formatStyle != null) {
result = getSubFormatter(formatType, formatStyle);
if (result == null) {
result = this.formatterMap.getFormatter(formatType, null);
}
}
}
if (result == null) {
if (formatType == null) {
throw new IllegalStateException();
}
}
return result;
}

/**
* @return the formatterMap
*/
protected NlsFormatterMap getFormatterMap() {

return this.formatterMap;
}

}
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.nls.formatter.impl;

import io.github.mmm.base.filter.CharFilter;
import io.github.mmm.base.filter.ListCharFilter;
import io.github.mmm.nls.formatter.NlsFormatter;
import io.github.mmm.nls.formatter.NlsFormatterManager;
import io.github.mmm.nls.formatter.NlsFormatterPlugin;
import io.github.mmm.nls.formatter.impl.plugin.AbstractNlsFormatterPlugin;
import io.github.mmm.nls.formatter.impl.plugin.NlsFormatterPluginChoice;
import io.github.mmm.nls.formatter.impl.plugin.NlsFormatterPluginDatePattern;
import io.github.mmm.nls.formatter.impl.plugin.NlsFormatterPluginNumberPattern;
import io.github.mmm.nls.variable.NlsVariableParser;
import io.github.mmm.scanner.CharSequenceScanner;

/**
* Implementation of {@link NlsFormatterManager}.
*/
public class NlsFormatterManagerImpl implements NlsFormatterManager {

/** The singleton instance. */
public static final NlsFormatterManagerImpl INSTANCE = new NlsFormatterManagerImpl();

/** A char filter that accepts everything except ',' and '}'. */
protected static final CharFilter NO_COMMA_OR_END_EXPRESSION = new ListCharFilter(
"" + NlsVariableParser.FORMAT_SEPARATOR + NlsVariableParser.END_EXPRESSION).negate();

/** A char filter that accepts everything except ',' and '}'. */
protected static final CharFilter NO_EXPRESSION = new ListCharFilter(
"" + NlsVariableParser.START_EXPRESSION + NlsVariableParser.END_EXPRESSION).negate();

private final NlsFormatterMap formatterMap;

/**
* The constructor.
*/
public NlsFormatterManagerImpl() {

this(new ConfiguredNlsFormatterMap());
}

/**
* The constructor.
*
* @param formatterMap is the map with the registered formatters.
*/
public NlsFormatterManagerImpl(NlsFormatterMap formatterMap) {

super();
this.formatterMap = formatterMap;
}

/**
* This method is like {@link #getFormatter(String, String)} but reads the
* {@link AbstractNlsFormatterPlugin#getStyle() style} from the given scanner.
*
* @param formatType is the type to be formatted.
* @param scanner is the current {@link CharSequenceScanner} for parsing the style defining details of formatting.
* @return the according {@link NlsFormatter}.
*/
@Override
public NlsFormatterPlugin getFormatter(String formatType, CharSequenceScanner scanner) {

if (TYPE_CHOICE.equals(formatType)) {
return new NlsFormatterPluginChoice(scanner);
}
String formatStyle = scanner.readWhile(NO_EXPRESSION);
return getFormatter(formatType, formatStyle);
}

/**
* This method creates the {@link NlsFormatter} for the given {@code formatType} and the custom {@code subformat}. It
* is called if no formatter is {@link NlsFormatterMap#registerFormatter(NlsFormatterPlugin, String, String)
* registered} for the given arguments. <br>
*
* @param formatType is the type to be formatted.
* @param subformat is the custom formatStyle for which no static formatter is registered.
* @return the according custom formatter or {@code null} if no such formatter is could be created.
*/
protected NlsFormatterPlugin getSubFormatter(String formatType, String subformat) {

if (TYPE_NUMBER.equals(formatType)) {
return new NlsFormatterPluginNumberPattern(subformat);
} else if ((TYPE_DATE.equals(formatType)) || (TYPE_TIME.equals(formatType)) || (TYPE_DATETIME.equals(formatType))) {
return new NlsFormatterPluginDatePattern(subformat);
}
return null;
}

@Override
public NlsFormatterPlugin getFormatter(String formatType, String formatStyle) {

NlsFormatterPlugin result = null;
result = this.formatterMap.getFormatter(formatType, formatStyle);
if (result == null) {
if (formatStyle != null) {
result = getSubFormatter(formatType, formatStyle);
if (result == null) {
result = this.formatterMap.getFormatter(formatType, null);
}
}
}
if (result == null) {
if (formatType == null) {
throw new IllegalStateException();
}
}
return result;
}

/**
* @return the formatterMap
*/
protected NlsFormatterMap getFormatterMap() {

return this.formatterMap;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ public class NlsFormatterPluginChoice extends AbstractNlsFormatterPlugin {
/**
* The {@link CharFilter} for the {@link CompareOperator#getSyntax() comparator symbol} .
*/
private static final CharFilter FILTER_COMPARATOR = new ListCharFilter('<', '=', '>', '!');
private static final CharFilter FILTER_COMPARATOR = new ListCharFilter("<=>!");

/** The {@link CharFilter} for the comparator argument. */
private static final CharFilter FILTER_COMPARATOR_ARGUMENT = CharFilter.LATIN_LETTER_OR_DIGIT
.compose(new ListCharFilter('-', '+', '.', ':'));
.compose(new ListCharFilter("-+.:"));

/** The {@link Choice}s. */
private final List<Choice> choices;
Expand All @@ -82,15 +82,15 @@ public NlsFormatterPluginChoice(CharSequenceScanner scanner) {
super();
this.choices = new ArrayList<>();
boolean hasElse = false;
char c = scanner.peek();
while ((c == CONDITION_START) && (!hasElse)) {
int cp = scanner.peek();
while ((cp == CONDITION_START) && (!hasElse)) {
scanner.next();
Choice choice = parseChoice(scanner);
if (choice.condition == FILTER_ELSE) {
hasElse = true;
}
this.choices.add(choice);
c = scanner.peek();
cp = scanner.peek();
}
if (!hasElse) {
throw new IllegalStateException("no (else) condition!");
Expand All @@ -112,18 +112,18 @@ private Choice parseChoice(CharSequenceScanner scanner) {
List<Segment> segments = new ArrayList<>();
while (scanner.hasNext()) {
int index = scanner.getCurrentIndex();
char c = scanner.peek();
int cp = scanner.peek();
String literal = null;
if ((c == '"') || (c == '\'')) {
if ((cp == '"') || (cp == '\'')) {
scanner.next();
literal = scanner.readUntil(c, false, c);
literal = scanner.readUntil(cp, false, cp);
if (literal == null) {
throw new IllegalArgumentException(scanner.substring(index, scanner.getCurrentIndex()));
}
c = scanner.peek();
cp = scanner.peek();
}
NlsVariable variable = null;
if (c == NlsVariableParser.START_EXPRESSION) {
if (cp == NlsVariableParser.START_EXPRESSION) {
scanner.next();
variable = NlsVariableParser.get().parse(scanner);
}
Expand Down Expand Up @@ -176,10 +176,10 @@ private Comparable<?> parseComparatorArgument(CharSequenceScanner scanner) {

int index = scanner.getCurrentIndex();
Comparable<?> comparatorArgument;
char c = scanner.peek();
if ((c == '"') || (c == '\'')) {
int cp = scanner.peek();
if ((cp == '"') || (cp == '\'')) {
scanner.next();
comparatorArgument = scanner.readUntil(c, false, c);
comparatorArgument = scanner.readUntil(cp, false, cp);
} else {
String argument = scanner.readWhile(FILTER_COMPARATOR_ARGUMENT);
if (argument.length() == 0) {
Expand Down
Loading

0 comments on commit e27c323

Please sign in to comment.