/*
* =============================================================================
*
* Copyright (c) 2011-2014, The THYMELEAF team (http://www.thymeleaf.org)
*
* 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.thymeleaf.standard.processor.attr;
import org.thymeleaf.Arguments;
import org.thymeleaf.Configuration;
import org.thymeleaf.dom.Element;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.processor.IAttributeNameProcessorMatcher;
import org.thymeleaf.processor.attr.AbstractIterationAttrProcessor;
import org.thymeleaf.standard.expression.Each;
import org.thymeleaf.standard.expression.EachUtils;
import org.thymeleaf.standard.expression.IStandardExpression;
import org.thymeleaf.util.StringUtils;
/**
*
* @author Daniel Fernández
*
* @since 1.0
*
*/
public abstract class AbstractStandardIterationAttrProcessor
extends AbstractIterationAttrProcessor {
protected AbstractStandardIterationAttrProcessor(final IAttributeNameProcessorMatcher matcher) {
super(matcher);
}
protected AbstractStandardIterationAttrProcessor(final String attributeName) {
super(attributeName);
}
@Override
protected void processClonedHostIterationElement(final Arguments arguments, final Element iteratedChild, final String attributeName) {
// Nothing to be done here, no additional processings for iterated elements
}
@Override
protected final IterationSpec getIterationSpec(
final Arguments arguments, final Element element, final String attributeName) {
final String attributeValue = element.getAttributeValue(attributeName);
final Configuration configuration = arguments.getConfiguration();
final Each each = EachUtils.parseEach(configuration, arguments, attributeValue);
final IStandardExpression iterVarExpr = each.getIterVar();
final Object iterVarValue = iterVarExpr.execute(configuration, arguments);
final IStandardExpression statusVarExpr = each.getStatusVar();
final Object statusVarValue;
if (statusVarExpr != null) {
statusVarValue = statusVarExpr.execute(configuration, arguments);
} else {
statusVarValue = null;
}
final IStandardExpression iterableExpr = each.getIterable();
final Object iteratedValue = iterableExpr.execute(configuration, arguments);
final String iterVarName = (iterVarValue == null? null : iterVarValue.toString());
if (StringUtils.isEmptyOrWhitespace(iterVarName)) {
throw new TemplateProcessingException(
"Iteration variable name expression evaluated as null: \"" + iterVarExpr + "\"");
}
final String statusVarName = (statusVarValue == null? null : statusVarValue.toString());
if (statusVarExpr != null && StringUtils.isEmptyOrWhitespace(statusVarName)) {
throw new TemplateProcessingException(
"Status variable name expression evaluated as null or empty: \"" + statusVarExpr + "\"");
}
return new IterationSpec(iterVarName, statusVarName, iteratedValue);
}
}