[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[155] | 2 | package org.openstreetmap.josm.tools;
|
---|
| 3 |
|
---|
| 4 | import java.io.BufferedReader;
|
---|
| 5 | import java.io.IOException;
|
---|
| 6 | import java.net.URL;
|
---|
| 7 |
|
---|
[1512] | 8 | import org.openstreetmap.josm.Main;
|
---|
[12846] | 9 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
[5915] | 10 | import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
|
---|
[1512] | 11 |
|
---|
[155] | 12 | /**
|
---|
| 13 | * Read a trac-wiki page.
|
---|
[2512] | 14 | *
|
---|
[155] | 15 | * @author imi
|
---|
| 16 | */
|
---|
| 17 | public class WikiReader {
|
---|
| 18 |
|
---|
[1169] | 19 | private final String baseurl;
|
---|
[155] | 20 |
|
---|
[6642] | 21 | /**
|
---|
| 22 | * Constructs a new {@code WikiReader} for the given base URL.
|
---|
| 23 | * @param baseurl The wiki base URL
|
---|
| 24 | */
|
---|
[1169] | 25 | public WikiReader(String baseurl) {
|
---|
| 26 | this.baseurl = baseurl;
|
---|
[155] | 27 | }
|
---|
| 28 |
|
---|
[6264] | 29 | /**
|
---|
| 30 | * Constructs a new {@code WikiReader}.
|
---|
| 31 | */
|
---|
[1512] | 32 | public WikiReader() {
|
---|
[12846] | 33 | this(Config.getPref().get("help.baseurl", Main.getJOSMWebsite()));
|
---|
[1512] | 34 | }
|
---|
| 35 |
|
---|
[1169] | 36 | /**
|
---|
[7434] | 37 | * Returns the base URL of wiki.
|
---|
| 38 | * @return the base URL of wiki
|
---|
| 39 | * @since 7434
|
---|
| 40 | */
|
---|
| 41 | public final String getBaseUrlWiki() {
|
---|
| 42 | return baseurl + "/wiki/";
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | /**
|
---|
[1169] | 46 | * Read the page specified by the url and return the content.
|
---|
[2512] | 47 | *
|
---|
[7401] | 48 | * If the url is within the baseurl path, parse it as an trac wikipage and replace relative paths etc..
|
---|
| 49 | * @param url the URL to read
|
---|
| 50 | * @return The page as string
|
---|
[2512] | 51 | *
|
---|
[1169] | 52 | * @throws IOException Throws, if the page could not be loaded.
|
---|
| 53 | */
|
---|
| 54 | public String read(String url) throws IOException {
|
---|
[5936] | 55 | URL u = new URL(url);
|
---|
[9168] | 56 | try (BufferedReader in = HttpClient.create(u).connect().getContentReader()) {
|
---|
[7401] | 57 | boolean txt = url.endsWith("?format=txt");
|
---|
[7434] | 58 | if (url.startsWith(getBaseUrlWiki()) && !txt)
|
---|
[5936] | 59 | return readFromTrac(in, u);
|
---|
[7401] | 60 | return readNormal(in, !txt);
|
---|
[5834] | 61 | }
|
---|
[1169] | 62 | }
|
---|
[155] | 63 |
|
---|
[7401] | 64 | /**
|
---|
| 65 | * Reads the localized version of the given wiki page.
|
---|
| 66 | * @param text The page title, without locale prefix
|
---|
| 67 | * @return the localized version of the given wiki page
|
---|
| 68 | * @throws IOException if any I/O error occurs
|
---|
| 69 | */
|
---|
[5061] | 70 | public String readLang(String text) throws IOException {
|
---|
[5915] | 71 | String languageCode;
|
---|
| 72 | String res = "";
|
---|
| 73 |
|
---|
| 74 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
|
---|
[8510] | 75 | if (languageCode != null) {
|
---|
[7434] | 76 | res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
|
---|
[1879] | 77 | }
|
---|
[5915] | 78 |
|
---|
[8510] | 79 | if (res.isEmpty()) {
|
---|
[5915] | 80 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
|
---|
[8510] | 81 | if (languageCode != null) {
|
---|
[7434] | 82 | res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
|
---|
[5915] | 83 | }
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[8510] | 86 | if (res.isEmpty()) {
|
---|
[5915] | 87 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
|
---|
[8510] | 88 | if (languageCode != null) {
|
---|
[7434] | 89 | res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
|
---|
[5915] | 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[8510] | 93 | if (res.isEmpty()) {
|
---|
[5061] | 94 | throw new IOException(text + " does not exist");
|
---|
| 95 | } else {
|
---|
| 96 | return res;
|
---|
[1512] | 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[5061] | 100 | private String readLang(URL url) throws IOException {
|
---|
[9168] | 101 | try (BufferedReader in = HttpClient.create(url).connect().getContentReader()) {
|
---|
[7033] | 102 | return readFromTrac(in, url);
|
---|
[6642] | 103 | }
|
---|
[5061] | 104 | }
|
---|
| 105 |
|
---|
[8870] | 106 | private static String readNormal(BufferedReader in, boolean html) throws IOException {
|
---|
[6264] | 107 | StringBuilder b = new StringBuilder();
|
---|
[1169] | 108 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
[1879] | 109 | if (!line.contains("[[TranslatedPages]]")) {
|
---|
[8390] | 110 | b.append(line.replaceAll(" />", ">")).append('\n');
|
---|
[1879] | 111 | }
|
---|
[1169] | 112 | }
|
---|
[7401] | 113 | return html ? "<html>" + b + "</html>" : b.toString();
|
---|
[155] | 114 | }
|
---|
| 115 |
|
---|
[5936] | 116 | protected String readFromTrac(BufferedReader in, URL url) throws IOException {
|
---|
[155] | 117 | boolean inside = false;
|
---|
[1483] | 118 | boolean transl = false;
|
---|
[4915] | 119 | boolean skip = false;
|
---|
[8849] | 120 | StringBuilder b = new StringBuilder();
|
---|
| 121 | StringBuilder full = new StringBuilder();
|
---|
[155] | 122 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
[8849] | 123 | full.append(line);
|
---|
[1879] | 124 | if (line.contains("<div id=\"searchable\">")) {
|
---|
[1169] | 125 | inside = true;
|
---|
[1879] | 126 | } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
|
---|
[1483] | 127 | transl = true;
|
---|
[1879] | 128 | } else if (line.contains("<div class=\"wikipage searchable\">")) {
|
---|
[1426] | 129 | inside = true;
|
---|
[1879] | 130 | } else if (line.contains("<div class=\"buttons\">")) {
|
---|
[1169] | 131 | inside = false;
|
---|
[2257] | 132 | } else if (line.contains("<h3>Attachments</h3>")) {
|
---|
| 133 | inside = false;
|
---|
[3569] | 134 | } else if (line.contains("<div id=\"attachments\">")) {
|
---|
| 135 | inside = false;
|
---|
| 136 | } else if (line.contains("<div class=\"trac-modifiedby\">")) {
|
---|
[4915] | 137 | skip = true;
|
---|
[1879] | 138 | }
|
---|
[4915] | 139 | if (inside && !transl && !skip) {
|
---|
[2274] | 140 | // add a border="0" attribute to images, otherwise the internal help browser
|
---|
| 141 | // will render a thick border around images inside an <a> element
|
---|
[8933] | 142 | // remove width information to avoid distorded images (fix #11262)
|
---|
[8849] | 143 | b.append(line.replaceAll("<img ", "<img border=\"0\" ")
|
---|
[8933] | 144 | .replaceAll("width=\"(\\d+)\"", "")
|
---|
[5937] | 145 | .replaceAll("<span class=\"icon\">.</span>", "")
|
---|
[8846] | 146 | .replaceAll("href=\"/", "href=\"" + baseurl + '/')
|
---|
[8849] | 147 | .replaceAll(" />", ">"))
|
---|
| 148 | .append('\n');
|
---|
[1879] | 149 | } else if (transl && line.contains("</div>")) {
|
---|
| 150 | transl = false;
|
---|
[1169] | 151 | }
|
---|
[4915] | 152 | if (line.contains("</div>")) {
|
---|
| 153 | skip = false;
|
---|
| 154 | }
|
---|
[155] | 155 | }
|
---|
[3589] | 156 | if (b.indexOf(" Describe ") >= 0
|
---|
| 157 | || b.indexOf(" does not exist. You can create it here.</p>") >= 0)
|
---|
[1512] | 158 | return "";
|
---|
[8849] | 159 | if (b.length() == 0)
|
---|
[5937] | 160 | b = full;
|
---|
[5936] | 161 | return "<html><base href=\""+url.toExternalForm() +"\"> " + b + "</html>";
|
---|
[155] | 162 | }
|
---|
| 163 | }
|
---|