Skip to content

Commit

Permalink
Export: Make license in publication table a hyperlink (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinFutterer authored Feb 14, 2024
1 parent 3d0fc86 commit ed284a3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ static void replaceInParagraphs(List<XWPFParagraph> xwpfParagraphs, Map<String,
for (Map.Entry<String, String> entry : replacements.entrySet()) {
if (xwpfRunText != null && xwpfRunText.contains(entry.getKey())) {
//handle new line for contributor list and storage information
if (entry.getValue().contains(";")){
String[] value=entry.getValue().split(";");
for(int i = 0; i < value.length; i++){
if (entry.getValue().contains(";")) {
String[] value = entry.getValue().split(";");
for (int i = 0; i < value.length; i++) {
xwpfParagraph.setAlignment(ParagraphAlignment.LEFT);
xwpfRun.setText(value[i].trim());
if (i < value.length - 1) {
Expand All @@ -79,7 +79,7 @@ static void replaceInParagraphs(List<XWPFParagraph> xwpfParagraphs, Map<String,
//general case for non contributor list
else {
if (entry.getKey().equals("[projectname]") && entry.getValue().contains("#oversize")) { //resize title to be smaller
xwpfRun.setFontSize(xwpfRun.getFontSize()-4);
xwpfRun.setFontSize(xwpfRun.getFontSize() - 4);
xwpfRunText = xwpfRunText.replace(entry.getKey(), entry.getValue().replace("#oversize", ""));
} else if (entry.getValue().contains("#color_green")) { // set the color to be green
xwpfRun.setColor("92D050");
Expand Down Expand Up @@ -108,7 +108,7 @@ public void addReplacement(Map<String, String> replacements, String variable, Ob
&& (dmpContent.getClass() == java.sql.Timestamp.class || dmpContent.getClass() == Date.class)) {
content = formatter.format(dmpContent);
}

replacements.put(variable, content);
}

Expand Down Expand Up @@ -431,4 +431,36 @@ public List<XWPFTable> getAllTables(XWPFDocument doc) {
}
return tables;
}

/**
* Replaces a run with a hyperlink version of itself. Replaces styling, but keeps fontsize and font family.
* As of apache 4.1.2 there is no way to insert a hyperlink run anywhere but the end. Therefore, the whole
* paragraph has to be deleted and rebuilt after the run in question.
*
* @param run
* @param URI
*/
void turnRunIntoHyperlinkRun(XWPFRun run, String URI) {
XWPFParagraph paragraph = (XWPFParagraph) run.getParent();
int runPos = paragraph.getRuns().indexOf(run);
int runsAfterHyperlinkRun = paragraph.getRuns().size() - runPos - 1;

XWPFHyperlinkRun hyperlink = paragraph.createHyperlinkRun(URI);
hyperlink.setStyle("Hyperlink");
hyperlink.setFontSize(run.getFontSize());
hyperlink.setFontFamily(run.getFontFamily());
hyperlink.setText(run.getText(0));

paragraph.removeRun(runPos);

for (int i = 0; i < runsAfterHyperlinkRun; i++) {
XWPFRun runToRemove = paragraph.getRuns().get(runPos);

XWPFRun createdRun = paragraph.createRun();
createdRun.getCTR().setRPr(runToRemove.getCTR().getRPr());
createdRun.setText(runToRemove.getText(0));

paragraph.removeRun(runPos);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import at.ac.tuwien.damap.rest.dmp.domain.ProjectDO;
import lombok.extern.jbosslog.JBossLog;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTVMerge;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -867,6 +868,7 @@ public void composeTableReusedDatasets(XWPFDocument document, XWPFTable xwpfTabl

if (reusedDatasets.get(i).getLicense() != null) {
//TODO second String license option for reused datasets.
//TODO use addHyperlinkRun to create hyperlinks - see publication table
docVar.add(""); }
else {
docVar.add("");
Expand Down Expand Up @@ -957,8 +959,8 @@ public void composeTableDatasetPublication(XWPFTable xwpfTable){
if (newDatasets.size() > 0) {
for (int i = 0; i < newDatasets.size(); i++) {

XWPFTableRow sourceTableRow = xwpfTable.getRow(2);
XWPFTableRow newRow = new XWPFTableRow(sourceTableRow.getCtRow(), xwpfTable);
XWPFTableRow sourceTableRow = xwpfTable.getRow(i + 2);
XWPFTableRow newRow = null;

try {
newRow = insertNewTableRow(sourceTableRow, i + 2);
Expand Down Expand Up @@ -1034,6 +1036,15 @@ public void composeTableDatasetPublication(XWPFTable xwpfTable){
docVar.add("");

insertTableCells(xwpfTable, newRow, docVar);

if (newDatasets.get(i).getLicense() != null
&& !newDatasets.get(i).getDataAccess().equals(EDataAccessType.CLOSED)) {

ELicense license = newDatasets.get(i).getLicense();
XWPFParagraph paragraph = newRow.getCell(6).getParagraphs().get(0);
turnRunIntoHyperlinkRun(paragraph.getRuns().get(0), license.getUrl());
commitTableRows(xwpfTable);
}
}
xwpfTable.removeRow(xwpfTable.getRows().size() - 1);
} else {
Expand Down

0 comments on commit ed284a3

Please sign in to comment.