1 package org.simantics.scl.compiler.source.repository;
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.Collections;
7 import org.simantics.scl.compiler.module.repository.ModuleRepository;
8 import org.simantics.scl.compiler.module.repository.UpdateListener;
9 import org.simantics.scl.compiler.source.ModuleSource;
10 import org.simantics.scl.compiler.source.TextualModuleSource;
12 import gnu.trove.procedure.TObjectProcedure;
15 * An interface for locating modules descriptors and listening if they change.
16 * An instance of this interface is used to create a {@link ModuleRepository}.
18 * @author Hannu Niemistö
20 public interface ModuleSourceRepository {
22 * Returns all module names governed by this repository. Some implementations
23 * may return empty collection, even if the contain modules, if it is hard
24 * to discover all modules (for example file system based module repository
27 Collection<String> getModuleNames();
30 * Calls the given procedure with all module names returned by {@link #getModuleNames}
32 default void forAllModules(TObjectProcedure<String> procedure) {
33 for(String module : getModuleNames())
34 if(!procedure.execute(module))
39 * Returns the module source of the given module name or null, if the module does not exists.
40 * If {@code listener} is not null, it is called when the module contents change.
42 ModuleSource getModuleSource(String moduleName, UpdateListener listener);
45 * Returns all documentation names governed by this repository. Some implementations
46 * may return empty collection, even if the contain documentation.
48 default Collection<String> getDocumentationNames() {
49 return Collections.emptyList();
53 * Calls the given procedure with all documentation names eturned by {@link #getDocumentationNames}
55 default void forAllDocumentations(TObjectProcedure<String> procedure) {
56 for(String module : getDocumentationNames())
57 if(!procedure.execute(module))
62 * Returns original markdown text for the given documentation name or null
63 * if the documentation does not exist.
65 default String getDocumentation(String documentationName) {
70 * Triggers repository to check if module contents have been changed. Some repositories listen
71 * changes and report them to update listeners even without manual triggering.
73 default void checkUpdates() {
77 * Resets the repository and removes listeners. This is only used during regression testing and shouldn't be called during normal operation.
79 default void clear() {
82 default String getPossibleSourceText(String moduleName) throws IOException {
83 ModuleSource moduleSource = getModuleSource(moduleName, null);
84 if(moduleSource == null)
86 if(!(moduleSource instanceof TextualModuleSource))
88 return ((TextualModuleSource)moduleSource).getSourceText(null);