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

Last change on this file since 10370 was 9168, checked in by simon04, 8 years ago

see #12231 - Uniform access to HTTP resources

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