Search in sources :

Example 1 with ZipReader

use of com.laytonsmith.PureUtilities.ZipReader in project CommandHelper by EngineHub.

the class SiteDeploy method deployResources.

/**
 * Pages deployed: All pages under the siteDeploy/resources folder. They are transferred as is, including
 * recursively looking through the folder structure. Binary files are also supported. index.js (after template
 * replacement)
 */
private void deployResources() {
    generateQueue.submit(new Runnable() {

        @Override
        public void run() {
            try {
                File root = new File(SiteDeploy.class.getResource("/siteDeploy/resources").toExternalForm());
                ZipReader reader = new ZipReader(root);
                Queue<File> q = new LinkedList<>();
                q.addAll(Arrays.asList(reader.listFiles()));
                while (q.peek() != null) {
                    ZipReader r = new ZipReader(q.poll());
                    if (r.isDirectory()) {
                        q.addAll(Arrays.asList(r.listFiles()));
                    } else {
                        String fileName = r.getFile().getAbsolutePath().replaceFirst(Pattern.quote(reader.getFile().getAbsolutePath()), "");
                        writeFromStream(r.getInputStream(), "resources" + fileName);
                    }
                }
            } catch (IOException ex) {
                Logger.getLogger(SiteDeploy.class.getName()).log(Level.SEVERE, null, ex);
            }
            String index_js = StreamUtils.GetString(SiteDeploy.class.getResourceAsStream("/siteDeploy/index.js"));
            try {
                writeFromString(DocGenTemplates.DoTemplateReplacement(index_js, getStandardGenerators()), "resources/js/index.js");
            } catch (Generator.GenerateException ex) {
                Logger.getLogger(SiteDeploy.class.getName()).log(Level.SEVERE, "GenerateException in /siteDeploy/index.js", ex);
            }
            currentGenerateTask.addAndGet(1);
        }
    });
    totalGenerateTasks.addAndGet(1);
}
Also used : ZipReader(com.laytonsmith.PureUtilities.ZipReader) IOException(java.io.IOException) GenerateException(com.laytonsmith.tools.docgen.DocGenTemplates.Generator.GenerateException) File(java.io.File) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Queue(java.util.Queue)

Example 2 with ZipReader

use of com.laytonsmith.PureUtilities.ZipReader in project CommandHelper by EngineHub.

the class ClassDiscoveryCache method getURLCache.

/**
 * Given a file location, retrieves the ClassDiscoveryURLCache from it. If it is a jar, the file is hashed, and
 * checked for a local cache copy, and if one exists, that cache is returned. If not, the jar is scanned for a
 * jarInfo.ser. If one exists, it is returned. Otherwise, the jar is scanned, a local cache is saved to disk, then
 * returned.
 *
 * No exceptions will be thrown from this class, if something fails, it will fall back to ultimately just
 * regenerating the cache from source.
 *
 * @param fromClassLocation The jar to be cached. Note that if this doesn't denote a jar, the cache will not be
 * written to disk, however a URLCache will be returned none-the-less.
 * @return
 */
public ClassDiscoveryURLCache getURLCache(URL fromClassLocation) {
    if (fromClassLocation.toString().endsWith(".jar")) {
        File cacheOutputName = null;
        try {
            File jarFile = new File(URLDecoder.decode(fromClassLocation.getFile(), "UTF8"));
            byte[] data;
            try (FileInputStream fis = new FileInputStream(jarFile)) {
                data = new byte[READ_SIZE];
                fis.read(data);
            }
            MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
            digest.update(data);
            String fileName = StringUtils.toHex(digest.digest());
            cacheOutputName = new File(cacheDir, fileName);
            if (cacheOutputName.exists()) {
                // Cool, already exists, so we'll just return this.
                // Note that we write the data out as a zip, since it is
                // huge otherwise, and compresses quite well, so we have
                // to read it in as a zip now.
                ZipReader cacheReader = new ZipReader(new File(cacheOutputName, "data"));
                return new ClassDiscoveryURLCache(fromClassLocation, cacheReader.getInputStream());
            }
        // Doesn't exist, but we set cacheOutputName, so it will save it there
        // after it scans.
        } catch (Exception ex) {
        // Hmm. Ok, well, we'll just regenerate.
        }
        JarFile jfile;
        try {
            jfile = new JarFile(URLDecoder.decode(fromClassLocation.getFile(), "UTF8"));
            for (Enumeration<JarEntry> ent = jfile.entries(); ent.hasMoreElements(); ) {
                JarEntry entry = ent.nextElement();
                if (entry.getName().equals(ClassDiscoveryCache.OUTPUT_FILENAME)) {
                    InputStream is = jfile.getInputStream(entry);
                    try {
                        return new ClassDiscoveryURLCache(fromClassLocation, is);
                    } catch (Exception ex) {
                    // Do nothing, we'll just re-load from disk.
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(ClassDiscoveryCache.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (logger != null) {
            logger.log(Level.INFO, "Performing one time scan of {0}, this may take a few moments.", fromClassLocation);
        }
        ClassDiscoveryURLCache cache = new ClassDiscoveryURLCache(fromClassLocation, progress);
        if (cacheOutputName != null) {
            try {
                try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(cacheOutputName, false))) {
                    zos.putNextEntry(new ZipEntry("data"));
                    cache.writeDescriptor(zos);
                }
            } catch (IOException ex) {
                // Well, we couldn't write it out, so report the error, but continue anyways.
                if (logger != null) {
                    logger.log(Level.SEVERE, null, ex);
                } else {
                    // Report errors even if the logger passed in is null.
                    Logger.getLogger(ClassDiscoveryCache.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return cache;
    } else {
        return new ClassDiscoveryURLCache(fromClassLocation, progress);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) ZipReader(com.laytonsmith.PureUtilities.ZipReader) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) MessageDigest(java.security.MessageDigest) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 3 with ZipReader

use of com.laytonsmith.PureUtilities.ZipReader 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)

Example 4 with ZipReader

use of com.laytonsmith.PureUtilities.ZipReader in project CommandHelper by EngineHub.

the class DocGenUIHandler method getTemplateCount.

private int getTemplateCount() throws IOException {
    try {
        int count = 0;
        ZipReader reader = new ZipReader(new File(DocGenUIHandler.class.getResource("/docs").toURI()));
        Queue<File> q = new LinkedList<>();
        q.addAll(Arrays.asList(reader.listFiles()));
        while (q.peek() != null) {
            ZipReader r = new ZipReader(q.poll());
            if (r.isDirectory()) {
                q.addAll(Arrays.asList(r.listFiles()));
            } else {
                count++;
            }
        }
        return count;
    } catch (URISyntaxException ex) {
        throw new APIException(ex);
    }
}
Also used : ZipReader(com.laytonsmith.PureUtilities.ZipReader) URISyntaxException(java.net.URISyntaxException) File(java.io.File) LinkedList(java.util.LinkedList)

Example 5 with ZipReader

use of com.laytonsmith.PureUtilities.ZipReader in project CommandHelper by EngineHub.

the class DocGenUIHandler method doTemplates.

private void doTemplates() throws IOException, XPathExpressionException {
    try {
        File root = new File(DocGenUIHandler.class.getResource("/docs").toURI());
        ZipReader reader = new ZipReader(root);
        Queue<File> q = new LinkedList<>();
        q.addAll(Arrays.asList(reader.listFiles()));
        while (q.peek() != null) {
            ZipReader r = new ZipReader(q.poll());
            if (r.isDirectory()) {
                q.addAll(Arrays.asList(r.listFiles()));
            } else {
                String articleName = "/" + r.getFile().getName();
                doUpload(DocGen.Template(r.getFile().getName(), isStaged), articleName, true);
            }
        }
    } catch (URISyntaxException ex) {
        Logger.getLogger(DocGenUIHandler.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : ZipReader(com.laytonsmith.PureUtilities.ZipReader) URISyntaxException(java.net.URISyntaxException) File(java.io.File) LinkedList(java.util.LinkedList)

Aggregations

ZipReader (com.laytonsmith.PureUtilities.ZipReader)8 File (java.io.File)7 IOException (java.io.IOException)5 ConfigCompileException (com.laytonsmith.core.exceptions.ConfigCompileException)2 ConfigCompileGroupException (com.laytonsmith.core.exceptions.ConfigCompileGroupException)2 URISyntaxException (java.net.URISyntaxException)2 LinkedList (java.util.LinkedList)2 SimpleVersion (com.laytonsmith.PureUtilities.SimpleVersion)1 ParseTree (com.laytonsmith.core.ParseTree)1 CString (com.laytonsmith.core.constructs.CString)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 ConfigRuntimeException (com.laytonsmith.core.exceptions.ConfigRuntimeException)1 GenerateException (com.laytonsmith.tools.docgen.DocGenTemplates.Generator.GenerateException)1 HeadlessException (java.awt.HeadlessException)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1