Search in sources :

Example 1 with Increments

use of com.devonfw.cobigen.impl.config.entity.io.Increments in project cobigen by devonfw.

the class TemplatesConfigurationReader method loadIncrements.

/**
 * Loads all increments of the static configuration into the local representation.
 *
 * @return the mapping of increment names to the corresponding {@link Increment}
 * @param templates {@link Map} of all templates (see {@link TemplatesConfigurationReader#loadTemplates(Trigger)}
 * @param trigger {@link Trigger} for which the templates should be loaded
 * @throws InvalidConfigurationException if there is an invalid ref attribute
 */
public Map<String, Increment> loadIncrements(Map<String, Template> templates, Trigger trigger) throws InvalidConfigurationException {
    Map<String, Increment> increments = new HashMap<>();
    Increments incrementsNode = this.configNode.getIncrements();
    if (incrementsNode != null) {
        // Add first all increments informally be able to resolve recursive increment references
        for (com.devonfw.cobigen.impl.config.entity.io.Increment source : incrementsNode.getIncrement()) {
            if (!increments.containsKey(source.getName())) {
                increments.put(source.getName(), new Increment(source.getName(), source.getDescription(), trigger));
            } else {
                throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "Duplicate increment found with name='" + source.getName() + "'.");
            }
        }
        // Collect templates
        for (com.devonfw.cobigen.impl.config.entity.io.Increment p : this.configNode.getIncrements().getIncrement()) {
            Increment target = increments.get(p.getName());
            addAllTemplatesRecursively(target, p, templates, increments);
        }
    }
    return increments;
}
Also used : HashMap(java.util.HashMap) Increment(com.devonfw.cobigen.impl.config.entity.Increment) Increments(com.devonfw.cobigen.impl.config.entity.io.Increments) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Example 2 with Increments

use of com.devonfw.cobigen.impl.config.entity.io.Increments in project cobigen by devonfw.

the class TemplatesConfigurationReader method loadSpecificIncrement.

/**
 * Loads an specific increment of the static configuration into the local representation. The return object must be a
 * map because maybe this increment references other increments
 *
 * @return the mapping of increment names to the corresponding {@link Increment}
 * @param templates {@link Map} of all templates (see {@link TemplatesConfigurationReader#loadTemplates(Trigger)}
 * @param trigger {@link Trigger} for which the templates should be loaded
 * @param incrementName the increment to search
 * @throws InvalidConfigurationException if there is an invalid ref attribute
 */
public Map<String, Increment> loadSpecificIncrement(Map<String, Template> templates, Trigger trigger, String incrementName) throws InvalidConfigurationException {
    Map<String, Increment> increments = new HashMap<>();
    Increments incrementsNode = this.configNode.getIncrements();
    if (incrementsNode != null) {
        // We only add the specific increment we want
        com.devonfw.cobigen.impl.config.entity.io.Increment source = getSpecificIncrement(incrementsNode.getIncrement(), incrementName);
        if (source == null) {
            throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "No increment found with name='" + incrementName + "' on the external templates.xml folder.");
        }
        increments.put(source.getName(), new Increment(source.getName(), source.getDescription(), trigger));
        // Collect templates for our specific increment
        Increment target = increments.get(source.getName());
        addAllTemplatesRecursively(target, source, templates, increments);
    }
    return increments;
}
Also used : HashMap(java.util.HashMap) Increment(com.devonfw.cobigen.impl.config.entity.Increment) Increments(com.devonfw.cobigen.impl.config.entity.io.Increments) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Example 3 with Increments

use of com.devonfw.cobigen.impl.config.entity.io.Increments in project cobigen by devonfw.

the class TemplatesConfigurationReader method addAllTemplatesRecursively.

/**
 * Adds all templates defined within the increment and sub increments recursively.
 *
 * @param rootIncrement the {@link Increment} on which the templates should be added
 * @param current the source {@link com.devonfw.cobigen.impl.config.entity.io.Increment} from which to retrieve the
 *        data
 * @param templates {@link Map} of all templates (see {@link TemplatesConfigurationReader#loadTemplates(Trigger)}
 * @param increments {@link Map} of all retrieved increments
 * @throws InvalidConfigurationException if there is an invalid ref attribute
 */
private void addAllTemplatesRecursively(Increment rootIncrement, com.devonfw.cobigen.impl.config.entity.io.Increment current, Map<String, Template> templates, Map<String, Increment> increments) throws InvalidConfigurationException {
    for (TemplateRef ref : current.getTemplateRefOrIncrementRefOrTemplateScanRef().stream().filter(e -> e instanceof TemplateRef).map(e -> (TemplateRef) e).collect(Collectors.toList())) {
        Template temp = templates.get(ref.getRef());
        if (temp == null) {
            if (isExternalRef(ref.getRef())) {
                rootIncrement.addTemplate(loadExternalTemplate(ref));
            } else {
                throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "No template found for ref='" + ref.getRef() + "'!");
            }
        } else {
            rootIncrement.addTemplate(temp);
        }
    }
    for (IncrementRef ref : current.getTemplateRefOrIncrementRefOrTemplateScanRef().stream().filter(e -> e instanceof IncrementRef).map(e -> (IncrementRef) e).collect(Collectors.toList())) {
        Increment parentPkg = increments.get(current.getName());
        Increment childPkg = increments.get(ref.getRef());
        if (childPkg == null) {
            // We try to find the increment inside our templates.xml file
            Increments incrementsNode = this.configNode.getIncrements();
            com.devonfw.cobigen.impl.config.entity.io.Increment source = null;
            if (incrementsNode != null) {
                // We only add the specific increment we want
                source = getSpecificIncrement(incrementsNode.getIncrement(), ref.getRef());
                if (source != null) {
                    addAllTemplatesRecursively(rootIncrement, source, templates, increments);
                } else // incrementRef contains "::". That would mean we have to search on another folder.
                if (isExternalRef(ref.getRef())) {
                    parentPkg.addIncrementDependency(loadExternalIncrement(ref));
                } else {
                    throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "No increment found for ref='" + ref.getRef() + "'!");
                }
            }
        } else {
            parentPkg.addIncrementDependency(childPkg);
            com.devonfw.cobigen.impl.config.entity.io.Increment pkg = getIncrementDeclaration(ref);
            addAllTemplatesRecursively(rootIncrement, pkg, templates, increments);
        }
    }
    for (TemplateScanRef ref : current.getTemplateRefOrIncrementRefOrTemplateScanRef().stream().filter(e -> e instanceof TemplateScanRef).map(e -> (TemplateScanRef) e).collect(Collectors.toList())) {
        List<String> scannedTemplateNames = this.templateScanTemplates.get(ref.getRef());
        if (scannedTemplateNames == null) {
            throw new InvalidConfigurationException(this.configFilePath.toUri().toString(), "No templateScan found for ref='" + ref.getRef() + "'!");
        } else {
            for (String scannedTemplateName : scannedTemplateNames) {
                rootIncrement.addTemplate(templates.get(scannedTemplateName));
            }
        }
    }
}
Also used : TemplateScans(com.devonfw.cobigen.impl.config.entity.io.TemplateScans) Unmarshaller(jakarta.xml.bind.Unmarshaller) TemplatesConfigurationVersion(com.devonfw.cobigen.impl.config.constant.TemplatesConfigurationVersion) StringUtils(org.apache.commons.lang3.StringUtils) Type(com.devonfw.cobigen.impl.config.versioning.VersionValidator.Type) BigDecimal(java.math.BigDecimal) JvmUtil(com.devonfw.cobigen.api.util.JvmUtil) Map(java.util.Map) VersionValidator(com.devonfw.cobigen.impl.config.versioning.VersionValidator) Path(java.nio.file.Path) ExceptionUtil(com.devonfw.cobigen.api.util.ExceptionUtil) Increments(com.devonfw.cobigen.impl.config.entity.io.Increments) TextTemplateEngine(com.devonfw.cobigen.api.extension.TextTemplateEngine) TemplatePath(com.devonfw.cobigen.impl.config.entity.TemplatePath) Set(java.util.Set) TemplateEngineRegistry(com.devonfw.cobigen.impl.extension.TemplateEngineRegistry) Collectors(java.util.stream.Collectors) Templates(com.devonfw.cobigen.impl.config.entity.io.Templates) UnknownContextVariableException(com.devonfw.cobigen.impl.exceptions.UnknownContextVariableException) Sets(com.google.common.collect.Sets) List(java.util.List) SAXException(org.xml.sax.SAXException) JXPathContext(org.apache.commons.jxpath.JXPathContext) ConfigurationConstants(com.devonfw.cobigen.api.constants.ConfigurationConstants) TemplatesConfiguration(com.devonfw.cobigen.impl.config.entity.io.TemplatesConfiguration) StreamSource(javax.xml.transform.stream.StreamSource) HashMap(java.util.HashMap) TemplateExtension(com.devonfw.cobigen.impl.config.entity.io.TemplateExtension) Schema(javax.xml.validation.Schema) ArrayList(java.util.ArrayList) TemplateScan(com.devonfw.cobigen.impl.config.entity.io.TemplateScan) HashSet(java.util.HashSet) TemplateScanRef(com.devonfw.cobigen.impl.config.entity.io.TemplateScanRef) XMLConstants(javax.xml.XMLConstants) MavenMetadata(com.devonfw.cobigen.impl.config.constant.MavenMetadata) TemplateFolder(com.devonfw.cobigen.impl.config.entity.TemplateFolder) UnmarshalException(jakarta.xml.bind.UnmarshalException) Properties(java.util.Properties) SchemaFactory(javax.xml.validation.SchemaFactory) Iterator(java.util.Iterator) JAXBContext(jakarta.xml.bind.JAXBContext) Files(java.nio.file.Files) ConfigurationHolder(com.devonfw.cobigen.impl.config.ConfigurationHolder) IncrementRef(com.devonfw.cobigen.impl.config.entity.io.IncrementRef) TemplateRef(com.devonfw.cobigen.impl.config.entity.io.TemplateRef) UnknownExpressionException(com.devonfw.cobigen.api.exception.UnknownExpressionException) IOException(java.io.IOException) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException) Template(com.devonfw.cobigen.impl.config.entity.Template) Maps(com.google.common.collect.Maps) Increment(com.devonfw.cobigen.impl.config.entity.Increment) SAXParseException(org.xml.sax.SAXParseException) JAXBException(jakarta.xml.bind.JAXBException) TemplateFile(com.devonfw.cobigen.impl.config.entity.TemplateFile) Paths(java.nio.file.Paths) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) InputStream(java.io.InputStream) IncrementRef(com.devonfw.cobigen.impl.config.entity.io.IncrementRef) Template(com.devonfw.cobigen.impl.config.entity.Template) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException) TemplateScanRef(com.devonfw.cobigen.impl.config.entity.io.TemplateScanRef) Increment(com.devonfw.cobigen.impl.config.entity.Increment) Increments(com.devonfw.cobigen.impl.config.entity.io.Increments) TemplateRef(com.devonfw.cobigen.impl.config.entity.io.TemplateRef)

Aggregations

InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)3 Increment (com.devonfw.cobigen.impl.config.entity.Increment)3 Increments (com.devonfw.cobigen.impl.config.entity.io.Increments)3 HashMap (java.util.HashMap)3 ConfigurationConstants (com.devonfw.cobigen.api.constants.ConfigurationConstants)1 UnknownExpressionException (com.devonfw.cobigen.api.exception.UnknownExpressionException)1 TextTemplateEngine (com.devonfw.cobigen.api.extension.TextTemplateEngine)1 ExceptionUtil (com.devonfw.cobigen.api.util.ExceptionUtil)1 JvmUtil (com.devonfw.cobigen.api.util.JvmUtil)1 ConfigurationHolder (com.devonfw.cobigen.impl.config.ConfigurationHolder)1 MavenMetadata (com.devonfw.cobigen.impl.config.constant.MavenMetadata)1 TemplatesConfigurationVersion (com.devonfw.cobigen.impl.config.constant.TemplatesConfigurationVersion)1 Template (com.devonfw.cobigen.impl.config.entity.Template)1 TemplateFile (com.devonfw.cobigen.impl.config.entity.TemplateFile)1 TemplateFolder (com.devonfw.cobigen.impl.config.entity.TemplateFolder)1 TemplatePath (com.devonfw.cobigen.impl.config.entity.TemplatePath)1 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)1 IncrementRef (com.devonfw.cobigen.impl.config.entity.io.IncrementRef)1 TemplateExtension (com.devonfw.cobigen.impl.config.entity.io.TemplateExtension)1 TemplateRef (com.devonfw.cobigen.impl.config.entity.io.TemplateRef)1