1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.net.URL;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 | import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Read a trac-wiki page.
|
---|
13 | *
|
---|
14 | * @author imi
|
---|
15 | */
|
---|
16 | public class WikiReader {
|
---|
17 |
|
---|
18 | private final String baseurl;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Constructs a new {@code WikiReader} for the given base URL.
|
---|
22 | * @param baseurl The wiki base URL
|
---|
23 | */
|
---|
24 | public WikiReader(String baseurl) {
|
---|
25 | this.baseurl = baseurl;
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new {@code WikiReader}.
|
---|
30 | */
|
---|
31 | public WikiReader() {
|
---|
32 | this(Main.pref.get("help.baseurl", Main.getJOSMWebsite()));
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Returns the base URL of wiki.
|
---|
37 | * @return the base URL of wiki
|
---|
38 | * @since 7434
|
---|
39 | */
|
---|
40 | public final String getBaseUrlWiki() {
|
---|
41 | return baseurl + "/wiki/";
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Read the page specified by the url and return the content.
|
---|
46 | *
|
---|
47 | * If the url is within the baseurl path, parse it as an trac wikipage and replace relative paths etc..
|
---|
48 | * @param url the URL to read
|
---|
49 | * @return The page as string
|
---|
50 | *
|
---|
51 | * @throws IOException Throws, if the page could not be loaded.
|
---|
52 | */
|
---|
53 | public String read(String url) throws IOException {
|
---|
54 | URL u = new URL(url);
|
---|
55 | try (BufferedReader in = HttpClient.create(u).connect().getContentReader()) {
|
---|
56 | boolean txt = url.endsWith("?format=txt");
|
---|
57 | if (url.startsWith(getBaseUrlWiki()) && !txt)
|
---|
58 | return readFromTrac(in, u);
|
---|
59 | return readNormal(in, !txt);
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Reads the localized version of the given wiki page.
|
---|
65 | * @param text The page title, without locale prefix
|
---|
66 | * @return the localized version of the given wiki page
|
---|
67 | * @throws IOException if any I/O error occurs
|
---|
68 | */
|
---|
69 | public String readLang(String text) throws IOException {
|
---|
70 | String languageCode;
|
---|
71 | String res = "";
|
---|
72 |
|
---|
73 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
|
---|
74 | if (languageCode != null) {
|
---|
75 | res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (res.isEmpty()) {
|
---|
79 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
|
---|
80 | if (languageCode != null) {
|
---|
81 | res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | if (res.isEmpty()) {
|
---|
86 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
|
---|
87 | if (languageCode != null) {
|
---|
88 | res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (res.isEmpty()) {
|
---|
93 | throw new IOException(text + " does not exist");
|
---|
94 | } else {
|
---|
95 | return res;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | private String readLang(URL url) throws IOException {
|
---|
100 | try (BufferedReader in = HttpClient.create(url).connect().getContentReader()) {
|
---|
101 | return readFromTrac(in, url);
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | private static String readNormal(BufferedReader in, boolean html) throws IOException {
|
---|
106 | StringBuilder b = new StringBuilder();
|
---|
107 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
108 | if (!line.contains("[[TranslatedPages]]")) {
|
---|
109 | b.append(line.replaceAll(" />", ">")).append('\n');
|
---|
110 | }
|
---|
111 | }
|
---|
112 | return html ? "<html>" + b + "</html>" : b.toString();
|
---|
113 | }
|
---|
114 |
|
---|
115 | protected String readFromTrac(BufferedReader in, URL url) throws IOException {
|
---|
116 | boolean inside = false;
|
---|
117 | boolean transl = false;
|
---|
118 | boolean skip = false;
|
---|
119 | StringBuilder b = new StringBuilder();
|
---|
120 | StringBuilder full = new StringBuilder();
|
---|
121 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
122 | full.append(line);
|
---|
123 | if (line.contains("<div id=\"searchable\">")) {
|
---|
124 | inside = true;
|
---|
125 | } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
|
---|
126 | transl = true;
|
---|
127 | } else if (line.contains("<div class=\"wikipage searchable\">")) {
|
---|
128 | inside = true;
|
---|
129 | } else if (line.contains("<div class=\"buttons\">")) {
|
---|
130 | inside = false;
|
---|
131 | } else if (line.contains("<h3>Attachments</h3>")) {
|
---|
132 | inside = false;
|
---|
133 | } else if (line.contains("<div id=\"attachments\">")) {
|
---|
134 | inside = false;
|
---|
135 | } else if (line.contains("<div class=\"trac-modifiedby\">")) {
|
---|
136 | skip = true;
|
---|
137 | }
|
---|
138 | if (inside && !transl && !skip) {
|
---|
139 | // add a border="0" attribute to images, otherwise the internal help browser
|
---|
140 | // will render a thick border around images inside an <a> element
|
---|
141 | // remove width information to avoid distorded images (fix #11262)
|
---|
142 | b.append(line.replaceAll("<img ", "<img border=\"0\" ")
|
---|
143 | .replaceAll("width=\"(\\d+)\"", "")
|
---|
144 | .replaceAll("<span class=\"icon\">.</span>", "")
|
---|
145 | .replaceAll("href=\"/", "href=\"" + baseurl + '/')
|
---|
146 | .replaceAll(" />", ">"))
|
---|
147 | .append('\n');
|
---|
148 | } else if (transl && line.contains("</div>")) {
|
---|
149 | transl = false;
|
---|
150 | }
|
---|
151 | if (line.contains("</div>")) {
|
---|
152 | skip = false;
|
---|
153 | }
|
---|
154 | }
|
---|
155 | if (b.indexOf(" Describe ") >= 0
|
---|
156 | || b.indexOf(" does not exist. You can create it here.</p>") >= 0)
|
---|
157 | return "";
|
---|
158 | if (b.length() == 0)
|
---|
159 | b = full;
|
---|
160 | return "<html><base href=\""+url.toExternalForm() +"\"> " + b + "</html>";
|
---|
161 | }
|
---|
162 | }
|
---|