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

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

see #8465 - global use of try-with-resources, according to

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