Skip to content

Commit

Permalink
#61 First draft: Use workaround to format inlineTags in Jsoup
Browse files Browse the repository at this point in the history
Problem: Java reflection should not be used to change the inlineTags in Jsoup
  • Loading branch information
helkv committed Aug 28, 2023
1 parent ba5fbff commit 8dc51a7
Showing 1 changed file with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import edu.harvard.iq.dataverse.branding.BrandingUtil;

import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand All @@ -27,6 +29,7 @@
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.parser.Tag;
import org.jsoup.select.Elements;

/**
Expand Down Expand Up @@ -597,17 +600,55 @@ private String addGrantInformation(DvObject dvObject, String xmlMetadata) {

String grantInformation = sb.toString();

//TODO: Replace this Workaround to format leaf node tags as inline tags in Jsoup using reflection.
this.formatLeafNodeAsInlineTagsInJsoup(xmlMetadata);
Document doc = Jsoup.parse(xmlMetadata, "", Parser.xmlParser());
Element resourceElement = doc.select("resource").get(0);
Element grantElement = doc.createElement("fundingReferences");
grantElement.html(grantInformation);
resourceElement.appendChild(grantElement);
doc.outputSettings().indentAmount(4).prettyPrint(true); //Problem: formatAsInlineTags can not be changed
doc.outputSettings().indentAmount(4).prettyPrint(true);
xmlMetadata = doc.toString();

return xmlMetadata;
}


/**
*
* This method is used to format leaf node tags as inline tags in Jsoup using reflection.
* See: https://github.com/jhy/jsoup/issues/1428
*
* @param xmlMetadata
*/
private void formatLeafNodeAsInlineTagsInJsoup(String xmlMetadata){
try {
Document doc = Jsoup.parse(xmlMetadata, "", Parser.xmlParser());

//String[] inlineTags = {"identifier", "publisher", "publicationYear", "description", "contributors", "funderName"};
List<String> inlineTags = new java.util.ArrayList<String>();
doc.getAllElements().forEach(element -> {
if (element.childrenSize() == 0) {
inlineTags.add(element.tagName());
}
});

for(String tagName : inlineTags) {
Tag tag = Tag.valueOf(tagName);
Field field = null;
field = Tag.class.getDeclaredField("formatAsBlock");
field.setAccessible(true);
field.set(tag, false);

Method method = Tag.class.getDeclaredMethod("register", Tag.class);
method.setAccessible(true);
method.invoke(null, tag);
}
} catch (Exception e) {
logger.log(Level.WARNING, "Error changing the format for Jsoup: " + e.getMessage());
}
}

private String generateRelatedIdentifiers(DvObject dvObject) {

StringBuilder sb = new StringBuilder();
Expand Down

0 comments on commit 8dc51a7

Please sign in to comment.