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

Last change on this file since 8390 was 8390, checked in by Don-vip, 9 years ago

Sonar - various performance improvements

  • Property svn:eol-style set to native
File size: 5.5 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;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
10
11/**
12 * Read a trac-wiki page.
13 *
14 * @author imi
15 */
16public class WikiReader {
17
18 private final String baseurl;
19
20 /**
21 * Constructs a new {@code WikiReader} for the given base URL.
22 * @param baseurl The wiki base URL
23 */
24 public WikiReader(String baseurl) {
25 this.baseurl = baseurl;
26 }
27
28 /**
29 * Constructs a new {@code WikiReader}.
30 */
31 public WikiReader() {
32 this(Main.pref.get("help.baseurl", Main.getJOSMWebsite()));
33 }
34
35 /**
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 /**
45 * Read the page specified by the url and return the content.
46 *
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
50 *
51 * @throws IOException Throws, if the page could not be loaded.
52 */
53 public String read(String url) throws IOException {
54 URL u = new URL(url);
55 try (BufferedReader in = Utils.openURLReader(u)) {
56 boolean txt = url.endsWith("?format=txt");
57 if (url.startsWith(getBaseUrlWiki()) && !txt)
58 return readFromTrac(in, u);
59 return readNormal(in, !txt);
60 }
61 }
62
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 */
69 public String readLang(String text) throws IOException {
70 String languageCode;
71 String res = "";
72
73 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
74 if(languageCode != null) {
75 res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
76 }
77
78 if(res.isEmpty()) {
79 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
80 if(languageCode != null) {
81 res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
82 }
83 }
84
85 if(res.isEmpty()) {
86 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
87 if(languageCode != null) {
88 res = readLang(new URL(getBaseUrlWiki() + languageCode + text));
89 }
90 }
91
92 if(res.isEmpty()) {
93 throw new IOException(text + " does not exist");
94 } else {
95 return res;
96 }
97 }
98
99 private String readLang(URL url) throws IOException {
100 try (BufferedReader in = Utils.openURLReader(url)) {
101 return readFromTrac(in, url);
102 } catch (IOException e) {
103 Main.addNetworkError(url, Utils.getRootCause(e));
104 throw e;
105 }
106 }
107
108 private String readNormal(BufferedReader in, boolean html) throws IOException {
109 StringBuilder b = new StringBuilder();
110 for (String line = in.readLine(); line != null; line = in.readLine()) {
111 if (!line.contains("[[TranslatedPages]]")) {
112 b.append(line.replaceAll(" />", ">")).append('\n');
113 }
114 }
115 return html ? "<html>" + b + "</html>" : b.toString();
116 }
117
118 protected String readFromTrac(BufferedReader in, URL url) throws IOException {
119 boolean inside = false;
120 boolean transl = false;
121 boolean skip = false;
122 String b = "";
123 String full = "";
124 for (String line = in.readLine(); line != null; line = in.readLine()) {
125 full += line;
126 if (line.contains("<div id=\"searchable\">")) {
127 inside = true;
128 } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
129 transl = true;
130 } else if (line.contains("<div class=\"wikipage searchable\">")) {
131 inside = true;
132 } else if (line.contains("<div class=\"buttons\">")) {
133 inside = false;
134 } else if (line.contains("<h3>Attachments</h3>")) {
135 inside = false;
136 } else if (line.contains("<div id=\"attachments\">")) {
137 inside = false;
138 } else if (line.contains("<div class=\"trac-modifiedby\">")) {
139 skip = true;
140 }
141 if (inside && !transl && !skip) {
142 // add a border="0" attribute to images, otherwise the internal help browser
143 // will render a thick border around images inside an <a> element
144 b += line.replaceAll("<img ", "<img border=\"0\" ")
145 .replaceAll("<span class=\"icon\">.</span>", "")
146 .replaceAll("href=\"/", "href=\"" + baseurl + "/")
147 .replaceAll(" />", ">")
148 + "\n";
149 } else if (transl && line.contains("</div>")) {
150 transl = false;
151 }
152 if (line.contains("</div>")) {
153 skip = false;
154 }
155 }
156 if (b.indexOf(" Describe ") >= 0
157 || b.indexOf(" does not exist. You can create it here.</p>") >= 0)
158 return "";
159 if(b.isEmpty())
160 b = full;
161 return "<html><base href=\""+url.toExternalForm() +"\"> " + b + "</html>";
162 }
163}
Note: See TracBrowser for help on using the repository browser.