1 package org.simantics.help.core;
\r
3 import java.nio.file.Path;
\r
4 import java.nio.file.Paths;
\r
5 import java.util.ArrayList;
\r
6 import java.util.HashMap;
\r
7 import java.util.List;
\r
8 import java.util.Map;
\r
10 import javax.xml.parsers.DocumentBuilder;
\r
11 import javax.xml.parsers.DocumentBuilderFactory;
\r
12 import javax.xml.parsers.ParserConfigurationException;
\r
14 import org.eclipse.help.AbstractTocProvider;
\r
15 import org.eclipse.help.ITocContribution;
\r
16 import org.eclipse.help.internal.HelpPlugin;
\r
17 import org.eclipse.help.internal.Topic;
\r
18 import org.eclipse.help.internal.entityresolver.LocalEntityResolver;
\r
19 import org.eclipse.help.internal.toc.Toc;
\r
20 import org.eclipse.help.internal.toc.TocContribution;
\r
21 import org.simantics.Simantics;
\r
22 import org.simantics.db.ReadGraph;
\r
23 import org.simantics.db.common.request.ReadRequest;
\r
24 import org.simantics.db.exception.DatabaseException;
\r
25 import org.w3c.dom.Document;
\r
26 import org.w3c.dom.Element;
\r
28 @SuppressWarnings("restriction")
\r
29 public class SimanticsTocProvider extends AbstractTocProvider {
\r
31 private static DocumentBuilder builder;
\r
32 private static Document document;
\r
34 private static Map<String, Path> widgetReferences;
\r
35 private static Map<String, List<Path>> tutorials;
\r
38 public ITocContribution[] getTocContributions(String locale) {
\r
39 List<ITocContribution> contributions = new ArrayList<>();
\r
41 if (widgetReferences == null || tutorials == null) {
\r
43 Simantics.getSession().syncRequest(new ReadRequest() {
\r
45 public void run(ReadGraph graph) throws DatabaseException {
\r
46 widgetReferences = HelpUtils.collectWidgetReferencesFromSharedLibraries(graph);
\r
47 tutorials = HelpUtils.collectHelpsFromSharedLibraries(graph);
\r
51 } catch (DatabaseException e) {
\r
52 e.printStackTrace();
\r
56 Map<String, Toc> tocs = new HashMap<>();
\r
58 if (widgetReferences != null) {
\r
59 for (Map.Entry<String, Path> widgetReference : widgetReferences.entrySet()) {
\r
60 String libName = widgetReference.getKey();
\r
61 Path htmlFile = widgetReference.getValue();
\r
62 Toc toc = tocs.get(libName);
\r
64 Element element = getDocument().createElement("toc");
\r
65 toc = new Toc(element);
\r
66 toc.setLabel(libName);
\r
67 // toc.setTopic(libName);
\r
68 // toc.setHref(libName);
\r
69 tocs.put(libName, toc);
\r
72 Topic topic = new Topic();
\r
73 //topic.setLabel(htmlFile.getFileName().toString());
\r
74 topic.setLabel("Widget Reference");
\r
75 // String href = htmlFile.toUri().toString();
\r
76 String href = htmlFile.toString().split(Activator.PLUGIN_ID)[1].substring(1);
\r
77 href = "PLUGINS_ROOT/platform:/meta/" + Activator.PLUGIN_ID + "/" + href;
\r
78 topic.setHref(href.replace("\\", "/").replace(" ", "%20"));
\r
80 toc.appendChild(topic);
\r
84 if (tutorials != null) {
\r
85 for (Map.Entry<String, List<Path>> tutorial : tutorials.entrySet()) {
\r
86 String libName = tutorial.getKey();
\r
87 Toc toc = tocs.get(libName);
\r
89 Element element = getDocument().createElement("toc");
\r
90 toc = new Toc(element);
\r
91 toc.setLabel(libName);
\r
92 toc.setTopic(libName);
\r
93 toc.setHref(libName);
\r
94 tocs.put(libName, toc);
\r
97 Map<String, Topic> topics = new HashMap<>();
\r
99 for (Path htmlFile : tutorial.getValue()) {
\r
100 Path path = Paths.get(htmlFile.toString().split(libName)[1]);
\r
101 Topic topic = getOrCreateTopic(topics, toc, path);
\r
102 String href = htmlFile.toString().split(Activator.PLUGIN_ID)[1].substring(1);
\r
103 href = "PLUGINS_ROOT/platform:/meta/" + Activator.PLUGIN_ID + "/" + href;
\r
104 // String href = htmlFile.toUri().toString();
\r
105 topic.setHref(href.replace("\\", "/").replace(" ", "%20"));
\r
110 for (Toc toc : tocs.values()) {
\r
111 TocContribution contribution = new TocContribution();
\r
112 contribution.setLocale(locale);
\r
113 contribution.setId(toc.getLabel());
\r
114 contribution.setCategoryId("category_" + toc.getLabel());
\r
115 contribution.setPrimary(true);
\r
116 contribution.setContributorId(Activator.PLUGIN_ID);
\r
118 contribution.setToc(toc);
\r
119 contributions.add(contribution);
\r
123 return (ITocContribution[])contributions.toArray(new ITocContribution[contributions.size()]);
\r
126 private static Topic getOrCreateTopic(Map<String, Topic> topics, Toc toc, Path topicPath) {
\r
127 String topicName = topicPath.getFileName().toString();
\r
128 Topic topic = topics.get(topicName);
\r
129 if (topic == null) {
\r
130 topic = new Topic();
\r
131 topic.setLabel(topicName);
\r
132 Path parentPath = topicPath.getParent();
\r
133 if (parentPath != null && parentPath.getFileName() != null) {
\r
134 Topic parentTopic = getOrCreateTopic(topics, toc, parentPath);
\r
135 parentTopic.appendChild(topic);
\r
137 toc.appendChild(topic);
\r
139 topics.put(topicName, topic);
\r
144 private static Document getDocument() {
\r
145 if (document == null) {
\r
146 if (builder == null) {
\r
148 builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
\r
149 builder.setEntityResolver(new LocalEntityResolver());
\r
151 catch (ParserConfigurationException e) {
\r
152 String msg = "Error creating document builder"; //$NON-NLS-1$
\r
153 HelpPlugin.logError(msg, e);
\r
156 document = builder.newDocument();
\r
161 public static void clearTocCache() {
\r
162 widgetReferences.clear();
\r
163 widgetReferences = null;
\r
166 HelpPlugin.getTocManager().clearCache();
\r