001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.tools;
003
004import java.io.BufferedReader;
005import java.io.IOException;
006import java.net.URL;
007
008import org.openstreetmap.josm.Main;
009import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
010
011/**
012 * Read a trac-wiki page.
013 *
014 * @author imi
015 */
016public class WikiReader {
017
018    private final String baseurl;
019
020    public WikiReader(String baseurl) {
021        this.baseurl = baseurl;
022    }
023
024    /**
025     * Constructs a new {@code WikiReader}.
026     */
027    public WikiReader() {
028        this.baseurl = Main.pref.get("help.baseurl", Main.JOSM_WEBSITE);
029    }
030
031    /**
032     * Read the page specified by the url and return the content.
033     *
034     * If the url is within the baseurl path, parse it as an trac wikipage and replace relative
035     * pathes etc..
036     *
037     * @throws IOException Throws, if the page could not be loaded.
038     */
039    public String read(String url) throws IOException {
040        URL u = new URL(url);
041        BufferedReader in = Utils.openURLReader(u);
042        try {
043            if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
044                return readFromTrac(in, u);
045            return readNormal(in);
046        } finally {
047            Utils.close(in);
048        }
049    }
050
051    public String readLang(String text) throws IOException {
052        String languageCode;
053        String res = "";
054
055        languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
056        if(languageCode != null) {
057            res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
058        }
059
060        if(res.isEmpty()) {
061            languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
062            if(languageCode != null) {
063                res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
064            }
065        }
066
067        if(res.isEmpty()) {
068            languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
069            if(languageCode != null) {
070                res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
071            }
072        }
073
074        if(res.isEmpty()) {
075            throw new IOException(text + " does not exist");
076        } else {
077            return res;
078        }
079    }
080
081    private String readLang(URL url) throws IOException {
082        BufferedReader in = Utils.openURLReader(url);
083        try {
084            return readFromTrac(in, url);
085        } finally {
086            Utils.close(in);
087        }
088    }
089
090    private String readNormal(BufferedReader in) throws IOException {
091        StringBuilder b = new StringBuilder();
092        for (String line = in.readLine(); line != null; line = in.readLine()) {
093            if (!line.contains("[[TranslatedPages]]")) {
094                b.append(line.replaceAll(" />", ">")).append("\n");
095            }
096        }
097        return "<html>" + b + "</html>";
098    }
099
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}