package com.onaro.sanscreen.client.help; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.help.HelpSetException; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * This class provides help context IDs based on a help module * @author rnoel * */ public class HelpContextProvider implements IHelpContextProvider { private static final String SANSCREEN_CONTEXT_MAPPING_FILE_PATH = "contexts.properties"; //$NON-NLS-1$ private static Logger logger = LogManager.getLogger(HelpContextProvider.class); /* * The help tools that IE uses generate non-human readable IDs. So we have a separate mapping file that will * map our context IDs (human readable, dot notation to establish a hierarchy) to theirs. */ private Properties contextsToTopicIds; /** * Constructor. * * @param loader the class loader we'll use to find the help resources * @param path the path to the root of the help directory containing the HelpSet */ public HelpContextProvider(ClassLoader loader, String helpPath) throws HelpSetException { contextsToTopicIds = new Properties(); // Handle master help set that is located at the root where we don't want to add the // "/" to the path. String path = (StringUtils.isEmpty(helpPath) ? StringUtils.EMPTY : helpPath + "/"); //$NON-NLS-1$ String propsFilePath = path + SANSCREEN_CONTEXT_MAPPING_FILE_PATH; InputStream inStream = loader.getResourceAsStream(propsFilePath); Exception e = null; if(inStream == null) { throw new HelpSetException("Context mapping file '" + propsFilePath + "' not found."); //$NON-NLS-1$ //$NON-NLS-2$ } try { contextsToTopicIds.load(inStream); } catch(IOException ioe) { e = ioe; } try { inStream.close(); } catch(IOException ioe) {} if(e != null) { String message = e.getMessage(); logger.error(message); throw new HelpSetException(message); } } /** * Returns the topic ID associated with the context ID or null if the context ID could not be found or was empty. * * @param contextId the context ID * @return the topic ID associated with the context ID or null if the context ID could not be found or was empty. */ public String getTopicId(String contextId) { String topicId = contextsToTopicIds.getProperty(contextId); return(StringUtils.isEmpty(topicId) ? null : topicId); } /** * Returns the closest topic ID associated with the context ID or null if the context ID could not be found or was empty. * * @param contextId the context ID * @return the closest topic ID associated with the context ID or null if the context ID could not be found or was empty. */ public String getClosestTopicId(String contextId) { while(StringUtils.isNotEmpty(contextId)) { String topicId = getTopicId(contextId); if(topicId != null) { return(topicId); } int dotNdx = contextId.lastIndexOf('.'); // If no more parts to strip off, return null if(dotNdx < 0) { break; } // Strip off last part of context and try again contextId = contextId.substring(0, dotNdx); } return(null); } }