Changeset 1290 in josm
- Timestamp:
- 2009-01-17T23:52:24+01:00 (16 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/GettingStarted.java
r1231 r1290 6 6 7 7 import java.io.IOException; 8 import java.util.ArrayList; 8 9 import java.util.regex.Matcher; 9 10 import java.util.regex.Pattern; … … 44 45 45 46 private void assignContent() { 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 && Main.pref.getBoolean("help.displaymotd", true)) 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 50 115 try { 51 motdcontent = wr.read(baseurl + "/wiki/MessageOfTheDay"); 116 // If we hit a non-localized link here, we already know there's no translated version available 117 String message = isLocalized ? wr.read(urlStart + languageCode + urlEnd) : ""; 118 // Look for non-localized version 119 if (message.equals("")) 120 message = wr.read(urlStart + urlEnd); 121 122 if (!message.equals("")) 123 if(isHelp) 124 content += message; 125 else 126 content += "<ul><li>"+ message.substring(8)+"</li></ul>"; 52 127 } catch (IOException ioe) { 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) { 119 134 } 120 135 } 121 content += motdcontent.substring(start);122 content = content.replace("<html>", "<html><style type=\"text/css\">\nbody { font-family: sans-serif; font-weight: bold; }\nh1 {text-align: center;}</style>");123 /* replace the wiki title */124 content = content.replace("Java OpenStreetMap Editor", tr("Java OpenStreetMap Editor"));125 136 } 126 137 138 content = "<html>\n"+ 139 "<style type=\"text/css\">\n"+ 140 "body { font-family: sans-serif; font-weight: bold; }\n"+ 141 "h1 {text-align: center;}\n"+ 142 "</style>\n"+ 143 "<h1>JOSM - " + tr("Java OpenStreetMap Editor") + "</h1>\n"+ 144 content+"\n"+ 145 "</html>"; 127 146 } 128 147 -
trunk/src/org/openstreetmap/josm/tools/WikiReader.java
r1169 r1290 32 32 public String read(String url) throws IOException { 33 33 BufferedReader in = new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8")); 34 if (url.startsWith(baseurl)) 34 if (url.startsWith(baseurl) && !url.endsWith("?format=txt")) 35 35 return readFromTrac(in, url); 36 36 return readNormal(in);
Note:
See TracChangeset
for help on using the changeset viewer.