From 99e26fbc0c21d79609a9493fb87624eefb51d73d Mon Sep 17 00:00:00 2001 From: Yvan Lussaud Date: Tue, 17 Oct 2023 15:42:00 +0200 Subject: [PATCH] Fixed #505 The new template creation wizard fail if the EClass of the selected model element has no EAttributes. --- .../m2doc/util/SampleTemplateGenerator.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/util/SampleTemplateGenerator.java b/plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/util/SampleTemplateGenerator.java index 5a4f9daab..053133aff 100644 --- a/plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/util/SampleTemplateGenerator.java +++ b/plugins/org.obeonetwork.m2doc/src/org/obeonetwork/m2doc/util/SampleTemplateGenerator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019 Obeo. + * Copyright (c) 2019, 2023 Obeo. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 * which accompanies this distribution, and is available at @@ -50,7 +50,8 @@ public class SampleTemplateGenerator { private static final int BUFFER_SIZE = 1024 * 8; /** - * Creates the sample template {@link XWPFDocument}. The returned {@link XWPFDocument} should be {@link XWPFDocument#close() closed} by the + * Creates the sample template {@link XWPFDocument}. The returned {@link XWPFDocument} should be {@link XWPFDocument#close() closed} by + * the * caller. * * @param variableName @@ -68,12 +69,17 @@ public XWPFDocument generate(String variableName, EClass eCls) throws InvalidFor final InputStream is = SampleTemplateGenerator.class.getResourceAsStream("/resources/sampleTemplate.docx"); final OPCPackage pkg = OPCPackage.open(is); - String featureName = eCls.getEAllAttributes().get(0).getName(); - for (EAttribute attribute : eCls.getEAllAttributes()) { - if (attribute.getEType() == EcorePackage.eINSTANCE.getEString()) { - featureName = attribute.getName(); - break; + String featureName; + if (!eCls.getEAllAttributes().isEmpty()) { + featureName = eCls.getEAllAttributes().get(0).getName(); + for (EAttribute attribute : eCls.getEAllAttributes()) { + if (attribute.getEType() == EcorePackage.eINSTANCE.getEString()) { + featureName = attribute.getName(); + break; + } } + } else { + featureName = null; } final StringBuilder builder = new StringBuilder(); @@ -86,8 +92,14 @@ public XWPFDocument generate(String variableName, EClass eCls) throws InvalidFor nbBytes = partIS.read(buffer); } } - String xml = builder.toString().replace(VARIABLE_NAME_TAG, variableName); - xml = xml.replace(FEATURE_NAME_TAG, featureName); + String xml; + if (featureName != null) { + xml = builder.toString().replace(VARIABLE_NAME_TAG, variableName); + xml = xml.replace(FEATURE_NAME_TAG, featureName); + } else { + xml = builder.toString().replace(VARIABLE_NAME_TAG, variableName + " EClass"); + xml = xml.replace(FEATURE_NAME_TAG, "eClass().name"); + } try (OutputStream partOS = part.getOutputStream()) { partOS.write(xml.getBytes("UTF-8"));