/*
 * 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 org.thymeleaf.Arguments;
import org.thymeleaf.dom.Element;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.processor.element.AbstractFragmentElementProcessor;
import org.thymeleaf.standard.expression.StandardExpressionProcessor;
import org.thymeleaf.standard.processor.attr.StandardFragmentAttrProcessor;

import java.util.Map;

/**
 * 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;
    protected String HEAD_PARTIAL_PATH = "layout/partials/head";
    
	/**
	 * 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) {
		// The pageTitle attribute could be an expression that needs to be evaluated. Try to evaluate, but fall back
		// to its text value if the expression wasn't able to be processed. This will allow things like
		// pageTitle="Hello this is a string"
		// as well as expressions like
		// pageTitle="${'Hello this is a ' + product.name}"
		
		String pageTitle = element.getAttributeValue("pageTitle");
		try {
			pageTitle = (String) StandardExpressionProcessor.processExpression(arguments, pageTitle);
		} catch (TemplateProcessingException e) {
			// Do nothing.
		}
		((Map<String, Object>) arguments.getExpressionEvaluationRoot()).put("pageTitle", pageTitle);
		((Map<String, Object>) arguments.getExpressionEvaluationRoot()).put("additionalCss", element.getAttributeValue("additionalCss"));
        return new CompleteTemplateFragmentSpec(HEAD_PARTIAL_PATH);
	}

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

}

@@ -788,16 +788,114 @@
 eption;%0A
+import org.thymeleaf.fragment.FragmentAndTarget;%0Aimport org.thymeleaf.fragment.WholeFragmentSpec;%0A
 import o
@@ -933,32 +933,40 @@
 AbstractFragment
+Handling
 ElementProcessor
@@ -1526,16 +1526,24 @@
 Fragment
+Handling
 ElementP
@@ -1942,16 +1942,135 @@
 verride%0A
+%09protected boolean getSubstituteInclusionNode(Arguments arguments, Element element) %7B%0A%09%09return true;%0A%09%7D%0A%0A    @Override%0A
 %09@Suppre
@@ -2093,17 +2093,20 @@
 ecked%22)%0A
-%09
+    
 protecte
@@ -2111,28 +2111,25 @@
 ted 
-AbstractFragmentSpec
+FragmentAndTarget
  get
@@ -2136,20 +2136,25 @@
 Fragment
-Spec
+AndTarget
 (Argumen
@@ -2178,24 +2178,57 @@
 ment element
+, boolean substituteInclusionNode
 ) %7B%0A%09%09// The
@@ -3013,24 +3013,18 @@
 Css%22));%0A
-        
+%09%09
 return n
@@ -3030,174 +3030,78 @@
 new 
-CompleteTemplateFragmentSpec(HEAD_PARTIAL_PATH);%0A%09%7D%0A%0A%09@Override%0A%09protected boolean getSubstituteInclusionNode(Arguments arguments, Element element) %7B%0A%09%09return true;%0A%09
+FragmentAndTarget(HEAD_PARTIAL_PATH, WholeFragmentSpec.INSTANCE);%0A    
 %7D%0A%0A%7D

Refactor now-deprecated Thymeleaf processor