Search in sources :

Example 46 with PageSource

use of lucee.runtime.PageSource in project Lucee by lucee.

the class ConfigImpl method getPageSourceExisting.

public PageSource getPageSourceExisting(PageContext pc, Mapping[] mappings, String realPath, boolean onlyTopLevel, boolean useSpecialMappings, boolean useDefaultMapping, boolean onlyPhysicalExisting) {
    realPath = realPath.replace('\\', '/');
    String lcRealPath = StringUtil.toLowerCase(realPath) + '/';
    Mapping mapping;
    PageSource ps;
    if (mappings != null) {
        for (int i = 0; i < mappings.length; i++) {
            mapping = mappings[i];
            // print.err(lcRealPath+".startsWith"+(mapping.getStrPhysical()));
            if (lcRealPath.startsWith(mapping.getVirtualLowerCaseWithSlash(), 0)) {
                ps = mapping.getPageSource(realPath.substring(mapping.getVirtual().length()));
                if (onlyPhysicalExisting) {
                    if (ps.physcalExists())
                        return ps;
                } else if (ps.exists())
                    return ps;
            }
        }
    }
    // / special mappings
    if (useSpecialMappings && lcRealPath.startsWith("/mapping-", 0)) {
        String virtual = "/mapping-tag";
        // tag mappings
        Mapping[] tagMappings = (this instanceof ConfigWebImpl) ? new Mapping[] { ((ConfigWebImpl) this).getServerTagMapping(), getTagMapping() } : new Mapping[] { getTagMapping() };
        if (lcRealPath.startsWith(virtual, 0)) {
            for (int i = 0; i < tagMappings.length; i++) {
                mapping = tagMappings[i];
                // if(lcRealPath.startsWith(mapping.getVirtualLowerCaseWithSlash(),0)) {
                ps = mapping.getPageSource(realPath.substring(virtual.length()));
                if (onlyPhysicalExisting) {
                    if (ps.physcalExists())
                        return ps;
                } else if (ps.exists())
                    return ps;
            // }
            }
        }
        // customtag mappings
        tagMappings = getCustomTagMappings();
        virtual = "/mapping-customtag";
        if (lcRealPath.startsWith(virtual, 0)) {
            for (int i = 0; i < tagMappings.length; i++) {
                mapping = tagMappings[i];
                // if(lcRealPath.startsWith(mapping.getVirtualLowerCaseWithSlash(),0)) {
                ps = mapping.getPageSource(realPath.substring(virtual.length()));
                if (onlyPhysicalExisting) {
                    if (ps.physcalExists())
                        return ps;
                } else if (ps.exists())
                    return ps;
            // }
            }
        }
    }
    // component mappings (only used for gateway)
    if (pc != null && ((PageContextImpl) pc).isGatewayContext()) {
        boolean isCFC = Constants.isComponentExtension(ResourceUtil.getExtension(realPath, null));
        if (isCFC) {
            Mapping[] cmappings = getComponentMappings();
            for (int i = 0; i < cmappings.length; i++) {
                ps = cmappings[i].getPageSource(realPath);
                if (onlyPhysicalExisting) {
                    if (ps.physcalExists())
                        return ps;
                } else if (ps.exists())
                    return ps;
            }
        }
    }
    // config mappings
    for (int i = 0; i < this.mappings.length - 1; i++) {
        mapping = this.mappings[i];
        if ((!onlyTopLevel || mapping.isTopLevel()) && lcRealPath.startsWith(mapping.getVirtualLowerCaseWithSlash(), 0)) {
            ps = mapping.getPageSource(realPath.substring(mapping.getVirtual().length()));
            if (onlyPhysicalExisting) {
                if (ps.physcalExists())
                    return ps;
            } else if (ps.exists())
                return ps;
        }
    }
    if (useDefaultMapping) {
        ps = this.mappings[this.mappings.length - 1].getPageSource(realPath);
        if (onlyPhysicalExisting) {
            if (ps.physcalExists())
                return ps;
        } else if (ps.exists())
            return ps;
    }
    return null;
}
Also used : Mapping(lucee.runtime.Mapping) PageSource(lucee.runtime.PageSource)

Example 47 with PageSource

use of lucee.runtime.PageSource in project Lucee by lucee.

the class ConfigImpl method listComponentCache.

public Struct listComponentCache() {
    Struct sct = new StructImpl();
    if (componentPathCache == null)
        return sct;
    Iterator<Entry<String, PageSource>> it = componentPathCache.entrySet().iterator();
    Entry<String, PageSource> entry;
    while (it.hasNext()) {
        entry = it.next();
        sct.setEL(entry.getKey(), entry.getValue().getDisplayPath());
    }
    return sct;
}
Also used : DumpWriterEntry(lucee.runtime.dump.DumpWriterEntry) Entry(java.util.Map.Entry) GatewayEntry(lucee.runtime.gateway.GatewayEntry) StructImpl(lucee.runtime.type.StructImpl) Struct(lucee.runtime.type.Struct) PageSource(lucee.runtime.PageSource)

Example 48 with PageSource

use of lucee.runtime.PageSource in project Lucee by lucee.

the class SourceLastModifiedClassAdapter method execute.

/**
 * convert the Page Object to java bytecode
 * @param className name of the genrated class (only necessary when Page object has no PageSource reference)
 * @return
 * @throws TransformerException
 */
public byte[] execute(String className) throws TransformerException {
    // not exists in any case, so every usage must have a plan b for not existence
    PageSource optionalPS = sourceCode instanceof PageSourceCode ? ((PageSourceCode) sourceCode).getPageSource() : null;
    List<LitString> keys = new ArrayList<LitString>();
    ClassWriter cw = ASMUtil.getClassWriter();
    ArrayList<String> imports = new ArrayList<String>();
    getImports(imports, this);
    // look for component if necessary
    TagCIObject comp = getTagCFObject(null);
    // in case we have a sub component
    if (className == null) {
        if (optionalPS == null)
            throw new IllegalArgumentException("when Page object has no PageSource, a className is necessary");
        className = optionalPS.getClassName();
    }
    if (comp != null)
        className = createSubClass(className, comp.getName(), sourceCode.getDialect());
    className = className.replace('.', '/');
    this.className = className;
    // parent
    // "lucee/runtime/Page";
    String parent = PageImpl.class.getName();
    if (// "lucee/runtime/ComponentPage";
    isComponent(comp))
        // "lucee/runtime/ComponentPage";
        parent = ComponentPageImpl.class.getName();
    else // "lucee/runtime/InterfacePage";
    if (isInterface(comp))
        parent = InterfacePageImpl.class.getName();
    parent = parent.replace('.', '/');
    cw.visit(Opcodes.V1_6, Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, className, null, parent, null);
    if (optionalPS != null) {
        // we use full path  when FD is enabled
        String path = config.allowRequestTimeout() ? optionalPS.getRealpathWithVirtual() : optionalPS.getPhyscalFile().getAbsolutePath();
        // when adding more use ; as delimiter
        cw.visitSource(path, null);
    // cw.visitSource(optionalPS.getPhyscalFile().getAbsolutePath(),
    // "rel:"+optionalPS.getRealpathWithVirtual()); // when adding more use ; as delimiter
    } else {
    // cw.visitSource("","rel:");
    }
    // static constructor
    // GeneratorAdapter statConstrAdapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC,STATIC_CONSTRUCTOR,null,null,cw);
    // StaticConstrBytecodeContext statConstr = null;//new BytecodeContext(null,null,this,externalizer,keys,cw,name,statConstrAdapter,STATIC_CONSTRUCTOR,writeLog(),suppressWSbeforeArg);
    // constructor
    GeneratorAdapter constrAdapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC, CONSTRUCTOR_PS, null, null, cw);
    ConstrBytecodeContext constr = new ConstrBytecodeContext(optionalPS, this, keys, cw, className, constrAdapter, CONSTRUCTOR_PS, writeLog(), suppressWSbeforeArg, output, returnValue);
    constrAdapter.loadThis();
    Type t;
    if (isComponent(comp)) {
        t = Types.COMPONENT_PAGE_IMPL;
        // extends
        // Attribute attr = comp.getAttribute("extends");
        // if(attr!=null) ExpressionUtil.writeOutSilent(attr.getValue(),constr, Expression.MODE_REF);
        // else constrAdapter.push("");
        constrAdapter.invokeConstructor(t, CONSTRUCTOR);
    } else if (isInterface(comp)) {
        t = Types.INTERFACE_PAGE_IMPL;
        constrAdapter.invokeConstructor(t, CONSTRUCTOR);
    } else {
        t = Types.PAGE_IMPL;
        constrAdapter.invokeConstructor(t, CONSTRUCTOR);
    }
    // call _init()
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
    constrAdapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, constr.getClassName(), "initKeys", "()V");
    // private static  ImportDefintion[] test=new ImportDefintion[]{...};
    {
        FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "imports", "[Llucee/runtime/component/ImportDefintion;", null, null);
        fv.visitEnd();
        constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
        ArrayVisitor av = new ArrayVisitor();
        av.visitBegin(constrAdapter, Types.IMPORT_DEFINITIONS, imports.size());
        int index = 0;
        Iterator<String> it = imports.iterator();
        while (it.hasNext()) {
            av.visitBeginItem(constrAdapter, index++);
            constrAdapter.push(it.next());
            ASMConstants.NULL(constrAdapter);
            constrAdapter.invokeStatic(Types.IMPORT_DEFINITIONS_IMPL, ID_GET_INSTANCE);
            av.visitEndItem(constrAdapter);
        }
        av.visitEnd();
        constrAdapter.visitFieldInsn(Opcodes.PUTFIELD, className, "imports", "[Llucee/runtime/component/ImportDefintion;");
    }
    // getVersion
    GeneratorAdapter adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, VERSION, null, null, cw);
    adapter.push(version);
    adapter.returnValue();
    adapter.endMethod();
    // public ImportDefintion[] getImportDefintions()
    if (imports.size() > 0) {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, GET_IMPORT_DEFINITIONS, null, null, cw);
        adapter.visitVarInsn(Opcodes.ALOAD, 0);
        adapter.visitFieldInsn(Opcodes.GETFIELD, className, "imports", "[Llucee/runtime/component/ImportDefintion;");
        adapter.returnValue();
        adapter.endMethod();
    } else {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, GET_IMPORT_DEFINITIONS, null, null, cw);
        adapter.visitInsn(Opcodes.ICONST_0);
        adapter.visitTypeInsn(Opcodes.ANEWARRAY, "lucee/runtime/component/ImportDefintion");
        adapter.returnValue();
        adapter.endMethod();
    }
    // getSourceLastModified
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, LAST_MOD, null, null, cw);
    adapter.push(lastModifed);
    adapter.returnValue();
    adapter.endMethod();
    // getSourceLength
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, LENGTH, null, null, cw);
    adapter.push(length);
    adapter.returnValue();
    adapter.endMethod();
    // getCompileTime
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, COMPILE_TIME, null, null, cw);
    adapter.push(System.currentTimeMillis());
    adapter.returnValue();
    adapter.endMethod();
    // getHash
    adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, HASH, null, null, cw);
    adapter.push(hash);
    adapter.returnValue();
    adapter.endMethod();
    if (comp != null) {
        writeOutStaticConstructor(constr, keys, cw, comp, className);
    }
    // newInstance/initComponent/call
    if (isComponent()) {
        writeOutNewComponent(constr, keys, cw, comp, className);
        writeOutInitComponent(constr, keys, cw, comp, className);
    } else if (isInterface()) {
        writeOutNewInterface(constr, keys, cw, comp, className);
        writeOutInitInterface(constr, keys, cw, comp, className);
    } else {
        writeOutCall(constr, keys, cw, className);
    }
    // write UDFProperties to constructor
    // writeUDFProperties(bc,funcs,pageType);
    // udfCall
    Function[] functions = getFunctions();
    ConditionVisitor cv;
    DecisionIntVisitor div;
    // less/equal than 10 functions
    if (isInterface()) {
    } else if (functions.length <= 10) {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_CALL, null, new Type[] { Types.THROWABLE }, cw);
        BytecodeContext bc = new BytecodeContext(optionalPS, constr, this, keys, cw, className, adapter, UDF_CALL, writeLog(), suppressWSbeforeArg, output, returnValue);
        if (functions.length == 0) {
        } else if (functions.length == 1) {
            ExpressionUtil.visitLine(bc, functions[0].getStart());
            functions[0].getBody().writeOut(bc);
            ExpressionUtil.visitLine(bc, functions[0].getEnd());
        } else
            writeOutUdfCallInner(bc, functions, 0, functions.length);
        adapter.visitInsn(Opcodes.ACONST_NULL);
        adapter.returnValue();
        adapter.endMethod();
    } else // more than 10 functions
    {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_CALL, null, new Type[] { Types.THROWABLE }, cw);
        BytecodeContext bc = new BytecodeContext(optionalPS, constr, this, keys, cw, className, adapter, UDF_CALL, writeLog(), suppressWSbeforeArg, output, returnValue);
        cv = new ConditionVisitor();
        cv.visitBefore();
        int count = 0;
        for (int i = 0; i < functions.length; i += 10) {
            cv.visitWhenBeforeExpr();
            div = new DecisionIntVisitor();
            div.visitBegin();
            adapter.loadArg(2);
            div.visitLT();
            adapter.push(i + 10);
            div.visitEnd(bc);
            cv.visitWhenAfterExprBeforeBody(bc);
            adapter.visitVarInsn(Opcodes.ALOAD, 0);
            adapter.visitVarInsn(Opcodes.ALOAD, 1);
            adapter.visitVarInsn(Opcodes.ALOAD, 2);
            adapter.visitVarInsn(Opcodes.ILOAD, 3);
            adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, createFunctionName(++count), "(Llucee/runtime/PageContext;Llucee/runtime/type/UDF;I)Ljava/lang/Object;");
            // adapter.returnValue();
            adapter.visitInsn(Opcodes.ARETURN);
            cv.visitWhenAfterBody(bc);
        }
        cv.visitAfter(bc);
        adapter.visitInsn(Opcodes.ACONST_NULL);
        adapter.returnValue();
        adapter.endMethod();
        count = 0;
        Method innerCall;
        for (int i = 0; i < functions.length; i += 10) {
            innerCall = new Method(createFunctionName(++count), Types.OBJECT, new Type[] { Types.PAGE_CONTEXT, USER_DEFINED_FUNCTION, Types.INT_VALUE });
            adapter = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, innerCall, null, new Type[] { Types.THROWABLE }, cw);
            writeOutUdfCallInner(new BytecodeContext(optionalPS, constr, this, keys, cw, className, adapter, innerCall, writeLog(), suppressWSbeforeArg, output, returnValue), functions, i, i + 10 > functions.length ? functions.length : i + 10);
            adapter.visitInsn(Opcodes.ACONST_NULL);
            adapter.returnValue();
            adapter.endMethod();
        }
    }
    // threadCall
    TagThread[] threads = getThreads();
    if (true) {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, THREAD_CALL, null, new Type[] { Types.THROWABLE }, cw);
        if (threads.length > 0)
            writeOutThreadCallInner(new BytecodeContext(optionalPS, constr, this, keys, cw, className, adapter, THREAD_CALL, writeLog(), suppressWSbeforeArg, output, returnValue), threads, 0, threads.length);
        // adapter.visitInsn(Opcodes.ACONST_NULL);
        adapter.returnValue();
        adapter.endMethod();
    }
    // less/equal than 10 functions
    if (isInterface()) {
    } else if (functions.length <= 10) {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_DEFAULT_VALUE, null, new Type[] { Types.PAGE_EXCEPTION }, cw);
        if (functions.length > 0)
            writeUdfDefaultValueInner(new BytecodeContext(optionalPS, constr, this, keys, cw, className, adapter, UDF_DEFAULT_VALUE, writeLog(), suppressWSbeforeArg, output, returnValue), functions, 0, functions.length);
        adapter.loadArg(DEFAULT_VALUE);
        adapter.returnValue();
        adapter.endMethod();
    } else {
        adapter = new GeneratorAdapter(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL, UDF_DEFAULT_VALUE, null, new Type[] { Types.PAGE_EXCEPTION }, cw);
        BytecodeContext bc = new BytecodeContext(optionalPS, constr, this, keys, cw, className, adapter, UDF_DEFAULT_VALUE, writeLog(), suppressWSbeforeArg, output, returnValue);
        cv = new ConditionVisitor();
        cv.visitBefore();
        int count = 0;
        for (int i = 0; i < functions.length; i += 10) {
            cv.visitWhenBeforeExpr();
            div = new DecisionIntVisitor();
            div.visitBegin();
            adapter.loadArg(1);
            div.visitLT();
            adapter.push(i + 10);
            div.visitEnd(bc);
            cv.visitWhenAfterExprBeforeBody(bc);
            adapter.visitVarInsn(Opcodes.ALOAD, 0);
            adapter.visitVarInsn(Opcodes.ALOAD, 1);
            adapter.visitVarInsn(Opcodes.ILOAD, 2);
            adapter.visitVarInsn(Opcodes.ILOAD, 3);
            adapter.visitVarInsn(Opcodes.ALOAD, 4);
            adapter.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, "udfDefaultValue" + (++count), "(Llucee/runtime/PageContext;IILjava/lang/Object;)Ljava/lang/Object;");
            // adapter.returnValue();
            adapter.visitInsn(Opcodes.ARETURN);
            cv.visitWhenAfterBody(bc);
        }
        cv.visitAfter(bc);
        adapter.visitInsn(Opcodes.ACONST_NULL);
        adapter.returnValue();
        adapter.endMethod();
        count = 0;
        Method innerDefaultValue;
        for (int i = 0; i < functions.length; i += 10) {
            innerDefaultValue = new Method("udfDefaultValue" + (++count), Types.OBJECT, new Type[] { Types.PAGE_CONTEXT, Types.INT_VALUE, Types.INT_VALUE, Types.OBJECT });
            adapter = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, innerDefaultValue, null, new Type[] { Types.PAGE_EXCEPTION }, cw);
            writeUdfDefaultValueInner(new BytecodeContext(optionalPS, constr, this, keys, cw, className, adapter, innerDefaultValue, writeLog(), suppressWSbeforeArg, output, returnValue), functions, i, i + 10 > functions.length ? functions.length : i + 10);
            adapter.loadArg(DEFAULT_VALUE);
            // adapter.visitInsn(Opcodes.ACONST_NULL);
            adapter.returnValue();
            adapter.endMethod();
        }
    }
    // CONSTRUCTOR
    List<Data> udfProperties = constr.getUDFProperties();
    Iterator<Data> it = udfProperties.iterator();
    String udfpropsClassName = Types.UDF_PROPERTIES_ARRAY.toString();
    // new UDFProperties Array
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
    constrAdapter.push(udfProperties.size());
    constrAdapter.newArray(Types.UDF_PROPERTIES);
    constrAdapter.visitFieldInsn(Opcodes.PUTFIELD, getClassName(), "udfs", udfpropsClassName);
    // set item
    Data data;
    while (it.hasNext()) {
        data = it.next();
        constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
        constrAdapter.visitFieldInsn(Opcodes.GETFIELD, constr.getClassName(), "udfs", Types.UDF_PROPERTIES_ARRAY.toString());
        constrAdapter.push(data.arrayIndex);
        data.function.createUDFProperties(constr, data.valueIndex, data.type);
        constrAdapter.visitInsn(Opcodes.AASTORE);
    }
    // setPageSource(pageSource);
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 0);
    constrAdapter.visitVarInsn(Opcodes.ALOAD, 1);
    constrAdapter.invokeVirtual(t, SET_PAGE_SOURCE);
    constrAdapter.returnValue();
    constrAdapter.endMethod();
    // INIT KEYS
    {
        GeneratorAdapter aInit = new GeneratorAdapter(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, INIT_KEYS, null, null, cw);
        BytecodeContext bcInit = new BytecodeContext(optionalPS, constr, this, keys, cw, className, aInit, INIT_KEYS, writeLog(), suppressWSbeforeArg, output, returnValue);
        registerFields(bcInit, keys);
        aInit.returnValue();
        aInit.endMethod();
    }
    // set field subs
    FieldVisitor fv = cw.visitField(Opcodes.ACC_PRIVATE + Opcodes.ACC_FINAL, "subs", "[Llucee/runtime/CIPage;", null, null);
    fv.visitEnd();
    // create sub components/interfaces
    if (comp != null && comp.isMain()) {
        List<TagCIObject> subs = getSubs(null);
        if (!ArrayUtil.isEmpty(subs)) {
            Iterator<TagCIObject> _it = subs.iterator();
            TagCIObject tc;
            while (_it.hasNext()) {
                tc = _it.next();
                tc.writeOut(this);
            }
            writeGetSubPages(cw, className, subs, sourceCode.getDialect());
        }
    }
    return cw.toByteArray();
}
Also used : InterfacePageImpl(lucee.runtime.InterfacePageImpl) PageSourceCode(lucee.transformer.util.PageSourceCode) ArrayList(java.util.ArrayList) TagThread(lucee.transformer.bytecode.statement.tag.TagThread) LitString(lucee.transformer.expression.literal.LitString) FieldVisitor(org.objectweb.asm.FieldVisitor) LitString(lucee.transformer.expression.literal.LitString) Function(lucee.transformer.bytecode.statement.udf.Function) IFunction(lucee.transformer.bytecode.statement.IFunction) ConditionVisitor(lucee.transformer.bytecode.visitor.ConditionVisitor) Iterator(java.util.Iterator) DecisionIntVisitor(lucee.transformer.bytecode.visitor.DecisionIntVisitor) TagCIObject(lucee.transformer.bytecode.statement.tag.TagCIObject) Data(lucee.transformer.bytecode.ConstrBytecodeContext.Data) Method(org.objectweb.asm.commons.Method) ClassWriter(org.objectweb.asm.ClassWriter) PageSource(lucee.runtime.PageSource) Type(org.objectweb.asm.Type) GeneratorAdapter(org.objectweb.asm.commons.GeneratorAdapter) ArrayVisitor(lucee.transformer.bytecode.visitor.ArrayVisitor)

Example 49 with PageSource

use of lucee.runtime.PageSource 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 50 with PageSource

use of lucee.runtime.PageSource in project Lucee by lucee.

the class AbstrCFMLScriptTransformer method funcStatement.

/**
 * Liest ein function Statement ein.
 * <br />
 * EBNF:<br />
 * <code>identifier spaces "(" spaces identifier spaces {"," spaces identifier spaces} ")" spaces block;</code>
 * @return function Statement
 * @throws TemplateException
 */
private final Statement funcStatement(ExprData data, Body parent) throws TemplateException {
    int pos = data.srcCode.getPos();
    // read 5 tokens (returntype,access modifier,"abstract|final|static","function", function name)
    String str = variableDec(data, false);
    // if there is no token at all we have no function
    if (str == null) {
        data.srcCode.setPos(pos);
        return null;
    }
    comments(data);
    String[] tokens = new String[] { str, null, null, null, null };
    tokens[1] = variableDec(data, false);
    comments(data);
    if (tokens[1] != null) {
        tokens[2] = variableDec(data, false);
        comments(data);
        if (tokens[2] != null) {
            tokens[3] = variableDec(data, false);
            comments(data);
            if (tokens[3] != null) {
                tokens[4] = identifier(data, false);
                comments(data);
            }
        }
    }
    // function name
    String functionName = null;
    for (int i = tokens.length - 1; i >= 0; i--) {
        // first from right is the function name
        if (tokens[i] != null) {
            functionName = tokens[i];
            tokens[i] = null;
            break;
        }
    }
    if (functionName == null || functionName.indexOf(',') != -1 || functionName.indexOf('[') != -1) {
        data.srcCode.setPos(pos);
        return null;
    }
    // throw new TemplateException(data.srcCode, "invalid syntax");
    String returnType = null;
    // search for "function"
    boolean hasOthers = false, first = true;
    for (int i = tokens.length - 1; i >= 0; i--) {
        if ("function".equalsIgnoreCase(tokens[i])) {
            // if it is the first "function" (from right) and we had already something else, the syntax is broken!
            if (hasOthers && first)
                throw new TemplateException(data.srcCode, "invalid syntax");
            else // we already have a return type,so this is the 3th "function"!
            if (returnType != null)
                throw new TemplateException(data.srcCode, "invalid syntax");
            else if (!first)
                returnType = tokens[i];
            first = false;
            tokens[i] = null;
        } else if (tokens[i] != null) {
            hasOthers = true;
        }
    }
    // no "function" found
    if (first) {
        data.srcCode.setPos(pos);
        return null;
    }
    // access modifier
    int _access, access = -1;
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i] != null && (_access = ComponentUtil.toIntAccess(tokens[i], -1)) != -1) {
            // we already have an access modifier
            if (access != -1) {
                // we already have a return type
                if (returnType != null)
                    throw new TemplateException(data.srcCode, "invalid syntax");
                returnType = tokens[i];
            } else
                access = _access;
            tokens[i] = null;
        }
    }
    // no access defined
    if (access == -1)
        access = Component.ACCESS_PUBLIC;
    // Non access modifier
    int _modifier, modifier = Component.MODIFIER_NONE;
    boolean isStatic = false;
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i] != null) {
            _modifier = ComponentUtil.toModifier(tokens[i], Component.MODIFIER_NONE, Component.MODIFIER_NONE);
            // abstract|final
            if (_modifier != Component.MODIFIER_NONE) {
                // we already have an Non access modifier
                if (modifier != Component.MODIFIER_NONE || isStatic) {
                    // we already have a return type
                    if (returnType != null)
                        throw new TemplateException(data.srcCode, "invalid syntax");
                    returnType = tokens[i];
                } else
                    modifier = _modifier;
                tokens[i] = null;
            } else // static
            if (tokens[i].equalsIgnoreCase("static")) {
                // we already have an Non access modifier
                if (modifier != Component.MODIFIER_NONE || isStatic) {
                    // we already have a return type
                    if (returnType != null)
                        throw new TemplateException(data.srcCode, "invalid syntax");
                    returnType = tokens[i];
                } else
                    isStatic = true;
                tokens[i] = null;
            }
        }
    }
    // return type
    for (int i = 0; i < tokens.length; i++) {
        if (tokens[i] != null) {
            if (returnType != null)
                throw new TemplateException(data.srcCode, "invalid syntax");
            returnType = tokens[i];
        }
    }
    Position line = data.srcCode.getPosition();
    // Name
    if (!data.isCFC && !data.isInterface) {
        FunctionLibFunction flf = getFLF(data, functionName);
        try {
            if (flf != null && flf.getFunctionClassDefinition().getClazz() != CFFunction.class) {
                PageSource ps = null;
                if (data.srcCode instanceof PageSourceCode) {
                    ps = ((PageSourceCode) data.srcCode).getPageSource();
                }
                String path = null;
                if (ps != null) {
                    path = ps.getDisplayPath();
                    path = path.replace('\\', '/');
                }
                if (// TODO make better
                path == null || path.indexOf("/library/function/") == -1)
                    throw new TemplateException(data.srcCode, "The name [" + functionName + "] is already used by a built in Function");
            }
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            throw new PageRuntimeException(Caster.toPageException(t));
        }
    }
    Function res = closurePart(data, functionName, access, modifier, returnType, line, false);
    if (isStatic) {
        if (data.context == CTX_INTERFACE)
            throw new TemplateException(data.srcCode, "static functions are not allowed within the interface body");
        TagOther tag = createStaticTag(data, res.getStart());
        tag.getBody().addStatement(res);
        return tag;
    }
    return res;
}
Also used : CFFunction(lucee.runtime.functions.system.CFFunction) PageSourceCode(lucee.transformer.util.PageSourceCode) TemplateException(lucee.runtime.exp.TemplateException) Position(lucee.transformer.Position) TagOther(lucee.transformer.bytecode.statement.tag.TagOther) PageSource(lucee.runtime.PageSource) Function(lucee.transformer.bytecode.statement.udf.Function) CFFunction(lucee.runtime.functions.system.CFFunction) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) FunctionLibFunction(lucee.transformer.library.function.FunctionLibFunction) PageRuntimeException(lucee.runtime.exp.PageRuntimeException)

Aggregations

PageSource (lucee.runtime.PageSource)59 PageContextImpl (lucee.runtime.PageContextImpl)19 Resource (lucee.commons.io.res.Resource)16 Struct (lucee.runtime.type.Struct)10 ConfigWebImpl (lucee.runtime.config.ConfigWebImpl)8 Mapping (lucee.runtime.Mapping)7 PageException (lucee.runtime.exp.PageException)7 StructImpl (lucee.runtime.type.StructImpl)7 ConfigImpl (lucee.runtime.config.ConfigImpl)6 ConfigWeb (lucee.runtime.config.ConfigWeb)6 ExpressionException (lucee.runtime.exp.ExpressionException)6 ArrayList (java.util.ArrayList)5 Component (lucee.runtime.Component)5 PageSourceCode (lucee.transformer.util.PageSourceCode)5 IOException (java.io.IOException)4 MissingIncludeException (lucee.runtime.exp.MissingIncludeException)4 Array (lucee.runtime.type.Array)4 LitString (lucee.transformer.expression.literal.LitString)4 HTTPResource (lucee.commons.io.res.type.http.HTTPResource)3 Entry (java.util.Map.Entry)2