Ticket #25043: #25043-Fix-Sonar-issues-in-TextTagParser.patch

File #25043-Fix-Sonar-issues-in-TextTagParser.patch, 6.1 KB (added by DanProgs <Abenteurer.2901@…>, 8 days ago)
  • src/org/openstreetmap/josm/tools/TextTagParser.java

    ### Eclipse Workspace Patch 1.0
    #P josm
    diff --git src/org/openstreetmap/josm/tools/TextTagParser.java src/org/openstreetmap/josm/tools/TextTagParser.java
    index d1b278c..9194122 100644
     
    3636                return k;
    3737            }
    3838        }
    39         String text = k.substring(1, k.length()-1);
     39        final String text = k.substring(1, k.length()-1);
    4040        return new TextAnalyzer(text).parseString("\r\t\n");
    4141    }
    4242
    … …  
    5252     */
    5353    public static Map<String, String> readTagsByRegexp(String text, String splitRegex, String tagRegex,
    5454            boolean unescapeTextInQuotes, boolean unescapePipe) {
    55         String[] lines = text.split(splitRegex, -1);
    56         Pattern p = Pattern.compile(tagRegex);
    57         Map<String, String> tags = new LinkedHashMap<>();
     55        final String[] lines = text.split(splitRegex, -1);
     56        final Pattern p = Pattern.compile(tagRegex);
     57        final Map<String, String> tags = new LinkedHashMap<>();
    5858        String k;
    5959        String v;
    6060        for (String line: lines) {
    61             if (line.isBlank()) continue; // skip empty lines
    62             Matcher m = p.matcher(line);
     61            if (line.isBlank()) {
     62                continue; // skip empty lines
     63            }
     64            final Matcher m = p.matcher(line);
    6365            if (m.matches()) {
    6466                 k = Utils.removeWhiteSpaces(m.group(1));
    6567                 v = Utils.removeWhiteSpaces(m.group(2));
    6668                 if (unescapeTextInQuotes) {
    6769                     k = unescape(k);
    6870                     v = unescape(v);
    69                      if (k == null || v == null) return null;
     71                     if (k == null || v == null) {
     72                        return null;
     73                     }
    7074                 }
    7175                 if (unescapePipe) {
    72                      k = k.replaceAll("\\\\\\|", "|");
    73                      v = v.replaceAll("\\\\\\|", "|");
     76                     k = k.replace("\\|", "|");
     77                     v = v.replace("\\|", "|");
    7478                 }
    7579                 tags.put(k, v);
    7680            } else {
    … …  
    9296     * @since 12683
    9397     */
    9498    public static Map<String, String> getValidatedTagsFromText(String buf, TagWarningCallback callback) {
    95         Map<String, String> tags = readTagsFromText(buf);
     99        final Map<String, String> tags = readTagsFromText(buf);
    96100        return validateTags(tags, callback) ? tags : null;
    97101    }
    98102
    … …  
    108112        // tag1\tval1\ntag2\tval2\n
    109113        tags = readTagsByRegexp(buf, "[\\r\\n]+", ".*?([a-zA-Z0-9:_]+).*\\t(.*?)", false, false);
    110114        // try "tag\tvalue\n" format
    111         if (tags != null) return tags;
     115        if (tags != null) {
     116            return tags;
     117        }
    112118
    113119        // Format
    114120        // a=b \n c=d \n "a b"=hello
    … …  
    117123        // a = b=c  - this method of parsing fails intentionally
    118124        tags = readTagsByRegexp(buf, "[\\n\\t\\r]+", "(.*?)=(.*?)", true, false);
    119125        // try format  t1=v1\n t2=v2\n ...
    120         if (tags != null) return tags;
     126        if (tags != null) {
     127            return tags;
     128        }
    121129
    122130        // JSON-format
    123131        String bufJson = buf.trim();
    124132        // trim { }, if there are any
    125         if (bufJson.startsWith("{") && bufJson.endsWith("}"))
     133        if (bufJson.startsWith("{") && bufJson.endsWith("}")) {
    126134            bufJson = bufJson.substring(1, bufJson.length()-1);
     135        }
    127136        tags = readTagsByRegexp(bufJson, "[\\s]*,[\\s]*",
    128137                "[\\s]*(\\\".*?[^\\\\]\\\")"+"[\\s]*:[\\s]*"+"(\\\".*?[^\\\\]\\\")[\\s]*", true, false);
    129         if (tags != null) return tags;
     138        if (tags != null) {
     139            return tags;
     140        }
    130141
    131142        // Free format
    132143        // a 1 "b" 2 c=3 d 4 e "5"
    … …  
    142153     */
    143154    public static boolean validateTags(Map<String, String> tags, TagWarningCallback callback) {
    144155        int r;
    145         int s = tags.size();
     156        final int s = tags.size();
    146157        if (s > MAX_KEY_COUNT) {
    147158            // Use trn() even if for english it makes no sense, as s > 30
    148159            r = callback.warning(trn("There was {0} tag found in the buffer, it is suspicious!",
    149160            "There were {0} tags found in the buffer, it is suspicious!", s,
    150161            s), "", "tags.paste.toomanytags");
    151             if (r == 2 || r == 3) return false; if (r == 4) return true;
     162            if (r == 2 || r == 3) {
     163                return false;
     164            } else if (r == 4) {
     165                return true;
     166            }
    152167        }
    153168        for (Entry<String, String> entry : tags.entrySet()) {
    154             String key = entry.getKey();
    155             String value = entry.getValue();
     169            final String key = entry.getKey();
     170            final String value = entry.getValue();
    156171            if (key.length() > MAX_KEY_LENGTH) {
    157172                r = callback.warning(tr("Key is too long (max {0} characters):", MAX_KEY_LENGTH), key+'='+value, "tags.paste.keytoolong");
    158                 if (r == 2 || r == 3) return false; if (r == 4) return true;
     173                if (r == 2 || r == 3) {
     174                    return false;
     175                } else if (r == 4) {
     176                    return true;
     177                }
    159178            }
    160179            if (!key.matches(KEY_PATTERN)) {
    161180                r = callback.warning(tr("Suspicious characters in key:"), key, "tags.paste.keydoesnotmatch");
    162                 if (r == 2 || r == 3) return false; if (r == 4) return true;
     181                if (r == 2 || r == 3) {
     182                    return false;
     183                } else if (r == 4) {
     184                    return true;
     185                }
    163186            }
    164187            if (!Utils.checkCodePointCount(value, MAX_VALUE_LENGTH)) {
    165188                r = callback.warning(tr("Value is too long (max {0} characters):", MAX_VALUE_LENGTH), value, "");
    166                 if (r == 2 || r == 3) return false; if (r == 4) return true;
     189                if (r == 2 || r == 3) {
     190                    return false;
     191                } else if (r == 4) {
     192                    return true;
     193                }
    167194            }
    168195        }
    169196        return true;