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

Last change on this file since 3779 was 3589, checked in by stoecker, 14 years ago

fix bug in startup page access

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.tools;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.BufferedReader;
7import java.io.IOException;
8import java.io.InputStream;
9import java.io.InputStreamReader;
10import java.net.URL;
11
12import org.openstreetmap.josm.Main;
13
14/**
15 * Read a trac-wiki page.
16 *
17 * @author imi
18 */
19public 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
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.getWikiLanguagePrefix();
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 } else if (line.contains("<h3>Attachments</h3>")) {
126 inside = false;
127 } else if (line.contains("<div id=\"attachments\">")) {
128 inside = false;
129 } else if (line.contains("<div class=\"trac-modifiedby\">")) {
130 continue;
131 }
132 if (inside && !transl) {
133 // add a border="0" attribute to images, otherwise the internal help browser
134 // will render a thick border around images inside an <a> element
135 //
136 b += line.replaceAll("<img src=\"/", "<img border=\"0\" src=\"" + baseurl + "/").replaceAll("href=\"/",
137 "href=\"" + baseurl + "/").replaceAll(" />", ">")
138 + "\n";
139 } else if (transl && line.contains("</div>")) {
140 transl = false;
141 }
142 }
143 if (b.indexOf(" Describe ") >= 0
144 || b.indexOf(" does not exist. You can create it here.</p>") >= 0)
145 return "";
146 return "<html>" + b + "</html>";
147 }
148}
Note: See TracBrowser for help on using the repository browser.