Ignore:
Timestamp:
2009-05-20T16:08:00+02:00 (15 years ago)
Author:
stoecker
Message:

fix #2612 - changed patch by avar - Cleanup regex search

File:
1 edited

Legend:

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

    r1603 r1607  
    9393        @Override public boolean match(OsmPrimitive osm) throws ParseError {
    9494
    95             if (regexSearch) { 
     95            if (regexSearch) {
    9696                if (osm.keys == null)
    9797                    return false;
     
    106106                Pattern searchKey   = null;
    107107                Pattern searchValue = null;
    108 
    109                 if (caseSensitive) {
    110                     try {
    111                         searchKey = Pattern.compile(key);
    112                     } catch (PatternSyntaxException e) {
    113                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    114                     }
    115                     try {
    116                         searchValue = Pattern.compile(value);
    117                     } catch (PatternSyntaxException e) {
    118                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    119                     }
    120                 } else {
    121                     try {
    122                         searchKey = Pattern.compile(key, Pattern.CASE_INSENSITIVE);
    123                     } catch (PatternSyntaxException e) {
    124                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    125                     }
    126                     try {
    127                         searchValue = Pattern.compile(value, Pattern.CASE_INSENSITIVE);
    128                     } catch (PatternSyntaxException e) {
    129                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    130                     }
     108                int searchFlags = regexFlags();
     109
     110                try {
     111                    searchKey = Pattern.compile(key, searchFlags);
     112                    searchValue = Pattern.compile(value, searchFlags);
     113                } catch (PatternSyntaxException e) {
     114                    throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    131115                }
    132116
     
    183167            if (regexSearch) {
    184168                search = s;
    185                 if (caseSensitive) {
    186                     try {
    187                         searchRegex = Pattern.compile(search);
    188                     } catch (PatternSyntaxException e) {
    189                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    190                     }
    191                 } else {
    192                     try {
    193                         searchRegex = Pattern.compile(search, Pattern.CASE_INSENSITIVE);
    194                     } catch (PatternSyntaxException e) {
    195                         throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    196                     }
     169                int searchFlags = regexFlags();
     170
     171                try {
     172                    searchRegex = Pattern.compile(search, searchFlags);
     173                } catch (PatternSyntaxException e) {
     174                    throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    197175                }
    198176            } else {
     
    275253        @Override public String toString() {return "nodes="+count;}
    276254    }
    277    
     255
    278256    private static class NodeCountRange extends Match {
    279257        private int minCount;
    280258        private int maxCount;
    281         public NodeCountRange(int minCount, int maxCount) { 
     259        public NodeCountRange(int minCount, int maxCount) {
    282260            if(maxCount < minCount) {
    283261                this.minCount = maxCount;
     
    323301        @Override public String toString() {return "untagged";}
    324302    }
    325        
     303
    326304    private static class Parent extends Match {
    327305        private Match child;
     
    334312            if (child == null)
    335313                child = new Always();
    336                
     314
    337315            if (osm instanceof Way) {
    338316                for (Node n : ((Way)osm).nodes)
     
    474452                return new NodeCount(Integer.parseInt(value));
    475453            } catch(Exception x) {}
    476        
     454
    477455            try {
    478456                String[] range = value.split("-", 2);
    479457                return new NodeCountRange(Integer.parseInt(range[0]), Integer.parseInt(range[1]));
    480458            } catch(Exception x) {}
    481            
     459
    482460            return new NodeCount(0);
    483461        } else if (key.equals("id")) {
     
    491469        }
    492470    }
     471
     472    private int regexFlags() {
     473        int searchFlags = 0;
     474
     475        // Enables canonical Unicode equivalence so that e.g. the two
     476        // forms of "\u00e9gal" and "e\u0301gal" will match.
     477        //
     478        // It makes sense to match no matter how the character
     479        // happened to be constructed.
     480        searchFlags |= Pattern.CANON_EQ;
     481
     482        // Make "." match any character including newline (/s in Perl)
     483        searchFlags |= Pattern.DOTALL;
     484
     485        // CASE_INSENSITIVE by itself only matches US-ASCII case
     486        // insensitively, but the OSM data is in Unicode. With
     487        // UNICODE_CASE casefolding is made Unicode-aware.
     488        if (!caseSensitive)
     489            searchFlags |= (Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
     490
     491        return searchFlags;
     492    }
    493493}
Note: See TracChangeset for help on using the changeset viewer.