Skip to content

Commit

Permalink
Export: Made license in publication table a hyperlink
Browse files Browse the repository at this point in the history
  • Loading branch information
ValentinFutterer committed Feb 5, 2024
1 parent ea00833 commit b8fe874
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import lombok.extern.jbosslog.JBossLog;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHyperlink;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow;

import javax.inject.Inject;
Expand All @@ -36,7 +35,7 @@ public abstract class AbstractTemplateExportFunctions {
* @return
* @throws Exception
*/
public XWPFDocument loadTemplate (InputStream template, String startChar, String endChar) throws Exception{
public XWPFDocument loadTemplate(InputStream template, String startChar, String endChar) throws Exception {
//Extract document using Apache POI https://poi.apache.org/
XWPFDocument document = new XWPFDocument(template);

Expand All @@ -60,9 +59,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 +78,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 +107,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 All @@ -122,7 +121,7 @@ public void addReplacement(Map<String, String> replacements, String variable, Ob
* @throws XmlException
* @throws Exception
*/
public XWPFTableRow insertNewTableRow(XWPFTableRow sourceTableRow, int pos) throws XmlException, IOException {
public XWPFTableRow insertNewTableRow(XWPFTableRow sourceTableRow, int pos) throws XmlException, IOException {
XWPFTable table = sourceTableRow.getTable();
CTRow newCTRrow = CTRow.Factory.parse(sourceTableRow.getCtRow().newInputStream());
XWPFTableRow tableRow = new XWPFTableRow(newCTRrow, table);
Expand Down Expand Up @@ -155,7 +154,7 @@ static void insertTableCells(XWPFTable table, XWPFTableRow newRow, ArrayList<Str
* @param table
* @param replacements
*/
static void replaceTableVariables(XWPFTable table, Map<String, String> replacements){
static void replaceTableVariables(XWPFTable table, Map<String, String> replacements) {
//this replaces variables in tables (e.g. costcurrency)
List<XWPFTableRow> tableRows = table.getRows();
for (XWPFTableRow xwpfTableRow : tableRows) {
Expand Down Expand Up @@ -263,7 +262,7 @@ public XWPFDocument templateFormatting(XWPFDocument document, String startChar,
* @param startChar
* @param endChar
*/
public void formattingTable(List<XWPFTable> xwpfTables, String startChar, String endChar){
public void formattingTable(List<XWPFTable> xwpfTables, String startChar, String endChar) {
if (xwpfTables != null) {
for (XWPFTable xwpfTable : xwpfTables) {
for (XWPFTableRow row : xwpfTable.getRows()) {
Expand Down Expand Up @@ -300,20 +299,18 @@ public void formattingParagraph(List<XWPFParagraph> xwpfParagraphs, String start
if (!xwpfRunText.contains(endChar)) {
removeRunIndex.add(xwpfRuns.indexOf(xwpfRun));
mergeRun = true;
if (sb.length()>0) {
if (sb.length() > 0) {
sb.delete(0, sb.length());
}
sb.append(xwpfRunText);
}
}
else {
} else {
if (mergeRun) {
sb.append(xwpfRunText);
if (xwpfRunText.contains(endChar)) {
mergeRun = false;
xwpfRun.setText(sb.toString(),0);
}
else {
xwpfRun.setText(sb.toString(), 0);
} else {
removeRunIndex.add(xwpfRuns.indexOf(xwpfRun));
}
}
Expand All @@ -331,20 +328,34 @@ public void formattingParagraph(List<XWPFParagraph> xwpfParagraphs, String start
}

/**
* Appends a HyperlinkRun to the input paragraph and sets the text
* @param paragraph
* 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
* @param text
*/
void addHyperlinkRun(XWPFParagraph paragraph, String URI, String text) {
String rId = paragraph.getDocument().getPackagePart().addExternalRelationship(URI,
XWPFRelation.HYPERLINK.getRelation()).getId();
void turnRunIntoHyperlinkRun(XWPFRun run, String URI) {
XWPFParagraph paragraph = (XWPFParagraph) run.getParent();
int runPos = paragraph.getRuns().indexOf(run);
int runsAfterHyperlinkRun = paragraph.getRuns().size() - runPos - 1;

CTHyperlink cthyperLink = paragraph.getCTP().addNewHyperlink();
cthyperLink.setId(rId);
cthyperLink.addNewR();
XWPFHyperlinkRun hyperlink = paragraph.createHyperlinkRun(URI);
hyperlink.setStyle("Hyperlink");
hyperlink.setFontSize(run.getFontSize());
hyperlink.setFontFamily(run.getFontFamily());
hyperlink.setText(run.getText(0));

XWPFHyperlinkRun hyperlink = new XWPFHyperlinkRun(cthyperLink, cthyperLink.getRArray(0), paragraph);
((XWPFRun) hyperlink).setText(text);
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 @@ -1036,8 +1036,7 @@ public void composeTableDatasetPublication(XWPFTable xwpfTable){

ELicense license = newDatasets.get(i).getLicense();
XWPFParagraph paragraph = newRow.getCell(6).getParagraphs().get(0);
paragraph.removeRun(0);
addHyperlinkRun(paragraph, license.getUrl(), license.getAcronym());
turnRunIntoHyperlinkRun(paragraph.getRuns().get(2), license.getUrl());
commitTableRows(xwpfTable);
}
}
Expand Down

0 comments on commit b8fe874

Please sign in to comment.