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

org.grails.gsp.compiler.SitemeshPreprocessor Maven / Gradle / Ivy

/*
 * Copyright 2011-2022 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.grails.gsp.compiler;

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

/**
 * Adds GSP Sitemesh integration directly to compiled GSP.
 *
 * head, meta, title, body and content tags are replaced with <sitemesh:capture*>...</sitemesh:capture*> taglibs
 *
 * The taglib is used to capture the content of each tag. This prevents the need to parse the content output like Sitemesh normally does.
 *
 * @author Lari Hotari, Sagire Software Oy
 */
public class SitemeshPreprocessor {

    Pattern parameterPattern = Pattern.compile("]+?)(/*?)>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    Pattern metaPattern = Pattern.compile("]+?)(/*?)>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    Pattern titlePattern = Pattern.compile("]*)?>(.*?)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    Pattern headPattern = Pattern.compile("]*)?>(.*?)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    Pattern bodyPattern = Pattern.compile("]*)?>(.*?)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    Pattern contentPattern = Pattern.compile("]+)>(.*?)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);

    public static final String XML_CLOSING_FOR_EMPTY_TAG_ATTRIBUTE_NAME = "gsp_sm_xmlClosingForEmptyTag";

    public String addGspSitemeshCapturing(String gspSource) {
        StringBuffer sb = addHeadCapturing(gspSource);
        sb = addBodyCapturing(sb);
        sb = addContentCapturing(sb);
        return sb.toString();
    }

    StringBuffer addHeadCapturing(String gspSource) {
        StringBuffer sb = new StringBuffer((int) (gspSource.length() * 1.2));
        Matcher m = this.headPattern.matcher(gspSource);
        if (m.find()) {
            m.appendReplacement(sb, "");
            sb.append("");
            sb.append(addMetaCapturing(addTitleCapturing(m.group(2))));
            sb.append("");
            m.appendTail(sb);
        }
        else if (!this.bodyPattern.matcher(gspSource).find()) {
            // no body either, so replace meta & title in the entire gsp source
            // fix title in sub-template -problem
            sb.append(addMetaCapturing(addTitleCapturing(gspSource)));
        }
        else {
            sb.append(gspSource);
        }
        return sb;
    }

    String addMetaCapturing(String headContent) {
        Matcher m = this.metaPattern.matcher(headContent);
        final String result = this.parameterPattern.matcher(
                m.replaceAll("")
        ).replaceAll("");
        return result;
    }

    String addTitleCapturing(String headContent) {
        Matcher m = this.titlePattern.matcher(headContent);
        return m.replaceAll("$2");
    }

    StringBuffer addBodyCapturing(StringBuffer sb) {
        Matcher m = this.bodyPattern.matcher(sb);
        return new StringBuffer(m.replaceAll("$2"));
    }

    StringBuffer addContentCapturing(StringBuffer sb) {
        Matcher m = this.contentPattern.matcher(sb);
        return new StringBuffer(m.replaceAll("$2"));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy