Search in sources :

Example 6 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class CreatorOperationsImpl method writeAssemblyFile.

private void writeAssemblyFile(JavaPackage topLevelPackage, String templateName, String destinationFolder) {
    final InputStream templateInputStream = FileUtils.getInputStream(getClass(), "xml/" + templateName);
    final Document assemblyDoc = XmlUtils.readXml(templateInputStream);
    final Element root = assemblyDoc.getDocumentElement();
    String projectFolder = topLevelPackage.getFullyQualifiedPackageName().replaceAll("\\.", "/");
    XmlUtils.findRequiredElement("/assembly/moduleSets/moduleSet/binaries/outputDirectory", root).setTextContent(projectFolder + "/${module.artifactId}/${module.version}");
    // Add includes
    Element includes = XmlUtils.findFirstElement("moduleSets/moduleSet/includes", root);
    if (includes != null) {
        // Adding addon-advanced include
        Element includeAdvancedElement = assemblyDoc.createElement("include");
        includeAdvancedElement.setTextContent(topLevelPackage.getFullyQualifiedPackageName() + ":" + topLevelPackage.getFullyQualifiedPackageName() + ".addon-advanced");
        // Adding addon-simple include
        Element includeSimpleElement = assemblyDoc.createElement("include");
        includeSimpleElement.setTextContent(topLevelPackage.getFullyQualifiedPackageName() + ":" + topLevelPackage.getFullyQualifiedPackageName() + ".addon-simple");
        // Adding roo-addon-suite include
        Element includeSuiteElement = assemblyDoc.createElement("include");
        includeSuiteElement.setTextContent(topLevelPackage.getFullyQualifiedPackageName() + ":" + topLevelPackage.getFullyQualifiedPackageName() + ".roo-addon-suite");
        includes.appendChild(includeAdvancedElement);
        includes.appendChild(includeSimpleElement);
        includes.appendChild(includeSuiteElement);
    }
    LogicalPath rootPath = LogicalPath.getInstance(Path.ROOT, "");
    MutableFile assemblyFile = fileManager.createFile(pathResolver.getIdentifier(rootPath, "src/main/assembly/repo-assembly.xml"));
    XmlUtils.writeXml(assemblyFile.getOutputStream(), assemblyDoc);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) LogicalPath(org.springframework.roo.project.LogicalPath) Document(org.w3c.dom.Document) MutableFile(org.springframework.roo.process.manager.MutableFile)

Example 7 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class CreatorOperationsImpl method installFlagGraphic.

private void installFlagGraphic(final Locale locale, final String packagePath) {
    boolean success = false;
    final String countryCode = locale.getCountry().toLowerCase();
    // Retrieve the icon file:
    BufferedInputStream bis = null;
    ZipInputStream zis = null;
    try {
        bis = new BufferedInputStream(httpService.openConnection(new URL(iconSetUrl)));
        zis = new ZipInputStream(bis);
        ZipEntry entry;
        final String expectedEntryName = "png/" + countryCode + ".png";
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getName().equals(expectedEntryName)) {
                final MutableFile target = fileManager.createFile(pathResolver.getFocusedIdentifier(Path.SRC_MAIN_RESOURCES, packagePath + "/" + countryCode + ".png"));
                OutputStream outputStream = null;
                try {
                    outputStream = target.getOutputStream();
                    IOUtils.copy(zis, outputStream);
                    success = true;
                } finally {
                    IOUtils.closeQuietly(outputStream);
                }
            }
        }
    } catch (final IOException e) {
        throw new IllegalStateException(getErrorMsg(locale.getCountry()), e);
    } finally {
        IOUtils.closeQuietly(bis);
        IOUtils.closeQuietly(zis);
    }
    if (!success) {
        throw new IllegalStateException(getErrorMsg(locale.toString()));
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipEntry(java.util.zip.ZipEntry) OutputStream(java.io.OutputStream) IOException(java.io.IOException) URL(java.net.URL) MutableFile(org.springframework.roo.process.manager.MutableFile)

Example 8 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class CreatorOperationsImpl method writeTextFile.

private void writeTextFile(final String fullPathFromRoot, final String message, String folder) {
    Validate.notBlank(fullPathFromRoot, "Text file name to write is required");
    Validate.notBlank(message, "Message required");
    MutableFile mutableFile;
    if (folder != null) {
        LogicalPath rootPath = LogicalPath.getInstance(Path.ROOT, "");
        mutableFile = fileManager.createFile(pathResolver.getIdentifier(rootPath, folder + "/" + fullPathFromRoot));
    } else {
        LogicalPath rootPath = LogicalPath.getInstance(Path.ROOT, "");
        mutableFile = fileManager.createFile(pathResolver.getIdentifier(rootPath, fullPathFromRoot));
    }
    OutputStream outputStream = null;
    try {
        outputStream = mutableFile.getOutputStream();
        IOUtils.write(message, outputStream);
    } catch (final IOException ioe) {
        throw new IllegalStateException(ioe);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
}
Also used : OutputStream(java.io.OutputStream) LogicalPath(org.springframework.roo.project.LogicalPath) IOException(java.io.IOException) MutableFile(org.springframework.roo.process.manager.MutableFile)

Example 9 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class CreatorOperationsImpl method install.

private void install(final String targetFilename, final JavaPackage topLevelPackage, final Path path, final Type type, String projectName, String folder) {
    if (StringUtils.isBlank(projectName)) {
        projectName = topLevelPackage.getFullyQualifiedPackageName().replace(".", "-");
    }
    final String topLevelPackageName = topLevelPackage.getFullyQualifiedPackageName();
    final String packagePath = topLevelPackageName.replace('.', separatorChar);
    String destinationFile = "";
    if (targetFilename.endsWith(".java")) {
        destinationFile = path.getDefaultLocation() + "/" + packagePath + separatorChar + StringUtils.capitalize(topLevelPackageName.substring(topLevelPackageName.lastIndexOf(".") + 1)) + targetFilename;
    } else {
        destinationFile = path.getDefaultLocation() + "/" + packagePath + separatorChar + targetFilename;
    }
    // Adjust name for Roo Annotation
    if (targetFilename.startsWith("RooAnnotation")) {
        destinationFile = path.getDefaultLocation() + "/" + packagePath + separatorChar + "Roo" + StringUtils.capitalize(topLevelPackageName.substring(topLevelPackageName.lastIndexOf(".") + 1)) + ".java";
    }
    if (!fileManager.exists(destinationFile)) {
        final InputStream templateInputStream = FileUtils.getInputStream(getClass(), type.name().toLowerCase() + "/" + targetFilename + "-template");
        OutputStream outputStream = null;
        try {
            // Read template and insert the user's package
            String input = IOUtils.toString(templateInputStream);
            input = input.replace("__TOP_LEVEL_PACKAGE__", topLevelPackage.getFullyQualifiedPackageName());
            input = input.replace("__APP_NAME__", StringUtils.capitalize(topLevelPackageName.substring(topLevelPackageName.lastIndexOf(".") + 1)));
            input = input.replace("__APP_NAME_LWR_CASE__", topLevelPackageName.substring(topLevelPackageName.lastIndexOf(".") + 1).toLowerCase());
            input = input.replace("__PROJECT_NAME__", projectName.toLowerCase());
            // Output the file for the user
            MutableFile mutableFile;
            if (folder != null) {
                LogicalPath rootPath = LogicalPath.getInstance(Path.ROOT, "");
                mutableFile = fileManager.createFile(pathResolver.getIdentifier(rootPath, folder + "/" + destinationFile));
            } else {
                LogicalPath rootPath = LogicalPath.getInstance(Path.ROOT, "");
                mutableFile = fileManager.createFile(pathResolver.getIdentifier(rootPath, destinationFile));
            }
            outputStream = mutableFile.getOutputStream();
            IOUtils.write(input, outputStream);
        } catch (final IOException ioe) {
            throw new IllegalStateException("Unable to create '" + targetFilename + "'", ioe);
        } finally {
            IOUtils.closeQuietly(templateInputStream);
            IOUtils.closeQuietly(outputStream);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) LogicalPath(org.springframework.roo.project.LogicalPath) IOException(java.io.IOException) MutableFile(org.springframework.roo.process.manager.MutableFile)

Example 10 with MutableFile

use of org.springframework.roo.process.manager.MutableFile in project spring-roo by spring-projects.

the class CreatorOperationsImpl method createI18nAddon.

public void createI18nAddon(final JavaPackage topLevelPackage, String language, final Locale locale, final File messageBundle, final File flagGraphic, String description, final String projectName) {
    Validate.notNull(topLevelPackage, "Top Level Package required");
    Validate.notNull(locale, "Locale required");
    Validate.notNull(messageBundle, "Message Bundle required");
    if (StringUtils.isBlank(language)) {
        language = "";
        final InputStream inputStream = FileUtils.getInputStream(getClass(), Type.I18N.name().toLowerCase() + "/iso3166.txt");
        try {
            for (String line : IOUtils.readLines(inputStream)) {
                final String[] split = line.split(";");
                if (split[1].startsWith(locale.getCountry().toUpperCase())) {
                    if (split[0].contains(",")) {
                        split[0] = split[0].substring(0, split[0].indexOf(",") - 1);
                    }
                    final String[] langWords = split[0].split("\\s");
                    final StringBuilder b = new StringBuilder();
                    for (final String word : langWords) {
                        b.append(StringUtils.capitalize(word.toLowerCase())).append(" ");
                    }
                    language = b.toString().substring(0, b.length() - 1);
                }
            }
        } catch (final IOException e) {
            throw new IllegalStateException("Could not parse ISO 3166 language list, please use --language option in command");
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }
    final String[] langWords = language.split("\\s");
    final StringBuilder builder = new StringBuilder();
    for (final String word : langWords) {
        builder.append(StringUtils.capitalize(word.toLowerCase()));
    }
    final String languageName = builder.toString();
    final String packagePath = topLevelPackage.getFullyQualifiedPackageName().replace('.', separatorChar);
    if (StringUtils.isBlank(description)) {
        description = languageName + " language support for Spring Roo Web MVC JSP Scaffolding";
    }
    if (!description.contains("#mvc") || !description.contains("#localization") || !description.contains("locale:")) {
        description = description + "; #mvc,#localization,locale:" + locale.getCountry().toLowerCase();
    }
    createProject(topLevelPackage, Type.I18N, description, projectName, null);
    OutputStream outputStream = null;
    try {
        outputStream = fileManager.createFile(pathResolver.getFocusedIdentifier(Path.SRC_MAIN_RESOURCES, packagePath + separatorChar + messageBundle.getName())).getOutputStream();
        org.apache.commons.io.FileUtils.copyFile(messageBundle, outputStream);
        if (flagGraphic != null) {
            outputStream = fileManager.createFile(pathResolver.getFocusedIdentifier(Path.SRC_MAIN_RESOURCES, packagePath + separatorChar + flagGraphic.getName())).getOutputStream();
            org.apache.commons.io.FileUtils.copyFile(flagGraphic, outputStream);
        } else {
            installFlagGraphic(locale, packagePath);
        }
    } catch (final IOException e) {
        throw new IllegalStateException("Could not copy addon resources into project", e);
    } finally {
        IOUtils.closeQuietly(outputStream);
    }
    final String destinationFile = pathResolver.getFocusedIdentifier(Path.SRC_MAIN_JAVA, packagePath + separatorChar + languageName + "Language.java");
    if (!fileManager.exists(destinationFile)) {
        final InputStream templateInputStream = FileUtils.getInputStream(getClass(), Type.I18N.name().toLowerCase() + "/Language.java-template");
        try {
            // Read template and insert the user's package
            String input = IOUtils.toString(templateInputStream);
            input = input.replace("__TOP_LEVEL_PACKAGE__", topLevelPackage.getFullyQualifiedPackageName());
            input = input.replace("__APP_NAME__", languageName);
            input = input.replace("__LOCALE__", locale.getLanguage());
            input = input.replace("__LANGUAGE__", StringUtils.capitalize(language));
            if (flagGraphic != null) {
                input = input.replace("__FLAG_FILE__", flagGraphic.getName());
            } else {
                input = input.replace("__FLAG_FILE__", locale.getCountry().toLowerCase() + ".png");
            }
            input = input.replace("__MESSAGE_BUNDLE__", messageBundle.getName());
            // Output the file for the user
            final MutableFile mutableFile = fileManager.createFile(destinationFile);
            outputStream = mutableFile.getOutputStream();
            IOUtils.write(input, outputStream);
        } catch (final IOException ioe) {
            throw new IllegalStateException("Unable to create '" + languageName + "Language.java'", ioe);
        } finally {
            IOUtils.closeQuietly(templateInputStream);
            IOUtils.closeQuietly(outputStream);
        }
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) IOException(java.io.IOException) MutableFile(org.springframework.roo.process.manager.MutableFile)

Aggregations

MutableFile (org.springframework.roo.process.manager.MutableFile)17 IOException (java.io.IOException)10 OutputStream (java.io.OutputStream)8 Date (java.util.Date)6 Properties (java.util.Properties)6 File (java.io.File)5 InputStream (java.io.InputStream)5 BufferedInputStream (java.io.BufferedInputStream)4 ZipInputStream (java.util.zip.ZipInputStream)4 LogicalPath (org.springframework.roo.project.LogicalPath)4 CreateFile (org.springframework.roo.file.undo.CreateFile)3 DeleteFile (org.springframework.roo.file.undo.DeleteFile)3 UpdateFile (org.springframework.roo.file.undo.UpdateFile)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 URL (java.net.URL)1 DateFormat (java.text.DateFormat)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1