1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.tools;
|
---|
3 |
|
---|
4 | import java.io.BufferedReader;
|
---|
5 | import java.io.IOException;
|
---|
6 | import java.io.InputStream;
|
---|
7 | import java.io.InputStreamReader;
|
---|
8 | import java.net.URL;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Read a trac-wiki page.
|
---|
14 | *
|
---|
15 | * @author imi
|
---|
16 | */
|
---|
17 | public class WikiReader {
|
---|
18 |
|
---|
19 | private final String baseurl;
|
---|
20 |
|
---|
21 | public WikiReader(String baseurl) {
|
---|
22 | this.baseurl = baseurl;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public WikiReader() {
|
---|
26 | this.baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Read the page specified by the url and return the content.
|
---|
31 | *
|
---|
32 | * If the url is within the baseurl path, parse it as an trac wikipage and replace relative
|
---|
33 | * pathes etc..
|
---|
34 | *
|
---|
35 | * @throws IOException Throws, if the page could not be loaded.
|
---|
36 | */
|
---|
37 | public String read(String url) throws IOException {
|
---|
38 | BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8"));
|
---|
39 | if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
|
---|
40 | return readFromTrac(in);
|
---|
41 | return readNormal(in);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public String readLang(String text) throws IOException {
|
---|
45 | String languageCode = LanguageInfo.getWikiLanguagePrefix();
|
---|
46 | String res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
|
---|
47 | if (res.isEmpty() && !languageCode.isEmpty()) {
|
---|
48 | res = readLang(new URL(baseurl + "/wiki/" + text));
|
---|
49 | }
|
---|
50 | if (res.isEmpty()) {
|
---|
51 | throw new IOException(text + " does not exist");
|
---|
52 | } else {
|
---|
53 | return res;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | private String readLang(URL url) throws IOException {
|
---|
58 | InputStream in = url.openStream();
|
---|
59 | return readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
|
---|
60 | }
|
---|
61 |
|
---|
62 | private String readNormal(BufferedReader in) throws IOException {
|
---|
63 | String b = "";
|
---|
64 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
65 | if (!line.contains("[[TranslatedPages]]")) {
|
---|
66 | b += line.replaceAll(" />", ">") + "\n";
|
---|
67 | }
|
---|
68 | }
|
---|
69 | return "<html>" + b + "</html>";
|
---|
70 | }
|
---|
71 |
|
---|
72 | private String readFromTrac(BufferedReader in) throws IOException {
|
---|
73 | boolean inside = false;
|
---|
74 | boolean transl = false;
|
---|
75 | boolean skip = false;
|
---|
76 | String b = "";
|
---|
77 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
78 | if (line.contains("<div id=\"searchable\">")) {
|
---|
79 | inside = true;
|
---|
80 | } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
|
---|
81 | transl = true;
|
---|
82 | } else if (line.contains("<div class=\"wikipage searchable\">")) {
|
---|
83 | inside = true;
|
---|
84 | } else if (line.contains("<div class=\"buttons\">")) {
|
---|
85 | inside = false;
|
---|
86 | } else if (line.contains("<h3>Attachments</h3>")) {
|
---|
87 | inside = false;
|
---|
88 | } else if (line.contains("<div id=\"attachments\">")) {
|
---|
89 | inside = false;
|
---|
90 | } else if (line.contains("<div class=\"trac-modifiedby\">")) {
|
---|
91 | skip = true;
|
---|
92 | }
|
---|
93 | if (inside && !transl && !skip) {
|
---|
94 | // add a border="0" attribute to images, otherwise the internal help browser
|
---|
95 | // will render a thick border around images inside an <a> element
|
---|
96 | //
|
---|
97 | b += line.replaceAll("<img src=\"/", "<img border=\"0\" src=\"" + baseurl + "/").replaceAll("href=\"/",
|
---|
98 | "href=\"" + baseurl + "/").replaceAll(" />", ">")
|
---|
99 | + "\n";
|
---|
100 | } else if (transl && line.contains("</div>")) {
|
---|
101 | transl = false;
|
---|
102 | }
|
---|
103 | if (line.contains("</div>")) {
|
---|
104 | skip = false;
|
---|
105 | }
|
---|
106 | }
|
---|
107 | if (b.indexOf(" Describe ") >= 0
|
---|
108 | || b.indexOf(" does not exist. You can create it here.</p>") >= 0)
|
---|
109 | return "";
|
---|
110 | return "<html>" + b + "</html>";
|
---|
111 | }
|
---|
112 | }
|
---|