Changeset 1507 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2009-03-19T20:18:19+01:00 (16 years ago)
Author:
stoecker
Message:

MOTD got much easier. Server does the work now

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/GettingStarted.java

    r1480 r1507  
    5353
    5454    /**
    55      * This class encapsulates the "reading URL" task and can be executed in background and in
    56      * parallel. Since the MOTD is many separate pages this speeds things up quite a lot. If no
    57      * localized version is available, it automatically falls back to the international one.
    58      */
    59     private class readMOTD implements Callable<String> {
    60         private boolean isLocalized;
    61         private boolean isHelp;
    62         private String urlLoc;
    63         private String urlIntl;
    64         private String urlBase;
    65 
    66         /**
    67          * Read a MOTD page
    68          * @param isLocalized If true, tries to get localized version as defined in urlLoc
    69          * @param urlBase Base URL (i.e. http://www.openstreetmap.de/wiki/)
    70          * @param urlLoc Part to append to base URL to receive localized version
    71          * @param urlIntl Part to append to base URL to receive international version
    72          * @param makeList If true, the URL's contents will be wrapped in a list (<ul><li>)
    73          */
    74         readMOTD(boolean isLocalized, String urlBase, String urlLoc, String urlIntl, boolean makeList) {
    75           this.isLocalized = isLocalized;
    76           this.urlBase = urlBase;
    77           this.urlLoc = urlLoc;
    78           this.urlIntl = urlIntl;
    79           this.isHelp = makeList;
    80         }
    81 
    82         /*
    83          * Does the actual work
    84          * @see java.util.concurrent.Callable#call()
    85          */
    86         public String call() {
    87             WikiReader wr = new WikiReader(urlBase);
    88             String content = "";
    89             try {
    90                 // If we hit a non-localized link here, we already know there's no translated
    91                 // version available
    92                 String message = isLocalized ? wr.read(urlLoc) : "";
    93                 // Look for non-localized version
    94                 if (message.equals(""))
    95                     message = wr.read(urlIntl);
    96 
    97                 if (!message.equals(""))
    98                     if(isHelp)
    99                         content += message;
    100                     else
    101                         content += "<ul><li>"+ message.substring(8)
    102                                             .replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
    103             } catch (IOException ioe) {
    104                 try {
    105                     if(isHelp)
    106                         content += wr.read(urlIntl);
    107                     else
    108                         content += "<ul><li>"+wr.read(urlIntl).substring(8)
    109                                             .replaceAll("\n *\\* +","</li><li>")+"</li></ul>";
    110                 } catch (IOException ioe2) {
    111                 }
    112             }
    113 
    114             return content;
    115         }
    116     }
    117 
    118     /**
    11955     * Grabs current MOTD from cache or webpage and parses it.
    12056     */
     
    13470            String baseurl = Main.pref.get("help.baseurl", "http://josm.openstreetmap.de");
    13571            WikiReader wr = new WikiReader(baseurl);
    136             String motdcontent = "";
     72            String vers = "";
     73            String languageCode = Main.getLanguageCodeU();
    13774            try {
    138                 motdcontent = wr.read(baseurl + "/wiki/MessageOfTheDay?format=txt");
     75                motd = wr.read(baseurl + "/wiki/"+languageCode+"StartupPage");
    13976            } catch (IOException ioe) {
    140                 motdcontent = "<html>" + styles + "<body><h1>" +
    141                     "JOSM - " + tr("Java OpenStreetMap Editor") +
    142                     "</h1>\n<h2 align=\"center\">(" +
    143                     tr ("Message of the day not available") +
    144                     ")</h2>";
     77                try {
     78                    motd = wr.read(baseurl + "/wiki/StartupPage");
     79                } catch (IOException ioe2) {
     80                    motd = "<html>" + styles + "<body><h1>" +
     81                        "JOSM - " + tr("Java OpenStreetMap Editor") +
     82                        "</h1>\n<h2 align=\"center\">(" +
     83                        tr("Message of the day not available") +
     84                        ")</h2></html>";
     85                }
    14586            }
    146 
    147             String languageCode = Main.getLanguageCodeU();
    148 
    149             // Finds wiki links like (underscores inserted for readability):
    150             // [wiki:LANGCODE:messageoftheday_CONDITON_REVISION LANGCODE]
    151             // Langcode usually consists of two letters describing the language and may be omitted
    152             // Condition may be one of the following: >  <  <=  =>
    153             // Revision is the JOSM version
    154             Pattern versionPattern = Pattern.compile(
    155                     "\\[wiki:(?:[A-Z]+:)?MessageOfTheDay(\\>\\=|\\<\\=|\\<|\\>)([0-9]+)\\s*([A-Z]*)\\]",
    156                     Pattern.CASE_INSENSITIVE);
    157             // 1=condition, 2=targetVersion, 3=lang
    158             Matcher matcher = versionPattern.matcher(motdcontent);
    159             matcher.reset();
    160 
    161             ArrayList<String[]> links = new ArrayList<String[]>();
    162             String linksList="";
    163             while (matcher.find()) {
    164                 // Discards all but the selected locale and non-localized links
    165                 if(!(matcher.group(3)+":").equals(languageCode) && !matcher.group(3).equals(""))
    166                     continue;
    167 
    168                 links.add(new String[] {matcher.group(1), matcher.group(2), matcher.group(3)});
    169                 linksList += matcher.group(1)+matcher.group(2)+matcher.group(3)+": ";
    170             }
    171 
    172             // We cannot use Main.worker here because it's single-threaded and
    173             // setting it to multi-threading will cause problems elsewhere
    174             ExecutorService slave = Executors.newCachedThreadPool();
    175 
    176             ArrayList<Future<String>> linkContents = new ArrayList<Future<String>>();
    177             for(int i=0; i < links.size(); i++) {
    178                 String[] obj = links.get(i);
    179                 int targetVersion = Integer.parseInt(obj[1]);
    180                 String condition = obj[0];
    181                 Boolean isLocalized = !obj[2].equals("");
    182 
    183                 // Prefer localized over non-localized links, if they're otherwise the same
    184                 if(!isLocalized && linksList.indexOf(condition + obj[1] + languageCode + " ") >= 0)
    185                     continue;
    186 
    187                 boolean included = false;
    188 
    189                 if(myVersion == 0)
    190                   included = true;
    191                 else if(condition.equals(">="))
    192                   included=myVersion >= targetVersion;
    193                 else if(condition.equals(">"))
    194                   included = myVersion > targetVersion;
    195                 else if(condition.equals("<"))
    196                   included=myVersion < targetVersion;
    197                 else
    198                   included = myVersion <= targetVersion;
    199 
    200                 if(!included) continue;
    201 
    202                 boolean isHelp = targetVersion == 1;
    203                 String urlStart = baseurl + "/wiki/";
    204                 String urlEnd = "MessageOfTheDay" + condition + targetVersion
    205                                     + (isHelp ? "" : "?format=txt");
    206                 String urlLoc = urlStart + languageCode + urlEnd;
    207                 String urlIntl = urlStart + urlEnd;
    208 
    209                 // This adds all links to the worker which will download them concurrently
    210                 linkContents.add(slave.submit(new readMOTD(isLocalized, baseurl, urlLoc, urlIntl, isHelp)));
    211             }
    212             // Gets newest version numbers
    213             linkContents.add(slave.submit(new readMOTD(false, baseurl, "",
    214                     baseurl + "/version?format=txt", true)));
    215 
    216             for(int i=0; i < linkContents.size()-1; i++) {
    217                 try {
    218                     motd += linkContents.get(i).get();
    219                 } catch (Exception e) {}
    220             }
    221 
    222             motd = "<html>"
    223                 + styles
    224                 + "<h1>JOSM - "
    225                 + tr("Java OpenStreetMap Editor")
    226                 + "</h1>"
    227                 + motd.replace("</html>", "")
    228                 + getVersionNumber(linkContents.get(linkContents.size()-1))
    229                 + "</html>";
    230 
    231             linkContents.clear();
    23287            try {
    233                 slave.shutdown();
    234             } catch(SecurityException x) {}
    235 
     88                vers = wr.read(baseurl + "/version?format=txt");
     89                motd = motd.replace("</html>", getVersionNumber(vers)+"</html>");
     90            } catch (IOException ioe) {}
    23691            // Save this to prefs in case JOSM is updated so MOTD can be refreshed
    23792            Main.pref.putInteger("cache.motd.html.version", myVersion);
     
    257112         * @return String with HTML Code
    258113         */
    259         private String getVersionNumber(Future<String> linkContent) {
     114        private String getVersionNumber(String str) {
    260115            try {
    261                 String str = linkContent.get();
    262116                Matcher m = Pattern.compile(".*josm-tested\\.jar: *(\\d+).*", Pattern.DOTALL).matcher(str);
    263117                m.matches();
Note: See TracChangeset for help on using the changeset viewer.