/*
 * Copyright 2012 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
 *
 *      http://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.broadleafcommerce.core.web.processor;

import java.util.Map;

import org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.processor.element.AbstractFragmentElementProcessor;
import org.thymeleaf.standard.processor.attr.StandardFragmentAttrProcessor;

/**
 * A Thymeleaf processor that will include the standard head element. It will also set the
 * following variables for use by the head fragment.
 * 
 * <ul>
 * 	<li><b>pageTitle</b> - The title of the page</li>
 * 	<li><b>additionalCss</b> - An additional, page specific CSS file to include</li>
 * </ul>
 * 
 * @author apazzolini
 */
public class HeadProcessor extends AbstractFragmentElementProcessor {

    public static final String FRAGMENT_ATTR_NAME = StandardFragmentAttrProcessor.ATTR_NAME;
    
	/**
	 * Sets the name of this processor to be used in Thymeleaf template
	 */
    public HeadProcessor() {
        super("head");
    }

    @Override
    public int getPrecedence() {
        return 10000;
    }

	@Override
	@SuppressWarnings("unchecked")
	protected AbstractFragmentSpec getFragmentSpec(Arguments arguments, Element element) {
		((Map<String, Object>) arguments.getExpressionEvaluationRoot()).put("pageTitle", element.getAttributeValue("pageTitle"));
		((Map<String, Object>) arguments.getExpressionEvaluationRoot()).put("additionalCss", element.getAttributeValue("additionalCss"));
        return new CompleteTemplateFragmentSpec("partials/head");
	}



	@Override
	protected boolean getSubstituteInclusionNode(Arguments arguments, Element element) {
		return true;
	}

}

@@ -665,31 +665,8 @@
 r;%0A%0A
-import java.util.Map;%0A%0A
 impo
@@ -727,16 +727,77 @@
 lement;%0A
+import org.thymeleaf.exceptions.TemplateProcessingException;%0A
 import o
@@ -861,16 +861,86 @@
 cessor;%0A
+import org.thymeleaf.standard.expression.StandardExpressionProcessor;%0A
 import o
@@ -1008,16 +1008,39 @@
 essor;%0A%0A
+import java.util.Map;%0A%0A
 /**%0A * A
@@ -1883,16 +1883,582 @@
 ment) %7B%0A
+%09%09// The pageTitle attribute could be an expression that needs to be evaluated. Try to evaluate, but fall back%0A%09%09// to its text value if the expression wasn't able to be processed. This will allow things like%0A%09%09// pageTitle=%22Hello this is a string%22%0A%09%09// as well as expressions like%0A%09%09// pageTitle=%22$%7B'Hello this is a ' + product.name%7D%22%0A%09%09%0A%09%09String pageTitle = element.getAttributeValue(%22pageTitle%22);%0A%09%09try %7B%0A%09%09%09pageTitle = (String) StandardExpressionProcessor.processExpression(arguments, pageTitle);%0A%09%09%7D catch (TemplateProcessingException e) %7B%0A%09%09%09// Do nothing%0A%09%09%7D%0A
 %09%09((Map%3C
@@ -2536,35 +2536,8 @@
 e%22, 
-element.getAttributeValue(%22
 page
@@ -2541,18 +2541,16 @@
 ageTitle
-%22)
 );%0A%09%09((M

Improve HeadProcessor to allow resolution of variables passed by the page