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