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

Last change on this file since 1879 was 1879, checked in by Gubaer, 15 years ago

towards a fix for #3142: JOSM applet class no longer functional

  • Property svn:eol-style set to native
File size: 5.0 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import java.io.BufferedReader;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.InputStreamReader;
8import java.net.URL;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.tools.LanguageInfo;
12
13import static org.openstreetmap.josm.tools.I18n.tr;
14
15/**
16 * Read a trac-wiki page.
17 *
18 * @author imi
19 */
20public class WikiReader {
21
22 private final String baseurl;
23
24 public WikiReader(String baseurl) {
25 this.baseurl = baseurl;
26 }
27
28 public WikiReader() {
29 this.baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
30 }
31
32 /**
33 * Read the page specified by the url and return the content.
34 *
35 * If the url is within the baseurl path, parse it as an trac wikipage and replace relative
36 * pathes etc..
37 *
38 * @return Either the string of the content of the wiki page.
39 * @throws IOException Throws, if the page could not be loaded.
40 */
41 public String read(String url) throws IOException {
42 BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8"));
43 if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
44 return readFromTrac(in);
45 return readNormal(in);
46 }
47
48 public String readLang(String text) {
49 String languageCode = LanguageInfo.getLanguageCodeWiki();
50 String url = baseurl + "/wiki/" + languageCode + text;
51 String res = "";
52 InputStream in = null;
53 try {
54 in = new URL(url).openStream();
55 res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
56 } catch (IOException ioe) {
57 System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe
58 .toString()));
59 } catch(SecurityException e) {
60 System.out.println(tr(
61 "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e
62 .toString()));
63 } finally {
64 if (in != null) {
65 try {
66 in.close();
67 } catch (IOException e) {
68 }
69 }
70 }
71 if (res.length() == 0 && languageCode.length() != 0) {
72 url = baseurl + "/wiki/" + text;
73 try {
74 in = new URL(url).openStream();
75 } catch (IOException e) {
76 System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, e
77 .toString()));
78 return res;
79 } catch (SecurityException e) {
80 System.out.println(tr(
81 "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e
82 .toString()));
83 return res;
84 }
85 try {
86 res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
87 } catch (IOException ioe) {
88 System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe
89 .toString()));
90 return res;
91 } finally {
92 if (in != null) {
93 try {
94 in.close();
95 } catch (IOException e) {
96 }
97 }
98 }
99 }
100 return res;
101 }
102
103 private String readNormal(BufferedReader in) throws IOException {
104 String b = "";
105 for (String line = in.readLine(); line != null; line = in.readLine()) {
106 if (!line.contains("[[TranslatedPages]]")) {
107 b += line.replaceAll(" />", ">") + "\n";
108 }
109 }
110 return "<html>" + b + "</html>";
111 }
112
113 private String readFromTrac(BufferedReader in) throws IOException {
114 boolean inside = false;
115 boolean transl = false;
116 String b = "";
117 for (String line = in.readLine(); line != null; line = in.readLine()) {
118 if (line.contains("<div id=\"searchable\">")) {
119 inside = true;
120 } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
121 transl = true;
122 } else if (line.contains("<div class=\"wikipage searchable\">")) {
123 inside = true;
124 } else if (line.contains("<div class=\"buttons\">")) {
125 inside = false;
126 }
127 if (inside && !transl) {
128 b += line.replaceAll("<img src=\"/", "<img src=\"" + baseurl + "/").replaceAll("href=\"/",
129 "href=\"" + baseurl + "/").replaceAll(" />", ">")
130 + "\n";
131 } else if (transl && line.contains("</div>")) {
132 transl = false;
133 }
134 }
135 if (b.indexOf(" Describe ") >= 0)
136 return "";
137 return "<html>" + b + "</html>";
138 }
139}
Note: See TracBrowser for help on using the repository browser.