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.net.URL;
|
---|
7 |
|
---|
8 | import org.openstreetmap.josm.Main;
|
---|
9 | import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * Read a trac-wiki page.
|
---|
13 | *
|
---|
14 | * @author imi
|
---|
15 | */
|
---|
16 | public class WikiReader {
|
---|
17 |
|
---|
18 | private final String baseurl;
|
---|
19 |
|
---|
20 | public WikiReader(String baseurl) {
|
---|
21 | this.baseurl = baseurl;
|
---|
22 | }
|
---|
23 |
|
---|
24 | public WikiReader() {
|
---|
25 | this.baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Read the page specified by the url and return the content.
|
---|
30 | *
|
---|
31 | * If the url is within the baseurl path, parse it as an trac wikipage and replace relative
|
---|
32 | * pathes etc..
|
---|
33 | *
|
---|
34 | * @throws IOException Throws, if the page could not be loaded.
|
---|
35 | */
|
---|
36 | public String read(String url) throws IOException {
|
---|
37 | URL u = new URL(url);
|
---|
38 | BufferedReader in = Utils.openURLReader(u);
|
---|
39 | try {
|
---|
40 | if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
|
---|
41 | return readFromTrac(in, u);
|
---|
42 | return readNormal(in);
|
---|
43 | } finally {
|
---|
44 | Utils.close(in);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | public String readLang(String text) throws IOException {
|
---|
49 | String languageCode;
|
---|
50 | String res = "";
|
---|
51 |
|
---|
52 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
|
---|
53 | if(languageCode != null) {
|
---|
54 | res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
|
---|
55 | }
|
---|
56 |
|
---|
57 | if(res.isEmpty()) {
|
---|
58 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
|
---|
59 | if(languageCode != null) {
|
---|
60 | res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | if(res.isEmpty()) {
|
---|
65 | languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
|
---|
66 | if(languageCode != null) {
|
---|
67 | res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | if(res.isEmpty()) {
|
---|
72 | throw new IOException(text + " does not exist");
|
---|
73 | } else {
|
---|
74 | return res;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | private String readLang(URL url) throws IOException {
|
---|
79 | BufferedReader in = Utils.openURLReader(url);
|
---|
80 | try {
|
---|
81 | return readFromTrac(in, url);
|
---|
82 | } finally {
|
---|
83 | Utils.close(in);
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | private String readNormal(BufferedReader in) throws IOException {
|
---|
88 | String b = "";
|
---|
89 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
90 | if (!line.contains("[[TranslatedPages]]")) {
|
---|
91 | b += line.replaceAll(" />", ">") + "\n";
|
---|
92 | }
|
---|
93 | }
|
---|
94 | return "<html>" + b + "</html>";
|
---|
95 | }
|
---|
96 |
|
---|
97 | protected String readFromTrac(BufferedReader in, URL url) throws IOException {
|
---|
98 | boolean inside = false;
|
---|
99 | boolean transl = false;
|
---|
100 | boolean skip = false;
|
---|
101 | String b = "";
|
---|
102 | String full = "";
|
---|
103 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
104 | full += line;
|
---|
105 | if (line.contains("<div id=\"searchable\">")) {
|
---|
106 | inside = true;
|
---|
107 | } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
|
---|
108 | transl = true;
|
---|
109 | } else if (line.contains("<div class=\"wikipage searchable\">")) {
|
---|
110 | inside = true;
|
---|
111 | } else if (line.contains("<div class=\"buttons\">")) {
|
---|
112 | inside = false;
|
---|
113 | } else if (line.contains("<h3>Attachments</h3>")) {
|
---|
114 | inside = false;
|
---|
115 | } else if (line.contains("<div id=\"attachments\">")) {
|
---|
116 | inside = false;
|
---|
117 | } else if (line.contains("<div class=\"trac-modifiedby\">")) {
|
---|
118 | skip = true;
|
---|
119 | }
|
---|
120 | if (inside && !transl && !skip) {
|
---|
121 | // add a border="0" attribute to images, otherwise the internal help browser
|
---|
122 | // will render a thick border around images inside an <a> element
|
---|
123 | //
|
---|
124 | b += line.replaceAll("<img ", "<img border=\"0\" ")
|
---|
125 | .replaceAll("<span class=\"icon\">.</span>", "")
|
---|
126 | .replaceAll("href=\"/", "href=\"" + baseurl + "/")
|
---|
127 | .replaceAll(" />", ">")
|
---|
128 | + "\n";
|
---|
129 | } else if (transl && line.contains("</div>")) {
|
---|
130 | transl = false;
|
---|
131 | }
|
---|
132 | if (line.contains("</div>")) {
|
---|
133 | skip = false;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | if (b.indexOf(" Describe ") >= 0
|
---|
137 | || b.indexOf(" does not exist. You can create it here.</p>") >= 0)
|
---|
138 | return "";
|
---|
139 | if(b.isEmpty())
|
---|
140 | b = full;
|
---|
141 | return "<html><base href=\""+url.toExternalForm() +"\"> " + b + "</html>";
|
---|
142 | }
|
---|
143 | }
|
---|