Search in sources :

Example 1 with OnlyNonHiddenDirectories

use of org.eclipse.winery.repository.backend.filebased.OnlyNonHiddenDirectories in project winery by eclipse.

the class YamlRepository method getDefinitionsChildIds.

/**
 * Creates Set of Definitions Child Id Mapps xml definition to compatible yaml definition
 *
 * @param inputIdClass            requested id class
 * @param omitDevelopmentVersions omit development versions
 * @return set of definitions child id
 */
@Override
public <T extends DefinitionsChildId> SortedSet<T> getDefinitionsChildIds(Class<T> inputIdClass, boolean omitDevelopmentVersions) {
    SortedSet<T> res = new TreeSet<>();
    List<Class<T>> idClasses = new ArrayList<>();
    idClasses.add(inputIdClass);
    idClasses = convertDefinitionsChildIdIfNeeded(idClasses);
    for (Class<T> idClass : idClasses) {
        String rootPathFragment = IdUtil.getRootPathFragment(idClass);
        Path dir = this.getRepositoryRoot().resolve(rootPathFragment);
        if (!Files.exists(dir)) {
            // return empty list if no ids are available
            return res;
        }
        assert (Files.isDirectory(dir));
        final OnlyNonHiddenDirectories onhdf = new OnlyNonHiddenDirectories();
        // list all directories contained in this directory
        try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, onhdf)) {
            for (Path nsP : ds) {
                // the current path is the namespace
                Namespace ns = new Namespace(nsP.getFileName().toString(), true);
                try (DirectoryStream<Path> idDS = Files.newDirectoryStream(nsP, onhdf)) {
                    for (Path idP : idDS) {
                        List<XmlId> xmlIds = new ArrayList<>();
                        if (ArtifactTemplateId.class.isAssignableFrom(inputIdClass)) {
                            List<String> artifactNames = getAllArtifactNamesFromType(idP, idClass, ns.getDecoded());
                            for (String artifactName : artifactNames) {
                                xmlIds.add(new XmlId(artifactName + "@" + IdUtil.getFolderName(idClass), true));
                            }
                        } else {
                            xmlIds.add(new XmlId(idP.getFileName().toString(), true));
                        }
                        for (XmlId xmlId : xmlIds) {
                            if (omitDevelopmentVersions) {
                                WineryVersion version = VersionUtils.getVersion(xmlId.getDecoded());
                                if (version.toString().length() > 0 && version.getWorkInProgressVersion() > 0) {
                                    continue;
                                }
                            }
                            Constructor<T> constructor;
                            try {
                                constructor = inputIdClass.getConstructor(Namespace.class, XmlId.class);
                            } catch (Exception e) {
                                LOGGER.debug("Internal error at determining id constructor", e);
                                // abort everything, return invalid result
                                return res;
                            }
                            T id;
                            try {
                                id = constructor.newInstance(ns, xmlId);
                            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                                LOGGER.debug("Internal error at invocation of id constructor", e);
                                // abort everything, return invalid result
                                return res;
                            }
                            res.add(id);
                        }
                    }
                }
            }
        } catch (IOException e) {
            LOGGER.debug("Cannot close ds", e);
        }
    }
    return res;
}
Also used : Path(java.nio.file.Path) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Namespace(org.eclipse.winery.model.ids.Namespace) JAXBException(javax.xml.bind.JAXBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OnlyNonHiddenDirectories(org.eclipse.winery.repository.backend.filebased.OnlyNonHiddenDirectories) TreeSet(java.util.TreeSet) XmlId(org.eclipse.winery.model.ids.XmlId) WineryVersion(org.eclipse.winery.common.version.WineryVersion)

Example 2 with OnlyNonHiddenDirectories

use of org.eclipse.winery.repository.backend.filebased.OnlyNonHiddenDirectories in project winery by eclipse.

the class XmlRepository method getDefinitionsChildIds.

public <T extends DefinitionsChildId> SortedSet<T> getDefinitionsChildIds(Class<T> idClass, boolean omitDevelopmentVersions) {
    SortedSet<T> res = new TreeSet<>();
    String rootPathFragment = IdUtil.getRootPathFragment(idClass);
    Path dir = makeAbsolute(Paths.get(rootPathFragment));
    if (!Files.exists(dir)) {
        // return empty list if no ids are available
        return res;
    }
    assert (Files.isDirectory(dir));
    final OnlyNonHiddenDirectories hiddenDirectories = new OnlyNonHiddenDirectories();
    // list all directories contained in this directory
    try (DirectoryStream<Path> ds = Files.newDirectoryStream(dir, hiddenDirectories)) {
        for (Path nsP : ds) {
            // the current path is the namespace
            Namespace ns = new Namespace(nsP.getFileName().toString(), true);
            try (DirectoryStream<Path> idDS = Files.newDirectoryStream(nsP, hiddenDirectories)) {
                for (Path idP : idDS) {
                    XmlId xmlId = new XmlId(idP.getFileName().toString(), true);
                    if (omitDevelopmentVersions) {
                        WineryVersion version = VersionUtils.getVersion(xmlId.getDecoded());
                        if (version.toString().length() > 0 && version.getWorkInProgressVersion() > 0) {
                            continue;
                        }
                    }
                    Constructor<T> constructor;
                    try {
                        constructor = idClass.getConstructor(Namespace.class, XmlId.class);
                    } catch (Exception e) {
                        LOGGER.debug("Internal error at determining id constructor", e);
                        // abort everything, return invalid result
                        return res;
                    }
                    T id;
                    try {
                        id = constructor.newInstance(ns, xmlId);
                    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                        LOGGER.debug("Internal error at invocation of id constructor", e);
                        // abort everything, return invalid result
                        return res;
                    }
                    res.add(id);
                }
            }
        }
    } catch (IOException e) {
        LOGGER.debug("Cannot close ds", e);
    }
    return res;
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) Namespace(org.eclipse.winery.model.ids.Namespace) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) OnlyNonHiddenDirectories(org.eclipse.winery.repository.backend.filebased.OnlyNonHiddenDirectories) TreeSet(java.util.TreeSet) XmlId(org.eclipse.winery.model.ids.XmlId) WineryVersion(org.eclipse.winery.common.version.WineryVersion)

Aggregations

IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Path (java.nio.file.Path)2 TreeSet (java.util.TreeSet)2 JAXBException (javax.xml.bind.JAXBException)2 WineryVersion (org.eclipse.winery.common.version.WineryVersion)2 Namespace (org.eclipse.winery.model.ids.Namespace)2 XmlId (org.eclipse.winery.model.ids.XmlId)2 OnlyNonHiddenDirectories (org.eclipse.winery.repository.backend.filebased.OnlyNonHiddenDirectories)2 ArrayList (java.util.ArrayList)1 MultiException (org.eclipse.winery.model.converter.support.exception.MultiException)1