All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.aemds.guide.utils.GuideHTMLParser Maven / Gradle / Ivy

/*
 *
 * ADOBE CONFIDENTIAL
 * Copyright 2015 Adobe Systems Incorporated All Rights Reserved.
 *
 * NOTICE: All information contained herein is, and remains the property of
 * Adobe Systems Incorporated and its suppliers, if any. The intellectual and
 * technical concepts contained herein are proprietary to Adobe Systems
 * Incorporated and its suppliers and may be covered by U.S. and Foreign
 * Patents, patents in process, and are protected by trade secret or copyright
 * law. Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained from
 * Adobe Systems Incorporated.
 */


/**
 * @pad.exclude Exclude from Published API.
 */

package com.adobe.aemds.guide.utils;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @pad.exclude Exclude from Published API.
 */
public class GuideHTMLParser {
    static Logger logger = LoggerFactory.getLogger(GuideHTMLParser.class);

    /**
     * This strips all the lazy children from Document
     *
     * @param guideHTMLParserOptions
     * @return HTML string  for a give {@link Document}
     */
    public static String stripAllOnDemandChildren(GuideHTMLParserOptions guideHTMLParserOptions) {
        String html = guideHTMLParserOptions.getHtml();
        return stripLazyHtml(html);
    }

    public static String getAssetHTMLFromFullHTML(String documentHtml, String templateId) {
        String lazyFragmentMarkerBeginText = lazyFragmentMarkerBegin(templateId);
        int lazyFragmentMarkerStartIndex = StringUtils.indexOf(documentHtml, lazyFragmentMarkerBeginText + " ");
        int lazyFragmentMarkerEndIndex = lazyFragmentMarkerStartIndex + lazyFragmentMarkerStaticLength(templateId);
        String lazyFragMarkerText = StringUtils.substring(documentHtml, lazyFragmentMarkerStartIndex,
                lazyFragmentMarkerEndIndex);
        Pattern pattern = Pattern.compile("data-guide-asset-ref-length=(.*?)-->");
        Matcher matcher = pattern.matcher(lazyFragMarkerText);
        int lazyLength = -1;
        if (matcher.find()) {
            lazyLength = Integer.parseInt(matcher.group(1).trim());
        }
        String assetHtml = StringUtils.substring(documentHtml, lazyFragmentMarkerEndIndex,
                lazyFragmentMarkerEndIndex + lazyLength);
        return stripLazyHtml(assetHtml);
    }

    private static String stripLazyHtml(String htmlContent) {
        Pattern pattern = Pattern.compile("");
        String strippedString = htmlContent;
        Matcher matcher = pattern.matcher(strippedString);
        while (matcher.find()) {
            int startIndex = matcher.start();
            int endIndex = matcher.end();
            int lazyLength = Integer.parseInt(matcher.group(2).trim());
            strippedString = strippedString.substring(0, startIndex) + strippedString.substring(endIndex + lazyLength);
            matcher = pattern.matcher(strippedString);
        }
        return strippedString;
    }

    public static String lazyFragmentMarker(String templateId, String bodyContent) {
        return lazyFragmentMarkerBegin(templateId) + lazyFragmentMarkerEnd(bodyContent);
    }

    private static String lazyFragmentMarkerBegin(String templateId) {
        return "";
    }

    private static int lazyFragmentMarkerStaticLength(String templateId) {
        String lazyFragmentMarker = lazyFragmentMarker(templateId, "");
        return lazyFragmentMarker.length();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy