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

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

Improve handling of network errors at startup, suggest to change proxy settings

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