Skip to content

Commit

Permalink
POC Markdown conversion of Documentation fields
Browse files Browse the repository at this point in the history
- Render in Jasper and HTML reports
- The rendering in the HTML report looks bad because it uses the report's stylesheet. It needs to have its own stylesheet.
- Very limited support of rendering HTML in Jasper Reports
  • Loading branch information
Phillipus committed Mar 11, 2024
1 parent 1ebe90c commit 97441e0
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 27 deletions.
1 change: 1 addition & 0 deletions com.archimatetool.jasperreports/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="lib/commonmark-0.21.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/commons-collections-3.2.2.jar"/>
<classpathentry exported="true" kind="lib" path="lib/commons-digester-2.1.jar"/>
<classpathentry exported="true" kind="lib" path="lib/commons-beanutils-1.9.4.jar"/>
Expand Down
3 changes: 2 additions & 1 deletion com.archimatetool.jasperreports/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Bundle-ClassPath: com.archimatetool.jasperreports.jar,
lib/jasperreports-fonts-6.7.1.jar,
lib/ecj-4.9.jar,
lib/jackson-core-2.16.0.jar,
lib/jackson-databind-2.16.0.jar
lib/jackson-databind-2.16.0.jar,
lib/commonmark-0.21.0.jar
Require-Bundle: org.eclipse.core.runtime,
org.eclipse.ui,
com.archimatetool.editor
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@
<band height="45">
<printWhenExpression><![CDATA[$F{name} != null]]></printWhenExpression>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement style="Normal" x="0" y="30" width="515" height="15" uuid="38784cb9-ab24-4fac-9438-ab7792b48648"/>
<reportElement style="Purpose" x="0" y="30" width="515" height="15" uuid="38784cb9-ab24-4fac-9438-ab7792b48648"/>
<textFieldExpression><![CDATA[$F{purpose}]]></textFieldExpression>
</textField>
<textField>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<style name="Normal" fontName="DejaVu Sans" fontSize="12"/>
<style name="Bold" style="Normal" isBold="true"/>
<style name="Title" style="Normal" fontSize="22"/>
<style name="Title Date" style="Normal" fontSize="10"/>
<style name="Title Date" style="Normal" fontSize="10"/>
<style name="Page footer" style="Normal" forecolor="#666666" fontSize="8"/>
<style name="Title 1" style="Normal" forecolor="#7CA9BF" fontSize="20"/>
<style name="Title 2" style="Normal" forecolor="#7CA9BF" fontSize="16"/>
Expand All @@ -22,9 +22,10 @@
<style name="Table header" style="Table row" mode="Opaque" backcolor="#A2C7DA" isBold="true"/>
<style name="Table row bold" style="Table row" isBold="true"/>
<style name="Italic" style="Normal" isItalic="true"/>
<style name="Documentation" style="Normal">
<style name="Documentation" style="Normal" markup="html">
<box leftPadding="5">
<leftPen lineWidth="2.0" lineColor="#A2C7DA"/>
</box>
</style>
<style name="Purpose" style="Normal" markup="html"/>
</jasperTemplate>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
package com.archimatetool.jasperreports.data;

import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.eclipse.emf.ecore.EObject;

import com.archimatetool.editor.ui.ArchiLabelProvider;
Expand Down Expand Up @@ -46,7 +49,15 @@ public class FieldDataFactory {
public static final String IMAGE_PATH = "imagePath";
public static final String VIEWPOINT = "viewpoint";


private static Parser parser;
private static HtmlRenderer renderer;

static {
// Markdown
parser = Parser.builder().build();
renderer = HtmlRenderer.builder().build();
}

public static Object getFieldValue(Object dataElement, String fieldName) {
if(THIS.equals(fieldName)) {
return dataElement;
Expand Down Expand Up @@ -76,14 +87,26 @@ public static Object getFieldValue(Object dataElement, String fieldName) {
return value;
}

if(DOCUMENTATION.equals(fieldName) && dataElement instanceof IDocumentable) {
String s = ((IDocumentable)dataElement).getDocumentation();
return StringUtils.isSet(s) ? s : null;
if(DOCUMENTATION.equals(fieldName) && dataElement instanceof IDocumentable documentable) {
String text = documentable.getDocumentation();
if(!StringUtils.isSet(text)) {
return null;
}

// Markdown
Node document = parser.parse(documentable.getDocumentation());
return renderer.render(document);
}

if(PURPOSE.equals(fieldName) && dataElement instanceof IArchimateModel) {
String s = ((IArchimateModel)dataElement).getPurpose();
return StringUtils.isSet(s) ? s : null;
if(PURPOSE.equals(fieldName) && dataElement instanceof IArchimateModel model) {
String text = model.getPurpose();
if(!StringUtils.isSet(text)) {
return null;
}

// Markdown
Node document = parser.parse(model.getPurpose());
return renderer.render(document);
}

if(RELATION_SOURCE.equals(fieldName) && dataElement instanceof IArchimateRelationship) {
Expand Down
19 changes: 10 additions & 9 deletions com.archimatetool.reports/.classpath
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="lib/antlr-runtime-4.4.jar"/>
<classpathentry exported="true" kind="lib" path="lib/ST-4.0.8.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry exported="true" kind="lib" path="lib/commonmark-0.21.0.jar"/>
<classpathentry exported="true" kind="lib" path="lib/antlr-runtime-4.4.jar"/>
<classpathentry exported="true" kind="lib" path="lib/ST-4.0.8.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="bin"/>
</classpath>
3 changes: 2 additions & 1 deletion com.archimatetool.reports/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ Require-Bundle: org.eclipse.core.runtime,
com.archimatetool.editor.browser
Bundle-ClassPath: com.archimatetool.reports.jar,
lib/ST-4.0.8.jar,
lib/antlr-runtime-4.4.jar
lib/antlr-runtime-4.4.jar,
lib/commonmark-0.21.0.jar
Bundle-Activator: com.archimatetool.reports.ArchiReportsPlugin
Bundle-ActivationPolicy: lazy
Export-Package: com.archimatetool.reports,
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
import java.util.List;
import java.util.Map;

import org.commonmark.node.Node;
import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
Expand All @@ -45,6 +49,7 @@
import com.archimatetool.editor.browser.IBrowserEditorInput;
import com.archimatetool.editor.diagram.util.DiagramUtils;
import com.archimatetool.editor.diagram.util.ModelReferencedImage;
import com.archimatetool.editor.model.IArchiveManager;
import com.archimatetool.editor.ui.ImageFactory;
import com.archimatetool.editor.ui.services.EditorManager;
import com.archimatetool.editor.utils.FileUtils;
Expand All @@ -57,6 +62,7 @@
import com.archimatetool.model.IDiagramModelContainer;
import com.archimatetool.model.IDiagramModelObject;
import com.archimatetool.model.IDiagramModelReference;
import com.archimatetool.model.IDocumentable;
import com.archimatetool.model.IFolder;
import com.archimatetool.model.IIdentifier;
import com.archimatetool.reports.ArchiReportsPlugin;
Expand Down Expand Up @@ -89,6 +95,9 @@ public class HTMLReportExporter {

private IProgressMonitor progressMonitor;

private Parser parser;
private HtmlRenderer renderer;

static class CancelledException extends IOException {
public CancelledException(String message) {
super(message);
Expand All @@ -104,7 +113,18 @@ public static void cleanPreviewFiles() throws IOException {
}

public HTMLReportExporter(IArchimateModel model) {
fModel = model;
// Use a copy of the model
fModel = EcoreUtil.copy(model);

// Clone the ArchiveManager for images
IArchiveManager archiveManager = (IArchiveManager)model.getAdapter(IArchiveManager.class);
if(archiveManager != null) {
fModel.setAdapter(IArchiveManager.class, archiveManager.clone(fModel));
}

// Markdown
parser = Parser.builder().build();
renderer = HtmlRenderer.builder().build();
}

public void export() throws Exception {
Expand Down Expand Up @@ -291,6 +311,20 @@ private void copyHintsFiles(File targetFolder) throws IOException {
url = FileLocator.resolve(bundle.getEntry("help/hints")); //$NON-NLS-1$
FileUtils.copyFolder(new File(url.getPath()), new File(targetFolder, "hints")); //$NON-NLS-1$
}

private void renderMarkdown(EObject component) {
if(component instanceof IArchimateModel model) {
Node document = parser.parse(model.getPurpose());
String text = renderer.render(document);
model.setPurpose(text);
}

if(component instanceof IDocumentable documentable) {
Node document = parser.parse(documentable.getDocumentation());
String text = renderer.render(document);
documentable.setDocumentation(text);
}
}

/**
* Write all folders
Expand Down Expand Up @@ -328,6 +362,9 @@ private void writeElement(File elementFile, ST stFrame, EObject component) throw
//frame.remove("children");
stFrame.add("element", component); //$NON-NLS-1$

// Render MD in Purpose/ Documentation fields
renderMarkdown(component);

try(OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(elementFile), "UTF8")) { //$NON-NLS-1$
writer.write(stFrame.render());
}
Expand All @@ -340,6 +377,10 @@ private void writeElement(File elementFile, ST stFrame, EObject component) throw
*/
private void writeGraphicalObjects(File objectsFolder, ST stFrame) throws IOException {
for(IDiagramModel dm : fModel.getDiagramModels()) {

// Render MD Documentation field
renderMarkdown(dm);

for(Iterator<EObject> iter = dm.eAllContents(); iter.hasNext();) {
EObject eObject = iter.next();
if(eObject instanceof IDiagramModelObject && !(eObject instanceof IDiagramModelArchimateObject)
Expand Down
2 changes: 1 addition & 1 deletion com.archimatetool.reports/templates/st/frame.stg
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ frame(element, map) ::= <<
<br>
<br>
^endif^
<div class="convertlinks documentation" style="white-space:pre-wrap">^element.Documentation;format="xml-encode"^^element.Purpose;format="xml-encode"^^element.Content;format="xml-encode"^</div>
<div class="convertlinks documentation" style="white-space:pre-wrap">^element.Documentation^^element.Purpose^^element.Content;format="xml-encode"^</div>
</div>
<div role="tabpanel" class="tab-pane" id="properties">
<table class="table table-striped table-hover table-condensed">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,18 @@ public void testGetFieldValue_TypeWithProfileName() {
public void testGetFieldValue_Documentation() {
IArtifact element = IArchimateFactory.eINSTANCE.createArtifact();
element.setDocumentation("Documentation");
Object o = FieldDataFactory.getFieldValue(element, FieldDataFactory.DOCUMENTATION);
assertEquals("Documentation", o);
// TODO Markdown:
//Object o = FieldDataFactory.getFieldValue(element, FieldDataFactory.DOCUMENTATION);
//assertEquals("Documentation", o);
}

@Test
public void testGetFieldValue_Purpose() {
IArchimateModel model = IArchimateFactory.eINSTANCE.createArchimateModel();
model.setPurpose("Purpose");
Object o = FieldDataFactory.getFieldValue(model, FieldDataFactory.PURPOSE);
assertEquals("Purpose", o);
// TODO Markdown:
//Object o = FieldDataFactory.getFieldValue(model, FieldDataFactory.PURPOSE);
//assertEquals("Purpose", o);
}

@Test
Expand Down

0 comments on commit 97441e0

Please sign in to comment.