Search in sources :

Example 1 with ResourceBundle

use of com.twinsoft.convertigo.engine.MinificationManager.ResourceBundle in project convertigo by convertigo.

the class MobileResourceHelper method prepareFiles.

public void prepareFiles(FileFilter fileFilterForCopy) throws ServiceException {
    try {
        String endPoint = this.endpoint + "/projects/" + project.getName();
        String applicationID = mobileApplication.getComputedApplicationId();
        if (!endPoint.endsWith("/")) {
            endPoint += "/";
        }
        // Check forbidden characters in application ID (a-zA-Z0-9.-)
        if (!Pattern.matches("[\\.\\w]*", applicationID)) {
            throw new ServiceException("The application ID is not valid: '" + applicationID + "'.\nThe only valid characters are upper and lower letters (A-Z, a-z), " + "digits (0-9), a period (.) and a hyphen (-).");
        }
        // Delete no existing files
        FileUtils.deleteQuietly(destDir);
        for (File directory : mobileDir) {
            currentMobileDir = directory;
            if (directory.exists()) {
                FileUtils.copyDirectory(directory, destDir, fileFilterForCopy, true);
            }
        }
        currentMobileDir = null;
        List<File> filesToDelete = new LinkedList<File>();
        for (File htmlFile : FileUtils.listFiles(destDir, new String[] { "html" }, true)) {
            String htmlContent = FileUtils.readFileToString(htmlFile, StandardCharsets.UTF_8);
            StringBuffer sbIndexHtml = new StringBuffer();
            BufferedReader br = new BufferedReader(new StringReader(htmlContent));
            String includeChar = null;
            String includeBuf = null;
            for (String line = br.readLine(); line != null; line = br.readLine()) {
                if (!line.contains("<!--")) {
                    if (includeChar == null && line.contains("<base ")) {
                        line = "  <base href=\"./\">";
                    } else if (includeChar == null && line.contains("\"../../../../")) {
                        String file = line.replaceFirst(".*\"\\.\\./\\.\\./\\.\\./\\.\\./(.*?)\".*", "$1");
                        if (file.endsWith("c8o.cordova.js")) {
                            file = file.replace("c8o.cordova.js", "c8o.cordova.device.js");
                        }
                        File inFile = new File(Engine.WEBAPP_PATH + "/f" + file);
                        try (InputStream is = inFile.exists() ? new FileInputStream(inFile) : getClass().getResourceAsStream("res/" + inFile.getName())) {
                            if (is != null) {
                                file = file.replace("scripts/", "js/");
                                File outFile = new File(destDir, file);
                                if (!outFile.exists()) {
                                    FileUtils.copyInputStreamToFile(is, outFile);
                                    if (!inFile.exists()) {
                                        outFile.setLastModified(0);
                                    }
                                }
                                line = line.replaceFirst("\"\\.\\./\\.\\./\\.\\./\\.\\./.*?\"", "\"" + file + "\"");
                                if (inFile.exists()) {
                                    handleJQMcssFolder(inFile, outFile);
                                }
                                if (file.matches(".*/jquery\\.mobilelib\\..*?js")) {
                                    String sJs = FileUtils.readFileToString(outFile, StandardCharsets.UTF_8);
                                    sJs = sJs.replaceAll(Pattern.quote("url : \"../../\""), "url : \"" + endPoint + "\"");
                                    writeStringToFile(outFile, sJs);
                                } else if (file.matches(".*/c8o\\.core\\..*?js")) {
                                    String sJs = FileUtils.readFileToString(outFile, StandardCharsets.UTF_8);
                                    sJs = sJs.replaceAll(Pattern.quote("endpoint_url: \"\""), "endpoint_url: \"" + endPoint + "\"");
                                    writeStringToFile(outFile, sJs);
                                }
                                if (file.matches(".*/flashupdate_.*?\\.css")) {
                                    if (inFile.exists()) {
                                        FileUtils.copyDirectory(new File(inFile.getParentFile(), "flashupdate_fonts"), new File(outFile.getParentFile(), "flashupdate_fonts"), defaultFilter, true);
                                        FileUtils.copyDirectory(new File(inFile.getParentFile(), "flashupdate_images"), new File(outFile.getParentFile(), "flashupdate_images"), defaultFilter, true);
                                    } else {
                                        for (String path : Arrays.asList("flashupdate_fonts/fontfaceROBOTO.css", "flashupdate_fonts/GoogleAndroidLicense.txt", "flashupdate_fonts/Roboto-Bold-webfont.eot", "flashupdate_fonts/Roboto-Bold-webfont.svg", "flashupdate_fonts/Roboto-Bold-webfont.ttf", "flashupdate_fonts/Roboto-Bold-webfont.woff", "flashupdate_fonts/Roboto-Regular-webfont.eot", "flashupdate_fonts/Roboto-Regular-webfont.svg", "flashupdate_fonts/Roboto-Regular-webfont.ttf", "flashupdate_fonts/Roboto-Regular-webfont.woff", "flashupdate_images/bg_ios.jpg")) {
                                            File outRes = new File(outFile.getParentFile(), path);
                                            FileUtils.copyInputStreamToFile(getClass().getResourceAsStream("res/" + path), outRes);
                                            outRes.setLastModified(0);
                                        }
                                    }
                                }
                            }
                        }
                    } else {
                        /**
                         * Handle multilines <script> and <link> urls for the resourceCompressorManager
                         */
                        if (includeChar == null) {
                            Matcher mStart = Pattern.compile("(?:(?:<script .*?src)|(?:<link .*?href))\\s*=\\s*(\"|')(.*?)(\\1|$)").matcher(line);
                            if (mStart.find()) {
                                String end = mStart.group(3);
                                if (end.length() == 0) {
                                    includeChar = mStart.group(1);
                                }
                                includeBuf = mStart.group(2);
                            } else {
                                includeBuf = null;
                            }
                        } else {
                            int index = line.indexOf(includeChar);
                            if (index != -1) {
                                includeBuf += line.substring(0, index);
                                includeChar = null;
                            } else {
                                includeBuf += line;
                            }
                        }
                        if (includeChar == null && includeBuf != null) {
                            String uri = includeBuf;
                            if (htmlFile.getAbsolutePath().startsWith(projectDir.getAbsolutePath())) {
                                uri = htmlFile.getParentFile().getAbsolutePath().substring(projectDir.getParentFile().getAbsolutePath().length() + 1) + "/" + uri;
                            }
                            ResourceBundle resourceBundle = Engine.theApp.minificationManager != null ? Engine.theApp.minificationManager.process(uri) : null;
                            if (resourceBundle != null) {
                                synchronized (resourceBundle) {
                                    String prepend = "";
                                    for (File file : resourceBundle.getFiles()) {
                                        String filename = file.getName();
                                        if (filename.matches("c8o\\.core\\..*?js")) {
                                            prepend += "C8O.vars.endpoint_url=\"" + endPoint + "\";";
                                        } else if (handleJQMcssFolder(file, resourceBundle.getVirtualFile())) {
                                        }
                                    }
                                    if (prepend.isEmpty()) {
                                        resourceBundle.writeFile();
                                    } else {
                                        resourceBundle.writeFile(prepend);
                                    }
                                    for (File file : resourceBundle.getFiles()) {
                                        if (file.getPath().indexOf(projectDir.getPath()) == 0) {
                                            filesToDelete.add(file);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                sbIndexHtml.append(line + "\n");
            }
            htmlContent = sbIndexHtml.toString();
            writeStringToFile(htmlFile, htmlContent);
        }
        for (File file : filesToDelete) {
            FileUtils.deleteQuietly(file);
        }
        long latestFile = 0;
        File lastFile = null;
        for (File file : FileUtils.listFiles(destDir, TrueFileFilter.INSTANCE, FileFilterUtils.notFileFilter(FileFilterUtils.nameFileFilter("res")))) {
            if (!file.getName().equals("config.xml") && !file.getName().equals("env.json")) {
                long curFile = file.lastModified();
                if (latestFile < curFile) {
                    latestFile = curFile;
                    lastFile = file;
                }
            }
        }
        Engine.logEngine.info("(MobileResourceHelper) prepareFiles, lastestFile is '" + latestFile + "' for " + lastFile);
        destDir.setLastModified(latestFile);
    } catch (ServiceException e) {
        throw e;
    } catch (Exception e) {
        throw new ServiceException(e.getClass().getName(), e);
    }
}
Also used : Matcher(java.util.regex.Matcher) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) LinkedList(java.util.LinkedList) FileInputStream(java.io.FileInputStream) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) EngineException(com.twinsoft.convertigo.engine.EngineException) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) ResourceBundle(com.twinsoft.convertigo.engine.MinificationManager.ResourceBundle) File(java.io.File)

Aggregations

EngineException (com.twinsoft.convertigo.engine.EngineException)1 ResourceBundle (com.twinsoft.convertigo.engine.MinificationManager.ResourceBundle)1 ServiceException (com.twinsoft.convertigo.engine.admin.services.ServiceException)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 LinkedList (java.util.LinkedList)1 Matcher (java.util.regex.Matcher)1 JSONException (org.codehaus.jettison.json.JSONException)1