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; 007import java.util.stream.Collectors; 008 009import org.openstreetmap.josm.spi.preferences.Config; 010import org.openstreetmap.josm.tools.LanguageInfo.LocaleType; 011 012/** 013 * Read a trac-wiki page. 014 * 015 * @author imi 016 */ 017public class WikiReader { 018 019 private final String baseurl; 020 021 /** 022 * Constructs a new {@code WikiReader} for the given base URL. 023 * @param baseurl The wiki base URL 024 */ 025 public WikiReader(String baseurl) { 026 this.baseurl = baseurl; 027 } 028 029 /** 030 * Constructs a new {@code WikiReader}. 031 */ 032 public WikiReader() { 033 this(Config.getPref().get("help.baseurl", Config.getUrls().getJOSMWebsite())); 034 } 035 036 /** 037 * Returns the base URL of wiki. 038 * @return the base URL of wiki 039 * @since 7434 040 */ 041 public final String getBaseUrlWiki() { 042 return baseurl + "/wiki/"; 043 } 044 045 /** 046 * Read the page specified by the url and return the content. 047 * 048 * If the url is within the baseurl path, parse it as an trac wikipage and replace relative paths etc.. 049 * @param url the URL to read 050 * @return The page as string 051 * 052 * @throws IOException Throws, if the page could not be loaded. 053 */ 054 public String read(String url) throws IOException { 055 URL u = new URL(url); 056 try (BufferedReader in = HttpClient.create(u).connect().getContentReader()) { 057 boolean txt = url.endsWith("?format=txt"); 058 if (url.startsWith(getBaseUrlWiki()) && !txt) 059 return readFromTrac(in, u); 060 return readNormal(in, !txt); 061 } 062 } 063 064 /** 065 * Reads the localized version of the given wiki page. 066 * @param text The page title, without locale prefix 067 * @return the localized version of the given wiki page 068 * @throws IOException if any I/O error occurs 069 */ 070 public String readLang(String text) throws IOException { 071 String languageCode; 072 String res = ""; 073 074 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH); 075 if (languageCode != null) { 076 res = readLang(new URL(getBaseUrlWiki() + languageCode + text)); 077 } 078 079 if (res.isEmpty()) { 080 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE); 081 if (languageCode != null) { 082 res = readLang(new URL(getBaseUrlWiki() + languageCode + text)); 083 } 084 } 085 086 if (res.isEmpty()) { 087 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH); 088 if (languageCode != null) { 089 res = readLang(new URL(getBaseUrlWiki() + languageCode + text)); 090 } 091 } 092 093 if (res.isEmpty()) { 094 throw new IOException(text + " does not exist"); 095 } else { 096 return res; 097 } 098 } 099 100 private String readLang(URL url) throws IOException { 101 try (BufferedReader in = HttpClient.create(url).connect().getContentReader()) { 102 return readFromTrac(in, url); 103 } 104 } 105 106 private static String readNormal(BufferedReader in, boolean html) { 107 String string = in.lines() 108 .filter(line -> !line.contains("[[TranslatedPages]]")) 109 .map(line -> line.replace(" />", ">") + '\n').collect(Collectors.joining()); 110 return html ? "<html>" + string + "</html>" : string; 111 } 112 113 protected String readFromTrac(BufferedReader in, URL url) throws IOException { 114 boolean inside = false; 115 boolean transl = false; 116 boolean skip = false; 117 StringBuilder b = new StringBuilder(); 118 StringBuilder full = new StringBuilder(); 119 for (String line = in.readLine(); line != null; line = in.readLine()) { 120 full.append(line); 121 if (line.contains("<div id=\"searchable\">")) { 122 inside = true; 123 } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) { 124 transl = true; 125 } else if (line.contains("<div class=\"wikipage searchable\">")) { 126 inside = true; 127 } else if (line.contains("<div class=\"buttons\">")) { 128 inside = false; 129 } else if (line.contains("<h3>Attachments</h3>")) { 130 inside = false; 131 } else if (line.contains("<div id=\"attachments\">")) { 132 inside = false; 133 } else if (line.contains("<div class=\"trac-modifiedby\">")) { 134 skip = true; 135 } 136 if (inside && !transl && !skip) { 137 // add a border="0" attribute to images, otherwise the internal help browser 138 // will render a thick border around images inside an <a> element 139 // remove width information to avoid distorded images (fix #11262) 140 b.append(line.replace("<img ", "<img border=\"0\" ") 141 .replaceAll("width=\"(\\d+)\"", "") 142 .replaceAll("<span class=\"icon\">.</span>", "") 143 .replace("href=\"/", "href=\"" + baseurl + '/') 144 .replace(" />", ">")) 145 .append('\n'); 146 } else if (transl && line.contains("</div>")) { 147 transl = false; 148 } 149 if (line.contains("</div>")) { 150 skip = false; 151 } 152 } 153 if (b.indexOf(" Describe ") >= 0 154 || b.indexOf(" does not exist. You can create it here.</p>") >= 0) 155 return ""; 156 if (b.length() == 0) 157 b = full; 158 return "<html><base href=\""+url.toExternalForm() +"\"> " + b + "</html>"; 159 } 160}