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