]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.help.core/src/org/simantics/help/core/SimanticsTocProvider.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.help.core / src / org / simantics / help / core / SimanticsTocProvider.java
1 package org.simantics.help.core;
2
3 import java.nio.file.Path;
4 import java.nio.file.Paths;
5 import java.util.ArrayList;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
13
14 import org.eclipse.help.AbstractTocProvider;
15 import org.eclipse.help.ITocContribution;
16 import org.eclipse.help.internal.HelpPlugin;
17 import org.eclipse.help.internal.Topic;
18 import org.eclipse.help.internal.entityresolver.LocalEntityResolver;
19 import org.eclipse.help.internal.toc.Toc;
20 import org.eclipse.help.internal.toc.TocContribution;
21 import org.simantics.Simantics;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.common.request.ReadRequest;
24 import org.simantics.db.exception.DatabaseException;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27
28 @SuppressWarnings("restriction")
29 public class SimanticsTocProvider extends AbstractTocProvider {
30
31     private static DocumentBuilder builder;
32     private static Document document;
33     
34     private static Map<String, Path> widgetReferences; 
35     private static Map<String, List<Path>> tutorials;
36     
37     @Override
38     public ITocContribution[] getTocContributions(String locale) {
39         List<ITocContribution> contributions = new ArrayList<>();
40         
41         if (widgetReferences == null || tutorials == null) {
42             try {
43                 Simantics.getSession().syncRequest(new ReadRequest() {
44                     @Override
45                     public void run(ReadGraph graph) throws DatabaseException {
46                         widgetReferences = HelpUtils.collectWidgetReferencesFromSharedLibraries(graph);
47                         tutorials = HelpUtils.collectHelpsFromSharedLibraries(graph);
48                         
49                     }
50                 });
51             } catch (DatabaseException e) {
52                 e.printStackTrace();
53             }
54         }
55
56         Map<String, Toc> tocs = new HashMap<>();
57         
58         if (widgetReferences != null) {
59             for (Map.Entry<String, Path> widgetReference : widgetReferences.entrySet()) {
60                 String libName = widgetReference.getKey();
61                 Path htmlFile = widgetReference.getValue();
62                 Toc toc = tocs.get(libName);
63                 if (toc == null) {
64                     Element element = getDocument().createElement("toc");
65                     toc = new Toc(element); 
66                     toc.setLabel(libName);
67 //                    toc.setTopic(libName);
68 //                    toc.setHref(libName);
69                     tocs.put(libName, toc);
70                 }
71                 
72                 Topic topic = new Topic();
73                 //topic.setLabel(htmlFile.getFileName().toString());
74                 topic.setLabel("Widget Reference");
75 //                String href = htmlFile.toUri().toString();
76                 String href = htmlFile.toString().split(Activator.PLUGIN_ID)[1].substring(1);
77                 href = "PLUGINS_ROOT/platform:/meta/" + Activator.PLUGIN_ID + "/" + href;
78                 topic.setHref(href.replace("\\", "/").replace(" ", "%20"));
79                 
80                 toc.appendChild(topic);
81             }
82         }
83
84         if (tutorials != null) {
85             for (Map.Entry<String, List<Path>> tutorial : tutorials.entrySet()) {
86                 String libName = tutorial.getKey();
87                 Toc toc = tocs.get(libName);
88                 if (toc == null) {
89                     Element element = getDocument().createElement("toc");
90                     toc = new Toc(element); 
91                     toc.setLabel(libName);
92                     toc.setTopic(libName);
93                     toc.setHref(libName);
94                     tocs.put(libName, toc);
95                 }
96                 
97                 Map<String, Topic> topics = new HashMap<>();
98                 
99                 for (Path htmlFile : tutorial.getValue()) {
100                     Path path = Paths.get(htmlFile.toString().split(libName)[1]);
101                     Topic topic = getOrCreateTopic(topics, toc, path);
102                     String href = htmlFile.toString().split(Activator.PLUGIN_ID)[1].substring(1);
103                     href = "PLUGINS_ROOT/platform:/meta/" + Activator.PLUGIN_ID + "/" + href;
104 //                    String href = htmlFile.toUri().toString();
105                     topic.setHref(href.replace("\\", "/").replace(" ", "%20"));
106                 }
107             }
108         }
109         
110         for (Toc toc : tocs.values()) {
111             TocContribution contribution = new TocContribution();
112             contribution.setLocale(locale);
113             contribution.setId(toc.getLabel());
114             contribution.setCategoryId("category_" + toc.getLabel());
115             contribution.setPrimary(true);
116             contribution.setContributorId(Activator.PLUGIN_ID);
117             contribution.setExtraDocuments(new String[0]);
118             contribution.setToc(toc);
119             contributions.add(contribution);
120         }
121
122         
123         return (ITocContribution[])contributions.toArray(new ITocContribution[contributions.size()]);
124     }
125     
126     private static Topic getOrCreateTopic(Map<String, Topic> topics, Toc toc, Path topicPath) {
127         String topicName = topicPath.getFileName().toString();
128         Topic topic = topics.get(topicName);
129         if (topic == null) {
130             topic = new Topic();
131             topic.setLabel(topicName);
132             Path parentPath = topicPath.getParent();
133             if (parentPath != null && parentPath.getFileName() != null) {
134                 Topic parentTopic = getOrCreateTopic(topics, toc, parentPath);
135                 parentTopic.appendChild(topic);
136             } else {
137                 toc.appendChild(topic);
138             }
139             topics.put(topicName, topic);
140         }
141         return topic;
142     }
143     
144     private static Document getDocument() {
145         if (document == null) {
146             if (builder == null) {
147                 try {
148                     builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
149                     builder.setEntityResolver(new LocalEntityResolver());
150                 }
151                 catch (ParserConfigurationException e) {
152                     String msg = "Error creating document builder"; //$NON-NLS-1$
153                     HelpPlugin.logError(msg, e);
154                 }
155             }
156             document = builder.newDocument();
157         }
158         return document;
159     }
160     
161     public static void clearTocCache() {
162         widgetReferences.clear();
163         widgetReferences = null;
164         tutorials.clear();
165         tutorials = null;
166         HelpPlugin.getTocManager().clearCache();
167     }
168 }