Commit 2efbe3f9 authored by Philip Makedonski's avatar Philip Makedonski
Browse files

+ add captions to tables #31

parent 9b60c70d
Loading
Loading
Loading
Loading
+104 −15
Original line number Diff line number Diff line
@@ -34,15 +34,24 @@ import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.etsi.mts.tdl.Behaviour;
import org.etsi.mts.tdl.Block;
import org.etsi.mts.tdl.CombinedBehaviour;
import org.etsi.mts.tdl.CompoundBehaviour;
import org.etsi.mts.tdl.ConditionalBehaviour;
import org.etsi.mts.tdl.LocalExpression;
import org.etsi.mts.tdl.Package;
import org.etsi.mts.tdl.SingleCombinedBehaviour;
import org.etsi.mts.tdl.TestConfiguration;
import org.etsi.mts.tdl.TestDescription;
import org.etsi.mts.tdl.resources.ResourceHandler;
import org.etsi.mts.tdl.structuredobjectives.StructuredTestObjective;
import org.etsi.mts.tdl.structuredobjectives.TestObjectiveVariant;
import org.etsi.mts.tdl.structuredobjectives.VariantBinding;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSimpleField;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTText;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;

public class Generator {

@@ -76,15 +85,16 @@ public class Generator {
		createTitle(title, document);
		
		int hLevel = 3;
		String prefix = "X.Y"; //TODO: export to configuration
		if (hierarchical ) {
			String prefix = "X.Y";
			generatePackageHeadings(resource, document, prefix, hLevel);
		} else {
			//flat generation
			EObject p = resource.getContents().get(0);
			List<StructuredTestObjective> stos = EcoreUtil2.getAllContentsOfType(p, StructuredTestObjective.class);
			generateSTOs(document, stos);
			generateTPDs(document, EcoreUtil2.getAllContentsOfType(p, TestDescription.class));
			generateSTOs(document, stos, prefix);
			List<TestDescription> tpds = EcoreUtil2.getAllContentsOfType(p, TestDescription.class);
			generateTPDs(document, tpds, prefix);
		}
		
		storeDocument(filename, document);
@@ -102,35 +112,51 @@ public class Generator {
		for (Package p : packages) {
			generatePackageHeading(document, prefix, i, p, hLevel);
			List<StructuredTestObjective> stos = getContentsOfType(p, StructuredTestObjective.class);
			generateSTOs(document, stos);
			generateTPDs(document, getContentsOfType(p, TestDescription.class));
			generatePackageHeadings(document, getContentsOfType(p, Package.class), prefix+"."+i, hLevel+1);
			String localPrefix = prefix+"."+i;
			generateSTOs(document, stos, localPrefix);
			generateTPDs(document, getContentsOfType(p, TestDescription.class), localPrefix);
			generatePackageHeadings(document, getContentsOfType(p, Package.class), localPrefix, hLevel+1);
			i++;
		}
	}

	private void generateSTOs(XWPFDocument document, List<StructuredTestObjective> stos) {
	private void generateSTOs(XWPFDocument document, List<StructuredTestObjective> stos, String prefix) {
		int i = 1;
		for (StructuredTestObjective sto : stos) {
			String sectionTitle = sto.getName();
			LinkedHashMap<String, String> map = getSTOReplacementMap(sto);
			generateTable(document, sectionTitle, map);
			generateTable(document, sectionTitle, map, prefix, i);
			LinkedHashMap<String, LinkedHashMap<String,String>> variants = getSTOVariantsMap(sto);
			generateVariants(document, variants);
			generateVariants(document, variants, sectionTitle, prefix, i);
			i++;
		}
	}

	private void generateTPDs(XWPFDocument document, List<TestDescription> tpds) {
	private void generateTPDs(XWPFDocument document, List<TestDescription> tpds, String prefix) {
		int i = 1;
		for (TestDescription tpd : tpds) {
			if (tpd.getAnnotation().stream().anyMatch(a -> a.getKey().getName().startsWith("Test Purpose"))) {
				//TODO: add constraints and validation, e.g. Objective present, etc.
				String sectionTitle = tpd.getName();
				LinkedHashMap<String, String> map = getTPDReplacementMap(tpd);
				generateTable(document, sectionTitle, map);
				generateTable(document, sectionTitle, map, prefix, i);
				//TODO: variants? reuse?
	//			LinkedHashMap<String, LinkedHashMap<String,String>> variants = getSTOVariantsMap(sto);
	//			generateVariants(document, variants);
				i++;
			}
		}
		
		//createTableToCtoUpdateFields(document);
	}

	private void createTableToCtoUpdateFields(XWPFDocument document) {
//		Create table of figures field. Word will updating that field while opening the file.
		XWPFParagraph paragraph = document.createParagraph();
		CTSimpleField toc = paragraph.getCTP().addNewFldSimple();
		toc.setInstr("TOC \\c \"tab\" \\* MERGEFORMAT");
		toc.setDirty(STOnOff.TRUE); // set dirty to forcing update
//		TODO: remove again? or move to top?
	}

	
@@ -168,12 +194,14 @@ public class Generator {
	
	//TODO: cleanup and restructure
	private void generateVariants(XWPFDocument document,
			LinkedHashMap<String, LinkedHashMap<String, String>> variants) {
			LinkedHashMap<String, LinkedHashMap<String, String>> variants, String sectionTitle, String prefix, int v) {
		LinkedHashSet<String> headers = new LinkedHashSet<>();
		for (LinkedHashMap<String, String> e : variants.values()) {
			headers.addAll(e.keySet());
		}
		if (headers.size() > 0) {
			createCaption("Variants for "+sectionTitle, document, prefix, v);
			
			XWPFTable table = loadTemplate(template, selectedTemplate+"_VARIANT");
			if (table == null) {
				return;
@@ -278,11 +306,11 @@ public class Generator {
		loadStyles(document, template);
		
		createTitle(title, document);
		
		String prefix = "X.Y.1";
		for (int i = 0; i < 5; i++) {
			String sectionTitle = "Test "+i;
			LinkedHashMap<String, String> map = getReplacementMap();
			generateTable(document, sectionTitle, map);
			generateTable(document, sectionTitle, map, prefix, i);
		}
		
		storeDocument(filename, document);
@@ -296,7 +324,12 @@ public class Generator {
	}

	private void generateTable(XWPFDocument document, String sectionTitle,
			LinkedHashMap<String, String> map) {
			LinkedHashMap<String, String> map, String prefix, int i) {

		//TODO: make configurable
		//TODO: include caption in template?
		createCaption("Test Purpose "+sectionTitle, document, prefix, i);
//		createCaptionWithFieldCodes("Test Purpose "+sectionTitle, document, prefix, i);
		//clone table
		XWPFTable table = loadTemplate(template, selectedTemplate);

@@ -453,6 +486,16 @@ public class Generator {
		map.put(Placeholders.CONFIGURATION, config);
		//TODO: how is this addressed? annotations? what about and/or? -> not supported yet
		String pics = "N/A";
		//TODO: make more robust
		SingleCombinedBehaviour tpdBehaviour = (SingleCombinedBehaviour) tpd.getBehaviourDescription().getBehaviour();
		EList<LocalExpression> guard = tpdBehaviour.getBlock().getGuard();
		if (!guard.isEmpty()) {
			ICompositeNode guardNode = NodeModelUtils.findActualNodeFor(guard.get(0));
			String tokenText = NodeModelUtils.getTokenText(guardNode);
			pics = tokenText
//					.replaceAll("\\(|\\)", "")
					;
		}
//		String pics = String.join(" ", tpd.getPicsReference()
//				.stream()
//				.map(p -> NodeModelUtils.getNode(p).getText())
@@ -711,6 +754,52 @@ public class Generator {
		titleRun.setFontSize(20);
	}

	public void createCaption(String text, XWPFDocument report, String prefix, int i) {
		//implementation without field codes and automated numbering
		XWPFParagraph paragraph = report.createParagraph();
	    paragraph.setStyle("TH");
	    paragraph.setSpacingAfter(0); // Set spacing after to 0. So caption will follow immediately under the figure.
	    XWPFRun run = paragraph.createRun();
	    run.setText("Table "+prefix+"-"+i+": " + text);
	}
	
	
	public void createCaptionWithFieldCodes(String text, XWPFDocument report, String prefix, int i) {
		//implementation with field codes if needed, more complex and requires 
		XWPFParagraph paragraph = report.createParagraph();
	    paragraph.setStyle("TH");
	    paragraph.setSpacingAfter(0); // Set spacing after to 0. So caption will follow immediately under the figure.
	    XWPFRun run = paragraph.createRun();
	    run.setText("Table ");
	    CTR ctr;
	    CTRPr ctrpr;
	    CTText ctText;

	    CTSimpleField seq = paragraph.getCTP().addNewFldSimple();
	    String sectionNumberPrefix = " Heading 3 2 \\s "; // TODO: Does not quite work yet, updating references causes error, probably needs headings to use field codes as well
	    seq.setInstr(sectionNumberPrefix);
	    ctr = seq.addNewR();
	    ctrpr = ctr.addNewRPr();
	    ctrpr.addNewNoProof();
	    ctText = ctr.addNewT();
	    ctText.setStringValue(""+prefix); //set numbers manually? alternatively need to manually or automatically force update

	    ctr = paragraph.getCTP().addNewR();
	    ctr.addNewNoBreakHyphen();

	    seq = paragraph.getCTP().addNewFldSimple();
	    String tableNumberText = " SEQ tab \\* ARABIC \\s 2 ";
	    seq.setInstr(tableNumberText);
	    ctr = seq.addNewR();
	    ctrpr = ctr.addNewRPr();
	    ctrpr.addNewNoProof();
	    ctText = ctr.addNewT();
	    ctText.setStringValue(""+i); //set numbers manually? alternatively need to manually or automatically force update

	    run = paragraph.createRun(); // needed or numbers are at end, ie Figure : some text1.1
	    run.setText(": " + text);
	}

	private void loadStyles(XWPFDocument report, XWPFDocument templateDocument) {
		XWPFStyles newStyles = report.createStyles();
		try {