Search in sources :

Example 1 with Include

use of org.eclipse.winery.model.tosca.TArtifactReference.Include in project winery by eclipse.

the class CsarImporter method adjustArtifactTemplate.

/**
 * Adjusts the given artifact template to conform with the repository format
 * <p>
 * We import the files given at the artifact references
 */
private void adjustArtifactTemplate(Path rootPath, TOSCAMetaFile tmf, ArtifactTemplateId atid, TArtifactTemplate ci, final List<String> errors) {
    ArtifactReferences refs = ci.getArtifactReferences();
    if (refs == null) {
        // no references stored - break
        return;
    }
    List<TArtifactReference> refList = refs.getArtifactReference();
    Iterator<TArtifactReference> iterator = refList.iterator();
    while (iterator.hasNext()) {
        TArtifactReference ref = iterator.next();
        String reference = ref.getReference();
        // URLs are stored encoded -> undo the encoding
        reference = Util.URLdecode(reference);
        URI refURI;
        try {
            refURI = new URI(reference);
        } catch (URISyntaxException e) {
            errors.add(String.format("Invalid URI %1$s", ref));
            continue;
        }
        if (refURI.isAbsolute()) {
            // We have to do nothing
            continue;
        }
        // we remove the current element as it will be handled during the export
        iterator.remove();
        Path path = rootPath.resolve(reference);
        if (!Files.exists(path)) {
            errors.add(String.format("Reference %1$s not found", reference));
            return;
        }
        Set<Path> allFiles;
        if (Files.isRegularFile(path)) {
            allFiles = new HashSet<>();
            allFiles.add(path);
        } else {
            if (!Files.isDirectory(path)) {
                LOGGER.error("path {} is not a directory", path);
            }
            Path localRoot = rootPath.resolve(path);
            List<Object> includeOrExclude = ref.getIncludeOrExclude();
            if (includeOrExclude.get(0) instanceof TArtifactReference.Exclude) {
                // Implicit semantics of an exclude listed first:
                // include all files and then exclude the files matched by the pattern
                allFiles = this.getAllFiles(localRoot);
            } else {
                // semantics if include listed as first:
                // same as listed at other places
                allFiles = new HashSet<>();
            }
            for (Object object : includeOrExclude) {
                if (object instanceof TArtifactReference.Include) {
                    this.handleInclude((TArtifactReference.Include) object, localRoot, allFiles);
                } else {
                    assert (object instanceof TArtifactReference.Exclude);
                    this.handleExclude((TArtifactReference.Exclude) object, localRoot, allFiles);
                }
            }
        }
        DirectoryId fileDir = new ArtifactTemplateFilesDirectoryId(atid);
        this.importAllFiles(rootPath, allFiles, fileDir, tmf, errors);
    }
    if (refList.isEmpty()) {
        // everything is imported and is a file stored locally
        // we don't need the references stored locally: they are generated on the fly when exporting
        ci.setArtifactReferences(null);
    }
}
Also used : Exclude(org.eclipse.winery.model.tosca.TArtifactReference.Exclude) ArtifactReferences(org.eclipse.winery.model.tosca.TArtifactTemplate.ArtifactReferences) Include(org.eclipse.winery.model.tosca.TArtifactReference.Include) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) ArtifactTemplateFilesDirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId) DirectoryId(org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId) Exclude(org.eclipse.winery.model.tosca.TArtifactReference.Exclude)

Aggregations

URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Exclude (org.eclipse.winery.model.tosca.TArtifactReference.Exclude)1 Include (org.eclipse.winery.model.tosca.TArtifactReference.Include)1 ArtifactReferences (org.eclipse.winery.model.tosca.TArtifactTemplate.ArtifactReferences)1 ArtifactTemplateFilesDirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.ArtifactTemplateFilesDirectoryId)1 DirectoryId (org.eclipse.winery.repository.datatypes.ids.elements.DirectoryId)1