Skip to content

Commit

Permalink
Fixed #505 The new template creation wizard fail if the EClass of the…
Browse files Browse the repository at this point in the history
… selected model element has no EAttributes.
  • Loading branch information
ylussaud committed Oct 17, 2023
1 parent 46fec3e commit 99e26fb
Showing 1 changed file with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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"));
Expand Down

0 comments on commit 99e26fb

Please sign in to comment.