use of lucee.transformer.bytecode.Statement in project Lucee by lucee.
the class ASMUtil method hasSisterTagAfter.
/**
* Prueft ob das das angegebene Tag in der gleichen Ebene nach dem angegebenen Tag vorkommt.
* @param tag Ausgangspunkt, nach diesem tag darf das angegebene nicht vorkommen.
* @param nameToFind Tag Name der nicht vorkommen darf
* @return kommt das Tag vor.
*/
public static boolean hasSisterTagAfter(Tag tag, String nameToFind) {
Body body = (Body) tag.getParent();
List<Statement> stats = body.getStatements();
Iterator<Statement> it = stats.iterator();
Statement other;
boolean isAfter = false;
while (it.hasNext()) {
other = it.next();
if (other instanceof Tag) {
if (isAfter) {
if (((Tag) other).getTagLibTag().getName().equals(nameToFind))
return true;
} else if (other == tag)
isAfter = true;
}
}
return false;
}
use of lucee.transformer.bytecode.Statement in project Lucee by lucee.
the class CFMLTransformer method transform.
/**
* Startmethode zum transfomieren einer CFML Datei.
* <br />
* EBNF:<br />
* <code>{body}</code>
* @param config
* @param ps CFML File
* @param tlibs Tag Library Deskriptoren, nach denen innerhalb der CFML Datei geprueft werden soll.
* @param flibs Function Library Deskriptoren, nach denen innerhalb der Expressions der CFML Datei geprueft werden soll.
* @param returnValue if true the method returns the value of the last expression executed inside when you call the method "call"
* @return uebersetztes CFXD Dokument Element.
* @throws TemplateException
* @throws IOException
*/
public Page transform(Factory factory, ConfigImpl config, PageSource ps, TagLib[] tlibs, FunctionLib[] flibs, boolean returnValue, boolean ignoreScopes) throws TemplateException, IOException {
Page p;
SourceCode sc;
boolean writeLog = config.getExecutionLogEnabled();
Charset charset = config.getTemplateCharset();
boolean dotUpper = ps.getDialect() == CFMLEngine.DIALECT_CFML && ((MappingImpl) ps.getMapping()).getDotNotationUpperCase();
// parse regular
while (true) {
try {
sc = new PageSourceCode(ps, charset, writeLog);
p = transform(factory, config, sc, tlibs, flibs, ps.getResource().lastModified(), dotUpper, returnValue, ignoreScopes);
break;
} catch (ProcessingDirectiveException pde) {
if (pde.getWriteLog() != null)
writeLog = pde.getWriteLog().booleanValue();
if (pde.getDotNotationUpperCase() != null)
dotUpper = pde.getDotNotationUpperCase().booleanValue();
if (!StringUtil.isEmpty(pde.getCharset()))
charset = pde.getCharset();
}
}
// could it be a component?
boolean isCFML = ps.getDialect() == CFMLEngine.DIALECT_CFML;
boolean isCFMLCompExt = isCFML && Constants.isCFMLComponentExtension(ResourceUtil.getExtension(ps.getResource(), ""));
boolean possibleUndetectedComponent = false;
// we don't have a component or interface
if (p.isPage()) {
if (isCFML)
possibleUndetectedComponent = isCFMLCompExt;
else if (Constants.isLuceeComponentExtension(ResourceUtil.getExtension(ps.getResource(), ""))) {
Expression expr;
Statement stat;
PrintOut po;
LitString ls;
List<Statement> statements = p.getStatements();
// check the root statements for component
Iterator<Statement> it = statements.iterator();
String str;
while (it.hasNext()) {
stat = it.next();
if (stat instanceof PrintOut && (expr = ((PrintOut) stat).getExpr()) instanceof LitString) {
ls = (LitString) expr;
str = ls.getString();
if (str.indexOf(Constants.LUCEE_COMPONENT_TAG_NAME) != -1 || str.indexOf(Constants.LUCEE_INTERFACE_TAG_NAME) != -1 || // cfml name is supported as alias
str.indexOf(Constants.CFML_COMPONENT_TAG_NAME) != -1) {
possibleUndetectedComponent = true;
break;
}
}
}
}
}
/*if(p.isPage() && (isCFML?
Constants.isCFMLComponentExtension(ResourceUtil.getExtension(ps.getResource(),"")):
Constants.isLuceeComponentExtension(ResourceUtil.getExtension(ps.getResource(),""))) &&
isPossibleRawScript(sc,config.getIdentification())){*/
if (possibleUndetectedComponent) {
Page _p;
TagLibTag scriptTag = CFMLTransformer.getTLT(sc, isCFML ? Constants.CFML_SCRIPT_TAG_NAME : Constants.LUCEE_SCRIPT_TAG_NAME, config.getIdentification());
sc.setPos(0);
SourceCode original = sc;
// try inside a cfscript
String text = "<" + scriptTag.getFullName() + ">" + original.getText() + "\n</" + scriptTag.getFullName() + ">";
sc = new PageSourceCode(ps, text, charset, writeLog);
try {
while (true) {
if (sc == null) {
sc = new PageSourceCode(ps, charset, writeLog);
text = "<" + scriptTag.getFullName() + ">" + sc.getText() + "\n</" + scriptTag.getFullName() + ">";
sc = new PageSourceCode(ps, text, charset, writeLog);
}
try {
_p = transform(factory, config, sc, tlibs, flibs, ps.getResource().lastModified(), dotUpper, returnValue, ignoreScopes);
break;
} catch (ProcessingDirectiveException pde) {
if (pde.getWriteLog() != null)
writeLog = pde.getWriteLog().booleanValue();
if (pde.getDotNotationUpperCase() != null)
dotUpper = pde.getDotNotationUpperCase().booleanValue();
if (!StringUtil.isEmpty(pde.getCharset()))
charset = pde.getCharset();
sc = null;
}
}
} catch (ComponentTemplateException e) {
throw e.getTemplateException();
}
// we only use that result if it is a component now
if (_p != null && !_p.isPage())
return _p;
}
if (isCFMLCompExt && !p.isComponent() && !p.isInterface()) {
String msg = "template [" + ps.getDisplayPath() + "] must contain a component or an interface.";
if (sc != null)
throw new TemplateException(sc, msg);
throw new TemplateException(msg);
}
return p;
}
use of lucee.transformer.bytecode.Statement in project Lucee by lucee.
the class Component method evaluate.
@Override
public void evaluate(Tag tag, TagLibTag tlt) throws EvaluatorException {
/*if(tag instanceof TagOther) {
print.e(((TagOther)tag).getFullname());
}*/
TagCIObject tc = (TagCIObject) tag;
Statement pPage = tag.getParent();
// String className=tag.getTagLibTag().getTagClassName();
Page page;
// move components inside script to root
if (pPage instanceof Page) {
page = (Page) pPage;
} else {
// is in script
Tag p = ASMUtil.getParentTag(tag);
if ((pPage = p.getParent()) instanceof Page && p.getTagLibTag().getName().equalsIgnoreCase(((Page) pPage).getSourceCode().getDialect() == CFMLEngine.DIALECT_CFML ? Constants.CFML_SCRIPT_TAG_NAME : Constants.LUCEE_SCRIPT_TAG_NAME)) {
// chnaged order of the condition, not sure if this is ok
page = (Page) pPage;
// move imports from script to component body
List<Statement> children = p.getBody().getStatements();
Iterator<Statement> it = children.iterator();
Statement stat;
Tag t;
while (it.hasNext()) {
stat = it.next();
if (!(stat instanceof Tag))
continue;
t = (Tag) stat;
if (t.getTagLibTag().getName().equals("import")) {
tag.getBody().addStatement(t);
}
}
// move to page
ASMUtil.move(tag, page);
// if(!inline)ASMUtil.replace(p, tag, false);
} else
throw new EvaluatorException("Wrong Context, tag " + tlt.getFullName() + " can't be inside other tags, tag is inside tag " + p.getFullname());
}
// Page page=(Page) pPage;
Boolean insideCITemplate = isInsideCITemplate(page);
boolean main = isMainComponent(page, tc);
// is a full grown component or a inline component
if (insideCITemplate == Boolean.FALSE) {
throw new EvaluatorException("Wrong Context, " + tlt.getFullName() + " tag must be inside a file with the extension " + Constants.getCFMLComponentExtension() + " or " + Constants.getLuceeComponentExtension());
}
// if(count>1)
// throw new EvaluatorException("inside one cfc file only one tag "+tlt.getFullName()+" is allowed, now we have "+count);
boolean isComponent = tlt.getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Component");
/*boolean isInterface="lucee.runtime.tag.Interface".equals(tlt.getTagClassName());
if(main) {
if(isComponent) page.setIsComponent(true);
else if(isInterface) page.setIsInterface(true);
}*/
tc.setMain(main);
// Attributes
// Name
String name = null;
if (!main) {
Map<String, Attribute> attrs = tag.getAttributes();
if (attrs.size() > 0) {
Attribute first = attrs.values().iterator().next();
if (first.isDefaultValue()) {
name = first.getName();
}
}
if (name == null) {
Attribute attr = tag.getAttribute("name");
if (attr != null) {
Expression expr = tag.getFactory().toExprString(attr.getValue());
if (!(expr instanceof LitString))
throw new EvaluatorException("Name of the component " + tlt.getFullName() + ", must be a literal string value");
name = ((LitString) expr).getString();
} else
throw new EvaluatorException("Missing name of the component " + tlt.getFullName() + "");
}
tc.setName(name);
}
// output
// "output=true" is handled in "lucee.transformer.cfml.attributes.impl.Function"
Attribute attr = tag.getAttribute("output");
if (attr != null) {
Expression expr = tag.getFactory().toExprBoolean(attr.getValue());
if (!(expr instanceof LitBoolean))
throw new EvaluatorException("Attribute output of the Tag " + tlt.getFullName() + ", must contain a static boolean value (true or false, yes or no)");
// boolean output = ((LitBoolean)expr).getBooleanValue();
// if(!output) ASMUtil.removeLiterlChildren(tag, true);
}
// extends
attr = tag.getAttribute("extends");
if (attr != null) {
Expression expr = tag.getFactory().toExprString(attr.getValue());
if (!(expr instanceof LitString))
throw new EvaluatorException("Attribute extends of the Tag " + tlt.getFullName() + ", must contain a literal string value");
}
// implements
if (isComponent) {
attr = tag.getAttribute("implements");
if (attr != null) {
Expression expr = tag.getFactory().toExprString(attr.getValue());
if (!(expr instanceof LitString))
throw new EvaluatorException("Attribute implements of the Tag " + tlt.getFullName() + ", must contain a literal string value");
}
}
// modifier
if (isComponent) {
attr = tag.getAttribute("modifier");
if (attr != null) {
Expression expr = tag.getFactory().toExprString(attr.getValue());
if (!(expr instanceof LitString))
throw new EvaluatorException("Attribute modifier of the Tag " + tlt.getFullName() + ", must contain a literal string value");
LitString ls = (LitString) expr;
int mod = ComponentUtil.toModifier(ls.getString(), lucee.runtime.Component.MODIFIER_NONE, -1);
if (mod == -1)
throw new EvaluatorException("Value [" + ls.getString() + "] from attribute modifier of the Tag " + tlt.getFullName() + " is invalid,valid values are [none,abstract,final]");
}
}
}
use of lucee.transformer.bytecode.Statement in project Lucee by lucee.
the class Function method throwIfNotEmpty.
public static void throwIfNotEmpty(Tag tag) throws EvaluatorException {
Body body = tag.getBody();
List<Statement> statments = body.getStatements();
Statement stat;
Iterator<Statement> it = statments.iterator();
TagLibTag tlt;
while (it.hasNext()) {
stat = it.next();
if (stat instanceof Tag) {
tlt = ((Tag) stat).getTagLibTag();
if (!tlt.getTagClassDefinition().isClassNameEqualTo("lucee.runtime.tag.Argument"))
throw new EvaluatorException("tag " + tlt.getFullName() + " is not allowed inside a function declaration");
}
/*else if(stat instanceof PrintOut) {
//body.remove(stat);
}*/
}
}
use of lucee.transformer.bytecode.Statement in project Lucee by lucee.
the class Query method translateChildren.
private void translateChildren(Iterator it) {
Statement stat;
while (it.hasNext()) {
stat = (Statement) it.next();
if (stat instanceof PrintOut) {
PrintOut printOut = ((PrintOut) stat);
Expression e = printOut.getExpr();
if (!(e instanceof Literal)) {
Expression expr = removeCastString(e);
if (expr instanceof Variable) {
// do not preserve BIF PreserveSingleQuotes return value
Member member = ((Variable) expr).getFirstMember();
if (member instanceof BIF) {
BIF bif = (BIF) member;
if (bif.getClassDefinition().getClassName().equals(PreserveSingleQuotes.class.getName())) {
printOut.setExpr(bif.getArguments()[0].getValue());
continue;
} else if (bif.getClassDefinition().getClassName().equals(ListQualify.class.getName())) {
Argument[] args = bif.getArguments();
List<Argument> arr = new ArrayList<Argument>();
// first get existing arguments
arr.add(args[0]);
arr.add(args[1]);
if (args.length >= 3)
arr.add(args[2]);
else
arr.add(new Argument(expr.getFactory().createLitString(","), "string"));
if (args.length >= 4)
arr.add(args[3]);
else
arr.add(new Argument(expr.getFactory().createLitString("all"), "string"));
if (args.length >= 5)
arr.add(args[4]);
else
arr.add(new Argument(expr.getFactory().createLitBoolean(false), "boolean"));
// PSQ-BIF DO NOT REMOVE THIS COMMENT
arr.add(new Argument(expr.getFactory().createLitBoolean(true), "boolean"));
bif.setArguments(arr.toArray(new Argument[arr.size()]));
continue;
} else if (bif.getClassDefinition().getClassName().equals(QuotedValueList.class.getName()) || bif.getClassDefinition().getClassName().equals(ValueList.class.getName())) {
// printOut.setPreserveSingleQuote(false);
continue;
}
}
// do not preserve UDF return value
member = ((Variable) expr).getLastMember();
if (member instanceof UDF)
continue;
}
printOut.setCheckPSQ(true);
if (e != expr)
printOut.setExpr(expr);
}
} else if (stat instanceof Tag) {
Body b = ((Tag) stat).getBody();
if (b != null)
translateChildren(b.getStatements().iterator());
} else if (stat instanceof Body) {
translateChildren(((Body) stat).getStatements().iterator());
}
}
}
Aggregations