use of lucee.transformer.cfml.ExprTransformer in project Lucee by lucee.
the class Loop method evaluate.
@Override
public void evaluate(Tag tag, TagLibTag tagLibTag, FunctionLib[] flibs) throws EvaluatorException {
TagLoop loop = (TagLoop) tag;
// attribute maxrows and endrow not allowd at the same time
if (tag.containsAttribute("maxrows") && tag.containsAttribute("endrow"))
throw new EvaluatorException("Wrong Context, you cannot use attribute maxrows and endrow at the same time.");
// file loop
if (tag.containsAttribute("file")) {
if (!tag.containsAttribute("index") && !tag.containsAttribute("item"))
throw new EvaluatorException("Wrong Context, when you use attribute file you must also use attribute index and/or item");
loop.setType(TagLoop.TYPE_FILE);
return;
}
// list loop
if (tag.containsAttribute("list")) {
if (!tag.containsAttribute("index") && !tag.containsAttribute("item"))
throw new EvaluatorException("Wrong Context, when you use attribute list,you must define attribute index and/or item");
loop.setType(TagLoop.TYPE_LIST);
return;
}
// array loop
if (tag.containsAttribute("array")) {
if (!tag.containsAttribute("index") && !tag.containsAttribute("item"))
throw new EvaluatorException("Wrong Context, when you use attribute array, you must define attribute index and/or item");
loop.setType(TagLoop.TYPE_ARRAY);
return;
}
// array loop
if (tag.containsAttribute("times")) {
if (tag.getAttributes().size() > 1)
throw new EvaluatorException("Wrong Context, when you use attribute times, no other attributes are allowed");
loop.setType(TagLoop.TYPE_TIMES);
return;
}
// struct loop
if (tag.containsAttribute("struct")) {
if (!tag.containsAttribute("index") && !tag.containsAttribute("item") && !tag.containsAttribute("key") && !tag.containsAttribute("value"))
throw new EvaluatorException("Wrong Context, when you use attribute struct, you must define attribute index (alias key) and/or item (alias value)");
loop.setType(TagLoop.TYPE_STRUCT);
return;
}
// collection loop
if (tag.containsAttribute("collection")) {
if (!tag.containsAttribute("index") && !tag.containsAttribute("item") && !tag.containsAttribute("key") && !tag.containsAttribute("value"))
throw new EvaluatorException("Wrong Context, when you use attribute struct, you must define attribute index (alias key) and/or item (alias value)");
loop.setType(TagLoop.TYPE_COLLECTION);
return;
}
/*if(tag.containsAttribute("index")) {
if(!tag.containsAttribute("from") || !tag.containsAttribute("to"))
throw new EvaluatorException("Wrong Context, when you use attribute index you must also use attribute from and to or list or file");
loop.setType(TagLoop.TYPE_INDEX);
return;
}*/
if (tag.containsAttribute("from") || tag.containsAttribute("to")) {
if (!tag.containsAttribute("from"))
throw new EvaluatorException("Wrong Context, when you use attribute to, you must also use attribute from.");
if (!tag.containsAttribute("to"))
throw new EvaluatorException("Wrong Context, when you use attribute from, you must also use attribute to.");
if (!tag.containsAttribute("index") && !tag.containsAttribute("item"))
throw new EvaluatorException("Wrong Context, when you use attribute from and to, you must define attribute index or item.");
if (tag.containsAttribute("index") && tag.containsAttribute("item"))
throw new EvaluatorException("For this type of loop, you cannot use attribute index and item at the same time.");
loop.setType(TagLoop.TYPE_FROM_TO);
return;
}
// condition loop
if (tag.containsAttribute("condition")) {
if (tag.isScriptBase())
throw new EvaluatorException("tag loop-condition is not supported within cfscript, use instead a while statement.");
TagLib tagLib = tagLibTag.getTagLib();
ExprTransformer transformer;
String text = ASMUtil.getAttributeString(tag, "condition");
try {
transformer = tagLib.getExprTransfomer();
Page page = ASMUtil.getAncestorPage(tag);
ConfigImpl config = (ConfigImpl) page.getConfig();
Expression expr = transformer.transform(BytecodeFactory.getInstance(config), page, new EvaluatorPool(), null, flibs, config.getCoreTagLib(page.getSourceCode().getDialect()).getScriptTags(), new SourceCode(text, false, page.getSourceCode().getDialect()), new TransfomerSettings(page.getSourceCode().getDialect() == CFMLEngine.DIALECT_CFML && config.getDotNotationUpperCase(), page.getSourceCode().getDialect() == CFMLEngine.DIALECT_CFML && config.getHandleUnQuotedAttrValueAsString(), page.ignoreScopes));
tag.addAttribute(new Attribute(false, "condition", page.getFactory().toExprBoolean(expr), "boolean"));
} catch (Exception e) {
throw new EvaluatorException(e.getMessage());
}
loop.setType(TagLoop.TYPE_CONDITION);
return;
}
// query loop
if (tag.containsAttribute("query")) {
loop.setType(TagLoop.TYPE_QUERY);
return;
}
Info info = getParentInfo(loop);
// query group
if (tag.containsAttribute("group") && info.hasParentWithQuery) {
loop.setType(TagLoop.TYPE_GROUP);
return;
}
if (info.hasParentWithQuery) {
if (info.hasParentWithGroup)
loop.setType(TagLoop.TYPE_INNER_GROUP);
else
loop.setType(TagLoop.TYPE_INNER_QUERY);
return;
}
/*
if(hasQuery)
output.setType(TagOutput.TYPE_QUERY);
else if(tag.containsAttribute("group") && hasParentWithQuery)
output.setType(TagOutput.TYPE_GROUP);
else if(hasParentWithQuery) {
if(hasParentWithGroup) output.setType(TagOutput.TYPE_INNER_GROUP);
else output.setType(TagOutput.TYPE_INNER_QUERY);
}
else
output.setType(TagOutput.TYPE_NORMAL);
*/
loop.setType(TagLoop.TYPE_NOTHING);
// throw new EvaluatorException("Wrong Context, invalid attributes in tag cfloop");
}
use of lucee.transformer.cfml.ExprTransformer in project Lucee by lucee.
the class CFMLTransformer method tag.
/**
* Liest einen Tag ein, prueft hierbei ob das Tag innerhalb einer der geladenen Tag-Lib existiert,
* ansonsten wird ein Tag einfach als literal-string aufgenommen.
* <br />
* EBNF:<br />
* <code>name-space identifier spaces attributes ("/>" | ">" [body "</" identifier spaces ">"]);(* Ob dem Tag ein Body und ein End-Tag folgt ist abhaengig von Definition des body-content in Tag-Lib, gleices gilt fuer appendix *)</code>
* @param parent uebergeornetes Tag
* @param parseExpression sollen Expresson innerhalb des Body geparste werden oder nicht.
* @return Gibt zurueck ob es sich um ein Tag as einer Tag-Lib handelte oder nicht.
* @throws TemplateException
*/
private boolean tag(TagData data, Body parent, boolean parseExpression) throws TemplateException {
// lucee.print.ln("--->"+data.cfml.getCurrent());
boolean hasBody = false;
Position line = data.srcCode.getPosition();
// int column=data.cfml.getColumn();
int start = data.srcCode.getPos();
data.srcCode.next();
// read in namespace of tag
TagLib tagLib = nameSpace(data);
// return if no matching tag lib
if (tagLib == null) {
data.srcCode.previous();
return false;
}
// Get matching tag from tag lib
String strNameNormal = identifier(data.srcCode, false, true);
if (strNameNormal == null) {
data.srcCode.setPos((data.srcCode.getPos() - tagLib.getNameSpaceAndSeparator().length()) - 1);
return false;
}
String strName = strNameNormal.toLowerCase();
String appendix = null;
TagLibTag tagLibTag = tagLib.getTag(strName);
// get taglib
if (tagLibTag == null) {
tagLibTag = tagLib.getAppendixTag(strName);
if (tagLibTag == null) {
if (tagLib.getIgnoreUnknowTags()) {
data.srcCode.setPos(start);
return false;
}
throw new TemplateException(data.srcCode, "undefined tag [" + tagLib.getNameSpaceAndSeparator() + strName + "]");
}
appendix = StringUtil.removeStartingIgnoreCase(strNameNormal, tagLibTag.getName());
}
// CFXD Element
Tag tag;
try {
tag = tagLibTag.getTag(data.factory, line, data.srcCode.getPosition());
} catch (Exception e) {
throw new TemplateException(data.srcCode, e);
}
parent.addStatement(tag);
// get tag from tag library
if (appendix != null) {
tag.setAppendix(appendix);
tag.setFullname(tagLibTag.getFullName().concat(appendix));
} else {
tag.setFullname(tagLibTag.getFullName());
}
// if(tag.getFullname().equalsIgnoreCase("cfcomponent"))data.page.setIsComponent(true); // MUST to hardcoded, to better
// else if(tag.getFullname().equalsIgnoreCase("cfinterface"))data.page.setIsInterface(true); // MUST to hardcoded, to better
tag.setTagLibTag(tagLibTag);
comment(data.srcCode, true);
// Tag Translator Evaluator
if (tagLibTag.hasTTE()) {
data.ep.add(tagLibTag, tag, data.flibs, data.srcCode);
}
// get Attributes
attributes(data, tagLibTag, tag);
if (tagLibTag.hasAttributeEvaluator()) {
try {
tagLibTag = tagLibTag.getAttributeEvaluator().evaluate(tagLibTag, tag);
} catch (AttributeEvaluatorException e) {
throw new TemplateException(data.srcCode, e);
}
}
// TODO muss erlaubt sein
if (data.srcCode.forwardIfCurrent('>')) {
hasBody = tagLibTag.getHasBody();
} else if (data.srcCode.forwardIfCurrent('/', '>')) {
if (tagLibTag.getHasBody())
tag.setBody(new BodyBase(data.factory));
} else {
throw createTemplateException(data.srcCode, "tag [" + tagLibTag.getFullName() + "] is not closed", tagLibTag);
}
// Body
if (hasBody) {
// get Body
if (tagLibTag.isTagDependent()) {
// get TagDependentBodyTransformer
TagDependentBodyTransformer tdbt = null;
try {
tdbt = tagLibTag.getBodyTransformer();
} catch (TagLibException e) {
throw new TemplateException(data.srcCode, e);
}
if (tdbt == null)
throw createTemplateException(data.srcCode, "Tag dependent body Transformer is invalid for Tag [" + tagLibTag.getFullName() + "]", tagLibTag);
tag.setBody(tdbt.transform(data.factory, data.root, data.ep, data.tlibs, data.flibs, tagLibTag.getFullName(), data.scriptTags, data.srcCode, data.settings));
// get TagLib of end Tag
if (!data.srcCode.forwardIfCurrent("</")) {
// MUST this is a patch, do a more proper implementation
TemplateException te = new TemplateException(data.srcCode, "invalid construct");
if (tdbt instanceof CFMLScriptTransformer && ASMUtil.containsComponent(tag.getBody())) {
throw new CFMLScriptTransformer.ComponentTemplateException(te);
}
throw te;
}
TagLib tagLibEnd = nameSpace(data);
// same NameSpace
if (!(tagLibEnd != null && tagLibEnd.getNameSpaceAndSeparator().equals(tagLib.getNameSpaceAndSeparator())))
throw new TemplateException(data.srcCode, "invalid construct");
// get end Tag
String strNameEnd = identifier(data.srcCode, true, true).toLowerCase();
// not the same name Tag
if (!strName.equals(strNameEnd)) {
data.srcCode.setPos(start);
throw new TemplateException(data.srcCode, "Start and End Tag has not the same Name [" + tagLib.getNameSpaceAndSeparator() + strName + "-" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "]");
}
data.srcCode.removeSpace();
if (!data.srcCode.forwardIfCurrent('>'))
throw new TemplateException(data.srcCode, "End Tag [" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "] not closed");
} else {
// get body of Tag
BodyBase body = new BodyBase(data.factory);
body.setParent(tag);
// parseExpression=(tagLibTag.getParseBody())?true:parseExpression;
if (tagLibTag.getParseBody())
parseExpression = true;
while (true) {
// Load Expession Transformer from TagLib
ExprTransformer transfomer = null;
if (parseExpression) {
try {
transfomer = tagLibTag.getTagLib().getExprTransfomer();
} catch (TagLibException e) {
throw new TemplateException(data.srcCode, e);
}
}
// call body
body(data, body, parseExpression, transfomer);
// no End Tag
if (data.srcCode.isAfterLast()) {
if (tagLibTag.isBodyReq()) {
data.srcCode.setPos(start);
throw createTemplateException(data.srcCode, "No matching end tag found for tag [" + tagLibTag.getFullName() + "]", tagLibTag);
}
body.moveStatmentsTo(parent);
return executeEvaluator(data, tagLibTag, tag);
}
// Invalid Construct
int posBeforeEndTag = data.srcCode.getPos();
if (!data.srcCode.forwardIfCurrent('<', '/'))
throw createTemplateException(data.srcCode, "Missing end tag for [" + tagLibTag.getFullName() + "]", tagLibTag);
// get TagLib of end Tag
int _start = data.srcCode.getPos();
TagLib tagLibEnd = nameSpace(data);
// same NameSpace
if (tagLibEnd != null) {
String strNameEnd = "";
// lucee.print.ln(data.cfml.getLine()+" - "+data.cfml.getColumn()+" - "+tagLibEnd.getNameSpaceAndSeperator()+".equals("+tagLib.getNameSpaceAndSeperator()+")");
if (tagLibEnd.getNameSpaceAndSeparator().equals(tagLib.getNameSpaceAndSeparator())) {
// get end Tag
strNameEnd = identifier(data.srcCode, true, true).toLowerCase();
// not the same name Tag
// new part
data.srcCode.removeSpace();
if (strName.equals(strNameEnd)) {
if (!data.srcCode.forwardIfCurrent('>'))
throw new TemplateException(data.srcCode, "End Tag [" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "] not closed");
break;
}
}
// new part
if (tagLibTag.isBodyReq()) {
TagLibTag endTag = tagLibEnd.getTag(strNameEnd);
if (endTag != null && !endTag.getHasBody())
throw new TemplateException(data.srcCode, "End Tag [" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "] is not allowed, for this tag only a Start Tag is allowed");
data.srcCode.setPos(start);
if (tagLibEnd.getIgnoreUnknowTags() && (tagLibEnd.getTag(strNameEnd)) == null) {
data.srcCode.setPos(_start);
} else
throw new TemplateException(data.srcCode, "Start and End Tag has not the same Name [" + tagLib.getNameSpaceAndSeparator() + strName + "-" + tagLibEnd.getNameSpaceAndSeparator() + strNameEnd + "]");
} else {
body.moveStatmentsTo(parent);
data.srcCode.setPos(posBeforeEndTag);
return executeEvaluator(data, tagLibTag, tag);
}
// / new part
}
body.addPrintOut(data.factory, "</", null, null);
}
tag.setBody(body);
}
}
if (tag instanceof StatementBase)
((StatementBase) tag).setEnd(data.srcCode.getPosition());
return executeEvaluator(data, tagLibTag, tag);
}
Aggregations