]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/html/HierarchicalDocumentationRef.java
Merge "(refs #7621) Fixed handling of $ in application ignoring return value"
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / markdown / html / HierarchicalDocumentationRef.java
1 package org.simantics.scl.compiler.markdown.html;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.simantics.scl.compiler.source.repository.ModuleSourceRepository;
8
9 import gnu.trove.map.hash.THashMap;
10 import gnu.trove.procedure.TObjectProcedure;
11 import gnu.trove.set.hash.THashSet;
12
13 public class HierarchicalDocumentationRef implements Comparable<HierarchicalDocumentationRef> {
14     final String name;
15     final ArrayList<HierarchicalDocumentationRef> children = new ArrayList<HierarchicalDocumentationRef>();
16     String documentationName;
17     
18     public HierarchicalDocumentationRef(String name) {
19         this.name = name;
20     }
21     
22     public String getName() {
23         return name;
24     }
25     
26     public String getDocumentationName() {
27         return documentationName;
28     }
29     
30     public List<HierarchicalDocumentationRef> getChildren() {
31         return children;
32     }
33
34     @Override
35     public int compareTo(HierarchicalDocumentationRef o) {
36         return name.compareTo(o.name);
37     }
38     
39     public static HierarchicalDocumentationRef generateTree(ModuleSourceRepository sourceRepository) {
40         final THashMap<String,HierarchicalDocumentationRef> refMap = new THashMap<String,HierarchicalDocumentationRef>();
41         HierarchicalDocumentationRef root = new HierarchicalDocumentationRef("");
42         refMap.put("", root);
43         final THashSet<String> documentationPaths = new THashSet<String>(); 
44         TObjectProcedure<String> collector = new TObjectProcedure<String>() {
45             @Override
46             public boolean execute(String path) {
47                 documentationPaths.add(path);
48                 return true;
49             }
50         };
51         sourceRepository.forAllDocumentations(collector);
52         sourceRepository.forAllModules(collector);
53         documentationPaths.forEach(new TObjectProcedure<String>() {
54             HierarchicalDocumentationRef getRef(String path) {
55                 HierarchicalDocumentationRef ref = refMap.get(path);
56                 if(ref == null) {
57                     String name;
58                     String parentName;
59                     int p = path.lastIndexOf('/');
60                     if(p >= 0) {
61                         name = path.substring(p+1);
62                         parentName = path.substring(0, p);
63                     }
64                     else {
65                         name = path;
66                         parentName = "";
67                     }
68                     ref = new HierarchicalDocumentationRef(name);
69                     refMap.put(path, ref);
70                     getRef(parentName).children.add(ref);
71                 }
72                 return ref;
73             }   
74             
75             @Override
76             public boolean execute(String documentationName) {
77                 getRef(documentationName.contains("/")
78                         ? documentationName : "StandardLibrary/" + documentationName)
79                         .documentationName = documentationName;
80                 return true;
81             }
82         });
83         
84         root.sort();
85         
86         return root;
87     }
88
89     private void sort() {
90         Collections.sort(children);
91         for(HierarchicalDocumentationRef child : children)
92             child.sort();
93     }
94 }