首页 > 解决方案 > 以编程方式使用 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);
    }

标签: javam2doc

解决方案


变量“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 文档中获得有关资源加载的更多详细信息。


推荐阅读