Skip to content
Snippets Groups Projects
Commit 8f090875 authored by felmer's avatar felmer
Browse files

Merge branch 'master' of sissource.ethz.ch:sispub/openbis

parents 3862a4d8 40a13b6e
No related branches found
No related tags found
No related merge requests found
package ch.ethz.sis; package ch.ethz.sis;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
...@@ -11,130 +10,147 @@ import org.docx4j.openpackaging.parts.PartName; ...@@ -11,130 +10,147 @@ import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart; import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart;
import org.docx4j.relationships.Relationship; import org.docx4j.relationships.Relationship;
import org.docx4j.wml.CTAltChunk; import org.docx4j.wml.CTAltChunk;
import org.jsoup.Jsoup;
public class DOCXBuilder import org.jsoup.nodes.Document;
{ import org.jsoup.nodes.Element;
public static void main(String[] args) throws Exception import org.jsoup.select.Elements;
{
DOCXBuilder docx = new DOCXBuilder(); public class DOCXBuilder {
docx.addTitle("TitleA"); public static void main(String[] args) throws Exception {
docx.addHeader("MetaA"); DOCXBuilder docx = new DOCXBuilder();
docx.addProperty("PropertyA", "ValueA"); docx.addTitle("TitleA");
docx.addProperty("PropertyB", "ValueB"); docx.addHeader("MetaA");
docx.addProperty("PropertyC", "<p>I am normal</p><p style=\"color:red;\">I am red</p><p style=\"color:blue;\">I am blue</p><p style=\"font-size:36px;\">I am big</p>"); docx.addProperty("PropertyA", "ValueA");
docx.addProperty("PropertyB", "ValueB");
FileOutputStream out = new FileOutputStream(new File("wordFromHTML.docx")); docx.addProperty("PropertyC",
out.write(docx.getDocBytes()); "<p>I am normal</p><p style=\"color:red;\">I am red</p><p style=\"color:blue;\">I am blue</p><p style=\"font-size:36px;\">I am big</p>");
out.close();
} FileOutputStream out = new FileOutputStream(new File("wordFromHTML.docx"));
out.write(docx.getDocBytes());
private static final String START_RICH_TEXT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<html><head></head><body>"; out.close();
}
private static final String END_RICH_TEXT = "</body></html>";
private static final String START_RICH_TEXT = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<html><head></head><body>";
private StringBuffer doc;
private static final String END_RICH_TEXT = "</body></html>";
private boolean closed;
private StringBuffer doc;
public DOCXBuilder()
{ private boolean closed;
closed = false;
doc = new StringBuffer(); public DOCXBuilder() {
startDoc(); closed = false;
} doc = new StringBuffer();
startDoc();
private void startDoc() }
{
if (!closed) private void startDoc() {
{ if (!closed) {
doc.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); doc.append(
doc.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">"); "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
doc.append("<head></head>"); doc.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
doc.append("<body>"); doc.append("<head></head>");
} doc.append("<body>");
} }
}
private void endDoc()
{ private void endDoc() {
if (!closed) if (!closed) {
{ doc.append("</body>");
doc.append("</body>"); doc.append("</html>");
doc.append("</html>"); closed = true;
closed = true; }
} }
}
public void addProperty(String key, String value) {
public void addProperty(String key, String value) if (!closed) {
{ doc.append("<p>").append("<b>").append(key).append(": ").append("</b>").append("</p>");
if (!closed) addParagraph(value);
{ }
value = cleanXMLEnvelope(value); }
doc.append("<p>").append("<b>").append(key).append(": ").append("</b>").append(value).append("</p>");
}
}
public void addParagraph(String value) public void addParagraph(String value)
{ {
if (!closed) if (!closed)
{ {
value = cleanXMLEnvelope(value); value = cleanXMLEnvelope(value);
doc.append("<p>").append(value).append("</p>"); doc.append("<p>").append(getImgFixed(value)).append("</p>");
}
}
public void addTitle(String title)
{
if (!closed)
{
doc.append("<h1>").append(title).append("</h1>");
}
}
public void addHeader(String header)
{
if (!closed)
{
doc.append("<h2>").append(header).append("</h2>");
} }
} }
public byte[] getHTMLBytes() throws Exception public void addTitle(String title) {
{ if (!closed) {
endDoc(); doc.append("<h1>").append(title).append("</h1>");
return doc.toString().getBytes(); }
} }
public byte[] getDocBytes() throws Exception public void addHeader(String header) {
{ if (!closed) {
// .. Finish Document doc.append("<h2>").append(header).append("</h2>");
endDoc(); }
}
// .. HTML Code
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); public byte[] getHTMLBytes() throws Exception {
AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html")); endDoc();
afiPart.setBinaryData(doc.toString().getBytes()); return doc.toString().getBytes();
afiPart.setContentType(new ContentType("text/html")); }
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
public byte[] getDocBytes() throws Exception {
// .. the bit in document body // .. Finish Document
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk(); endDoc();
ac.setId(altChunkRel.getId());
wordMLPackage.getMainDocumentPart().addObject(ac); // .. HTML Code
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
// .. content type AlternativeFormatInputPart afiPart = new AlternativeFormatInputPart(new PartName("/hw.html"));
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html"); afiPart.setBinaryData(doc.toString().getBytes());
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); afiPart.setContentType(new ContentType("text/html"));
wordMLPackage.save(outStream); Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(afiPart);
return outStream.toByteArray(); // .. the bit in document body
} CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId());
private String cleanXMLEnvelope(String value) wordMLPackage.getMainDocumentPart().addObject(ac);
{
if (value.startsWith(START_RICH_TEXT) && value.endsWith(END_RICH_TEXT)) // .. content type
{ wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
value = value.substring(START_RICH_TEXT.length() + 3, value.length() - END_RICH_TEXT.length()); ByteArrayOutputStream outStream = new ByteArrayOutputStream();
} wordMLPackage.save(outStream);
return value;
} return outStream.toByteArray();
}
private String cleanXMLEnvelope(String value) {
if (value.startsWith(START_RICH_TEXT) && value.endsWith(END_RICH_TEXT)) {
value = value.substring(START_RICH_TEXT.length() + 3, value.length() - END_RICH_TEXT.length());
}
return value;
}
private String getImgFixed(String value) {
Document doc = Jsoup.parse(value);
Elements elements = doc.select("img");
for (Element element : elements) {
String style = element.attr("style");
if (style != null) {
String[] rules = style.split(";");
if(rules != null) {
for (int rIdx = 0; rIdx < rules.length; rIdx++) {
String rule = rules[rIdx];
String[] ruleElements = rule.split(":");
if (ruleElements != null && ruleElements.length == 2) {
String ruleKey = ruleElements[0].trim();
String ruleValue = ruleElements[1].trim();
if ((ruleKey.toLowerCase().equals("width") || ruleKey.toLowerCase().equals("height"))
&& ruleValue.endsWith("px")) {
element.attr(ruleKey, ruleValue.substring(0, ruleValue.length() - 2));
}
}
}
}
}
}
return doc.html();
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment