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