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

Last change on this file since 5915 was 5915, checked in by stoecker, 11 years ago

use 3 step wiki loading fallback, cleanup handling of language fallbacks

  • Property svn:eol-style set to native
File size: 4.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.net.URL;
7
8import org.openstreetmap.josm.Main;
9import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
10
11/**
12 * Read a trac-wiki page.
13 *
14 * @author imi
15 */
16public 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 BufferedReader in = Utils.openURLReader(new URL(url));
38 try {
39 if (url.startsWith(baseurl) && !url.endsWith("?format=txt"))
40 return readFromTrac(in);
41 return readNormal(in);
42 } finally {
43 Utils.close(in);
44 }
45 }
46
47 public String readLang(String text) throws IOException {
48 String languageCode;
49 String res = "";
50
51 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
52 if(languageCode != null) {
53 res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
54 }
55
56 if(res.isEmpty()) {
57 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
58 if(languageCode != null) {
59 res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
60 }
61 }
62
63 if(res.isEmpty()) {
64 languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
65 if(languageCode != null) {
66 res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
67 }
68 }
69
70 if(res.isEmpty()) {
71 throw new IOException(text + " does not exist");
72 } else {
73 return res;
74 }
75 }
76
77 private String readLang(URL url) throws IOException {
78 BufferedReader in = Utils.openURLReader(url);
79 try {
80 return readFromTrac(in);
81 } finally {
82 Utils.close(in);
83 }
84 }
85
86 private String readNormal(BufferedReader in) throws IOException {
87 String b = "";
88 for (String line = in.readLine(); line != null; line = in.readLine()) {
89 if (!line.contains("[[TranslatedPages]]")) {
90 b += line.replaceAll(" />", ">") + "\n";
91 }
92 }
93 return "<html>" + b + "</html>";
94 }
95
96 protected String readFromTrac(BufferedReader in) throws IOException {
97 boolean inside = false;
98 boolean transl = false;
99 boolean skip = false;
100 String b = "";
101 for (String line = in.readLine(); line != null; line = in.readLine()) {
102 if (line.contains("<div id=\"searchable\">")) {
103 inside = true;
104 } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
105 transl = true;
106 } else if (line.contains("<div class=\"wikipage searchable\">")) {
107 inside = true;
108 } else if (line.contains("<div class=\"buttons\">")) {
109 inside = false;
110 } else if (line.contains("<h3>Attachments</h3>")) {
111 inside = false;
112 } else if (line.contains("<div id=\"attachments\">")) {
113 inside = false;
114 } else if (line.contains("<div class=\"trac-modifiedby\">")) {
115 skip = true;
116 }
117 if (inside && !transl && !skip) {
118 // add a border="0" attribute to images, otherwise the internal help browser
119 // will render a thick border around images inside an <a> element
120 //
121 b += line.replaceAll("<img ", "<img border=\"0\" ").replaceAll(" />", ">") + "\n";
122 } else if (transl && line.contains("</div>")) {
123 transl = false;
124 }
125 if (line.contains("</div>")) {
126 skip = false;
127 }
128 }
129 if (b.indexOf(" Describe ") >= 0
130 || b.indexOf(" does not exist. You can create it here.</p>") >= 0)
131 return "";
132 return "<html><base href=\""+baseurl+"\"> " + b + "</html>";
133 }
134}
Note: See TracBrowser for help on using the repository browser.