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 | public WikiReader(String baseurl) {
|
---|
21 | this.baseurl = baseurl;
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Constructs a new {@code WikiReader}.
|
---|
26 | */
|
---|
27 | public WikiReader() {
|
---|
28 | this.baseurl = Main.pref.get("help.baseurl", Main.JOSM_WEBSITE);
|
---|
29 | }
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Read the page specified by the url and return the content.
|
---|
33 | *
|
---|
34 | * If the url is within the baseurl path, parse it as an trac wikipage and replace relative
|
---|
35 | * pathes etc..
|
---|
36 | *
|
---|
37 | * @throws IOException Throws, if the page could not be loaded.
|
---|
38 | */
|
---|
39 | public String read(String url) throws IOException {
|
---|
40 | URL u = new URL(url);
|
---|
41 | BufferedReader in = Utils.openURLReader(u);
|
---|
42 | try {
|
---|
43 | if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
|
---|
44 | return readFromTrac(in, u);
|
---|
45 | return readNormal(in);
|
---|
46 | } finally {
|
---|
47 | Utils.close(in);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | public String readLang(String text) throws IOException {
|
---|
52 | String languageCode;
|
---|
53 | String res = "";
|
---|
54 |
|
---|
55 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
|
---|
56 | if(languageCode != null) {
|
---|
57 | res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
|
---|
58 | }
|
---|
59 |
|
---|
60 | if(res.isEmpty()) {
|
---|
61 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
|
---|
62 | if(languageCode != null) {
|
---|
63 | res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | if(res.isEmpty()) {
|
---|
68 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
|
---|
69 | if(languageCode != null) {
|
---|
70 | res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | if(res.isEmpty()) {
|
---|
75 | throw new IOException(text + " does not exist");
|
---|
76 | } else {
|
---|
77 | return res;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | private String readLang(URL url) throws IOException {
|
---|
82 | BufferedReader in = Utils.openURLReader(url);
|
---|
83 | try {
|
---|
84 | return readFromTrac(in, url);
|
---|
85 | } finally {
|
---|
86 | Utils.close(in);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | private String readNormal(BufferedReader in) throws IOException {
|
---|
91 | StringBuilder b = new StringBuilder();
|
---|
92 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
93 | if (!line.contains("[[TranslatedPages]]")) {
|
---|
94 | b.append(line.replaceAll(" />", ">")).append("\n");
|
---|
95 | }
|
---|
96 | }
|
---|
97 | return "<html>" + b + "</html>";
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected String readFromTrac(BufferedReader in, URL url) throws IOException {
|
---|
101 | boolean inside = false;
|
---|
102 | boolean transl = false;
|
---|
103 | boolean skip = false;
|
---|
104 | String b = "";
|
---|
105 | String full = "";
|
---|
106 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
107 | full += line;
|
---|
108 | if (line.contains("<div id=\"searchable\">")) {
|
---|
109 | inside = true;
|
---|
110 | } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
|
---|
111 | transl = true;
|
---|
112 | } else if (line.contains("<div class=\"wikipage searchable\">")) {
|
---|
113 | inside = true;
|
---|
114 | } else if (line.contains("<div class=\"buttons\">")) {
|
---|
115 | inside = false;
|
---|
116 | } else if (line.contains("<h3>Attachments</h3>")) {
|
---|
117 | inside = false;
|
---|
118 | } else if (line.contains("<div id=\"attachments\">")) {
|
---|
119 | inside = false;
|
---|
120 | } else if (line.contains("<div class=\"trac-modifiedby\">")) {
|
---|
121 | skip = true;
|
---|
122 | }
|
---|
123 | if (inside && !transl && !skip) {
|
---|
124 | // add a border="0" attribute to images, otherwise the internal help browser
|
---|
125 | // will render a thick border around images inside an <a> element
|
---|
126 | //
|
---|
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 | }
|
---|