--- /dev/null
+package org.simantics.help.core;\r
+\r
+import java.nio.file.Path;\r
+import java.nio.file.Paths;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
+\r
+import javax.xml.parsers.DocumentBuilder;\r
+import javax.xml.parsers.DocumentBuilderFactory;\r
+import javax.xml.parsers.ParserConfigurationException;\r
+\r
+import org.eclipse.help.AbstractTocProvider;\r
+import org.eclipse.help.ITocContribution;\r
+import org.eclipse.help.internal.HelpPlugin;\r
+import org.eclipse.help.internal.Topic;\r
+import org.eclipse.help.internal.entityresolver.LocalEntityResolver;\r
+import org.eclipse.help.internal.toc.Toc;\r
+import org.eclipse.help.internal.toc.TocContribution;\r
+import org.simantics.Simantics;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.w3c.dom.Document;\r
+import org.w3c.dom.Element;\r
+\r
+@SuppressWarnings("restriction")\r
+public class SimanticsTocProvider extends AbstractTocProvider {\r
+\r
+ private static DocumentBuilder builder;\r
+ private static Document document;\r
+ \r
+ private static Map<String, Path> widgetReferences; \r
+ private static Map<String, List<Path>> tutorials;\r
+ \r
+ @Override\r
+ public ITocContribution[] getTocContributions(String locale) {\r
+ List<ITocContribution> contributions = new ArrayList<>();\r
+ \r
+ if (widgetReferences == null || tutorials == null) {\r
+ try {\r
+ Simantics.getSession().syncRequest(new ReadRequest() {\r
+ @Override\r
+ public void run(ReadGraph graph) throws DatabaseException {\r
+ widgetReferences = HelpUtils.collectWidgetReferencesFromSharedLibraries(graph);\r
+ tutorials = HelpUtils.collectHelpsFromSharedLibraries(graph);\r
+ \r
+ }\r
+ });\r
+ } catch (DatabaseException e) {\r
+ e.printStackTrace();\r
+ }\r
+ }\r
+\r
+ Map<String, Toc> tocs = new HashMap<>();\r
+ \r
+ if (widgetReferences != null) {\r
+ for (Map.Entry<String, Path> widgetReference : widgetReferences.entrySet()) {\r
+ String libName = widgetReference.getKey();\r
+ Path htmlFile = widgetReference.getValue();\r
+ Toc toc = tocs.get(libName);\r
+ if (toc == null) {\r
+ Element element = getDocument().createElement("toc");\r
+ toc = new Toc(element); \r
+ toc.setLabel(libName);\r
+// toc.setTopic(libName);\r
+// toc.setHref(libName);\r
+ tocs.put(libName, toc);\r
+ }\r
+ \r
+ Topic topic = new Topic();\r
+ //topic.setLabel(htmlFile.getFileName().toString());\r
+ topic.setLabel("Widget Reference");\r
+// String href = htmlFile.toUri().toString();\r
+ String href = htmlFile.toString().split(Activator.PLUGIN_ID)[1].substring(1);\r
+ href = "PLUGINS_ROOT/platform:/meta/" + Activator.PLUGIN_ID + "/" + href;\r
+ topic.setHref(href.replace("\\", "/").replace(" ", "%20"));\r
+ \r
+ toc.appendChild(topic);\r
+ }\r
+ }\r
+\r
+ if (tutorials != null) {\r
+ for (Map.Entry<String, List<Path>> tutorial : tutorials.entrySet()) {\r
+ String libName = tutorial.getKey();\r
+ Toc toc = tocs.get(libName);\r
+ if (toc == null) {\r
+ Element element = getDocument().createElement("toc");\r
+ toc = new Toc(element); \r
+ toc.setLabel(libName);\r
+ toc.setTopic(libName);\r
+ toc.setHref(libName);\r
+ tocs.put(libName, toc);\r
+ }\r
+ \r
+ Map<String, Topic> topics = new HashMap<>();\r
+ \r
+ for (Path htmlFile : tutorial.getValue()) {\r
+ Path path = Paths.get(htmlFile.toString().split(libName)[1]);\r
+ Topic topic = getOrCreateTopic(topics, toc, path);\r
+ String href = htmlFile.toString().split(Activator.PLUGIN_ID)[1].substring(1);\r
+ href = "PLUGINS_ROOT/platform:/meta/" + Activator.PLUGIN_ID + "/" + href;\r
+// String href = htmlFile.toUri().toString();\r
+ topic.setHref(href.replace("\\", "/").replace(" ", "%20"));\r
+ }\r
+ }\r
+ }\r
+ \r
+ for (Toc toc : tocs.values()) {\r
+ TocContribution contribution = new TocContribution();\r
+ contribution.setLocale(locale);\r
+ contribution.setId(toc.getLabel());\r
+ contribution.setCategoryId("category_" + toc.getLabel());\r
+ contribution.setPrimary(true);\r
+ contribution.setContributorId(Activator.PLUGIN_ID);\r
+ \r
+ contribution.setToc(toc);\r
+ contributions.add(contribution);\r
+ }\r
+\r
+ \r
+ return (ITocContribution[])contributions.toArray(new ITocContribution[contributions.size()]);\r
+ }\r
+ \r
+ private static Topic getOrCreateTopic(Map<String, Topic> topics, Toc toc, Path topicPath) {\r
+ String topicName = topicPath.getFileName().toString();\r
+ Topic topic = topics.get(topicName);\r
+ if (topic == null) {\r
+ topic = new Topic();\r
+ topic.setLabel(topicName);\r
+ Path parentPath = topicPath.getParent();\r
+ if (parentPath != null && parentPath.getFileName() != null) {\r
+ Topic parentTopic = getOrCreateTopic(topics, toc, parentPath);\r
+ parentTopic.appendChild(topic);\r
+ } else {\r
+ toc.appendChild(topic);\r
+ }\r
+ topics.put(topicName, topic);\r
+ }\r
+ return topic;\r
+ }\r
+ \r
+ private static Document getDocument() {\r
+ if (document == null) {\r
+ if (builder == null) {\r
+ try {\r
+ builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();\r
+ builder.setEntityResolver(new LocalEntityResolver());\r
+ }\r
+ catch (ParserConfigurationException e) {\r
+ String msg = "Error creating document builder"; //$NON-NLS-1$\r
+ HelpPlugin.logError(msg, e);\r
+ }\r
+ }\r
+ document = builder.newDocument();\r
+ }\r
+ return document;\r
+ }\r
+ \r
+ public static void clearTocCache() {\r
+ widgetReferences.clear();\r
+ widgetReferences = null;\r
+ tutorials.clear();\r
+ tutorials = null;\r
+ HelpPlugin.getTocManager().clearCache();\r
+ }\r
+}\r