Skip to content

Commit

Permalink
Kleinere Verbesserungen für die Formelberechnung
Browse files Browse the repository at this point in the history
Die Formelberechnung erlaubt jetzt auch solche Platzhalter-Namen, die bisher zu Fehlern geführt haben:
* C#-Schlüsselwörter
* Klassennamen in benutzten Namensräumen
Um dies zu testen, wurde ein automatischer Testfall erweitert.

Platzhalter, die in Kommentaren oder String-Literalen enthalten sind, werden nicht mehr als Platzhalter behandelt. Ein Testfall wurde entsprechend geändert. Die überlange Methode PlaceholderNodeBase::validateInternal wurde in mehrere Methoden aufgeteilt und die Parameter-Reihenfolgen einiger Methoden konsistenter gemacht.

Die Dokumentation zur Formelberechnnung weist jetzt auf die Möglichkeit hin, temporäre Objekte anzulegen und gibt ein Beispiel, in dem dies verwendet wird.
  • Loading branch information
recomedia committed Aug 28, 2021
1 parent 4fbb9b8 commit da5c6c9
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 142 deletions.
114 changes: 83 additions & 31 deletions VisuWebNodes/03-PlaceholderNodeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,12 @@ private ValidationResult validateSeparators(string language)
return new ValidationResult { HasError = false };
}

protected struct SplitElement
{
public string text;
public bool isFixedToken;
}

private ValidationResult validateInternal(StringValueObject template,
ref List<TokenBase> templateTokens,
ref int binCount,
Expand All @@ -387,40 +393,85 @@ private ValidationResult validateInternal(StringValueObject template,
string language)
{
System.Diagnostics.Trace.Assert(template.HasValue && (template.Value.Length > 0));

splitTokens(template, ref templateTokens);

ValidationResult result = validateTotalNumberOfTokens(ref templateTokens, language);
if (result.HasError)
{
return result;
}

result = countTokenTypes(template.Name, ref templateTokens,
ref binCount, ref intCount, ref numCount, ref strCount, language);
if (result.HasError)
{
return result;
}

return validateTokens(template.Name, ref templateTokens, language);
}

private void splitTokens(StringValueObject template,
ref List<TokenBase> templateTokens)
{
TemplateTokenFactory ttf = TemplateTokenFactory.Instance;

for (int curPos = 0; curPos < template.Value.Length;)
List<SplitElement> splitElements = SplitNonTokenizedElements(template.Value);

foreach (SplitElement elem in splitElements)
{
Match curMatch = PLACEHOLDER_REGEX.Match(template.Value, curPos);
if (curMatch.Success && (curMatch.Length >= 2))
if (elem.isFixedToken)
{
// constant string before placeholder
templateTokens.Add(ttf.createConstStringToken(
template.Value.Substring(curPos, curMatch.Index - curPos)));
// placeholder string between delimiters
templateTokens.Add(
ttf.createPlaceholderToken(
template.Value.Substring(curMatch.Index + 1, curMatch.Length - 2),
getGroupSeparator(), getDecimalSeparator()
)
);
curPos = curMatch.Index + curMatch.Length;
// Treat fixed tokens like constant strings between placeholders
templateTokens.Add(ttf.createConstStringToken(elem.text));
}
else
{
// constant string after last placeholder
templateTokens.Add(ttf.createConstStringToken(
template.Value.Substring(curPos)));

curPos = template.Value.Length;
for (int curPos = 0; curPos < elem.text.Length;)
{
Match curMatch = PLACEHOLDER_REGEX.Match(elem.text, curPos);
if (curMatch.Success && (curMatch.Length >= 2))
{
// Constant string before placeholder
templateTokens.Add(ttf.createConstStringToken(
elem.text.Substring(curPos, curMatch.Index - curPos)));
// Placeholder string between delimiters
templateTokens.Add(
ttf.createPlaceholderToken(
elem.text.Substring(curMatch.Index + 1, curMatch.Length - 2),
getGroupSeparator(), getDecimalSeparator()
)
);
curPos = curMatch.Index + curMatch.Length;
}
else
{
// Constant string after last placeholder
templateTokens.Add(ttf.createConstStringToken(
elem.text.Substring(curPos)));
curPos = elem.text.Length;
}
}
}
}
ValidationResult result = validateNumberOfTokens(language, ref templateTokens);
if (result.HasError)
{
return result;
}
}

protected virtual List<SplitElement> SplitNonTokenizedElements(string templateText)
{
// Base implementation has no elements which are exempted from tokenization
SplitElement singleElem = new SplitElement { text = templateText, isFixedToken = false };
return new List<SplitElement>() { singleElem };
}

private ValidationResult countTokenTypes(string templateName,
ref List<TokenBase> templateTokens,
ref int binCount,
ref int intCount,
ref int numCount,
ref int strCount,
string language)
{
foreach (TokenBase token in templateTokens)
{
switch (token.getType())
Expand All @@ -443,7 +494,7 @@ private ValidationResult validateInternal(StringValueObject template,
break;
case TokenType.Error:
default:
return createTokenError(language, template.Name, token);
return createTokenError(language, templateName, token);
}
}
if (binCount > MAX_INPUTS)
Expand Down Expand Up @@ -478,7 +529,7 @@ private ValidationResult validateInternal(StringValueObject template,
Message = Localize(language, "TooManyStrPlaceholders")
};
}
return validateTokens(language, template.Name, ref templateTokens);
return new ValidationResult { HasError = false };
}

protected ValidationResult createTokenError(string language,
Expand Down Expand Up @@ -507,12 +558,13 @@ protected ValidationResult createTextError(string language,
};
}

protected abstract ValidationResult validateTokens(string language,
string templateName,
ref List<TokenBase> templateTokens);
protected abstract ValidationResult validateTokens(string templateName,
ref List<TokenBase> templateTokens,
string language);

private ValidationResult validateNumberOfTokens(string language,
ref List<TokenBase> templateTokens)
private ValidationResult validateTotalNumberOfTokens(
ref List<TokenBase> templateTokens,
string language)
{
if (templateTokens.Count < (isPlaceholderEnforced() ? 2 : 1))
{
Expand Down
6 changes: 3 additions & 3 deletions VisuWebNodes/04-StringFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ protected override void updateTemplateHelpers()
// We have no other members that need to be updated along with templates
}

protected override ValidationResult validateTokens(string language,
string templateName,
ref List<TokenBase> templateTokens)
protected override ValidationResult validateTokens(string templateName,
ref List<TokenBase> templateTokens,
string language)
{
// no additional validations; we fully support all token types
return new ValidationResult { HasError = false };
Expand Down
Loading

0 comments on commit da5c6c9

Please sign in to comment.