]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/source/repository/ModuleSourceRepository.java
Added a function to read the module source text
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / source / repository / ModuleSourceRepository.java
1 package org.simantics.scl.compiler.source.repository;
2
3 import java.io.IOException;
4 import java.util.Collection;
5 import java.util.Collections;
6
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;
11
12 import gnu.trove.procedure.TObjectProcedure;
13
14 /**
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}.
17  * 
18  * @author Hannu Niemistö
19  */
20 public interface ModuleSourceRepository {
21     /**
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 
25      * works like this).
26      */
27     Collection<String> getModuleNames();
28     
29     /**
30      * Calls the given procedure with all module names returned by {@link #getModuleNames}
31      */
32     default void forAllModules(TObjectProcedure<String> procedure) {
33         for(String module : getModuleNames())
34             if(!procedure.execute(module))
35                 return;
36     }
37     
38     /**
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.
41      */
42     ModuleSource getModuleSource(String moduleName, UpdateListener listener);
43     
44     /**
45      * Returns all documentation names governed by this repository. Some implementations
46      * may return empty collection, even if the contain documentation.
47      */
48     default Collection<String> getDocumentationNames() {
49         return Collections.emptyList();
50     }
51     
52     /**
53      * Calls the given procedure with all documentation names eturned by {@link #getDocumentationNames}
54      */
55     default void forAllDocumentations(TObjectProcedure<String> procedure) {
56         for(String module : getDocumentationNames())
57             if(!procedure.execute(module))
58                 return;
59     }
60
61     /**
62      * Returns original markdown text for the given documentation name or null
63      * if the documentation does not exist.
64      */
65     default String getDocumentation(String documentationName) {
66         return null;
67     }
68     
69     /**
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.
72      */
73     default void checkUpdates() {
74     }
75
76     /**
77      * Resets the repository and removes listeners. This is only used during regression testing and shouldn't be called during normal operation.
78      */
79     default void clear() {
80     }
81     
82     default String getPossibleSourceText(String moduleName) throws IOException {
83         ModuleSource moduleSource = getModuleSource(moduleName, null);
84         if(moduleSource == null)
85             return null;
86         if(!(moduleSource instanceof TextualModuleSource))
87             return null;
88         return ((TextualModuleSource)moduleSource).getSourceText(null);
89     }
90 }