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

Last change on this file since 7423 was 7401, checked in by Don-vip, 10 years ago

fix #10175 - ask for JOSM update in case of crash with old version

  • Property svn:eol-style set to native
File size: 5.3 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.tools.LanguageInfo.LocaleType;
10
11/**
12 * Read a trac-wiki page.
13 *
14 * @author imi
15 */
16public 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.baseurl = Main.pref.get("help.baseurl", Main.getJOSMWebsite());
33 }
34
35 /**
36 * Read the page specified by the url and return the content.
37 *
38 * If the url is within the baseurl path, parse it as an trac wikipage and replace relative paths etc..
39 * @param url the URL to read
40 * @return The page as string
41 *
42 * @throws IOException Throws, if the page could not be loaded.
43 */
44 public String read(String url) throws IOException {
45 URL u = new URL(url);
46 try (BufferedReader in = Utils.openURLReader(u)) {
47 boolean txt = url.endsWith("?format=txt");
48 if (url.startsWith(baseurl) && !txt)
49 return readFromTrac(in, u);
50 return readNormal(in, !txt);
51 }
52 }
53
54 /**
55 * Reads the localized version of the given wiki page.
56 * @param text The page title, without locale prefix
57 * @return the localized version of the given wiki page
58 * @throws IOException if any I/O error occurs
59 */
60 public String readLang(String text) throws IOException {
61 String languageCode;
62 String res = "";
63
64 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
65 if(languageCode != null) {
66 res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
67 }
68
69 if(res.isEmpty()) {
70 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
71 if(languageCode != null) {
72 res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
73 }
74 }
75
76 if(res.isEmpty()) {
77 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
78 if(languageCode != null) {
79 res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
80 }
81 }
82
83 if(res.isEmpty()) {
84 throw new IOException(text + " does not exist");
85 } else {
86 return res;
87 }
88 }
89
90 private String readLang(URL url) throws IOException {
91 try (BufferedReader in = Utils.openURLReader(url)) {
92 return readFromTrac(in, url);
93 } catch (IOException e) {
94 Main.addNetworkError(url, Utils.getRootCause(e));
95 throw e;
96 }
97 }
98
99 private String readNormal(BufferedReader in, boolean html) throws IOException {
100 StringBuilder b = new StringBuilder();
101 for (String line = in.readLine(); line != null; line = in.readLine()) {
102 if (!line.contains("[[TranslatedPages]]")) {
103 b.append(line.replaceAll(" />", ">")).append("\n");
104 }
105 }
106 return html ? "<html>" + b + "</html>" : b.toString();
107 }
108
109 protected String readFromTrac(BufferedReader in, URL url) throws IOException {
110 boolean inside = false;
111 boolean transl = false;
112 boolean skip = false;
113 String b = "";
114 String full = "";
115 for (String line = in.readLine(); line != null; line = in.readLine()) {
116 full += line;
117 if (line.contains("<div id=\"searchable\">")) {
118 inside = true;
119 } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
120 transl = true;
121 } else if (line.contains("<div class=\"wikipage searchable\">")) {
122 inside = true;
123 } else if (line.contains("<div class=\"buttons\">")) {
124 inside = false;
125 } else if (line.contains("<h3>Attachments</h3>")) {
126 inside = false;
127 } else if (line.contains("<div id=\"attachments\">")) {
128 inside = false;
129 } else if (line.contains("<div class=\"trac-modifiedby\">")) {
130 skip = true;
131 }
132 if (inside && !transl && !skip) {
133 // add a border="0" attribute to images, otherwise the internal help browser
134 // will render a thick border around images inside an <a> element
135 b += line.replaceAll("<img ", "<img border=\"0\" ")
136 .replaceAll("<span class=\"icon\">.</span>", "")
137 .replaceAll("href=\"/", "href=\"" + baseurl + "/")
138 .replaceAll(" />", ">")
139 + "\n";
140 } else if (transl && line.contains("</div>")) {
141 transl = false;
142 }
143 if (line.contains("</div>")) {
144 skip = false;
145 }
146 }
147 if (b.indexOf(" Describe ") >= 0
148 || b.indexOf(" does not exist. You can create it here.</p>") >= 0)
149 return "";
150 if(b.isEmpty())
151 b = full;
152 return "<html><base href=\""+url.toExternalForm() +"\"> " + b + "</html>";
153 }
154}
Note: See TracBrowser for help on using the repository browser.