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

Revision 5061, 4.0 KB checked in by simon04, 2 months ago (diff)

see #7395 - don't cache erroneously downloaded MOTD

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