1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.help;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStreamReader;
|
---|
7 | import java.net.HttpURLConnection;
|
---|
8 | import java.net.MalformedURLException;
|
---|
9 | import java.net.URL;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.tools.Utils;
|
---|
12 | import org.openstreetmap.josm.tools.WikiReader;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Reads help content from the JOSM Wiki and prepares it for rendering in the internal
|
---|
16 | * help browser.
|
---|
17 | *
|
---|
18 | * The help content has to be <strong>filtered</strong> because only the main content <tt><div></tt>
|
---|
19 | * of a Wiki help page is displayed in the internal help browser.
|
---|
20 | *
|
---|
21 | * It also has to be <strong>transformed</strong> because the internal help browser required slightly
|
---|
22 | * different HTML than what is provided by the Wiki.
|
---|
23 | */
|
---|
24 | public class HelpContentReader extends WikiReader {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * constructor
|
---|
28 | *
|
---|
29 | * @param baseUrl the base url of the JOSM help wiki, i.e. http://josm.openstreetmap.org
|
---|
30 | */
|
---|
31 | public HelpContentReader(String baseUrl) {
|
---|
32 | super(baseUrl);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Fetches the content of a help topic from the JOSM wiki.
|
---|
37 | *
|
---|
38 | * @param helpTopicUrl the absolute help topic URL
|
---|
39 | * @return the content, filtered and transformed for being displayed in the internal help browser
|
---|
40 | * @throws HelpContentReaderException thrown if problem occurs
|
---|
41 | * @throws MissingHelpContentException thrown if this helpTopicUrl doesn't point to an existing Wiki help page
|
---|
42 | */
|
---|
43 | public String fetchHelpTopicContent(String helpTopicUrl, boolean dotest) throws HelpContentReaderException {
|
---|
44 | if(helpTopicUrl == null)
|
---|
45 | throw new MissingHelpContentException();
|
---|
46 | HttpURLConnection con = null;
|
---|
47 | BufferedReader in = null;
|
---|
48 | try {
|
---|
49 | URL u = new URL(helpTopicUrl);
|
---|
50 | con = Utils.openHttpConnection(u);
|
---|
51 | con.connect();
|
---|
52 | in = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf-8"));
|
---|
53 | return prepareHelpContent(in, dotest, u);
|
---|
54 | } catch(MalformedURLException e) {
|
---|
55 | throw new HelpContentReaderException(e);
|
---|
56 | } catch(IOException e) {
|
---|
57 | HelpContentReaderException ex = new HelpContentReaderException(e);
|
---|
58 | if (con != null) {
|
---|
59 | try {
|
---|
60 | ex.setResponseCode(con.getResponseCode());
|
---|
61 | } catch(IOException e1) {
|
---|
62 | // ignore
|
---|
63 | }
|
---|
64 | }
|
---|
65 | throw ex;
|
---|
66 | } finally {
|
---|
67 | Utils.close(in);
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Reads help content from the input stream and prepares it to be rendered later
|
---|
73 | * in the internal help browser.
|
---|
74 | *
|
---|
75 | * Throws a {@link MissingHelpContentException} if the content read from the stream
|
---|
76 | * most likely represents a stub help page.
|
---|
77 | *
|
---|
78 | * @param in the input stream
|
---|
79 | * @return the content
|
---|
80 | * @throws HelpContentReaderException thrown if an exception occurs
|
---|
81 | * @throws MissingHelpContentException thrown, if the content read isn't a help page
|
---|
82 | * @since 5936
|
---|
83 | */
|
---|
84 | protected String prepareHelpContent(BufferedReader in, boolean dotest, URL url) throws HelpContentReaderException {
|
---|
85 | String s = "";
|
---|
86 | try {
|
---|
87 | s = readFromTrac(in, url);
|
---|
88 | } catch(IOException e) {
|
---|
89 | throw new HelpContentReaderException(e);
|
---|
90 | }
|
---|
91 | if(dotest && s.isEmpty())
|
---|
92 | throw new MissingHelpContentException();
|
---|
93 | return s;
|
---|
94 | }
|
---|
95 | }
|
---|