1 | package org.openstreetmap.josm.tools;
|
---|
2 |
|
---|
3 | import java.io.BufferedReader;
|
---|
4 | import java.io.IOException;
|
---|
5 | import java.io.InputStreamReader;
|
---|
6 | import java.net.URL;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Read a trac-wiki page.
|
---|
10 | *
|
---|
11 | * @author imi
|
---|
12 | */
|
---|
13 | public class WikiReader {
|
---|
14 |
|
---|
15 | public static final String JOSM_EXTERN = "http://josm-extern.";
|
---|
16 | private final String baseurl;
|
---|
17 |
|
---|
18 | public WikiReader(String baseurl) {
|
---|
19 | this.baseurl = baseurl;
|
---|
20 | }
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Read the page specified by the url and return the content.
|
---|
24 | *
|
---|
25 | * If the url is within the baseurl path, parse it as an trac wikipage and
|
---|
26 | * replace relative pathes etc..
|
---|
27 | *
|
---|
28 | * @return Either the string of the content of the wiki page.
|
---|
29 | * @throws IOException Throws, if the page could not be loaded.
|
---|
30 | */
|
---|
31 | public String read(String url) throws IOException {
|
---|
32 | BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream()));
|
---|
33 | if (url.startsWith(baseurl))
|
---|
34 | return readFromTrac(in, url);
|
---|
35 | return readNormal(in);
|
---|
36 | }
|
---|
37 |
|
---|
38 | private String readNormal(BufferedReader in) throws IOException {
|
---|
39 | StringBuilder b = new StringBuilder("<html>");
|
---|
40 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
41 | line = adjustText(line);
|
---|
42 | b.append(line);
|
---|
43 | b.append("\n");
|
---|
44 | }
|
---|
45 | return b.toString();
|
---|
46 | }
|
---|
47 |
|
---|
48 | private String readFromTrac(BufferedReader in, String url) throws IOException {
|
---|
49 | boolean inside = false;
|
---|
50 | StringBuilder b = new StringBuilder("<html>");
|
---|
51 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
52 | if (line.contains("<div id=\"searchable\">"))
|
---|
53 | inside = true;
|
---|
54 | else if (line.contains("<div class=\"buttons\">"))
|
---|
55 | inside = false;
|
---|
56 | if (inside) {
|
---|
57 | line = line.replaceAll("<img src=\"/", "<img src=\""+baseurl+"/");
|
---|
58 | line = line.replaceAll("href=\"/", "href=\""+baseurl+"/");
|
---|
59 | if (!line.contains("$"))
|
---|
60 | line = line.replaceAll("<p>Describe \"([^\"]+)\" here</p>", "<p>Describe \"$1\" <a href=\""+JOSM_EXTERN+url.substring(7)+"\">here</a></p>");
|
---|
61 | line = adjustText(line);
|
---|
62 | b.append(line);
|
---|
63 | b.append("\n");
|
---|
64 | }
|
---|
65 | }
|
---|
66 | b.append("</html>");
|
---|
67 | return b.toString();
|
---|
68 | }
|
---|
69 |
|
---|
70 | private String adjustText(String text) {
|
---|
71 | text = text.replaceAll(" />", ">");
|
---|
72 | return text;
|
---|
73 | }
|
---|
74 | }
|
---|