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

Last change on this file since 17534 was 16436, checked in by simon04, 4 years ago

see #19251 - Java 8: use Stream

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.io.BufferedReader;
5import java.io.IOException;
6import java.net.URL;
7import java.util.stream.Collectors;
8
9import org.openstreetmap.josm.spi.preferences.Config;
10import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
11
12/**
13 * Read a trac-wiki page.
14 *
15 * @author imi
16 */
17public class WikiReader {
18
19 private final String baseurl;
20
21 /**
22 * Constructs a new {@code WikiReader} for the given base URL.
23 * @param baseurl The wiki base URL
24 */
25 public WikiReader(String baseurl) {
26 this.baseurl = baseurl;
27 }
28
29 /**
30 * Constructs a new {@code WikiReader}.
31 */
32 public WikiReader() {
33 this(Config.getPref().get("help.baseurl", Config.getUrls().getJOSMWebsite()));
34 }
35
36 /**
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 /**
46 * Read the page specified by the url and return the content.
47 *
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
51 *
52 * @throws IOException Throws, if the page could not be loaded.
53 */
54 public String read(String url) throws IOException {
55 URL u = new URL(url);
56 try (BufferedReader in = HttpClient.create(u).connect().getContentReader()) {
57 boolean txt = url.endsWith("?format=txt");
58 if (url.startsWith(getBaseUrlWiki()) && !txt)
59 return readFromTrac(in, u);
60 return readNormal(in, !txt);
61 }
62 }
63
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 */
70 public String readLang(String text) throws IOException {
71 String languageCode;
72 String res = "";
73
74 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
75 if (languageCode != null) {
76 res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
77 }
78
79 if (res.isEmpty()) {
80 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
81 if (languageCode != null) {
82 res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
83 }
84 }
85
86 if (res.isEmpty()) {
87 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
88 if (languageCode != null) {
89 res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
90 }
91 }
92
93 if (res.isEmpty()) {
94 throw new IOException(text + " does not exist");
95 } else {
96 return res;
97 }
98 }
99
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}
Note: See TracBrowser for help on using the repository browser.