| 46 | | if (content.length() == 0) { |
| 47 | | String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de"); |
| 48 | | WikiReader wr = new WikiReader(baseurl); |
| 49 | | String motdcontent = ""; |
| | 47 | if (content.length() > 0) return; |
| | 48 | |
| | 49 | String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de"); |
| | 50 | WikiReader wr = new WikiReader(baseurl); |
| | 51 | String motdcontent = ""; |
| | 52 | try { |
| | 53 | motdcontent = wr.read(baseurl + "/wiki/MessageOfTheDay?format=txt"); |
| | 54 | } catch (IOException ioe) { |
| | 55 | motdcontent = "<html><body>\n<h1>" + |
| | 56 | "JOSM - " + tr("Java OpenStreetMap Editor") + |
| | 57 | "</h1>\n<h2 align=\"center\">(" + |
| | 58 | tr ("Message of the day not available") + |
| | 59 | ")</h2>"; |
| | 60 | } |
| | 61 | |
| | 62 | int myVersion = AboutAction.getVersionNumber(); |
| | 63 | String languageCode = Main.getLanguageCodeU(); |
| | 64 | |
| | 65 | // Finds wiki links like (underscores inserted for readability): [wiki:LANGCODE:messageoftheday_CONDITON_REVISION LANGCODE] |
| | 66 | // Langcode usually consists of two letters describing the language and may be omitted |
| | 67 | // Condition may be one of the following: > < <= => |
| | 68 | // Revision is the JOSM version |
| | 69 | Pattern versionPattern = Pattern.compile("\\[wiki:(?:[A-Z]+:)?MessageOfTheDay(\\>\\=|\\<\\=|\\<|\\>)([0-9]+)\\s*([A-Z]*)\\]", Pattern.CASE_INSENSITIVE); |
| | 70 | // 1=condition, 2=targetVersion, 3=lang |
| | 71 | Matcher matcher = versionPattern.matcher(motdcontent); |
| | 72 | matcher.reset(); |
| | 73 | |
| | 74 | ArrayList<Object> links = new ArrayList<Object>(); |
| | 75 | String linksList=""; |
| | 76 | while (matcher.find()) { |
| | 77 | // Discards all but the selected locale and non-localized links |
| | 78 | if(!(matcher.group(3)+":").equals(languageCode) && !matcher.group(3).equals("")) |
| | 79 | continue; |
| | 80 | |
| | 81 | links.add(new String[] {matcher.group(1), matcher.group(2), matcher.group(3)}); |
| | 82 | linksList += matcher.group(1)+matcher.group(2)+matcher.group(3)+": "; |
| | 83 | } |
| | 84 | |
| | 85 | for(int i=0; i < links.size(); i++) { |
| | 86 | String[] obj = (String[])links.get(i); |
| | 87 | int targetVersion = Integer.parseInt(obj[1]); |
| | 88 | String condition = obj[0]; |
| | 89 | Boolean isLocalized = !obj[2].equals(""); |
| | 90 | |
| | 91 | // Prefer localized over non-localized links, if they're otherwise the same |
| | 92 | if(!isLocalized && linksList.indexOf(condition+obj[1]+languageCode+" ") >= 0) |
| | 93 | continue; |
| | 94 | |
| | 95 | boolean included = false; |
| | 96 | |
| | 97 | if(myVersion == 0) |
| | 98 | included = true; |
| | 99 | else if(condition.equals(">=")) |
| | 100 | included=myVersion >= targetVersion; |
| | 101 | else if(condition.equals(">")) |
| | 102 | included = myVersion > targetVersion; |
| | 103 | else if(condition.equals("<")) |
| | 104 | included=myVersion < targetVersion; |
| | 105 | else |
| | 106 | included = myVersion <= targetVersion; |
| | 107 | |
| | 108 | if(!included) continue; |
| | 109 | |
| | 110 | Boolean isHelp = targetVersion == 1; |
| | 111 | |
| | 112 | String urlStart = baseurl + "/wiki/"; |
| | 113 | String urlEnd = "MessageOfTheDay" + condition + targetVersion + (isHelp ? "" : "?format=txt"); |
| | 114 | |
| 53 | | motdcontent = "<html><body>\n<h1>" + |
| 54 | | "JOSM - " + tr("Java OpenStreetMap Editor") + |
| 55 | | "</h1>\n<h2 align=\"center\">(" + |
| 56 | | tr ("Message of the day not available") + |
| 57 | | ")</h2>"; |
| 58 | | } |
| 59 | | |
| 60 | | int myVersion = AboutAction.getVersionNumber(); |
| 61 | | |
| 62 | | Pattern commentPattern = Pattern.compile("\\<p\\>\\s*\\/\\*[^\\*]*\\*\\/\\s*\\<\\/p\\>", Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE); |
| 63 | | Matcher matcherComment = commentPattern.matcher(motdcontent); |
| 64 | | motdcontent = matcherComment.replaceAll(""); |
| 65 | | |
| 66 | | /* look for hrefs of the form wiki/MessageOfTheDay>123 where > can also be <,<=,>= and the number is the revision number */ |
| 67 | | int start = 0; |
| 68 | | Pattern versionPattern = Pattern.compile("(?:\\<p\\>\\s*)?\\<a[^\\>]*href\\=\\\"([^\\\"]*\\/wiki\\/)(MessageOfTheDay(\\%3E%3D|%3C%3D|\\%3E|\\%3C)([0-9]+))\\\"[^\\>]*\\>[^\\<]*\\<\\/a\\>(?:\\s*\\</p\\>)?", Pattern.CASE_INSENSITIVE|Pattern.DOTALL|Pattern.MULTILINE); |
| 69 | | Matcher matcher = versionPattern.matcher(motdcontent); |
| 70 | | matcher.reset(); |
| 71 | | String languageCode = Main.getLanguageCodeU(); |
| 72 | | while (matcher.find()) { |
| 73 | | int targetVersion = Integer.parseInt(matcher.group(4)); |
| 74 | | String condition = matcher.group(3); |
| 75 | | boolean included = false; |
| 76 | | if (condition.equals("%3E")) { |
| 77 | | if ((myVersion == 0 || myVersion > targetVersion) |
| 78 | | /* && ! Main.pref.getBoolean("motd.gt."+targetVersion) */) { |
| 79 | | /* Main.pref.put("motd.gt."+targetVersion, true); */ |
| 80 | | included = true; |
| 81 | | } |
| 82 | | } else if (condition.equals("%3E%3D")) { |
| 83 | | if ((myVersion == 0 || myVersion >= targetVersion) |
| 84 | | /* && ! Main.pref.getBoolean("motd.ge."+targetVersion) */) { |
| 85 | | /* Main.pref.put("motd.ge."+targetVersion, true); */ |
| 86 | | included = true; |
| 87 | | } |
| 88 | | } else if (condition.equals("%3C")) { |
| 89 | | included = myVersion < targetVersion; |
| 90 | | } else { |
| 91 | | included = myVersion <= targetVersion; |
| 92 | | } |
| 93 | | if (matcher.start() > start) { |
| 94 | | content += motdcontent.substring(start, matcher.start() - 1); |
| 95 | | } |
| 96 | | start = matcher.end(); |
| 97 | | if (included) { |
| 98 | | String url = matcher.group(1) + languageCode + matcher.group(2); |
| 99 | | try { |
| 100 | | String message = wr.read(url); |
| 101 | | // a return containing the article name indicates that the page didn't |
| 102 | | // exist in the Wiki. |
| 103 | | String emptyIndicator = "Describe \"" + languageCode + "MessageOfTheDay"; |
| 104 | | if (message.indexOf(emptyIndicator) >= 0) { |
| 105 | | url = matcher.group(1) + matcher.group(2); |
| 106 | | message = wr.read(url); |
| 107 | | emptyIndicator = "Describe \"MessageOfTheDay"; |
| 108 | | } |
| 109 | | if (message.indexOf(emptyIndicator) == -1) { |
| 110 | | content += message.replace("<html>", "").replace("</html>", "").replace("<div id=\"searchable\">", "").replace("</div>", ""); |
| 111 | | } |
| 112 | | } catch (IOException ioe) { |
| 113 | | url = matcher.group(1) + matcher.group(2); |
| 114 | | try { |
| 115 | | content += wr.read(url).replace("<html>", "").replace("</html>", "").replace("<div id=\"searchable\">", "").replace("</div>", ""); |
| 116 | | } catch (IOException ioe2) { |
| 117 | | } |
| 118 | | } |
| | 128 | try { |
| | 129 | if(isHelp) |
| | 130 | content += wr.read(urlStart + urlEnd); |
| | 131 | else |
| | 132 | content += "<ul><li>"+wr.read(urlStart + urlEnd).substring(8)+"</li></ul>"; |
| | 133 | } catch (IOException ioe2) { |