[298] | 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
[155] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
[2017] | 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[155] | 6 | import java.io.BufferedReader;
|
---|
| 7 | import java.io.IOException;
|
---|
[1879] | 8 | import java.io.InputStream;
|
---|
[155] | 9 | import java.io.InputStreamReader;
|
---|
| 10 | import java.net.URL;
|
---|
| 11 |
|
---|
[1512] | 12 | import org.openstreetmap.josm.Main;
|
---|
| 13 |
|
---|
[155] | 14 | /**
|
---|
| 15 | * Read a trac-wiki page.
|
---|
[1879] | 16 | *
|
---|
[155] | 17 | * @author imi
|
---|
| 18 | */
|
---|
| 19 | public class WikiReader {
|
---|
| 20 |
|
---|
[1169] | 21 | private final String baseurl;
|
---|
[155] | 22 |
|
---|
[1169] | 23 | public WikiReader(String baseurl) {
|
---|
| 24 | this.baseurl = baseurl;
|
---|
[155] | 25 | }
|
---|
| 26 |
|
---|
[1512] | 27 | public WikiReader() {
|
---|
| 28 | this.baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[1169] | 31 | /**
|
---|
| 32 | * Read the page specified by the url and return the content.
|
---|
[1879] | 33 | *
|
---|
| 34 | * If the url is within the baseurl path, parse it as an trac wikipage and replace relative
|
---|
| 35 | * pathes etc..
|
---|
| 36 | *
|
---|
[1169] | 37 | * @return Either the string of the content of the wiki page.
|
---|
| 38 | * @throws IOException Throws, if the page could not be loaded.
|
---|
| 39 | */
|
---|
| 40 | public String read(String url) throws IOException {
|
---|
[740] | 41 | BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8"));
|
---|
[1290] | 42 | if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
|
---|
[1512] | 43 | return readFromTrac(in);
|
---|
[167] | 44 | return readNormal(in);
|
---|
[1169] | 45 | }
|
---|
[155] | 46 |
|
---|
[1512] | 47 | public String readLang(String text) {
|
---|
[1755] | 48 | String languageCode = LanguageInfo.getLanguageCodeWiki();
|
---|
[1879] | 49 | String url = baseurl + "/wiki/" + languageCode + text;
|
---|
[1512] | 50 | String res = "";
|
---|
[1879] | 51 | InputStream in = null;
|
---|
[1512] | 52 | try {
|
---|
[1879] | 53 | in = new URL(url).openStream();
|
---|
| 54 | res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
|
---|
| 55 | } catch (IOException ioe) {
|
---|
| 56 | System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe
|
---|
| 57 | .toString()));
|
---|
| 58 | } catch(SecurityException e) {
|
---|
| 59 | System.out.println(tr(
|
---|
| 60 | "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e
|
---|
| 61 | .toString()));
|
---|
| 62 | } finally {
|
---|
| 63 | if (in != null) {
|
---|
| 64 | try {
|
---|
| 65 | in.close();
|
---|
| 66 | } catch (IOException e) {
|
---|
| 67 | }
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | if (res.length() == 0 && languageCode.length() != 0) {
|
---|
| 71 | url = baseurl + "/wiki/" + text;
|
---|
[1512] | 72 | try {
|
---|
[1879] | 73 | in = new URL(url).openStream();
|
---|
| 74 | } catch (IOException e) {
|
---|
| 75 | System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, e
|
---|
| 76 | .toString()));
|
---|
| 77 | return res;
|
---|
| 78 | } catch (SecurityException e) {
|
---|
| 79 | System.out.println(tr(
|
---|
| 80 | "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e
|
---|
| 81 | .toString()));
|
---|
| 82 | return res;
|
---|
| 83 | }
|
---|
| 84 | try {
|
---|
| 85 | res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
|
---|
| 86 | } catch (IOException ioe) {
|
---|
| 87 | System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe
|
---|
| 88 | .toString()));
|
---|
| 89 | return res;
|
---|
| 90 | } finally {
|
---|
| 91 | if (in != null) {
|
---|
| 92 | try {
|
---|
| 93 | in.close();
|
---|
| 94 | } catch (IOException e) {
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
[1512] | 98 | }
|
---|
| 99 | return res;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[1169] | 102 | private String readNormal(BufferedReader in) throws IOException {
|
---|
[1480] | 103 | String b = "";
|
---|
[1169] | 104 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
[1879] | 105 | if (!line.contains("[[TranslatedPages]]")) {
|
---|
[1480] | 106 | b += line.replaceAll(" />", ">") + "\n";
|
---|
[1879] | 107 | }
|
---|
[1169] | 108 | }
|
---|
[1480] | 109 | return "<html>" + b + "</html>";
|
---|
[155] | 110 | }
|
---|
| 111 |
|
---|
[1512] | 112 | private String readFromTrac(BufferedReader in) throws IOException {
|
---|
[155] | 113 | boolean inside = false;
|
---|
[1483] | 114 | boolean transl = false;
|
---|
[1480] | 115 | String b = "";
|
---|
[155] | 116 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
[1879] | 117 | if (line.contains("<div id=\"searchable\">")) {
|
---|
[1169] | 118 | inside = true;
|
---|
[1879] | 119 | } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
|
---|
[1483] | 120 | transl = true;
|
---|
[1879] | 121 | } else if (line.contains("<div class=\"wikipage searchable\">")) {
|
---|
[1426] | 122 | inside = true;
|
---|
[1879] | 123 | } else if (line.contains("<div class=\"buttons\">")) {
|
---|
[1169] | 124 | inside = false;
|
---|
[1879] | 125 | }
|
---|
[1483] | 126 | if (inside && !transl) {
|
---|
[1879] | 127 | b += line.replaceAll("<img src=\"/", "<img src=\"" + baseurl + "/").replaceAll("href=\"/",
|
---|
| 128 | "href=\"" + baseurl + "/").replaceAll(" />", ">")
|
---|
| 129 | + "\n";
|
---|
| 130 | } else if (transl && line.contains("</div>")) {
|
---|
| 131 | transl = false;
|
---|
[1169] | 132 | }
|
---|
[155] | 133 | }
|
---|
[1879] | 134 | if (b.indexOf(" Describe ") >= 0)
|
---|
[1512] | 135 | return "";
|
---|
[1480] | 136 | return "<html>" + b + "</html>";
|
---|
[155] | 137 | }
|
---|
| 138 | }
|
---|