I was using m2doc programmatically, and this is my code.
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);
}
And when I ran the program, the validation file was created, and it shows lots of errors. The errors were like this: <---Couldn't find the 'getText(EClassifier=Model)' service. They appeared everywhere I used the service getText().
When I use the same template and the same uml file to generate document by using the m2doc plugin in eclipse, it went alright.
I wonder if my query environment setting was wrong. Thank you if you can help me.
M2DocUtils.parse() initialize the IQueryEnvironment with services and nsURIs imported in the template. So you should not need to add anything.
If you are running inside Eclipse you should use:
It should help loading your service Class (it use OSGi to load classes from bundles).
You can also check if any TemplateValidationMessage are present with more details:
At this point either the UML metamodel is not registered or it's your service Class (more likely).
You can register the UML EPackage and see if it helps:
You can also try to register your service class: