static void convert()

in src/main/java/org/apache/sling/maven/slingstart/FeatureModelConverter.java [129:221]


    static void convert(List<FeatureFileEntry> files, MavenProject project, String defaultProvName, FeatureProvider fp)
            throws MavenExecutionException {
        File processedFeaturesDir = new File(project.getBuild().getDirectory(), "features/processed");
        processedFeaturesDir.mkdirs();

        List<File> substedFiles = new ArrayList<>();
        for (FeatureFileEntry featureFile : files) {
            final File f = featureFile.file;
            File outFile = new File(processedFeaturesDir, f.getName());
            if (!outFile.exists() || outFile.lastModified() <= f.lastModified()) {
                try {
                    final String suggestedClassifier;
                    if (!f.getName().equals("feature.json")) {
                        final int lastDot = f.getName().lastIndexOf('.');
                        suggestedClassifier = f.getName().substring(0, lastDot);
                    } else {
                        suggestedClassifier = null;
                    }
                    String json = readFeatureFile(project, f, suggestedClassifier);

                    // handle extensions
                    try (final Reader reader = new StringReader(json)) {
                        final Feature feature = FeatureJSONReader.read(reader, f.getAbsolutePath());
                        JSONFeatures.handleExtensions(feature, f);
                        try (final Writer writer = new StringWriter()) {
                            FeatureJSONWriter.write(writer, feature);
                            writer.flush();
                            json = writer.toString();
                        }
                    }

                    // check for prov model name
                    if (defaultProvName != null || featureFile.runModes != null || featureFile.model != null) {
                        try (final Reader reader = new StringReader(json)) {
                            final Feature feature = FeatureJSONReader.read(reader, f.getAbsolutePath());
                            boolean update = false;
                            if (featureFile.runModes != null) {
                                String oldValue = feature.getVariables().get(PROVISIONING_RUNMODES);
                                String newValue;
                                if (oldValue == null) {
                                    newValue = featureFile.runModes;
                                } else {
                                    newValue = oldValue.concat(",").concat(featureFile.runModes);
                                }
                                feature.getVariables().put(PROVISIONING_RUNMODES, newValue);
                                update = true;
                            }

                            if (feature.getVariables().get(PROVISIONING_MODEL_NAME_VARIABLE) == null) {
                                boolean updateInner = true;
                                if (featureFile.model != null) {
                                    feature.getVariables().put(PROVISIONING_MODEL_NAME_VARIABLE, featureFile.model);
                                }
                                else if (defaultProvName != null) {
                                    feature.getVariables().put(PROVISIONING_MODEL_NAME_VARIABLE, defaultProvName);
                                }
                                else {
                                    updateInner = update;
                                }
                                update = updateInner;
                            }

                            if (update) {
                                try (final Writer writer = new StringWriter()) {
                                    FeatureJSONWriter.write(writer, feature);
                                    writer.flush();
                                    json = writer.toString();
                                }
                            }
                        }
                    }
                    try (final Writer fileWriter = new FileWriter(outFile)) {
                        fileWriter.write(json);
                    }
                } catch (IOException e) {
                    throw new MavenExecutionException("Problem processing feature file " + f.getAbsolutePath(), e);
                }
            }
            substedFiles.add(outFile);
        }

        File targetDir = new File(project.getBuild().getDirectory(), BUILD_DIR);
        targetDir.mkdirs();

        try {
            for (File f : substedFiles) {
                File genFile = new File(targetDir, f.getName() + ".txt");
                FeatureToProvisioning.convert(f, genFile, fp, substedFiles.toArray(new File[] {}));
            }
        } catch (Exception e) {
            throw new MavenExecutionException("Cannot convert feature files to provisioning model", e);
        }
    }