Changeset 2510 in josm for trunk


Ignore:
Timestamp:
2009-11-23T20:55:56+01:00 (14 years ago)
Author:
jttt
Message:

SearchCompiler - prepare everything in constructor, don't throw ParseError from match()

File:
1 edited

Legend:

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

    r2407 r2510  
    3131    private boolean caseSensitive = false;
    3232    private boolean regexSearch = false;
    33     private String  rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}");
     33    private static String  rxErrorMsg = marktr("The regex \"{0}\" had a parse error at offset {1}, full error:\n\n{2}");
    3434    private PushbackTokenizer tokenizer;
    3535    private CollectBackReferencesVisitor childBackRefs;
     
    4343
    4444    abstract public static class Match {
    45         abstract public boolean match(OsmPrimitive osm) throws ParseError;
     45        abstract public boolean match(OsmPrimitive osm);
    4646    }
    4747
     
    5555        private final Match match;
    5656        public Not(Match match) {this.match = match;}
    57         @Override public boolean match(OsmPrimitive osm) throws ParseError {
     57        @Override public boolean match(OsmPrimitive osm) {
    5858            return !match.match(osm);
    5959        }
     
    6565        private Match rhs;
    6666        public And(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
    67         @Override public boolean match(OsmPrimitive osm) throws ParseError {
     67        @Override public boolean match(OsmPrimitive osm) {
    6868            return lhs.match(osm) && rhs.match(osm);
    6969        }
     
    7575        private Match rhs;
    7676        public Or(Match lhs, Match rhs) {this.lhs = lhs; this.rhs = rhs;}
    77         @Override public boolean match(OsmPrimitive osm) throws ParseError {
     77        @Override public boolean match(OsmPrimitive osm) {
    7878            return lhs.match(osm) || rhs.match(osm);
    7979        }
     
    9090    }
    9191
    92     private class KeyValue extends Match {
    93         private String key;
    94         private String value;
    95         public KeyValue(String key, String value) {this.key = key; this.value = value; }
    96         @Override public boolean match(OsmPrimitive osm) throws ParseError {
    97 
     92    private static class KeyValue extends Match {
     93        private final String key;
     94        private final Pattern keyPattern;
     95        private final String value;
     96        private final Pattern valuePattern;
     97        private final boolean caseSensitive;
     98
     99        public KeyValue(String key, String value, boolean regexSearch, boolean caseSensitive) throws ParseError {
     100            this.caseSensitive = caseSensitive;
    98101            if (regexSearch) {
     102                int searchFlags = regexFlags(caseSensitive);
     103
     104                try {
     105                    this.keyPattern = Pattern.compile(key, searchFlags);
     106                    this.valuePattern = Pattern.compile(value, searchFlags);
     107                } catch (PatternSyntaxException e) {
     108                    throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
     109                }
     110                this.key = key;
     111                this.value = value;
     112
     113            } else if (caseSensitive) {
     114                this.key = key;
     115                this.value = value;
     116                this.keyPattern = null;
     117                this.valuePattern = null;
     118            } else {
     119                this.key = key.toLowerCase();
     120                this.value = value;
     121                this.keyPattern = null;
     122                this.valuePattern = null;
     123            }
     124        }
     125
     126        @Override public boolean match(OsmPrimitive osm) {
     127
     128            if (keyPattern != null) {
    99129                if (!osm.hasKeys())
    100130                    return false;
     
    107137                 */
    108138
    109                 Pattern searchKey   = null;
    110                 Pattern searchValue = null;
    111                 int searchFlags = regexFlags();
    112 
    113                 try {
    114                     searchKey = Pattern.compile(key, searchFlags);
    115                     searchValue = Pattern.compile(value, searchFlags);
    116                 } catch (PatternSyntaxException e) {
    117                     throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    118                 }
    119 
    120139                for (Entry<String, String> e : osm.entrySet()) {
    121140                    String k = e.getKey();
    122141                    String v = e.getValue();
    123142
    124                     Matcher matcherKey = searchKey.matcher(k);
     143                    Matcher matcherKey = keyPattern.matcher(k);
    125144                    boolean matchedKey = matcherKey.find();
    126145
    127146                    if (matchedKey) {
    128                         Matcher matcherValue = searchValue.matcher(v);
     147                        Matcher matcherValue = valuePattern.matcher(v);
    129148                        boolean matchedValue = matcherValue.find();
    130149
     
    146165
    147166                String v1 = caseSensitive ? value : value.toLowerCase();
    148                 String v2 = caseSensitive ? this.value : this.value.toLowerCase();
    149167
    150168                // is not Java 1.5
    151169                //v1 = java.text.Normalizer.normalize(v1, java.text.Normalizer.Form.NFC);
    152170                //v2 = java.text.Normalizer.normalize(v2, java.text.Normalizer.Form.NFC);
    153                 return v1.indexOf(v2) != -1;
     171                return v1.indexOf(value) != -1;
    154172            }
    155173
     
    224242
    225243        @Override
    226         public boolean match(OsmPrimitive osm) throws ParseError {
     244        public boolean match(OsmPrimitive osm) {
    227245
    228246            if (!osm.hasKeys())
     
    279297    }
    280298
    281     private class Any extends Match {
    282         private String s;
    283         public Any(String s) {this.s = s;}
    284         @Override public boolean match(OsmPrimitive osm) throws ParseError {
    285             if (!osm.hasKeys())
    286                 return s.equals("");
    287 
    288             String search;
    289             Pattern searchRegex = null;
    290 
     299    private static class Any extends Match {
     300        private final String search;
     301        private final Pattern searchRegex;
     302        private final boolean caseSensitive;
     303
     304        public Any(String s, boolean regexSearch, boolean caseSensitive) throws ParseError {
     305            this.caseSensitive = caseSensitive;
    291306            if (regexSearch) {
    292                 search = s;
    293                 int searchFlags = regexFlags();
    294 
    295307                try {
    296                     searchRegex = Pattern.compile(search, searchFlags);
     308                    this.searchRegex = Pattern.compile(s, regexFlags(caseSensitive));
    297309                } catch (PatternSyntaxException e) {
    298310                    throw new ParseError(tr(rxErrorMsg, e.getPattern(), e.getIndex(), e.getMessage()));
    299311                }
    300             } else {
    301                 search = caseSensitive ? s : s.toLowerCase();
    302             }
     312                this.search = s;
     313            } else if (caseSensitive) {
     314                this.search = s;
     315                this.searchRegex = null;
     316            } else {
     317                this.search = s.toLowerCase();
     318                this.searchRegex = null;
     319            }
     320        }
     321
     322        @Override public boolean match(OsmPrimitive osm) {
     323            if (!osm.hasKeys())
     324                return search.equals("");
    303325
    304326            // is not Java 1.5
    305327            //search = java.text.Normalizer.normalize(search, java.text.Normalizer.Form.NFC);
    306328            for (Entry<String, String> e : osm.entrySet()) {
    307                 if (regexSearch) {
     329                if (searchRegex != null) {
    308330                    String key = e.getKey();
    309331                    String value = e.getValue();
     
    343365            return false;
    344366        }
    345         @Override public String toString() {return s;}
     367        @Override public String toString() {
     368            return search;
     369        }
    346370    }
    347371
     
    475499        private Match child;
    476500        public Parent(Match m) { child = m; }
    477         @Override public boolean match(OsmPrimitive osm) throws ParseError {
     501        @Override public boolean match(OsmPrimitive osm) {
    478502            boolean isParent = false;
    479503
     
    516540        }
    517541
    518         @Override public boolean match(OsmPrimitive osm) throws ParseError {
     542        @Override public boolean match(OsmPrimitive osm) {
    519543            boolean isChild = false;
    520544            childBackRefs.initialize();
     
    631655            return new Parent(parseParens());
    632656        else
    633             return new Any(tok);
     657            return new Any(tok, regexSearch, caseSensitive);
    634658    }
    635659
     
    639663        else if (key.equals("user"))
    640664            return new UserMatch(value);
    641         else if (key.equals("tags"))
     665        else if (key.equals("tags")) {
    642666            try {
    643667                String[] range = value.split("-");
     
    651675                throw new ParseError(tr("Incorrect value of tags operator: {0}. Tags operator expects number of tags or range, for example tags:1 or tags:2-5", value));
    652676            }
    653         else if (key.equals("nodes")) {
     677        } else if (key.equals("nodes")) {
    654678            try {
    655679                String[] range = value.split("-");
     
    671695            }
    672696        } else
    673             return new KeyValue(key, value);
    674     }
    675 
    676     private int regexFlags() {
     697            return new KeyValue(key, value, regexSearch, caseSensitive);
     698    }
     699
     700    private static int regexFlags(boolean caseSensitive) {
    677701        int searchFlags = 0;
    678702
Note: See TracChangeset for help on using the changeset viewer.