M2Doc 错误<---找不到'getText(EClassifier=Model)'服务



我以编程方式使用m2doc,这是我的代码。

final URI templateURI = URI.createFileURI(templateName+"."+M2DocUtils.DOCX_EXTENSION_FILE); 
final URI modelURI = URI.createFileURI(modelName);

// can be empty, if you are using a Generation use GenconfUtils.getOptions(generation)
final Map<String, String> options = new HashMap<>();
List<Exception> exceptions = new ArrayList<>();

final ResourceSet resourceSetForModels = M2DocUtils.createResourceSetForModels(exceptions , "key", new ResourceSetImpl(), options);
//resourceSetForModels.getResource(modelURI, true);
final Resource r = resourceSetForModels.getResource(modelURI, true);
System.out.println(r.getContents());
final EObject value = r.getContents().get(0);
// if you are using a Generation, use GenconfUtils.getQueryEnvironment(resourceSetForModels, generation)
final IQueryEnvironment queryEnvironment = M2DocUtils.getQueryEnvironment(resourceSetForModels, templateURI, options); // delegate to IServicesConfigurator

final IClassProvider classProvider =  new ClassProvider(this.getClass().getClassLoader()); // use M2DocPlugin.getClassProvider() when running inside Eclipse
final Monitor monitor = new BasicMonitor.Printing(System.out);
try (DocumentTemplate template = M2DocUtils.parse(resourceSetForModels.getURIConverter(), templateURI, queryEnvironment, classProvider, monitor)) {

// validate
final ValidationMessageLevel validationLevel = M2DocUtils.validate(template, queryEnvironment, monitor);
if (validationLevel != ValidationMessageLevel.OK) {
final URI validationResulURI = URI.createFileURI(templateName+"-validation."+M2DocUtils.DOCX_EXTENSION_FILE); // some place to serialize the result of the validation
M2DocUtils.serializeValidatedDocumentTemplate(resourceSetForModels.getURIConverter(), template, validationResulURI);}

//generate
final Map<String, Object> variables = new HashMap<>(); // your variables and values
variables.put("self", value);
final URI outputURI = URI.createFileURI(templateName+"-result."+M2DocUtils.DOCX_EXTENSION_FILE); // some place to serialize the result of the generation
M2DocUtils.generate(template, queryEnvironment, variables, resourceSetForModels, outputURI, monitor);
}finally {

M2DocUtils.cleanResourceSetForModels("key", resourceSetForModels);
}

当我运行程序时,创建了验证文件,它显示了许多错误。误差是这样的:<---Couldn't find the 'getText(EClassifier=Model)' service。它们出现在我使用getText()服务的任何地方。

当我使用eclipse中的m2doc插件使用相同的模板和相同的uml文件生成文档时,它运行正常。

我想知道我的查询环境设置是错误的。如果你能帮助我,谢谢你。

M2DocUtils.parse()用模板中导入的服务和nsURIs初始化IQueryEnvironment。所以你不需要添加任何东西。

如果你在Eclipse中运行,你应该使用:

final IClassProvider classProvider = M2DocPlugin.getClassProvider()

它应该有助于加载你的服务类(它使用OSGi从包中加载类)。

您还可以检查是否有TemplateValidationMessage存在,并提供更多详细信息:

DocumentTemplate.getBody().getValidationMessages()

此时,要么UML元模型没有注册,要么它是您的服务类(更有可能)。

您可以注册UML eppackage,看看它是否有帮助:

queryEnvironment.registerEPackage(UMLPackage.eINSTANCE)

你也可以尝试注册你的服务类:

final Set<IService> s = ServiceUtils.getServices(queryEnvironment, SomeServiceClass.class);
ServiceUtils.registerServices(queryEnvironment, s);

最新更新