java - 以编程方式使用 M2Doc:生成的 .docx 文档中的错误
问题描述
我正在尝试以编程方式使用 M2Doc,我设法生成了我的.docx
文件,而在验证部分没有出现错误,但在生成的文档中出现以下错误:
{m:self.Name} Couldn't find the 'aqlFeatureAccess(org.eclipse.emf.common.util.URI.Hierarchical,java.lang.String)' service
“self.Name”部分是我在模板中写的。
我想我缺少对服务的某种参考,但我不知道如何解决它。
self 变量是对基于我创建的元模型的模型的引用。但我不确定我是否在我的代码中正确导入了它。
我的代码基于我在M2Doc 网站上找到的代码+ 我在他们的 GitHub 上找到的一些代码,尤其是关于如何在 queryEnvironment 中添加服务的代码。
我搜索了 acceleo 和 M2Doc 的源代码以查看它们添加了哪些服务,但似乎它们已经导入了我正在使用的所有服务。
正如我所说,验证部分进展顺利,不会生成验证文件。
public static void parseDocument(String templateName) throws Exception{
final URI templateURI = URI.createFileURI("Template/"+templateName+"."+M2DocUtils.DOCX_EXTENSION_FILE);
final IQueryEnvironment queryEnvironment =
org.eclipse.acceleo.query.runtime.Query.newEnvironmentWithDefaultServices(null);
final Map<String, String> options = new HashMap<>(); // can be empty
M2DocUtils.prepareEnvironmentServices(queryEnvironment, templateURI, options); // delegate to IServicesConfigurator
prepareEnvironmentServicesCustom(queryEnvironment, options);
final IClassProvider classProvider = new ClassProvider(ClassLoader.getSystemClassLoader()); // use M2DocPlugin.getClassProvider() when running inside Eclipse
try (DocumentTemplate template = M2DocUtils.parse(templateURI, queryEnvironment, classProvider)) {
ValidationMessageLevel validationLevel = validateDocument(template, queryEnvironment, templateName);
if(validationLevel == ValidationMessageLevel.OK){
generateDocument(template, queryEnvironment, templateName, "Model/ComplexKaosModel.kaos");
}
}
}
public static void prepareEnvironmentServicesCustom(IQueryEnvironment queryEnvironment, Map<String, String> options){
Set<IService> services = ServiceUtils.getServices(queryEnvironment, FilterService.class);
ServiceUtils.registerServices(queryEnvironment, services);
M2DocUtils.getConfigurators().forEach((configurator) -> {
ServiceUtils.registerServices(queryEnvironment, configurator.getServices(queryEnvironment, options));
});
}
public static void generateDocument(DocumentTemplate template, IQueryEnvironment queryEnvironment,
String templateName, String modelPath)throws Exception{
final Map<String, Object> variable = new HashMap<>();
variable.put("self", URI.createFileURI(modelPath));
final Monitor monitor = new BasicMonitor.Printing(System.out);
final URI outputURI = URI.createFileURI("Generated/"+templateName+".generated."+M2DocUtils.DOCX_EXTENSION_FILE);
M2DocUtils.generate(template, queryEnvironment, variable, outputURI, monitor);
}
解决方案
变量“self”包含一个 URI:
variable.put("self", URI.createFileURI(modelPath));
您必须加载模型并将 self 的值设置为模型中的元素,例如:
final ResourceSet rs = new ResourceSetImpl();
final Resource r = rs.getResource(uri, true);
final EObject value = r.getContents()...;
variable.put("self", value);
您可以在EMF 文档中获得有关资源加载的更多详细信息。
推荐阅读
- splunk - 带变量的 Splunk Cloud 搜索查询不返回结果
- google-sheets - 从其他 2 个表创建百分比表 - Google Query
- concatenation - 谷歌表格连接选定行的某些单元格内容
- list - 飞镖映射,如何替换列表中的特定键值
- php - 我遇到了 HTTP 错误 500.0 - 内部服务器错误的问题
- python - 更改不可变数据类型的值会导致内存浪费?
- html - 我得到多个按钮而不是 1
- salt-stack - 是否有退出状态执行的本机方式?
- python - SQLite:如何使用 Python API 启用解释计划?
- sql - 不在选择查询中