Ignore:
Timestamp:
2009-08-02T14:36:40+02:00 (15 years ago)
Author:
Gubaer
Message:

towards a fix for #3142: JOSM applet class no longer functional

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/WikiReader.java

    r1755 r1879  
    44import java.io.BufferedReader;
    55import java.io.IOException;
     6import java.io.InputStream;
    67import java.io.InputStreamReader;
    78import java.net.URL;
     
    1011import org.openstreetmap.josm.tools.LanguageInfo;
    1112
     13import static org.openstreetmap.josm.tools.I18n.tr;
     14
    1215/**
    1316 * Read a trac-wiki page.
    14  *
     17 * 
    1518 * @author imi
    1619 */
     
    2932    /**
    3033     * Read the page specified by the url and return the content.
    31      *
    32      * If the url is within the baseurl path, parse it as an trac wikipage and
    33      * replace relative pathes etc..
    34      *
     34     * 
     35     * If the url is within the baseurl path, parse it as an trac wikipage and replace relative
     36     * pathes etc..
     37     * 
    3538     * @return Either the string of the content of the wiki page.
    3639     * @throws IOException Throws, if the page could not be loaded.
     
    4548    public String readLang(String text) {
    4649        String languageCode = LanguageInfo.getLanguageCodeWiki();
    47         String url = baseurl + "/wiki/"+languageCode+text;
     50        String url = baseurl + "/wiki/" + languageCode + text;
    4851        String res = "";
     52        InputStream in = null;
    4953        try {
    50             res = readFromTrac(new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8")));
    51         } catch (IOException ioe) {}
    52         if(res.length() == 0 && languageCode.length() != 0)
    53         {
    54             url = baseurl + "/wiki/"+text;
     54            in = new URL(url).openStream();
     55            res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
     56        } catch (IOException ioe) {
     57            System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe
     58                    .toString()));
     59        } catch(SecurityException e) {
     60            System.out.println(tr(
     61                    "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e
     62                    .toString()));
     63        } finally {
     64            if (in != null) {
     65                try {
     66                    in.close();
     67                } catch (IOException e) {
     68                }
     69            }
     70        }
     71        if (res.length() == 0 && languageCode.length() != 0) {
     72            url = baseurl + "/wiki/" + text;
    5573            try {
    56                 res = readFromTrac(new BufferedReader(new InputStreamReader(new URL(url).openStream(), "utf-8")));
    57             } catch (IOException ioe) {}
     74                in = new URL(url).openStream();
     75            } catch (IOException e) {
     76                System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, e
     77                        .toString()));
     78                return res;
     79            } catch (SecurityException e) {
     80                System.out.println(tr(
     81                        "Warning: failed to read MOTD from ''{0}'' for security reasons. Exception was: {1}", url, e
     82                        .toString()));
     83                return res;
     84            }
     85            try {
     86                res = readFromTrac(new BufferedReader(new InputStreamReader(in, "utf-8")));
     87            } catch (IOException ioe) {
     88                System.out.println(tr("Warning: failed to read MOTD from ''{0}''. Exception was: {1}", url, ioe
     89                        .toString()));
     90                return res;
     91            } finally {
     92                if (in != null) {
     93                    try {
     94                        in.close();
     95                    } catch (IOException e) {
     96                    }
     97                }
     98            }
    5899        }
    59100        return res;
     
    63104        String b = "";
    64105        for (String line = in.readLine(); line != null; line = in.readLine()) {
    65             if(!line.contains("[[TranslatedPages]]"))
     106            if (!line.contains("[[TranslatedPages]]")) {
    66107                b += line.replaceAll(" />", ">") + "\n";
     108            }
    67109        }
    68110        return "<html>" + b + "</html>";
     
    74116        String b = "";
    75117        for (String line = in.readLine(); line != null; line = in.readLine()) {
    76             if (line.contains("<div id=\"searchable\">"))
     118            if (line.contains("<div id=\"searchable\">")) {
    77119                inside = true;
    78             else if (line.contains("<div class=\"wiki-toc trac-nav\""))
     120            } else if (line.contains("<div class=\"wiki-toc trac-nav\"")) {
    79121                transl = true;
    80             else if (line.contains("<div class=\"wikipage searchable\">"))
     122            } else if (line.contains("<div class=\"wikipage searchable\">")) {
    81123                inside = true;
    82             else if (line.contains("<div class=\"buttons\">"))
     124            } else if (line.contains("<div class=\"buttons\">")) {
    83125                inside = false;
     126            }
    84127            if (inside && !transl) {
    85                 b += line.replaceAll("<img src=\"/", "<img src=\""+baseurl+"/")
    86                          .replaceAll("href=\"/", "href=\""+baseurl+"/")
    87                          .replaceAll(" />", ">") + "\n";
     128                b += line.replaceAll("<img src=\"/", "<img src=\"" + baseurl + "/").replaceAll("href=\"/",
     129                        "href=\"" + baseurl + "/").replaceAll(" />", ">")
     130                        + "\n";
     131            } else if (transl && line.contains("</div>")) {
     132                transl = false;
    88133            }
    89             else if (transl && line.contains("</div>"))
    90                 transl = false;
    91134        }
    92         if(b.indexOf("      Describe ") >= 0)
     135        if (b.indexOf("      Describe ") >= 0)
    93136            return "";
    94137        return "<html>" + b + "</html>";
Note: See TracChangeset for help on using the changeset viewer.