Example usage for org.apache.commons.collections.map LazySortedMap decorate

List of usage examples for org.apache.commons.collections.map LazySortedMap decorate

Introduction

In this page you can find the example usage for org.apache.commons.collections.map LazySortedMap decorate.

Prototype

public static SortedMap decorate(SortedMap map, Transformer factory) 

Source Link

Document

Factory method to create a lazily instantiated sorted map.

Usage

From source file:org.jahia.services.render.filter.StaticAssetsFilter.java

@Override
public String execute(String previousOut, RenderContext renderContext,
        org.jahia.services.render.Resource resource, RenderChain chain) throws Exception {

    String out = previousOut;//from   w ww .j a v a2 s  . com
    Source source = new Source(previousOut);
    Map<String, Map<String, Map<String, Map<String, String>>>> assetsByTarget = new LinkedHashMap<>();

    List<Element> esiResourceElements = source.getAllElements("jahia:resource");
    Set<String> keys = new HashSet<>();
    for (Element esiResourceElement : esiResourceElements) {
        StartTag esiResourceStartTag = esiResourceElement.getStartTag();
        Map<String, Map<String, Map<String, String>>> assets;
        String targetTag = esiResourceStartTag.getAttributeValue(TARGET_TAG);
        if (targetTag == null) {
            targetTag = "HEAD";
        } else {
            targetTag = targetTag.toUpperCase();
        }
        if (!assetsByTarget.containsKey(targetTag)) {
            assets = LazySortedMap.decorate(TransformedSortedMap.decorate(
                    new TreeMap<String, Map<String, Map<String, String>>>(ASSET_COMPARATOR),
                    LOW_CASE_TRANSFORMER, NOPTransformer.INSTANCE), new AssetsMapFactory());
            assetsByTarget.put(targetTag, assets);
        } else {
            assets = assetsByTarget.get(targetTag);
        }

        String type = esiResourceStartTag.getAttributeValue("type");
        String path = StringUtils.equals(type, "inline")
                ? StringUtils.substring(out, esiResourceStartTag.getEnd(),
                        esiResourceElement.getEndTag().getBegin())
                : URLDecoder.decode(esiResourceStartTag.getAttributeValue("path"), "UTF-8");
        Boolean insert = Boolean.parseBoolean(esiResourceStartTag.getAttributeValue("insert"));
        String key = esiResourceStartTag.getAttributeValue("key");

        // get options
        Map<String, String> optionsMap = getOptionMaps(esiResourceStartTag);

        Map<String, Map<String, String>> stringMap = assets.get(type);
        if (stringMap == null) {
            Map<String, Map<String, String>> assetMap = new LinkedHashMap<>();
            stringMap = assets.put(type, assetMap);
        }

        if (insert) {
            Map<String, Map<String, String>> my = new LinkedHashMap<>();
            my.put(path, optionsMap);
            my.putAll(stringMap);
            stringMap = my;
        } else {
            if ("".equals(key) || !keys.contains(key)) {
                Map<String, Map<String, String>> my = new LinkedHashMap<>();
                my.put(path, optionsMap);
                stringMap.putAll(my);
                keys.add(key);
            }
        }
        assets.put(type, stringMap);
    }

    OutputDocument outputDocument = new OutputDocument(source);

    if (renderContext.isAjaxRequest()) {
        String templateContent = getAjaxResolvedTemplate();
        if (templateContent != null) {
            for (Map.Entry<String, Map<String, Map<String, Map<String, String>>>> entry : assetsByTarget
                    .entrySet()) {
                renderContext.getRequest().setAttribute(STATIC_ASSETS, entry.getValue());
                Element element = source.getFirstElement(TARGET_TAG);
                final EndTag tag = element != null ? element.getEndTag() : null;
                ScriptEngine scriptEngine = scriptEngineUtils.scriptEngine(ajaxTemplateExtension);
                ScriptContext scriptContext = new AssetsScriptContext();
                final Bindings bindings = scriptEngine.createBindings();
                bindings.put(TARGET_TAG, entry.getKey());
                bindings.put("renderContext", renderContext);
                bindings.put("resource", resource);
                scriptContext.setBindings(bindings, ScriptContext.GLOBAL_SCOPE);
                // The following binding is necessary for Javascript, which doesn't offer a console by default.
                bindings.put("out", new PrintWriter(scriptContext.getWriter()));
                scriptEngine.eval(templateContent, scriptContext);
                StringWriter writer = (StringWriter) scriptContext.getWriter();
                final String staticsAsset = writer.toString();

                if (StringUtils.isNotBlank(staticsAsset)) {
                    if (tag != null) {
                        outputDocument.replace(tag.getBegin(), tag.getBegin() + 1, "\n" + staticsAsset + "\n<");
                        out = outputDocument.toString();
                    } else {
                        out = staticsAsset + "\n" + previousOut;
                    }
                }
            }
        }
    } else if (resource.getContextConfiguration().equals("page")) {
        if (renderContext.isEditMode()) {
            if (renderContext.getServletPath().endsWith("frame")) {
                boolean doParse = true;
                if (renderContext.getEditModeConfig().getSkipMainModuleTypesDomParsing() != null) {
                    for (String nt : renderContext.getEditModeConfig().getSkipMainModuleTypesDomParsing()) {
                        doParse = !resource.getNode().isNodeType(nt);
                        if (!doParse) {
                            break;
                        }
                    }
                }
                List<Element> bodyElementList = source.getAllElements(HTMLElementName.BODY);
                if (bodyElementList.size() > 0) {
                    Element bodyElement = bodyElementList.get(bodyElementList.size() - 1);
                    EndTag bodyEndTag = bodyElement.getEndTag();
                    outputDocument.replace(bodyEndTag.getBegin(), bodyEndTag.getBegin() + 1, "</div><");

                    bodyElement = bodyElementList.get(0);

                    StartTag bodyStartTag = bodyElement.getStartTag();
                    outputDocument.replace(bodyStartTag.getEnd(), bodyStartTag.getEnd(), "\n"
                            + "<div jahiatype=\"mainmodule\"" + " path=\"" + resource.getNode().getPath()
                            + "\" locale=\"" + resource.getLocale() + "\"" + " template=\""
                            + (resource.getTemplate() != null && !resource.getTemplate().equals("default")
                                    ? resource.getTemplate()
                                    : "")
                            + "\"" + " nodetypes=\""
                            + ConstraintsHelper.getConstraints(renderContext.getMainResource().getNode()) + "\""
                            + ">");
                    if (doParse) {
                        outputDocument.replace(bodyStartTag.getEnd() - 1, bodyStartTag.getEnd(),
                                " jahia-parse-html=\"true\">");
                    }
                }
            }
        }
        if (!assetsByTarget.containsKey("HEAD")) {
            addResources(renderContext, resource, source, outputDocument, "HEAD",
                    new HashMap<String, Map<String, Map<String, String>>>());
        }
        for (Map.Entry<String, Map<String, Map<String, Map<String, String>>>> entry : assetsByTarget
                .entrySet()) {
            String targetTag = entry.getKey();
            Map<String, Map<String, Map<String, String>>> assets = entry.getValue();
            addResources(renderContext, resource, source, outputDocument, targetTag, assets);
        }
        out = outputDocument.toString();
    }

    // Clean all jahia:resource tags
    source = new Source(out);
    outputDocument = new OutputDocument(source);
    for (Element el : source.getAllElements("jahia:resource")) {
        outputDocument.replace(el, "");
    }
    String s = outputDocument.toString();
    s = removeTempTags(s);
    return s.trim();
}