1 package org.simantics.help.core;
3 import java.nio.file.Path;
4 import java.nio.file.Paths;
5 import java.util.ArrayList;
6 import java.util.HashMap;
10 import javax.xml.parsers.DocumentBuilder;
11 import javax.xml.parsers.DocumentBuilderFactory;
12 import javax.xml.parsers.ParserConfigurationException;
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;
28 @SuppressWarnings("restriction")
29 public class SimanticsTocProvider extends AbstractTocProvider {
31 private static DocumentBuilder builder;
32 private static Document document;
34 private static Map<String, Path> widgetReferences;
35 private static Map<String, List<Path>> tutorials;
38 public ITocContribution[] getTocContributions(String locale) {
39 List<ITocContribution> contributions = new ArrayList<>();
41 if (widgetReferences == null || tutorials == null) {
43 Simantics.getSession().syncRequest(new ReadRequest() {
45 public void run(ReadGraph graph) throws DatabaseException {
46 widgetReferences = HelpUtils.collectWidgetReferencesFromSharedLibraries(graph);
47 tutorials = HelpUtils.collectHelpsFromSharedLibraries(graph);
51 } catch (DatabaseException e) {
56 Map<String, Toc> tocs = new HashMap<>();
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);
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);
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"));
80 toc.appendChild(topic);
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);
89 Element element = getDocument().createElement("toc");
90 toc = new Toc(element);
91 toc.setLabel(libName);
92 toc.setTopic(libName);
94 tocs.put(libName, toc);
97 Map<String, Topic> topics = new HashMap<>();
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"));
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);
123 return (ITocContribution[])contributions.toArray(new ITocContribution[contributions.size()]);
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);
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);
137 toc.appendChild(topic);
139 topics.put(topicName, topic);
144 private static Document getDocument() {
145 if (document == null) {
146 if (builder == null) {
148 builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
149 builder.setEntityResolver(new LocalEntityResolver());
151 catch (ParserConfigurationException e) {
152 String msg = "Error creating document builder"; //$NON-NLS-1$
153 HelpPlugin.logError(msg, e);
156 document = builder.newDocument();
161 public static void clearTocCache() {
162 widgetReferences.clear();
163 widgetReferences = null;
166 HelpPlugin.getTocManager().clearCache();