source: josm/trunk/src/org/openstreetmap/josm/tools/WikiReader.java@ 13060

Last change on this file since 13060 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

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