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