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 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.tools.LanguageInfo;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Read a trac-wiki page.
|
---|
14 | *
|
---|
15 | * @author imi
|
---|
16 | */
|
---|
17 | public class WikiReader {
|
---|
18 |
|
---|
19 | private final String baseurl;
|
---|
20 |
|
---|
21 | public WikiReader(String baseurl) {
|
---|
22 | this.baseurl = baseurl;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public WikiReader() {
|
---|
26 | this.baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
|
---|
27 | }
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Read the page specified by the url and return the content.
|
---|
31 | *
|
---|
32 | * If the url is within the baseurl path, parse it as an trac wikipage and
|
---|
33 | * replace relative pathes etc..
|
---|
34 | *
|
---|
35 | * @return Either the string of the content of the wiki page.
|
---|
36 | * @throws IOException Throws, if the page could not be loaded.
|
---|
37 | */
|
---|
38 | public String read(String url) throws IOException {
|
---|
39 | BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8"));
|
---|
40 | if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
|
---|
41 | return readFromTrac(in);
|
---|
42 | return readNormal(in);
|
---|
43 | }
|
---|
44 |
|
---|
45 | public String readLang(String text) {
|
---|
46 | String languageCode = LanguageInfo.getLanguageCodeWiki();
|
---|
47 | String url = baseurl + "/wiki/"+languageCode+text;
|
---|
48 | String res = "";
|
---|
49 | try {
|
---|
50 | res = readFromTrac(new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8")));
|
---|
51 | } catch (IOException ioe) {}
|
---|
52 | if(res.length() == 0 && languageCode.length() != 0)
|
---|
53 | {
|
---|
54 | url = baseurl + "/wiki/"+text;
|
---|
55 | try {
|
---|
56 | res = readFromTrac(new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8")));
|
---|
57 | } catch (IOException ioe) {}
|
---|
58 | }
|
---|
59 | return res;
|
---|
60 | }
|
---|
61 |
|
---|
62 | private String readNormal(BufferedReader in) throws IOException {
|
---|
63 | String b = "";
|
---|
64 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
65 | if(!line.contains("[[TranslatedPages]]"))
|
---|
66 | b += line.replaceAll(" />", ">") + "\n";
|
---|
67 | }
|
---|
68 | return "<html>" + b + "</html>";
|
---|
69 | }
|
---|
70 |
|
---|
71 | private String readFromTrac(BufferedReader in) throws IOException {
|
---|
72 | boolean inside = false;
|
---|
73 | boolean transl = false;
|
---|
74 | String b = "";
|
---|
75 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
76 | if (line.contains("<div id=\"searchable\">"))
|
---|
77 | inside = true;
|
---|
78 | else if (line.contains("<div class=\"wiki-toc trac-nav\""))
|
---|
79 | transl = true;
|
---|
80 | else if (line.contains("<div class=\"wikipage searchable\">"))
|
---|
81 | inside = true;
|
---|
82 | else if (line.contains("<div class=\"buttons\">"))
|
---|
83 | inside = false;
|
---|
84 | if (inside && !transl) {
|
---|
85 | b += line.replaceAll("<img src=\"/", "<img src=\""+baseurl+"/")
|
---|
86 | .replaceAll("href=\"/", "href=\""+baseurl+"/")
|
---|
87 | .replaceAll(" />", ">") + "\n";
|
---|
88 | }
|
---|
89 | else if (transl && line.contains("</div>"))
|
---|
90 | transl = false;
|
---|
91 | }
|
---|
92 | if(b.indexOf(" Describe ") >= 0)
|
---|
93 | return "";
|
---|
94 | return "<html>" + b + "</html>";
|
---|
95 | }
|
---|
96 | }
|
---|