Changeset 5915 in josm for trunk


Ignore:
Timestamp:
2013-04-29T17:25:45+02:00 (12 years ago)
Author:
stoecker
Message:

use 3 step wiki loading fallback, cleanup handling of language fallbacks

Location:
trunk/src/org/openstreetmap/josm
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/PasteTagsAction.java

    r5890 r5915  
    2424import org.openstreetmap.josm.data.osm.TagCollection;
    2525import org.openstreetmap.josm.gui.conflict.tags.PasteTagsConflictResolverDialog;
    26 import org.openstreetmap.josm.gui.help.HelpUtil;
    2726import org.openstreetmap.josm.tools.Shortcut;
    2827import org.openstreetmap.josm.tools.TextTagParser;
     
    4039
    4140    private static final String help = ht("/Action/PasteTags");
    42     private static final String helpUrl = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(help));
    4341   
    4442    public PasteTagsAction() {
     
    267265        List<Command> commands = new ArrayList<Command>();
    268266        if (tags==null || tags.isEmpty()) {
    269             TextTagParser.showBadBufferMessage(helpUrl);
     267            TextTagParser.showBadBufferMessage(help);
    270268            return false;
    271269        }
  • trunk/src/org/openstreetmap/josm/gui/help/HelpBrowser.java

    r5886 r5915  
    5050import org.openstreetmap.josm.gui.widgets.JosmEditorPane;
    5151import org.openstreetmap.josm.tools.ImageProvider;
     52import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
    5253import org.openstreetmap.josm.tools.OpenBrowser;
    5354import org.openstreetmap.josm.tools.Utils;
     
    261262                relativeHelpTopic,
    262263                Locale.getDefault().getDisplayName(),
    263                 getHelpTopicEditUrl(buildAbsoluteHelpTopic(relativeHelpTopic)),
    264                 getHelpTopicEditUrl(buildAbsoluteHelpTopic(relativeHelpTopic, Locale.ENGLISH))
     264                getHelpTopicEditUrl(buildAbsoluteHelpTopic(relativeHelpTopic, LocaleType.DEFAULT)),
     265                getHelpTopicEditUrl(buildAbsoluteHelpTopic(relativeHelpTopic, LocaleType.ENGLISH))
    265266        );
    266267        loadTopic(message);
     
    294295     */
    295296    protected void loadRelativeHelpTopic(String relativeHelpTopic) {
    296         String url = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(relativeHelpTopic));
     297        String url = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(relativeHelpTopic, LocaleType.DEFAULTNOTENGLISH));
    297298        String content = null;
    298299        try {
    299300            content = reader.fetchHelpTopicContent(url, true);
    300301        } catch(MissingHelpContentException e) {
    301             url = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(relativeHelpTopic, Locale.ENGLISH));
     302            url = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(relativeHelpTopic, LocaleType.BASELANGUAGE));
    302303            try {
    303304                content = reader.fetchHelpTopicContent(url, true);
    304305            } catch(MissingHelpContentException e1) {
    305                 this.url = url;
    306                 handleMissingHelpContent(relativeHelpTopic);
    307                 return;
     306                url = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(relativeHelpTopic, LocaleType.ENGLISH));
     307                try {
     308                    content = reader.fetchHelpTopicContent(url, true);
     309                } catch(MissingHelpContentException e2) {
     310                    this.url = url;
     311                    handleMissingHelpContent(relativeHelpTopic);
     312                    return;
     313                } catch(HelpContentReaderException e2) {
     314                    e2.printStackTrace();
     315                    handleHelpContentReaderException(relativeHelpTopic, e2);
     316                    return;
     317                }
    308318            } catch(HelpContentReaderException e1) {
    309319                e1.printStackTrace();
    310                 handleHelpContentReaderException(relativeHelpTopic,e1);
     320                handleHelpContentReaderException(relativeHelpTopic, e1);
    311321                return;
    312322            }
  • trunk/src/org/openstreetmap/josm/gui/help/HelpContentReader.java

    r5874 r5915  
    4242     */
    4343    public String fetchHelpTopicContent(String helpTopicUrl, boolean dotest) throws HelpContentReaderException {
     44        if(helpTopicUrl == null)
     45            throw new MissingHelpContentException();
    4446        HttpURLConnection con = null;
    4547        BufferedReader in = null;
  • trunk/src/org/openstreetmap/josm/gui/help/HelpUtil.java

    r5899 r5915  
    1414import org.openstreetmap.josm.actions.HelpAction;
    1515import org.openstreetmap.josm.tools.LanguageInfo;
     16import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
    1617
    1718public class HelpUtil {
     
    4041     * @param absoluteHelpTopic the absolute help topic
    4142     * @return the url
    42      * @see #buildAbsoluteHelpTopic(String)
    43      * @see #buildAbsoluteHelpTopic(String, Locale)
     43     * @see #buildAbsoluteHelpTopic
    4444     */
    4545    static public String getHelpTopicUrl(String absoluteHelpTopic) {
     46        if(absoluteHelpTopic == null)
     47            return null;
    4648        String ret = getWikiBaseHelpUrl();
    4749        ret = ret.replaceAll("\\/+$", "");
    48         absoluteHelpTopic  =absoluteHelpTopic.replace(" ", "%20");
     50        absoluteHelpTopic = absoluteHelpTopic.replace(" ", "%20");
    4951        absoluteHelpTopic = absoluteHelpTopic.replaceAll("^\\/+", "/");
    5052        return ret + absoluteHelpTopic;
     
    7274    static public String extractRelativeHelpTopic(String url) {
    7375        String topic = extractAbsoluteHelpTopic(url);
    74         if (topic == null) return null;
    75         String pattern = "/[A-Z][a-z]:" + getHelpTopicPrefix(Locale.ENGLISH).replaceAll("^\\/+", "");
    76         if (url.matches(pattern))
     76        if (topic == null)
     77            return null;
     78        String pattern = "/[A-Z][a-z]{1,2}(_[A-Z]{2})?:" + getHelpTopicPrefix(LocaleType.ENGLISH).replaceAll("^\\/+", "");
     79        if (url.matches(pattern)) {
    7780            return topic.substring(pattern.length());
     81        }
    7882        return null;
    7983    }
     
    8993        if (!url.startsWith(getWikiBaseHelpUrl())) return null;
    9094        url = url.substring(getWikiBaseHelpUrl().length());
    91         String prefix = getHelpTopicPrefix(Locale.ENGLISH);
     95        String prefix = getHelpTopicPrefix(LocaleType.ENGLISH);
    9296        if (url.startsWith(prefix))
    9397            return url;
    9498
    95         String pattern = "/[A-Z][a-z]:" + prefix.replaceAll("^\\/+", "");
     99        String pattern = "/[A-Z][a-z]{1,2}(_[A-Z]{2})?:" + prefix.replaceAll("^\\/+", "");
    96100        if (url.matches(pattern))
    97101            return url;
    98102
    99103        return null;
    100     }
    101 
    102     /**
    103      * Replies the help topic prefix for the current locale. Examples:
    104      * <ul>
    105      *   <li>/Help if the current locale is a locale with language "en"</li>
    106      *   <li>/De:Help if the current locale is a locale with language "de"</li>
    107      * </ul>
    108      *
    109      * @return the help topic prefix
    110      * @see #getHelpTopicPrefix(Locale)
    111      */
    112     static public String getHelpTopicPrefix() {
    113         return getHelpTopicPrefix(Locale.getDefault());
    114104    }
    115105
     
    121111     * </ul>
    122112     *
    123      * @param locale the locale. {@link Locale#ENGLISH} assumed, if null.
     113     * @param type the type of the locale to use
    124114     * @return the help topic prefix
    125      * @see #getHelpTopicPrefix(Locale)
    126      */
    127     static public String getHelpTopicPrefix(Locale locale) {
    128         if (locale == null) {
    129             locale = Locale.ENGLISH;
    130         }
    131         String ret = Main.pref.get("help.pathhelp", "/Help");
    132         ret = ret.replaceAll("^\\/+", ""); // remove leading /
    133         ret = "/" + LanguageInfo.getWikiLanguagePrefix(locale) + ret;
    134         return ret;
     115     */
     116    static private String getHelpTopicPrefix(LocaleType type) {
     117        String ret = LanguageInfo.getWikiLanguagePrefix(type);
     118        if(ret == null)
     119            return ret;
     120        ret = "/" + ret + Main.pref.get("help.pathhelp", "/Help").replaceAll("^\\/+", ""); // remove leading /;
     121        return ret.replaceAll("\\/+", "\\/"); // collapse sequences of //
    135122    }
    136123
     
    142129     *
    143130     * @param topic the relative help topic. Home help topic assumed, if null.
    144      * @param locale the locale. {@link Locale#ENGLISH} assumed, if null.
     131     * @param type the locale. {@link Locale#ENGLISH} assumed, if null.
    145132     * @return the absolute, localized help topic
    146133     */
    147     static public String buildAbsoluteHelpTopic(String topic, Locale locale) {
    148         if (locale == null) {
    149             locale = Locale.ENGLISH;
    150         }
    151         if (topic == null || topic.trim().length() == 0 || topic.trim().equals("/"))
    152             return getHelpTopicPrefix(locale);
    153         String ret = getHelpTopicPrefix(locale);
    154         if (topic.startsWith("/")) {
    155             ret += topic;
    156         } else {
    157             ret += "/" + topic;
    158         }
    159         ret = ret.replaceAll("\\/+", "\\/"); // just in case, collapse sequences of //
    160         return ret;
    161     }
    162 
    163     /**
    164      * Replies the absolute, localized help topic for the given topic and the
    165      * current locale.
    166      *
    167      * @param topic the relative help topic. Home help topic assumed, if null.
    168      * @return the absolute, localized help topic
    169      * @see Locale#getDefault()
    170      * @see #buildAbsoluteHelpTopic(String, Locale)
    171      */
    172     static public String buildAbsoluteHelpTopic(String topic) {
    173         return buildAbsoluteHelpTopic(topic, Locale.getDefault());
     134    static public String buildAbsoluteHelpTopic(String topic, LocaleType type) {
     135        String prefix = getHelpTopicPrefix(type);
     136        if (prefix == null || topic == null || topic.trim().length() == 0 || topic.trim().equals("/"))
     137            return prefix;
     138        prefix += "/" + topic;
     139        return prefix.replaceAll("\\/+", "\\/"); // collapse sequences of //
    174140    }
    175141
  • trunk/src/org/openstreetmap/josm/tools/LanguageInfo.java

    r5275 r5915  
    22package org.openstreetmap.josm.tools;
    33
    4 import static org.openstreetmap.josm.tools.I18n.tr;
     4import static org.openstreetmap.josm.tools.I18n.marktr;
     5
     6import org.openstreetmap.josm.Main;
    57
    68import java.util.Locale;
    79
    810public class LanguageInfo {
     11    /** Type of the locale to use */
     12    public enum LocaleType {
     13        /** The current default language */
     14        DEFAULT,
     15        /** The current default language, but not english */
     16        DEFAULTNOTENGLISH,
     17        /** The base language (i.e. pt for pt_BR) */
     18        BASELANGUAGE,
     19        /** The standard english texts */
     20        ENGLISH
     21    };
    922
    1023    /**
    1124     * Replies the wiki language prefix for the given locale. The wiki language
    1225     * prefix has the form 'Xy:' where 'Xy' is a ISO 639 language code in title
    13      * case.
     26     * case (or Xy_AB: for sub languages).
    1427     *
    15      * @param locale  the locale
    16      * @return the wiki language prefix
     28     * @param type the type
     29     * @return the wiki language prefix or {@code null} for {@link LocaleType#BASELANGUAGE}, when
     30     * base language is identical to default or english
     31     * @since 8636
    1732     */
    18     static public String getWikiLanguagePrefix(Locale locale) {
    19         String code = getJOSMLocaleCode(locale);
    20         if (code.length() == 2) {
    21             if (code.equals("en")) return "";
    22         } else if (code.equals("zh_TW") || code.equals("zh_CN")) {
    23             /* do nothing */
    24         } else if (code.matches("[^_]+_[^_]+")) {
    25             code = code.substring(0,2);
    26             if (code.equals("en")) return "";
    27         } else {
    28             System.err.println(tr("Warning: failed to derive wiki language prefix from JOSM locale code ''{0}''. Using default code ''en''.", code));
    29             return "";
    30         }
     33    static public String getWikiLanguagePrefix(LocaleType type) {
     34        if(type == LocaleType.ENGLISH)
     35          return "";
     36
     37        String code = getJOSMLocaleCode();
     38        if(type == LocaleType.BASELANGUAGE) {
     39            if(code.matches("[^_]+_[^_]+")) {
     40                code = code.substring(0,2);
     41                if(code == "en")
     42                    return null;
     43            } else {
     44                return null;
     45            }
     46        } else if(type == LocaleType.DEFAULTNOTENGLISH && code == "en")
     47            return null;
    3148        return code.substring(0,1).toUpperCase() + code.substring(1) + ":";
    3249    }
     
    3754     * @return the wiki language prefix
    3855     * @see Locale#getDefault()
    39      * @see #getWikiLanguagePrefix(Locale)
     56     * @see #getWikiLanguagePrefix(LocaleType)
    4057     */
    4158    static public String getWikiLanguagePrefix() {
    42         return getWikiLanguagePrefix(Locale.getDefault());
     59        return getWikiLanguagePrefix(LocaleType.DEFAULT);
    4360    }
    4461
  • trunk/src/org/openstreetmap/josm/tools/TextTagParser.java

    r5813 r5915  
    1111import javax.swing.JPanel;
    1212import org.openstreetmap.josm.Main;
     13import org.openstreetmap.josm.gui.help.HelpUtil;
    1314import org.openstreetmap.josm.gui.ExtendedDialog;
    1415import org.openstreetmap.josm.io.XmlWriter;
     16import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
    1517
    1618import static org.openstreetmap.josm.tools.I18n.tr;
     
    279281    /**
    280282     * Shows message that the buffer can not be pasted, allowing user to clean the buffer
    281      * @param helpUrl
     283     * @param helpTopic the help topic of the parent action
     284     * TODO: Replace by proper HelpAwareOptionPane instead of self-made help link
    282285     */
    283     public static void showBadBufferMessage(String helpUrl) {
     286    public static void showBadBufferMessage(String helpTopic) {
    284287        String msg = tr("<html><p> Sorry, it is impossible to paste tags from buffer. It does not contain any JOSM object"
    285288            + " or suitable text. </p></html>");
    286289        JPanel p = new JPanel(new GridBagLayout());
    287290        p.add(new JLabel(msg),GBC.eop());
     291        String helpUrl = HelpUtil.getHelpTopicUrl(HelpUtil.buildAbsoluteHelpTopic(helpTopic, LocaleType.DEFAULT));
    288292        if (helpUrl != null) {
    289293            p.add(new UrlLabel(helpUrl), GBC.eop());
  • trunk/src/org/openstreetmap/josm/tools/WikiReader.java

    r5874 r5915  
    77
    88import org.openstreetmap.josm.Main;
     9import org.openstreetmap.josm.tools.LanguageInfo.LocaleType;
    910
    1011/**
     
    4546
    4647    public String readLang(String text) throws IOException {
    47         String languageCode = LanguageInfo.getWikiLanguagePrefix();
    48         String res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
    49         if (res.isEmpty() && !languageCode.isEmpty()) {
    50             res = readLang(new URL(baseurl + "/wiki/" + text));
     48        String languageCode;
     49        String res = "";
     50
     51        languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.DEFAULTNOTENGLISH);
     52        if(languageCode != null) {
     53            res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
    5154        }
    52         if (res.isEmpty()) {
     55
     56        if(res.isEmpty()) {
     57            languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.BASELANGUAGE);
     58            if(languageCode != null) {
     59                res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
     60            }
     61        }
     62
     63        if(res.isEmpty()) {
     64            languageCode = LanguageInfo.getWikiLanguagePrefix(LocaleType.ENGLISH);
     65            if(languageCode != null) {
     66                res = readLang(new URL(baseurl + "/wiki/" + languageCode + text));
     67            }
     68        }
     69
     70        if(res.isEmpty()) {
    5371            throw new IOException(text + " does not exist");
    5472        } else {
     
    101119                // will render a thick  border around images inside an <a> element
    102120                //
    103                 b += line.replaceAll("<img src=\"/", "<img border=\"0\" src=\"" + baseurl + "/").replaceAll("href=\"/",
    104                         "href=\"" + baseurl + "/").replaceAll(" />", ">")
    105                         + "\n";
     121                b += line.replaceAll("<img ", "<img border=\"0\" ").replaceAll(" />", ">") + "\n";
    106122            } else if (transl && line.contains("</div>")) {
    107123                transl = false;
     
    114130        || b.indexOf(" does not exist. You can create it here.</p>") >= 0)
    115131            return "";
    116         return "<html>" + b + "</html>";
     132        return "<html><base href=\""+baseurl+"\"> " + b + "</html>";
    117133    }
    118134}
Note: See TracChangeset for help on using the changeset viewer.