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