Skip to content

Commit e60f7b3

Browse files
committed
Removed EPackage and service registration from parsing.
1 parent 539c95c commit e60f7b3

5 files changed

Lines changed: 55 additions & 55 deletions

File tree

plugins/org.obeonetwork.m2doc.genconf.editor/src/org/obeonetwork/m2doc/genconf/editor/view/M2DocInterpreterView.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,9 @@ public void setGenconfURI(URI genconfURI) {
637637
final URIConverter uriConverter = resourceSetForModels.getURIConverter();
638638
try (XWPFDocument document = POIServices.getInstance()
639639
.getXWPFDocument(resourceSetForModels.getURIConverter(), templateURI);
640-
DocumentTemplate documentTemplate = M2DocUtils.parse(uriConverter, templateURI, queryEnvironment,
641-
classProvider, new BasicMonitor());) {
642-
final TemplateCustomProperties properties = new TemplateCustomProperties(document);
640+
DocumentTemplate documentTemplate = M2DocUtils.parse(uriConverter, templateURI,
641+
new BasicMonitor());) {
642+
final TemplateCustomProperties properties = documentTemplate.getProperties();
643643
properties.configureQueryEnvironmentWithResult(queryEnvironment);
644644
properties.configureQueryEnvironmentWithResult(queryEnvironment, classProvider);
645645
if (!documentTemplate.getTemplates().isEmpty()) {
@@ -859,8 +859,10 @@ private Map<String, Set<IType>> parseVariableTypes(IReadOnlyQueryEnvironment env
859859
Map<String, Set<IType>> varTypes, URIConverter uriConv, URI tpltURI) {
860860
final Map<String, Set<IType>> res = new HashMap<>();
861861

862-
try (DocumentTemplate template = M2DocUtils.parse(uriConv, tpltURI, (IQueryEnvironment) env,
863-
M2DocPlugin.getClassProvider(), new BasicMonitor())) {
862+
try (DocumentTemplate template = M2DocUtils.parse(uriConv, tpltURI, new BasicMonitor())) {
863+
final TemplateCustomProperties properties = template.getProperties();
864+
properties.configureQueryEnvironmentWithResult(queryEnvironment);
865+
properties.configureQueryEnvironmentWithResult(queryEnvironment, M2DocPlugin.getClassProvider());
864866
final Iterator<EObject> it = template.eAllContents();
865867
while (it.hasNext()) {
866868
final EObject current = it.next();
@@ -921,8 +923,10 @@ private Map<String, Object> parseVariableValues(IReadOnlyQueryEnvironment env, Q
921923
Map<String, Object> variables, URIConverter uriConverter, URI tpltURI) {
922924
final Map<String, Object> res = new HashMap<>(variables);
923925

924-
try (DocumentTemplate template = M2DocUtils.parse(uriConverter, tpltURI, (IQueryEnvironment) env,
925-
M2DocPlugin.getClassProvider(), new BasicMonitor())) {
926+
try (DocumentTemplate template = M2DocUtils.parse(uriConverter, tpltURI, new BasicMonitor())) {
927+
final TemplateCustomProperties properties = template.getProperties();
928+
properties.configureQueryEnvironmentWithResult(queryEnvironment);
929+
properties.configureQueryEnvironmentWithResult(queryEnvironment, M2DocPlugin.getClassProvider());
926930
final Iterator<EObject> it = template.eAllContents();
927931
while (it.hasNext()) {
928932
final EObject current = it.next();

plugins/org.obeonetwork.m2doc.genconf/src/org/obeonetwork/m2doc/genconf/GenconfUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,8 @@ private static List<URI> generate(Generation generation, ResourceSet resourceSet
695695
}
696696

697697
// create generated file
698-
try (DocumentTemplate documentTemplate = M2DocUtils.parse(uriConverter, templateURI, queryEnvironment,
699-
classProvider, monitor)) {
700-
698+
try (DocumentTemplate documentTemplate = M2DocUtils.parse(uriConverter, templateURI, monitor)) {
699+
M2DocUtils.prepareEnvironment(queryEnvironment, classProvider, documentTemplate);
701700
// create definitions
702701
Map<String, Object> definitions = GenconfUtils.getVariables(generation, resourceSetForModels);
703702

@@ -774,7 +773,8 @@ public static boolean validate(Generation generation, ResourceSet resourceSetFor
774773

775774
// parse template
776775
try (DocumentTemplate documentTemplate = M2DocUtils.parse(resourceSetForModels.getURIConverter(), templateURI,
777-
queryEnvironment, classProvider, monitor)) {
776+
monitor)) {
777+
M2DocUtils.prepareEnvironment(queryEnvironment, classProvider, documentTemplate);
778778
final XWPFRun run = documentTemplate.getDocument().getParagraphs().get(0).getRuns().get(0);
779779
for (Exception e : exceptions) {
780780
documentTemplate.getBody().getValidationMessages()

plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/util/M2DocUtils.java

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.LinkedHashMap;
2020
import java.util.List;
2121
import java.util.Map;
22-
import java.util.Map.Entry;
2322
import java.util.Set;
2423
import java.util.concurrent.CancellationException;
2524

@@ -541,10 +540,6 @@ public static void prepareEnvironmentServices(IQueryEnvironment queryEnvironment
541540
* the {@link URIConverter uri converter} to use.
542541
* @param templateURI
543542
* URI for the template, used when external links (images, includes) have to be resolved
544-
* @param queryEnvironment
545-
* the {@link IQueryEnvironment}
546-
* @param classProvider
547-
* the {@link IClassProvider} to use for service Loading
548543
* @param monitor
549544
* used to track the progress will generating
550545
* @return the {@link DocumentTemplate} resulting from parsing the specified
@@ -553,8 +548,8 @@ public static void prepareEnvironmentServices(IQueryEnvironment queryEnvironment
553548
* if a problem occurs while parsing the document.
554549
*/
555550
@SuppressWarnings("resource")
556-
public static DocumentTemplate parse(URIConverter uriConverter, URI templateURI, IQueryEnvironment queryEnvironment,
557-
IClassProvider classProvider, Monitor monitor) throws DocumentParserException {
551+
public static DocumentTemplate parse(URIConverter uriConverter, URI templateURI, Monitor monitor)
552+
throws DocumentParserException {
558553
final DocumentTemplate result = (DocumentTemplate) EcoreUtil.create(TemplatePackage.Literals.DOCUMENT_TEMPLATE);
559554
final ResourceImpl r = new ResourceImpl(templateURI);
560555

@@ -570,8 +565,6 @@ public static DocumentTemplate parse(URIConverter uriConverter, URI templateURI,
570565

571566
final TemplateCustomProperties properties = new TemplateCustomProperties(document);
572567
result.setProperties(properties);
573-
final List<TemplateValidationMessage> messages = parseTemplateCustomProperties(queryEnvironment,
574-
classProvider, properties, document);
575568
r.getContents().add(result);
576569

577570
nextSubTask(monitor, PARSE_TEMPLATE_CUSTOM_PROPERTIES_MONITOR_WORK, "Parsing template body");
@@ -580,9 +573,6 @@ public static DocumentTemplate parse(URIConverter uriConverter, URI templateURI,
580573

581574
final M2DocParser parser = new M2DocParser(document);
582575
final Block documentBody = parser.parseBlock(result.getTemplates(), TokenType.EOF);
583-
for (TemplateValidationMessage validationMessage : messages) {
584-
documentBody.getValidationMessages().add(validationMessage);
585-
}
586576
result.setBody(documentBody);
587577
result.setInputStream(is);
588578
result.setOpcPackage(oPackage);
@@ -618,48 +608,40 @@ public static DocumentTemplate parse(URIConverter uriConverter, URI templateURI,
618608
}
619609

620610
/**
621-
* Parses {@link TemplateCustomProperties} for the given {@link XWPFDocument} and initializes the given {@link IQueryEnvironment}.
611+
* Prepares the given {@link IQueryEnvironment} for the given {@link DocumentTemplate}.
622612
*
623613
* @param queryEnvironment
624614
* the {@link IQueryEnvironment}
625615
* @param classProvider
626616
* the {@link IClassProvider} to use for service Loading
627-
* @param properties
628-
* the {@link TemplateCustomProperties}
629-
* @param document
617+
* @param documentTemplate
630618
* the {@link XWPFDocument}
631-
* @return the {@link List} of {@link TemplateValidationMessage} produced while reading the {@link TemplateCustomProperties}
619+
* @return the {@link List} of {@link TemplateValidationMessage} produced preparing the {@link IQueryEnvironment}.
632620
*/
633-
private static List<TemplateValidationMessage> parseTemplateCustomProperties(IQueryEnvironment queryEnvironment,
634-
IClassProvider classProvider, TemplateCustomProperties properties, XWPFDocument document) {
621+
public static List<TemplateValidationMessage> prepareEnvironment(IQueryEnvironment queryEnvironment,
622+
IClassProvider classProvider, DocumentTemplate documentTemplate) {
635623
final List<TemplateValidationMessage> messages = new ArrayList<>();
624+
final TemplateCustomProperties properties = documentTemplate.getProperties();
636625
final List<String> missingNsURIs = properties.configureQueryEnvironmentWithResult(queryEnvironment);
637626
for (String nsURI : missingNsURIs) {
638-
final XWPFRun run = getOrCreateFirstRun(document);
627+
@SuppressWarnings("resource")
628+
final XWPFRun run = getOrCreateFirstRun(documentTemplate.getDocument());
639629
final TemplateValidationMessage validationMessage = new TemplateValidationMessage(
640630
ValidationMessageLevel.ERROR, "can't find EPackage: " + nsURI, run);
641631
messages.add(validationMessage);
642632
}
643-
for (Entry<String, String> entry : properties.getServiceClasses().entrySet()) {
644-
try {
645-
final Class<?> cls = classProvider.getClass(entry.getKey(), entry.getValue());
646-
final Set<IService<?>> s = ServiceUtils.getServices(queryEnvironment, cls);
647-
ServiceUtils.registerServices(queryEnvironment, s);
648-
} catch (ClassNotFoundException e) {
649-
final XWPFRun run = getOrCreateFirstRun(document);
650-
final TemplateValidationMessage validationMessage = new TemplateValidationMessage(
651-
ValidationMessageLevel.ERROR, "can't load service class: " + entry.getKey(), run);
652-
messages.add(validationMessage);
653-
}
654-
}
655633
final List<String> notLoadedClasses = properties.configureQueryEnvironmentWithResult(queryEnvironment,
656634
classProvider);
657635
for (String notLoadedClass : notLoadedClasses) {
658-
final XWPFRun run = getOrCreateFirstRun(document);
636+
@SuppressWarnings("resource")
637+
final XWPFRun run = getOrCreateFirstRun(documentTemplate.getDocument());
659638
final TemplateValidationMessage validationMessage = new TemplateValidationMessage(
660639
ValidationMessageLevel.ERROR, "can't load service class: " + notLoadedClass, run);
661640
messages.add(validationMessage);
662641
}
642+
for (TemplateValidationMessage validationMessage : messages) {
643+
documentTemplate.getBody().getValidationMessages().add(validationMessage);
644+
}
663645

664646
return messages;
665647
}

tests/org.obeonetwork.m2doc.tests/src/org/obeonetwork/m2doc/tests/AbstractTemplatesTestSuite.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,9 @@ public AbstractTemplatesTestSuite(String testFolder) throws IOException, Documen
166166
final List<Exception> exceptions = new ArrayList<>();
167167
resourceSetForModels = getResourceSetForModel(exceptions);
168168
queryEnvironment = GenconfUtils.getQueryEnvironment(resourceSetForModels, generation, false);
169-
documentTemplate = M2DocUtils.parse(resourceSetForModels.getURIConverter(), templateURI, queryEnvironment,
170-
new ClassProvider(this.getClass().getClassLoader()), new BasicMonitor());
169+
documentTemplate = M2DocUtils.parse(resourceSetForModels.getURIConverter(), templateURI, new BasicMonitor());
170+
M2DocUtils.prepareEnvironment(queryEnvironment, new ClassProvider(this.getClass().getClassLoader()),
171+
documentTemplate);
171172
for (Exception e : exceptions) {
172173
@SuppressWarnings("resource")
173174
final XWPFRun run = M2DocUtils.getOrCreateFirstRun(documentTemplate.getDocument());

tests/org.obeonetwork.m2doc.tests/src/org/obeonetwork/m2doc/tests/generator/TemplateValidationGeneratorTests.java

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2016 Obeo.
2+
* Copyright (c) 2016, 2025 Obeo.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v2.0
55
* which accompanies this distribution, and is available at
@@ -30,6 +30,7 @@
3030
import org.obeonetwork.m2doc.parser.DocumentParserException;
3131
import org.obeonetwork.m2doc.parser.TemplateValidationMessage;
3232
import org.obeonetwork.m2doc.parser.ValidationMessageLevel;
33+
import org.obeonetwork.m2doc.properties.TemplateCustomProperties;
3334
import org.obeonetwork.m2doc.template.DocumentTemplate;
3435
import org.obeonetwork.m2doc.tests.M2DocTestUtils;
3536
import org.obeonetwork.m2doc.util.ClassProvider;
@@ -62,8 +63,11 @@ public void testInfoGeneration()
6263
final File tempFile = File.createTempFile("testParsingErrorSimpleTag", ".docx");
6364

6465
try (DocumentTemplate documentTemplate = M2DocUtils.parse(URIConverter.INSTANCE,
65-
URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), queryEnvironment,
66-
new ClassProvider(this.getClass().getClassLoader()), new BasicMonitor())) {
66+
URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), new BasicMonitor())) {
67+
final TemplateCustomProperties properties = documentTemplate.getProperties();
68+
properties.configureQueryEnvironmentWithResult(queryEnvironment);
69+
properties.configureQueryEnvironmentWithResult(queryEnvironment,
70+
new ClassProvider(this.getClass().getClassLoader()));
6771
final XWPFRun location = ((XWPFParagraph) documentTemplate.getDocument().getBodyElements().get(0)).getRuns()
6872
.get(0);
6973
documentTemplate.getBody().getValidationMessages().add(
@@ -103,8 +107,11 @@ public void testWarningGeneration()
103107
final File tempFile = File.createTempFile("testParsingErrorSimpleTag", ".docx");
104108

105109
try (DocumentTemplate documentTemplate = M2DocUtils.parse(URIConverter.INSTANCE,
106-
URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), queryEnvironment,
107-
new ClassProvider(this.getClass().getClassLoader()), new BasicMonitor())) {
110+
URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), new BasicMonitor())) {
111+
final TemplateCustomProperties properties = documentTemplate.getProperties();
112+
properties.configureQueryEnvironmentWithResult(queryEnvironment);
113+
properties.configureQueryEnvironmentWithResult(queryEnvironment,
114+
new ClassProvider(this.getClass().getClassLoader()));
108115
final XWPFRun location = ((XWPFParagraph) documentTemplate.getDocument().getBodyElements().get(0)).getRuns()
109116
.get(0);
110117
documentTemplate.getBody().getValidationMessages().add(new TemplateValidationMessage(
@@ -144,8 +151,11 @@ public void testErrorGeneration()
144151
final File tempFile = File.createTempFile("testParsingErrorSimpleTag", ".docx");
145152

146153
try (DocumentTemplate documentTemplate = M2DocUtils.parse(URIConverter.INSTANCE,
147-
URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), queryEnvironment,
148-
new ClassProvider(this.getClass().getClassLoader()), new BasicMonitor())) {
154+
URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), new BasicMonitor())) {
155+
final TemplateCustomProperties properties = documentTemplate.getProperties();
156+
properties.configureQueryEnvironmentWithResult(queryEnvironment);
157+
properties.configureQueryEnvironmentWithResult(queryEnvironment,
158+
new ClassProvider(this.getClass().getClassLoader()));
149159
final XWPFRun location = ((XWPFParagraph) documentTemplate.getDocument().getBodyElements().get(0)).getRuns()
150160
.get(0);
151161
documentTemplate.getBody().getValidationMessages().add(
@@ -185,8 +195,11 @@ public void testErrorGenerationOrder()
185195
final File tempFile = File.createTempFile("testParsingErrorSimpleTag", ".docx");
186196

187197
try (DocumentTemplate documentTemplate = M2DocUtils.parse(URIConverter.INSTANCE,
188-
URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), queryEnvironment,
189-
new ClassProvider(this.getClass().getClassLoader()), new BasicMonitor())) {
198+
URI.createFileURI("resources/document/notEmpty/notEmpty-template.docx"), new BasicMonitor())) {
199+
final TemplateCustomProperties properties = documentTemplate.getProperties();
200+
properties.configureQueryEnvironmentWithResult(queryEnvironment);
201+
properties.configureQueryEnvironmentWithResult(queryEnvironment,
202+
new ClassProvider(this.getClass().getClassLoader()));
190203
final XWPFRun location = ((XWPFParagraph) documentTemplate.getDocument().getBodyElements().get(0)).getRuns()
191204
.get(0);
192205
documentTemplate.getBody().getValidationMessages()

0 commit comments

Comments
 (0)