All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cqframework.cql.cql2elm.DefaultLibrarySourceProvider Maven / Gradle / Ivy

Go to download

The cql-to-elm library for the Clinical Quality Language Java reference implementation

There is a newer version: 3.18.0
Show newest version
package org.cqframework.cql.cql2elm;

import java.io.*;
import java.nio.file.Path;
import org.cqframework.cql.cql2elm.model.Version;
import org.hl7.elm.r1.VersionedIdentifier;

// NOTE: This implementation is naive and assumes library file names will always take the form:
// [-].cql
// And further that  will never contain dashes, and that  will always be of the form
// [.[.]]
// Usage outside these boundaries will result in errors or incorrect behavior.
public class DefaultLibrarySourceProvider implements LibrarySourceProvider, PathAware {

    public DefaultLibrarySourceProvider(Path path) {
        setPath(path);
    }

    private Path path;

    public void setPath(Path path) {
        if (path == null || !path.toFile().isDirectory()) {
            throw new IllegalArgumentException(String.format("path '%s' is not a valid directory", path));
        }

        this.path = path;
    }

    @Override
    public InputStream getLibrarySource(VersionedIdentifier libraryIdentifier) {
        if (path != null) {
            String libraryName = libraryIdentifier.getId();
            Path libraryPath = this.path.resolve(String.format(
                    "%s%s.cql",
                    libraryName, libraryIdentifier.getVersion() != null ? ("-" + libraryIdentifier.getVersion()) : ""));
            File libraryFile = libraryPath.toFile();
            if (!libraryFile.exists()) {
                FilenameFilter filter = new FilenameFilter() {
                    @Override
                    public boolean accept(File path, String name) {
                        return name.startsWith(libraryName) && name.endsWith(".cql");
                    }
                };

                File mostRecentFile = null;
                Version mostRecent = null;
                Version requestedVersion =
                        libraryIdentifier.getVersion() == null ? null : new Version(libraryIdentifier.getVersion());
                for (File file : path.toFile().listFiles(filter)) {
                    String fileName = file.getName();
                    int indexOfExtension = fileName.lastIndexOf(".");
                    if (indexOfExtension >= 0) {
                        fileName = fileName.substring(0, indexOfExtension);
                    }

                    int indexOfVersionSeparator = fileName.indexOf("-");
                    if (indexOfVersionSeparator >= 0) {
                        Version version = new Version(fileName.substring(indexOfVersionSeparator + 1));
                        // If the file has a version, make sure it is compatible with the version we are looking for
                        if (indexOfVersionSeparator == libraryName.length() && requestedVersion == null
                                || version.compatibleWith(requestedVersion)) {
                            if (mostRecent == null
                                    || ((version != null && version.isComparable())
                                            && (mostRecent != null && mostRecent.isComparable())
                                            && version.compareTo(mostRecent) > 0)) {
                                mostRecent = version;
                                mostRecentFile = file;
                            } else if (version != null && version.matchStrictly(mostRecent)) {
                                mostRecent = version;
                                mostRecentFile = file;
                            }
                        }
                    } else {
                        // If the file is named correctly, but has no version, consider it the most recent version
                        if (fileName.equals(libraryName) && mostRecent == null) {
                            mostRecentFile = file;
                        }
                    }
                }

                // Do not throw, allow the loader to throw, just report null
                // if (mostRecentFile == null) {
                //    throw new IllegalArgumentException(String.format("Could not resolve most recent source library for
                // library %s.", libraryIdentifier.getId()));
                // }

                libraryFile = mostRecentFile;
            }
            try {
                if (libraryFile != null) {
                    return new FileInputStream(libraryFile);
                }
            } catch (FileNotFoundException e) {
                throw new IllegalArgumentException(
                        String.format("Could not load source for library %s.", libraryIdentifier.getId()), e);
            }
        }

        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy