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.net.MalformedURLException;
|
---|
7 | import java.net.URL;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
10 | import org.openstreetmap.josm.tools.WikiReader;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Reads help content from the JOSM Wiki and prepares it for rendering in the internal
|
---|
14 | * help browser.
|
---|
15 | *
|
---|
16 | * The help content has to be <strong>filtered</strong> because only the main content <code><div></code>
|
---|
17 | * of a Wiki help page is displayed in the internal help browser.
|
---|
18 | *
|
---|
19 | * It also has to be <strong>transformed</strong> because the internal help browser required slightly
|
---|
20 | * different HTML than what is provided by the Wiki.
|
---|
21 | */
|
---|
22 | public class HelpContentReader extends WikiReader {
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Constructs a new {@code HelpContentReader}.
|
---|
26 | *
|
---|
27 | * @param baseUrl the base url of the JOSM help wiki, i.e. https://josm.openstreetmap.org
|
---|
28 | */
|
---|
29 | public HelpContentReader(String baseUrl) {
|
---|
30 | super(baseUrl);
|
---|
31 | }
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Fetches the content of a help topic from the JOSM wiki.
|
---|
35 | *
|
---|
36 | * @param helpTopicUrl the absolute help topic URL
|
---|
37 | * @param dotest if {@code true}, checks if help content is empty
|
---|
38 | * @return the content, filtered and transformed for being displayed in the internal help browser
|
---|
39 | * @throws HelpContentReaderException if problem occurs
|
---|
40 | * @throws MissingHelpContentException if this helpTopicUrl doesn't point to an existing Wiki help page
|
---|
41 | */
|
---|
42 | public String fetchHelpTopicContent(String helpTopicUrl, boolean dotest) throws HelpContentReaderException {
|
---|
43 | if (helpTopicUrl == null)
|
---|
44 | throw new MissingHelpContentException("helpTopicUrl is null");
|
---|
45 | HttpClient.Response con = null;
|
---|
46 | try {
|
---|
47 | URL u = new URL(helpTopicUrl);
|
---|
48 | con = HttpClient.create(u).connect();
|
---|
49 | try (BufferedReader in = con.getContentReader()) {
|
---|
50 | return prepareHelpContent(in, dotest, u);
|
---|
51 | }
|
---|
52 | } catch (MalformedURLException e) {
|
---|
53 | throw new HelpContentReaderException(e, 0);
|
---|
54 | } catch (IOException e) {
|
---|
55 | throw new HelpContentReaderException(e, con != null ? con.getResponseCode() : 0);
|
---|
56 | }
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Reads help content from the input stream and prepares it to be rendered later
|
---|
61 | * in the internal help browser.
|
---|
62 | *
|
---|
63 | * Throws a {@link MissingHelpContentException} if the content read from the stream
|
---|
64 | * most likely represents a stub help page.
|
---|
65 | *
|
---|
66 | * @param in the input stream
|
---|
67 | * @param dotest if {@code true}, checks if help content is empty
|
---|
68 | * @param url help topic URL
|
---|
69 | * @return the content
|
---|
70 | * @throws HelpContentReaderException if an exception occurs
|
---|
71 | * @throws MissingHelpContentException if the content read isn't a help page
|
---|
72 | * @since 5936
|
---|
73 | */
|
---|
74 | protected String prepareHelpContent(BufferedReader in, boolean dotest, URL url) throws HelpContentReaderException {
|
---|
75 | String s = "";
|
---|
76 | try {
|
---|
77 | s = readFromTrac(in, url);
|
---|
78 | } catch (IOException e) {
|
---|
79 | throw new HelpContentReaderException(e, 0);
|
---|
80 | }
|
---|
81 | if (dotest && s.isEmpty())
|
---|
82 | throw new MissingHelpContentException(s);
|
---|
83 | return s;
|
---|
84 | }
|
---|
85 | }
|
---|