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

Last change on this file since 1180 was 1169, checked in by stoecker, 15 years ago

removed usage of tab stops

  • Property svn:eol-style set to native
File size: 2.5 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.InputStreamReader;
7import java.net.URL;
8
9/**
10 * Read a trac-wiki page.
11 *
12 * @author imi
13 */
14public 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))
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=\"buttons\">"))
56 inside = false;
57 if (inside) {
58 line = line.replaceAll("<img src=\"/", "<img src=\""+baseurl+"/");
59 line = line.replaceAll("href=\"/", "href=\""+baseurl+"/");
60 if (!line.contains("$"))
61 line = line.replaceAll("<p>Describe \"([^\"]+)\" here</p>", "<p>Describe \"$1\" <a href=\""+JOSM_EXTERN+url.substring(7)+"\">here</a></p>");
62 line = adjustText(line);
63 b.append(line);
64 b.append("\n");
65 }
66 }
67 b.append("</html>");
68 return b.toString();
69 }
70
71 private String adjustText(String text) {
72 text = text.replaceAll(" />", ">");
73 return text;
74 }
75}
Note: See TracBrowser for help on using the repository browser.