BrightSide Workbench Full Report + Source Code
HelpContext.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2020 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.help;
20 
21 import java.io.IOException;
22 import java.nio.file.DirectoryStream;
23 import java.nio.file.Files;
24 import java.nio.file.Path;
25 import java.nio.file.Paths;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28 import org.turro.string.Strings;
29 import org.turro.elephant.context.ElephantContext;
30 import org.turro.elephant.context.ElephantProperties;
31 import org.turro.elephant.web.ElContext;
32 import org.turro.elephant.web.ElContextMap;
33 
38 public class HelpContext {
39 
40  public static String helpLink(String current) {
41  ElContext ctx = help(current);
42  if(ctx != null && ctx.getFile().exists()) {
43  String webPath = ctx.getWebPath();
44  if(webPath.startsWith("/")) {
45  try(DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(ElephantContext.getRealPath(ctx.getPath())), "*.html")) {
46  if(stream.iterator().hasNext()) {
47  return webPath;
48  }
49  } catch (IOException ex) {
50  Logger.getLogger(HelpContext.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(current), ex);
51  }
52  } else {
53  return webPath;
54  }
55  }
56  return null;
57  }
58 
59  public static String helpLink(ElContext current) {
60  return current != null ? helpLink(current.getPath()) : null;
61  }
62 
63  public static ElContext help(String path) {
64  return !Strings.isBlank(path) ? ElContextMap.getContext(getHelpContextAttribute("root", "/docs/contextual") + path) : null;
65  }
66 
67  public static ElContext help(ElContext current) {
68  return current != null ? ElContextMap.getContext(getHelpContextAttribute("root", "/docs/contextual") + current.getPath()) : null;
69  }
70 
71  public static boolean exists(ElContext current) {
72  return help(current)!= null;
73  }
74 
75  /* Help context */
76 
77  private static final String HELP_CONTEXT = "help-context";
78 
79  public static String getHelpContextAttribute(String attribute, String defaultValue) {
80  return ElephantProperties.getContextProperty(HELP_CONTEXT, attribute, defaultValue);
81  }
82 
83 }
static String getContextProperty(String context, String property)
static ElContext getContext(IConstructor constructor)
static ElContext help(ElContext current)
static String helpLink(String current)
static ElContext help(String path)
static String getHelpContextAttribute(String attribute, String defaultValue)
static boolean exists(ElContext current)
static String helpLink(ElContext current)