ContentConverterUtils.java 8.4 KB
/*
 * 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;
    }
}