001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.help; 003 004import java.io.BufferedReader; 005import java.io.IOException; 006import java.net.MalformedURLException; 007import java.net.URL; 008 009import org.openstreetmap.josm.tools.HttpClient; 010import org.openstreetmap.josm.tools.WikiReader; 011 012/** 013 * Reads help content from the JOSM Wiki and prepares it for rendering in the internal 014 * help browser. 015 * 016 * The help content has to be <strong>filtered</strong> because only the main content <tt><div></tt> 017 * of a Wiki help page is displayed in the internal help browser. 018 * 019 * It also has to be <strong>transformed</strong> because the internal help browser required slightly 020 * different HTML than what is provided by the Wiki. 021 */ 022public class HelpContentReader extends WikiReader { 023 024 /** 025 * Constructs a new {@code HelpContentReader}. 026 * 027 * @param baseUrl the base url of the JOSM help wiki, i.e. https://josm.openstreetmap.org 028 */ 029 public HelpContentReader(String baseUrl) { 030 super(baseUrl); 031 } 032 033 /** 034 * Fetches the content of a help topic from the JOSM wiki. 035 * 036 * @param helpTopicUrl the absolute help topic URL 037 * @param dotest if {@code true}, checks if help content is empty 038 * @return the content, filtered and transformed for being displayed in the internal help browser 039 * @throws HelpContentReaderException if problem occurs 040 * @throws MissingHelpContentException if this helpTopicUrl doesn't point to an existing Wiki help page 041 */ 042 public String fetchHelpTopicContent(String helpTopicUrl, boolean dotest) throws HelpContentReaderException { 043 if (helpTopicUrl == null) 044 throw new MissingHelpContentException("helpTopicUrl is null"); 045 HttpClient.Response con = null; 046 try { 047 URL u = new URL(helpTopicUrl); 048 con = HttpClient.create(u).connect(); 049 try (BufferedReader in = con.getContentReader()) { 050 return prepareHelpContent(in, dotest, u); 051 } 052 } catch (MalformedURLException e) { 053 throw new HelpContentReaderException(e); 054 } catch (IOException e) { 055 HelpContentReaderException ex = new HelpContentReaderException(e); 056 if (con != null) { 057 ex.setResponseCode(con.getResponseCode()); 058 } 059 throw ex; 060 } 061 } 062 063 /** 064 * Reads help content from the input stream and prepares it to be rendered later 065 * in the internal help browser. 066 * 067 * Throws a {@link MissingHelpContentException} if the content read from the stream 068 * most likely represents a stub help page. 069 * 070 * @param in the input stream 071 * @param dotest if {@code true}, checks if help content is empty 072 * @param url help topic URL 073 * @return the content 074 * @throws HelpContentReaderException if an exception occurs 075 * @throws MissingHelpContentException if the content read isn't a help page 076 * @since 5936 077 */ 078 protected String prepareHelpContent(BufferedReader in, boolean dotest, URL url) throws HelpContentReaderException { 079 String s = ""; 080 try { 081 s = readFromTrac(in, url); 082 } catch (IOException e) { 083 throw new HelpContentReaderException(e); 084 } 085 if (dotest && s.isEmpty()) 086 throw new MissingHelpContentException(s); 087 return s; 088 } 089}