ContentConverterUtils.java
8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.dom4j.Document
* org.dom4j.Element
* org.dom4j.Node
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.forms.foundation.util;
import com.adobe.forms.foundation.util.XMLHelperUtil;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ContentConverterUtils {
private static final Logger log = LoggerFactory.getLogger((String)ContentConverterUtils.class.getName());
public static final Pattern xhtmlPattern = Pattern.compile("<body.*?>", 2);
public static final Pattern tlfPattern = Pattern.compile("<TextFlow.*?>", 2);
public static final Pattern flashHtmlPattern = Pattern.compile("<TEXTFORMAT.*?>", 2);
private static Pattern letterSpacingInPxPattern = Pattern.compile("(<span\\s+style\\s*=\\s*['\"].*letter-spacing\\s*:\\s*\\d+)px(\\s*[;'\"].*\\s*>)", 2);
private static Pattern fontSizeInPxPattern = Pattern.compile("(<span\\s+style\\s*=\\s*['\"].*font-size\\s*:\\s*\\d+)px(\\s*[;'\"].*\\s*>)", 2);
private static boolean autoCorrectPixelStyles = true;
private static final double[] HEADER_STYLE = new double[]{0.0, 18.0, 16.5, 13.5, 12.0, 9.0, 7.5};
public static String convertToXFAHTML(String content) throws UnsupportedEncodingException {
String resolvedContent = ContentConverterUtils.processNewLines(content);
resolvedContent = ContentConverterUtils.convertNewLinesToXhml(resolvedContent);
resolvedContent = ContentConverterUtils.updateHeaderTags(resolvedContent);
resolvedContent = ContentConverterUtils.convertPixelStylesToPoints(resolvedContent);
return resolvedContent;
}
public static String extractPlainText(String richText, boolean escapeNewLines) {
if (richText == null || richText.length() == 0) {
return "";
}
if (xhtmlPattern.matcher(richText).find() || tlfPattern.matcher(richText).find() || flashHtmlPattern.matcher(richText).find()) {
XMLHelperUtil richXml = new XMLHelperUtil("<root>" + richText + "</root>");
boolean isRichText = false;
Element xhtmlBodyNode = (Element)richXml.getNode("/descendant::*[name()='root']/*[name()='body']");
if (xhtmlBodyNode != null) {
isRichText = true;
} else {
Element textFlowNode = (Element)richXml.getNode("/descendant::*[name()='root']/*[name()='TextFlow']");
if (textFlowNode != null) {
isRichText = true;
} else if (flashHtmlPattern.matcher(richText).find()) {
isRichText = true;
}
}
if (isRichText) {
Element rootNode = (Element)richXml.getNode("/root");
String plainText = ContentConverterUtils.extractPlainText(rootNode);
if (escapeNewLines) {
plainText = plainText.replaceAll("\\n", "\\\\n");
}
return plainText;
}
}
return richText;
}
private static String extractPlainText(Element node) {
if (node == null) {
return "";
}
String plainText = "";
Iterator it = node.nodeIterator();
while (it.hasNext()) {
Node childNode = (Node)it.next();
if (childNode == null) continue;
short nodeType = childNode.getNodeType();
if (nodeType == 3) {
plainText = plainText + childNode.getText();
continue;
}
if (nodeType != 1) continue;
plainText = plainText + ContentConverterUtils.extractPlainText((Element)childNode);
}
String className = node.getName().toLowerCase();
if (className.equals("p") || className.equals("br")) {
plainText = plainText + "\n";
}
return plainText;
}
public static String processNewLines(String richText) {
if (richText == null || richText.length() == 0) {
return "";
}
richText = richText.replace("<br/>", " <br/>");
return richText;
}
public static String convertNewLinesToXhml(String richText) {
if (richText == null || richText.length() == 0) {
return "";
}
if (xhtmlPattern.matcher(richText).find() || tlfPattern.matcher(richText).find() || flashHtmlPattern.matcher(richText).find()) {
richText = richText.replace("\\n", "<br/>");
richText = richText.replace("\n", "<br/>");
}
return richText;
}
public static String updateHeaderTags(String resolvedContent) {
String resolveContentBeginTag = "<resolvedContent>";
String resolveContentEndTag = "</resolvedContent>";
resolvedContent = resolveContentBeginTag + resolvedContent + resolveContentEndTag;
XMLHelperUtil xml = new XMLHelperUtil(resolvedContent);
List headers = xml.getList("//*[name()='h1'] | //*[name()='h2'] | //*[name()='h3'] | //*[name()='h4'] | //*[name()='h5'] | //*[name()='h6']|//*[name()='H1'] | //*[name()='H2'] | //*[name()='H3'] | //*[name()='H4'] | //*[name()='H5'] | //*[name()='H6']");
if (headers != null && !headers.isEmpty()) {
ContentConverterUtils.convertHeadingTags(headers);
}
String contentXML = xml.getXmlDocument().getRootElement().asXML();
return contentXML.substring(resolveContentBeginTag.length(), contentXML.length() - resolveContentEndTag.length());
}
private static void convertHeadingTags(List headingElements) {
for (Object obj : headingElements) {
Element headingElement = (Element)obj;
String name = headingElement.getName();
double fontSize = HEADER_STYLE[name.charAt(name.length() - 1) - 48];
headingElement.setName("p");
ContentConverterUtils.appendXHTMLStyle(headingElement, "font-size", "" + fontSize + "pt");
ContentConverterUtils.appendXHTMLStyle(headingElement, "font-weight", "bold");
}
}
public static Element appendXHTMLStyle(Element element, String targetAttribute, Object value) {
String styleAttribute = "style";
if (element == null) {
return null;
}
String spanElementStyle = element.attributeValue(styleAttribute);
String styleWithoutTargetAttribute = null;
if (spanElementStyle != null && spanElementStyle.length() > 0) {
int targetAttributeStartIndex = spanElementStyle.indexOf(targetAttribute + ":");
if (targetAttributeStartIndex != -1) {
int targetAttributeEndIndex = spanElementStyle.indexOf(";", targetAttributeStartIndex + targetAttribute.length() + 1);
if (targetAttributeEndIndex == -1) {
targetAttributeEndIndex = spanElementStyle.length();
}
styleWithoutTargetAttribute = spanElementStyle.substring(0, targetAttributeStartIndex);
if (targetAttributeEndIndex != spanElementStyle.length()) {
styleWithoutTargetAttribute = styleWithoutTargetAttribute + spanElementStyle.substring(targetAttributeEndIndex + 1, spanElementStyle.length()).trim();
}
} else {
styleWithoutTargetAttribute = spanElementStyle;
}
}
if (styleWithoutTargetAttribute != null) {
element.addAttribute(styleAttribute, targetAttribute + ": " + value.toString() + "; " + styleWithoutTargetAttribute);
} else {
element.addAttribute(styleAttribute, targetAttribute + ": " + value.toString() + "; ");
}
return element;
}
public static String convertPixelStylesToPoints(String resolvedContent) {
if (!ContentConverterUtils.isAutoCorrectPixelStyles()) {
return resolvedContent;
}
if (resolvedContent == null) {
return null;
}
resolvedContent = letterSpacingInPxPattern.matcher(resolvedContent).replaceAll("$1pt$2");
resolvedContent = fontSizeInPxPattern.matcher(resolvedContent).replaceAll("$1pt$2");
return resolvedContent;
}
public static boolean isAutoCorrectPixelStyles() {
return autoCorrectPixelStyles;
}
}