Search in sources :

Example 1 with SourceCode

use of lucee.transformer.util.SourceCode in project Lucee by lucee.

the class EvaluatorPool method run.

/**
 * Die Methode run wird aufgerufen sobald, der CFML Transformer den uebersetzungsprozess angeschlossen hat.
 * Die metode run rauft darauf alle Evaluatoren auf die intern gespeicher wurden und loescht den internen Speicher.
 * @throws TemplateException
 */
public void run() throws TemplateException {
    {
        // tags
        Iterator<TagData> it = tags.iterator();
        while (it.hasNext()) {
            TagData td = it.next();
            SourceCode cfml = td.cfml;
            cfml.setPos(td.pos);
            try {
                if (td.libTag.getEvaluator() != null)
                    td.libTag.getEvaluator().evaluate(td.tag, td.libTag, td.flibs);
            } catch (EvaluatorException e) {
                // print.printST(e);
                clear();
                throw new TemplateException(cfml, e);
            } catch (Throwable e) {
                ExceptionUtil.rethrowIfNecessary(e);
                clear();
                throw new TemplateException(cfml, e);
            }
        }
        tags.clear();
    }
    // functions
    Iterator<FunctionData> it = functions.iterator();
    while (it.hasNext()) {
        FunctionData td = it.next();
        SourceCode cfml = td.cfml;
        cfml.setPos(td.pos);
        try {
            if (td.flf.getEvaluator() != null)
                td.flf.getEvaluator().evaluate(td.bif, td.flf);
        } catch (EvaluatorException e) {
            // print.printST(e);
            clear();
            throw new TemplateException(cfml, e);
        } catch (Throwable e) {
            ExceptionUtil.rethrowIfNecessary(e);
            clear();
            throw new TemplateException(cfml, e);
        }
    }
    functions.clear();
}
Also used : SourceCode(lucee.transformer.util.SourceCode) TemplateException(lucee.runtime.exp.TemplateException) Iterator(java.util.Iterator)

Example 2 with SourceCode

use of lucee.transformer.util.SourceCode in project Lucee by lucee.

the class Function method evaluate.

@Override
public void evaluate(Tag tag, TagLibTag libTag, FunctionLib[] flibs) throws EvaluatorException {
    // Body p=(Body) tag.getParent();
    // Statement pp = p.getParent();
    boolean isCI = true;
    try {
        isCI = ASMUtil.getAncestorPage(tag).isComponent() || ASMUtil.getAncestorPage(tag).isInterface();
    } catch (TransformerException e) {
    }
    Attribute attrName = tag.getAttribute("name");
    if (attrName != null) {
        Expression expr = attrName.getValue();
        PageSource ps = null;
        if (expr instanceof LitString && !isCI) {
            Page p = ASMUtil.getAncestorPage(tag, null);
            if (p != null) {
                SourceCode sc = p.getSourceCode();
                if (sc instanceof PageSourceCode) {
                    PageSourceCode psc = (PageSourceCode) sc;
                    ps = psc.getPageSource();
                }
            }
            checkFunctionName(((LitString) expr).getString(), flibs, ps);
        }
    }
    // attribute modifier
    boolean isStatic = false;
    {
        Attribute attrModifier = tag.getAttribute("modifier");
        if (attrModifier != null) {
            ExprString expr = tag.getFactory().toExprString(attrModifier.getValue());
            if (!(expr instanceof Literal))
                throw new EvaluatorException("Attribute modifier of the Tag Function, must be one of the following literal string values: [abstract,final,static]");
            String modifier = StringUtil.emptyIfNull(((Literal) expr).getString()).trim();
            if (!StringUtil.isEmpty(modifier) && !"abstract".equalsIgnoreCase(modifier) && !"final".equalsIgnoreCase(modifier) && !"static".equalsIgnoreCase(modifier))
                throw new EvaluatorException("Attribute modifier of the Tag Function, must be one of the following literal string values: [abstract,final,static]");
            isStatic = "static".equalsIgnoreCase(modifier);
            boolean abstr = "abstract".equalsIgnoreCase(modifier);
            if (abstr)
                throwIfNotEmpty(tag);
        }
    }
    // cachedWithin
    {
        Attribute attrCachedWithin = tag.getAttribute("cachedwithin");
        if (attrCachedWithin != null) {
            Expression val = attrCachedWithin.getValue();
            tag.addAttribute(new Attribute(attrCachedWithin.isDynamicType(), attrCachedWithin.getName(), ASMUtil.cachedWithinValue(val), attrCachedWithin.getType()));
        }
    }
    // Attribute localMode
    {
        Attribute attrLocalMode = tag.getAttribute("localmode");
        if (attrLocalMode != null) {
            Expression expr = attrLocalMode.getValue();
            String str = ASMUtil.toString(expr, null);
            if (!StringUtil.isEmpty(str) && AppListenerUtil.toLocalMode(str, -1) == -1)
                throw new EvaluatorException("Attribute localMode of the Tag Function, must be a literal value (modern, classic, true or false)");
        // boolean output = ((LitBoolean)expr).getBooleanValue();
        // if(!output) ASMUtil.removeLiterlChildren(tag, true);
        }
    }
    // Attribute Output
    {
        Attribute attrOutput = tag.getAttribute("output");
        if (attrOutput != null) {
            Expression expr = tag.getFactory().toExprBoolean(attrOutput.getValue());
            if (!(expr instanceof LitBoolean))
                throw new EvaluatorException("Attribute output of the Tag Function, must be a literal boolean value (true or false, yes or no)");
        }
    }
    // Buffer output
    {
        Attribute attrBufferOutput = tag.getAttribute("bufferoutput");
        if (attrBufferOutput != null) {
            Expression expr = tag.getFactory().toExprBoolean(attrBufferOutput.getValue());
            if (!(expr instanceof LitBoolean))
                throw new EvaluatorException("Attribute bufferOutput of the Tag Function, must be a literal boolean value (true or false, yes or no)");
        }
    }
    // check attribute values
    Map<String, Attribute> attrs = tag.getAttributes();
    Iterator<Attribute> it = attrs.values().iterator();
    while (it.hasNext()) {
        checkAttributeValue(tag, it.next());
    }
    // add to static scope
    if (isStatic) {
        // remove that tag from parent
        ASMUtil.remove(tag);
        Body body = (Body) tag.getParent();
        StaticBody sb = Static.getStaticBody(body);
        sb.addStatement(tag);
    }
}
Also used : SourceCode(lucee.transformer.util.SourceCode) PageSourceCode(lucee.transformer.util.PageSourceCode) PageSourceCode(lucee.transformer.util.PageSourceCode) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) ExprString(lucee.transformer.expression.ExprString) StaticBody(lucee.transformer.bytecode.StaticBody) Page(lucee.transformer.bytecode.Page) LitBoolean(lucee.transformer.expression.literal.LitBoolean) LitString(lucee.transformer.expression.literal.LitString) ExprString(lucee.transformer.expression.ExprString) PageSource(lucee.runtime.PageSource) LitString(lucee.transformer.expression.literal.LitString) Expression(lucee.transformer.expression.Expression) EvaluatorException(lucee.transformer.cfml.evaluator.EvaluatorException) Literal(lucee.transformer.expression.literal.Literal) StaticBody(lucee.transformer.bytecode.StaticBody) Body(lucee.transformer.bytecode.Body) TransformerException(lucee.transformer.TransformerException)

Example 3 with SourceCode

use of lucee.transformer.util.SourceCode 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");
}
Also used : SourceCode(lucee.transformer.util.SourceCode) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) ExprTransformer(lucee.transformer.cfml.ExprTransformer) Page(lucee.transformer.bytecode.Page) LitString(lucee.transformer.expression.literal.LitString) EvaluatorException(lucee.transformer.cfml.evaluator.EvaluatorException) TemplateException(lucee.runtime.exp.TemplateException) TransfomerSettings(lucee.transformer.cfml.TransfomerSettings) EvaluatorPool(lucee.transformer.cfml.evaluator.EvaluatorPool) EvaluatorException(lucee.transformer.cfml.evaluator.EvaluatorException) Expression(lucee.transformer.expression.Expression) TagLib(lucee.transformer.library.tag.TagLib) ConfigImpl(lucee.runtime.config.ConfigImpl) TagLoop(lucee.transformer.bytecode.statement.tag.TagLoop)

Example 4 with SourceCode

use of lucee.transformer.util.SourceCode in project Lucee by lucee.

the class URLResolver method transform.

/**
 * transform the HTML String
 * @param html HTML String to transform
 * @param url Absolute URL path to set
 * @return transformed HTMl String
 * @throws PageException
 */
public String transform(String html, URL url, boolean setBaseTag) throws PageException {
    StringBuffer target = new StringBuffer();
    SourceCode cfml = new SourceCode(html, false, CFMLEngine.DIALECT_CFML);
    while (!cfml.isAfterLast()) {
        if (cfml.forwardIfCurrent('<')) {
            target.append('<');
            try {
                for (int i = 0; i < tags.length; i++) {
                    if (cfml.forwardIfCurrent(tags[i].tag + " ")) {
                        target.append(tags[i].tag + " ");
                        transformTag(target, cfml, tags[i], url);
                    }
                }
            } catch (MalformedURLException me) {
                throw Caster.toPageException(me);
            }
        } else {
            target.append(cfml.getCurrent());
            cfml.next();
        }
    }
    if (!setBaseTag)
        return target.toString();
    html = target.toString();
    String prefix = "", postfix = "";
    int index = StringUtil.indexOfIgnoreCase(html, "</head>");
    if (index == -1) {
        prefix = "<head>";
        postfix = "</head>";
        index = StringUtil.indexOfIgnoreCase(html, "</html>");
    }
    if (index != -1) {
        StringBuffer sb = new StringBuffer();
        sb.append(html.substring(0, index));
        String port = url.getPort() == -1 ? "" : ":" + url.getPort();
        sb.append(prefix + "<base href=\"" + (url.getProtocol() + "://" + url.getHost() + port) + "\">" + postfix);
        sb.append(html.substring(index));
        html = sb.toString();
    }
    return html;
}
Also used : SourceCode(lucee.transformer.util.SourceCode) MalformedURLException(java.net.MalformedURLException)

Example 5 with SourceCode

use of lucee.transformer.util.SourceCode in project Lucee by lucee.

the class AbstrCFMLExprTransformer method multiLineComment.

/**
 * Liest einen Mehrzeiligen Kommentar ein.
 * <br />
 * EBNF:<br />
 * <code>?-"*<!-- -->/";</code>
 * @return bool Wurde ein Kommentar entfernt?
 * @throws TemplateException
 */
private boolean multiLineComment(ExprData data) throws TemplateException {
    SourceCode cfml = data.srcCode;
    if (!cfml.forwardIfCurrent("/*"))
        return false;
    int pos = cfml.getPos();
    boolean isDocComment = cfml.isCurrent('*');
    while (cfml.isValidIndex()) {
        if (cfml.isCurrent("*/"))
            break;
        cfml.next();
    }
    if (!cfml.forwardIfCurrent("*/")) {
        cfml.setPos(pos);
        throw new TemplateException(cfml, "comment is not closed");
    }
    if (isDocComment) {
        String comment = cfml.substring(pos - 2, cfml.getPos() - pos);
        data.docComment = docCommentTransformer.transform(data.factory, comment);
    }
    return true;
}
Also used : SourceCode(lucee.transformer.util.SourceCode) TemplateException(lucee.runtime.exp.TemplateException) LitString(lucee.transformer.expression.literal.LitString) ExprString(lucee.transformer.expression.ExprString)

Aggregations

SourceCode (lucee.transformer.util.SourceCode)10 LitString (lucee.transformer.expression.literal.LitString)5 TemplateException (lucee.runtime.exp.TemplateException)4 Page (lucee.transformer.bytecode.Page)4 Expression (lucee.transformer.expression.Expression)4 Attribute (lucee.transformer.bytecode.statement.tag.Attribute)3 PageSourceCode (lucee.transformer.util.PageSourceCode)3 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 PageSource (lucee.runtime.PageSource)2 EvaluatorException (lucee.transformer.cfml.evaluator.EvaluatorException)2 ExprString (lucee.transformer.expression.ExprString)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Charset (java.nio.charset.Charset)1 List (java.util.List)1 Page (lucee.runtime.Page)1 ConfigImpl (lucee.runtime.config.ConfigImpl)1 ConfigWebImpl (lucee.runtime.config.ConfigWebImpl)1 PageRuntimeException (lucee.runtime.exp.PageRuntimeException)1