Search in sources :

Example 1 with CREIncludeException

use of com.laytonsmith.core.exceptions.CRE.CREIncludeException in project CommandHelper by EngineHub.

the class IncludeCache method get.

public static ParseTree get(File file, Target t) {
    CHLog.GetLogger().Log(TAG, LogLevel.DEBUG, "Loading " + file, t);
    if (cache.containsKey(file)) {
        CHLog.GetLogger().Log(TAG, LogLevel.INFO, "Returning " + file + " from cache", t);
        return cache.get(file);
    }
    CHLog.GetLogger().Log(TAG, LogLevel.VERBOSE, "Cache does not already contain file. Compiling and caching.", t);
    // We have to pull the file from the FS, and compile it.
    if (!Security.CheckSecurity(file)) {
        throw new CRESecurityException("The script cannot access " + file + " due to restrictions imposed by the base-dir setting.", t);
    }
    CHLog.GetLogger().Log(TAG, LogLevel.VERBOSE, "Security check passed", t);
    try {
        String s = new ZipReader(file).getFileContents();
        ParseTree tree = MethodScriptCompiler.compile(MethodScriptCompiler.lex(s, file, true));
        CHLog.GetLogger().Log(TAG, LogLevel.VERBOSE, "Compilation succeeded, adding to cache.", t);
        IncludeCache.add(file, tree);
        return tree;
    } catch (ConfigCompileException ex) {
        throw new CREIncludeException("There was a compile error when trying to include the script at " + file + "\n" + ex.getMessage() + " :: " + file.getName() + ":" + ex.getLineNum(), t);
    } catch (ConfigCompileGroupException ex) {
        StringBuilder b = new StringBuilder();
        b.append("There were compile errors when trying to include the script at ").append(file).append("\n");
        for (ConfigCompileException e : ex.getList()) {
            b.append(e.getMessage()).append(" :: ").append(e.getFile().getName()).append(":").append(e.getLineNum());
        }
        throw new CREIncludeException(b.toString(), t);
    } catch (IOException ex) {
        throw new CREIOException("The script at " + file + " could not be found or read in.", t);
    }
}
Also used : CREIncludeException(com.laytonsmith.core.exceptions.CRE.CREIncludeException) ZipReader(com.laytonsmith.PureUtilities.ZipReader) IOException(java.io.IOException) CREIOException(com.laytonsmith.core.exceptions.CRE.CREIOException) CREIOException(com.laytonsmith.core.exceptions.CRE.CREIOException) ConfigCompileGroupException(com.laytonsmith.core.exceptions.ConfigCompileGroupException) ConfigCompileException(com.laytonsmith.core.exceptions.ConfigCompileException) ParseTree(com.laytonsmith.core.ParseTree) CRESecurityException(com.laytonsmith.core.exceptions.CRE.CRESecurityException)

Aggregations

ZipReader (com.laytonsmith.PureUtilities.ZipReader)1 ParseTree (com.laytonsmith.core.ParseTree)1 CREIOException (com.laytonsmith.core.exceptions.CRE.CREIOException)1 CREIncludeException (com.laytonsmith.core.exceptions.CRE.CREIncludeException)1 CRESecurityException (com.laytonsmith.core.exceptions.CRE.CRESecurityException)1 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)1 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)1 IOException (java.io.IOException)1