Search in sources :

Example 6 with SourceCode

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

the class Renderer method loadClass.

private static Class<? extends Page> loadClass(ConfigWeb config, String className, String cfml, int dialect, boolean ignoreScopes) throws Exception {
    ConfigWebImpl cw = (ConfigWebImpl) config;
    CFMLCompilerImpl compiler = cw.getCompiler();
    // create className based o the content
    Class<? extends Page> clazz = null;
    if (mcl == null) {
        mcl = createMemoryClassLoader(cw);
    } else
        clazz = ClassUtil.loadClass(mcl, className, null);
    if (clazz != null)
        return clazz;
    SourceCode sc = new SourceCode(cfml, false, dialect);
    // compile
    lucee.runtime.compiler.CFMLCompilerImpl.Result result = compiler.compile(cw, sc, cw.getTLDs(dialect), cw.getFLDs(dialect), null, className, true, ignoreScopes);
    // before we add a new class, we make sure we are still in range
    if (mcl.getSize() + result.barr.length > MAX_SIZE) {
        mcl = createMemoryClassLoader(cw);
        pages.clear();
    }
    return (Class<? extends Page>) mcl.loadClass(className, result.barr);
}
Also used : ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) SourceCode(lucee.transformer.util.SourceCode) Page(lucee.runtime.Page)

Example 7 with SourceCode

use of lucee.transformer.util.SourceCode 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;
}
Also used : TagLibTag(lucee.transformer.library.tag.TagLibTag) PageSourceCode(lucee.transformer.util.PageSourceCode) SourceCode(lucee.transformer.util.SourceCode) PageSourceCode(lucee.transformer.util.PageSourceCode) ComponentTemplateException(lucee.transformer.cfml.script.AbstrCFMLScriptTransformer.ComponentTemplateException) TemplateException(lucee.runtime.exp.TemplateException) Statement(lucee.transformer.bytecode.Statement) ComponentTemplateException(lucee.transformer.cfml.script.AbstrCFMLScriptTransformer.ComponentTemplateException) Charset(java.nio.charset.Charset) Page(lucee.transformer.bytecode.Page) ProcessingDirectiveException(lucee.transformer.cfml.evaluator.impl.ProcessingDirectiveException) LitString(lucee.transformer.expression.literal.LitString) LitString(lucee.transformer.expression.literal.LitString) PrintOut(lucee.transformer.bytecode.statement.PrintOut) Expression(lucee.transformer.expression.Expression) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 8 with SourceCode

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

the class Component method isInsideCITemplate.

/**
 * is the template ending with a component extension?
 * @param page
 * @return return true if so false otherwse and null if the code is not depending on a template
 */
private Boolean isInsideCITemplate(Page page) {
    SourceCode sc = page.getSourceCode();
    if (!(sc instanceof PageSourceCode))
        return null;
    PageSource psc = ((PageSourceCode) sc).getPageSource();
    String src = psc.getDisplayPath();
    return Constants.isComponentExtension(ResourceUtil.getExtension(src, ""));
// int pos=src.lastIndexOf(".");
// return  pos!=-1 && pos<src.length() && src.substring(pos+1).equals(Constants.COMPONENT_EXTENSION);
}
Also used : PageSourceCode(lucee.transformer.util.PageSourceCode) SourceCode(lucee.transformer.util.SourceCode) PageSourceCode(lucee.transformer.util.PageSourceCode) LitString(lucee.transformer.expression.literal.LitString) PageSource(lucee.runtime.PageSource)

Example 9 with SourceCode

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

the class Sprite method evaluate.

@Override
public void evaluate(Tag tag, TagLibTag tagLibTag, FunctionLib[] flibs) throws EvaluatorException {
    String id = "sprite_" + IDGenerator.intId();
    try {
        Page page = ASMUtil.getAncestorPage(tag);
        SourceCode sc = page.getSourceCode();
        String key = sc.id();
        key = HashUtil.create64BitHashAsString(Thread.currentThread().getId() + ":" + key);
        Expression src = tag.getAttribute("src").getValue();
        // get data from previous sprites
        Previous previous = sprites.get(key);
        if (previous != null) {
            previous.tag.removeAttribute("_ids");
            previous.tag.removeAttribute("_srcs");
            previous.tag = tag;
        } else {
            sprites.put(key, previous = new Previous(tag));
        }
        previous.ids.add(id);
        if (previous.src == null)
            previous.src = src;
        else {
            previous.src = tag.getFactory().opString(previous.src, tag.getFactory().createLitString(","));
            previous.src = tag.getFactory().opString(previous.src, src);
        }
        tag.addAttribute(new Attribute(false, "_id", tag.getFactory().createLitString(id), "string"));
        tag.addAttribute(new Attribute(false, "_ids", tag.getFactory().createLitString(lucee.runtime.type.util.ListUtil.listToList(previous.ids, ",")), "string"));
        tag.addAttribute(new Attribute(false, "_srcs", previous.src, "string"));
    } catch (Throwable e) {
        // TODO handle Excpetion much more precise
        ExceptionUtil.rethrowIfNecessary(e);
        throw new PageRuntimeException(Caster.toPageException(e));
    }
}
Also used : SourceCode(lucee.transformer.util.SourceCode) Expression(lucee.transformer.expression.Expression) Attribute(lucee.transformer.bytecode.statement.tag.Attribute) Page(lucee.transformer.bytecode.Page) PageRuntimeException(lucee.runtime.exp.PageRuntimeException)

Example 10 with SourceCode

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

the class HTMLUtil method getURLS.

/**
 * returns all urls in a html String
 * @param html HTML String to search urls
 * @param url Absolute URL path to set
 * @return urls found in html String
 */
public List<URL> getURLS(String html, URL url) {
    List<URL> urls = new ArrayList<URL>();
    SourceCode cfml = new SourceCode(html, false, CFMLEngine.DIALECT_CFML);
    while (!cfml.isAfterLast()) {
        if (cfml.forwardIfCurrent('<')) {
            for (int i = 0; i < tags.length; i++) {
                if (cfml.forwardIfCurrent(tags[i].tag + " ")) {
                    getSingleUrl(urls, cfml, tags[i], url);
                }
            }
        } else {
            cfml.next();
        }
    }
    return urls;
}
Also used : SourceCode(lucee.transformer.util.SourceCode) ArrayList(java.util.ArrayList) URL(java.net.URL)

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