Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package modelengine.fel.core.template.support;

import modelengine.fel.core.template.StringTemplate;
import modelengine.fitframework.inspection.Validation;
import modelengine.fitframework.merge.ConflictResolutionPolicy;
import modelengine.fitframework.parameterization.ParameterizationMode;
import modelengine.fitframework.parameterization.ParameterizedString;
import modelengine.fitframework.parameterization.ParameterizedStringResolver;
import modelengine.fitframework.parameterization.ResolvedParameter;
Expand All @@ -28,7 +29,7 @@
*/
public class DefaultStringTemplate implements StringTemplate {
private static final ParameterizedStringResolver FORMATTER =
ParameterizedStringResolver.create("{{", "}}", '\\', false);
ParameterizedStringResolver.create("{{", "}}", '\\', ParameterizationMode.LENIENT_STRICT_PARAMETERS);

private final ParameterizedString parameterizedString;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package modelengine.fel.core.template;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.catchThrowableOfType;

import modelengine.fel.core.template.support.DefaultBulkStringTemplate;
import modelengine.fitframework.parameterization.StringFormatException;
import modelengine.fitframework.util.MapBuilder;

import org.junit.jupiter.api.DisplayName;
Expand Down Expand Up @@ -80,7 +81,8 @@ void giveMissValueThenThrowException() {
List<Map<String, String>> values = new ArrayList<>();
values.add(MapBuilder.<String, String>get().put("adjective", "funny").build());
values.add(MapBuilder.<String, String>get().put("content", "rabbits").build());
assertThatThrownBy(() -> new DefaultBulkStringTemplate(template, "\n").render(values)).isInstanceOf(
IllegalArgumentException.class);
StringFormatException cause = catchThrowableOfType(StringFormatException.class,
() -> new DefaultBulkStringTemplate(template, "\n").render(values));
assertThat(cause).hasMessage("Required parameters are missing.");
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package modelengine.fitframework.exception;

/**
* 当访问类型发生异常时引发的异常。
*
* @author 梁济时
* @since 1.0
* @since 2020-07-24
*/
public class ClassAccessException extends RuntimeException {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package modelengine.fitframework.exception;

/**
* 当发现日期格式错误时引发的异常。
*
* @author 梁济时
* @since 1.0
* @since 2020-07-24
*/
public class DateFormatException extends IllegalArgumentException {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package modelengine.fitframework.exception;

/**
* 当访问字段失败时引发的异常。
*
* @author 梁济时
* @since 1.0
* @since 2020-07-24
*/
public class FieldVisitException extends RuntimeException {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package modelengine.fitframework.exception;

/**
* 当调用方法失败时引发的异常。
*
* @author 梁济时
* @since 1.0
* @since 2020-07-24
*/
public class MethodInvocationException extends RuntimeException {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package modelengine.fitframework.exception;

/**
* 当没有指定的方法时引发的异常。
*
* @author 梁济时
* @since 1.0
* @since 2020-07-24
*/
public class MethodNotFoundException extends RuntimeException {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

package modelengine.fitframework.parameterization;

/**
* 参数化字符串的格式化模式。
*
* @author 季聿阶
* @since 2025-09-23
*/
public enum ParameterizationMode {
/**
* 严格模式:
* <ul>
* <li>参数数量必须完全匹配</li>
* <li>语法错误抛异常</li>
* <li>缺失参数抛异常</li>
* </ul>
*/
STRICT(true, true, MissingParameterBehavior.THROW_EXCEPTION),

/**
* 宽松模式 - 使用空字符串:
* <ul>
* <li>允许多余参数</li>
* <li>语法错误当普通字符处理</li>
* <li>缺失参数使用空字符串</li>
* </ul>
*/
LENIENT_EMPTY(false, false, MissingParameterBehavior.USE_EMPTY_STRING),

/**
* 宽松模式 - 使用默认值:
* <ul>
* <li>允许多余参数</li>
* <li>语法错误当普通字符处理</li>
* <li>缺失参数使用默认值</li>
* </ul>
*/
LENIENT_DEFAULT(false, false, MissingParameterBehavior.USE_DEFAULT_VALUE),

/**
* 宽松模式 - 保持占位符:
* <ul>
* <li>允许多余参数</li>
* <li>语法错误当普通字符处理</li>
* <li>缺失参数保持原占位符</li>
* </ul>
*/
LENIENT_KEEP_PLACEHOLDER(false, false, MissingParameterBehavior.KEEP_PLACEHOLDER),

/**
* 混合模式:
* <ul>
* <li>允许多余参数</li>
* <li>语法错误当普通字符处理</li>
* <li>但缺失参数仍抛异常(便于调试)</li>
* </ul>
*/
LENIENT_STRICT_PARAMETERS(false, false, MissingParameterBehavior.THROW_EXCEPTION);

private final boolean requireExactParameterCount;
private final boolean strictSyntax;
private final MissingParameterBehavior missingParameterBehavior;

ParameterizationMode(boolean requireExactParameterCount, boolean strictSyntax,
MissingParameterBehavior missingParameterBehavior) {
this.requireExactParameterCount = requireExactParameterCount;
this.strictSyntax = strictSyntax;
this.missingParameterBehavior = missingParameterBehavior;
}

/**
* 是否要求参数数量必须完全匹配。
*
* @return 如果要求参数数量必须完全匹配,则返回 {@code true},否则,返回 {@code false}。
*/
public boolean isRequireExactParameterCount() {
return this.requireExactParameterCount;
}

/**
* 是否要求语法错误抛异常。
*
* @return 如果要求语法错误抛异常,则返回 {@code true},否则,返回 {@code false}。
*/
public boolean isStrictSyntax() {
return this.strictSyntax;
}

/**
* 获取缺失参数处理行为。
*
* @return 表示缺失参数处理行为的 {@link MissingParameterBehavior}。
*/
public MissingParameterBehavior getMissingParameterBehavior() {
return this.missingParameterBehavior;
}

/**
* 缺失参数处理行为。
*/
public enum MissingParameterBehavior {
/**
* 抛异常。
*/
THROW_EXCEPTION,

/**
* 使用空字符串。
*/
USE_EMPTY_STRING,

/**
* 使用默认值。
*/
USE_DEFAULT_VALUE,

/**
* 保持原占位符。
*/
KEEP_PLACEHOLDER
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
* This file is a part of the ModelEngine Project.
* Licensed under the MIT License. See License.txt in the project root for license information.
*/

package modelengine.fitframework.parameterization;

Expand All @@ -13,7 +13,7 @@
* 为参数解析提供结果。
*
* @author 梁济时
* @since 1.0
* @since 2020-07-24
*/
public interface ParameterizedString {
/**
Expand Down Expand Up @@ -45,4 +45,14 @@ public interface ParameterizedString {
* @throws StringFormatException 当需要但是未提供指定名称的参数时。
*/
String format(Map<?, ?> args);

/**
* 使用指定的参数映射格式化字符串。
*
* @param args 表示参数映射的 {@link Map}{@code <?, ?>}。
* @param defaultValue 表示缺少对应参数时的默认值的 {@link String}。
* @return 表示格式化后的字符串的 {@link String}。
* @throws StringFormatException 当需要但是未提供指定名称的参数时。
*/
String format(Map<?, ?> args, String defaultValue);
}
Loading