001 // License: GPL. Copyright 2007 by Immanuel Scholz and others 002 package org.openstreetmap.josm.tools; 003 004 import java.io.BufferedReader; 005 import java.io.IOException; 006 import java.io.InputStream; 007 import java.io.InputStreamReader; 008 import java.net.URL; 009 010 import org.openstreetmap.josm.Main; 011 012 /** 013 * Read a trac-wiki page. 014 * 015 * @author imi 016 */ 017 public class WikiReader { 018 019 private final String baseurl; 020 021 public WikiReader(String baseurl) { 022 this.baseurl = baseurl; 023 } 024 025 public WikiReader() { 026 this.baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de"); 027 } 028 029 /** 030 * Read the page specified by the url and return the content. 031 * 032 * If the url is within the baseurl path, parse it as an trac wikipage and replace relative 033 * pathes etc.. 034 * 035 * @throws IOException Throws, if the page could not be loaded. 036 */ 037 public String read(String url) throws IOException { 038 BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8")); 039 if (url.startsWith(baseurl) && !url.endsWith("?format=txt")) 040 return readFromTrac(in); 041 return readNormal(in); 042 } 043 044 public String readLang(String text) throws IOException { 045 String languageCode = LanguageInfo.getWikiLanguagePrefix(); 046 String res = readLang(new URL(baseurl + "/wiki/" + languageCode + text)); 047 if (res.isEmpty() && !languageCode.isEmpty()) { 048 res = readLang(new URL(baseurl + "/wiki/" + text)); 049 } 050 if (res.isEmpty()) { 051 throw new IOException(text + " does not exist"); 052 } else { 053 return res; 054 } 055 } 056 057 private String readLang(URL url) throws IOException { 058 InputStream in = url.openStream(); 059 return readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8"))); 060 } 061 062 private String readNormal(BufferedReader in) throws IOException { 063 String b = ""; 064 for (String line = in.readLine(); line != null; line = in.readLine()) { 065 if (!line.contains("[[TranslatedPages]]")) { 066 b += line.replaceAll(" />", ">") + "\n"; 067 } 068 } 069 return "<html>" + b + "</html>"; 070 } 071 072 private String readFromTrac(BufferedReader in) throws IOException { 073 boolean inside = false; 074 boolean transl = false; 075 boolean skip = false; 076 String b = ""; 077 for (String line = in.readLine(); line != null; line = in.readLine()) { 078 if (line.contains("<div id=\"searchable\">")) { 079 inside = true; 080 } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) { 081 transl = true; 082 } else if (line.contains("<div class=\"wikipage searchable\">")) { 083 inside = true; 084 } else if (line.contains("<div class=\"buttons\">")) { 085 inside = false; 086 } else if (line.contains("<h3>Attachments</h3>")) { 087 inside = false; 088 } else if (line.contains("<div id=\"attachments\">")) { 089 inside = false; 090 } else if (line.contains("<div class=\"trac-modifiedby\">")) { 091 skip = true; 092 } 093 if (inside && !transl && !skip) { 094 // add a border="0" attribute to images, otherwise the internal help browser 095 // will render a thick border around images inside an <a> element 096 // 097 b += line.replaceAll("<img src=\"/", "<img border=\"0\" src=\"" + baseurl + "/").replaceAll("href=\"/", 098 "href=\"" + baseurl + "/").replaceAll(" />", ">") 099 + "\n"; 100 } else if (transl && line.contains("</div>")) { 101 transl = false; 102 } 103 if (line.contains("</div>")) { 104 skip = false; 105 } 106 } 107 if (b.indexOf(" Describe ") >= 0 108 || b.indexOf(" does not exist. You can create it here.</p>") >= 0) 109 return ""; 110 return "<html>" + b + "</html>"; 111 } 112 }