Ignore:
Timestamp:
2012-01-26T21:52:34+01:00 (12 years ago)
Author:
jttt
Message:

Use static class were appropriate

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/imagery/ImageryReader.java

    r4713 r4874  
    3030    private String source;
    3131
    32     private enum State { 
     32    private enum State {
    3333        INIT,               // initial state, should always be at the bottom of the stack
    3434        IMAGERY,            // inside the imagery element
     
    6363    }
    6464
    65     private class Parser extends DefaultHandler {
     65    private static class Parser extends DefaultHandler {
    6666        private StringBuffer accumulator = new StringBuffer();
    6767
     
    9797            State newState = null;
    9898            switch (states.peek()) {
    99                 case INIT:
    100                     if (qName.equals("imagery")) {
    101                         newState = State.IMAGERY;
    102                     }
    103                     break;
    104                 case IMAGERY:
    105                     if (qName.equals("entry")) {
    106                         entry = new ImageryInfo();
    107                         skipEntry = false;
    108                         newState = State.ENTRY;
    109                     }
    110                     break;
    111                 case ENTRY:
    112                     if (Arrays.asList(new String[] {
     99            case INIT:
     100                if (qName.equals("imagery")) {
     101                    newState = State.IMAGERY;
     102                }
     103                break;
     104            case IMAGERY:
     105                if (qName.equals("entry")) {
     106                    entry = new ImageryInfo();
     107                    skipEntry = false;
     108                    newState = State.ENTRY;
     109                }
     110                break;
     111            case ENTRY:
     112                if (Arrays.asList(new String[] {
    113113                        "name",
    114114                        "type",
     
    126126                        "country-code",
    127127                        "icon",
    128                     }).contains(qName)) {
    129                         newState = State.ENTRY_ATTRIBUTE;
    130                     } else if (qName.equals("bounds")) {
    131                         try {
    132                             bounds = new ImageryBounds(
    133                                     atts.getValue("min-lat") + "," +
    134                                     atts.getValue("min-lon") + "," +
    135                                     atts.getValue("max-lat") + "," +
    136                                     atts.getValue("max-lon"), ",");
    137                         } catch (IllegalArgumentException e) {
    138                             break;
    139                         }
    140                         newState = State.BOUNDS;
    141                     } else if (qName.equals("projections")) {
    142                         projections = new ArrayList<String>();
    143                         newState = State.PROJECTIONS;
    144                     }
    145                     break;
    146                 case BOUNDS:
    147                     if (qName.equals("shape")) {
    148                         shape = new Shape();
    149                         newState = State.SHAPE;
    150                     }
    151                     break;
    152                 case SHAPE:
    153                     if (qName.equals("point")) {
    154                         try {
    155                             shape.addPoint(atts.getValue("lat"), atts.getValue("lon"));
    156                         } catch (IllegalArgumentException e) {
    157                             break;
    158                         }
    159                     }
    160                     break;
    161                 case PROJECTIONS:
    162                     if (qName.equals("code")) {
    163                         newState = State.CODE;
    164                     }
    165                     break;
     128                }).contains(qName)) {
     129                    newState = State.ENTRY_ATTRIBUTE;
     130                } else if (qName.equals("bounds")) {
     131                    try {
     132                        bounds = new ImageryBounds(
     133                                atts.getValue("min-lat") + "," +
     134                                        atts.getValue("min-lon") + "," +
     135                                        atts.getValue("max-lat") + "," +
     136                                        atts.getValue("max-lon"), ",");
     137                    } catch (IllegalArgumentException e) {
     138                        break;
     139                    }
     140                    newState = State.BOUNDS;
     141                } else if (qName.equals("projections")) {
     142                    projections = new ArrayList<String>();
     143                    newState = State.PROJECTIONS;
     144                }
     145                break;
     146            case BOUNDS:
     147                if (qName.equals("shape")) {
     148                    shape = new Shape();
     149                    newState = State.SHAPE;
     150                }
     151                break;
     152            case SHAPE:
     153                if (qName.equals("point")) {
     154                    try {
     155                        shape.addPoint(atts.getValue("lat"), atts.getValue("lon"));
     156                    } catch (IllegalArgumentException e) {
     157                        break;
     158                    }
     159                }
     160                break;
     161            case PROJECTIONS:
     162                if (qName.equals("code")) {
     163                    newState = State.CODE;
     164                }
     165                break;
    166166            }
    167167            /**
     
    189189        public void endElement(String namespaceURI, String qName, String rqName) {
    190190            switch (states.pop()) {
    191                 case INIT:
    192                     throw new RuntimeException("parsing error: more closing than opening elements");
    193                 case ENTRY:
    194                     if (qName.equals("entry")) {
    195                         if (!skipEntry) {
    196                             entries.add(entry);
     191            case INIT:
     192                throw new RuntimeException("parsing error: more closing than opening elements");
     193            case ENTRY:
     194                if (qName.equals("entry")) {
     195                    if (!skipEntry) {
     196                        entries.add(entry);
     197                    }
     198                    entry = null;
     199                }
     200                break;
     201            case ENTRY_ATTRIBUTE:
     202                if (qName.equals("name")) {
     203                    entry.setName(tr(accumulator.toString()));
     204                } else if (qName.equals("type")) {
     205                    boolean found = false;
     206                    for (ImageryType type : ImageryType.values()) {
     207                        if (equal(accumulator.toString(), type.getUrlString())) {
     208                            entry.setImageryType(type);
     209                            found = true;
     210                            break;
    197211                        }
    198                         entry = null;
    199                     }
    200                     break;
    201                 case ENTRY_ATTRIBUTE:
    202                     if (qName.equals("name")) {
    203                         entry.setName(tr(accumulator.toString()));
    204                     } else if (qName.equals("type")) {
    205                         boolean found = false;
    206                         for (ImageryType type : ImageryType.values()) {
    207                             if (equal(accumulator.toString(), type.getUrlString())) {
    208                                 entry.setImageryType(type);
    209                                 found = true;
    210                                 break;
    211                             }
     212                    }
     213                    if (!found) {
     214                        skipEntry = true;
     215                    }
     216                } else if (qName.equals("default")) {
     217                    if (accumulator.toString().equals("true")) {
     218                        entry.setDefaultEntry(true);
     219                    } else if (accumulator.toString().equals("false")) {
     220                        entry.setDefaultEntry(false);
     221                    } else {
     222                        skipEntry = true;
     223                    }
     224                } else if (qName.equals("url")) {
     225                    entry.setUrl(accumulator.toString());
     226                } else if (qName.equals("eula")) {
     227                    entry.setEulaAcceptanceRequired(accumulator.toString());
     228                } else if (qName.equals("min-zoom") || qName.equals("max-zoom")) {
     229                    Integer val = null;
     230                    try {
     231                        val = Integer.parseInt(accumulator.toString());
     232                    } catch(NumberFormatException e) {
     233                        val = null;
     234                    }
     235                    if (val == null) {
     236                        skipEntry = true;
     237                    } else {
     238                        if (qName.equals("min-zoom")) {
     239                            entry.setDefaultMinZoom(val);
     240                        } else {
     241                            entry.setDefaultMaxZoom(val);
    212242                        }
    213                         if (!found) {
    214                             skipEntry = true;
    215                         }
    216                     } else if (qName.equals("default")) {
    217                         if (accumulator.toString().equals("true")) {
    218                             entry.setDefaultEntry(true);
    219                         } else if (accumulator.toString().equals("false")) {
    220                             entry.setDefaultEntry(false);
    221                         } else {
    222                             skipEntry = true;
    223                         }
    224                     } else if (qName.equals("url")) {
    225                         entry.setUrl(accumulator.toString());
    226                     } else if (qName.equals("eula")) {
    227                         entry.setEulaAcceptanceRequired(accumulator.toString());
    228                     } else if (qName.equals("min-zoom") || qName.equals("max-zoom")) {
    229                         Integer val = null;
    230                         try {
    231                             val = Integer.parseInt(accumulator.toString());
    232                         } catch(NumberFormatException e) {
    233                             val = null;
    234                         }
    235                         if (val == null) {
    236                             skipEntry = true;
    237                         } else {
    238                             if (qName.equals("min-zoom")) {
    239                                 entry.setDefaultMinZoom(val);
    240                             } else {
    241                                 entry.setDefaultMaxZoom(val);
    242                             }
    243                         }
    244                     } else if (qName.equals("attribution-text")) {
    245                         entry.setAttributionText(accumulator.toString());
    246                     } else if (qName.equals("attribution-url")) {
    247                         entry.setAttributionLinkURL(accumulator.toString());
    248                     } else if (qName.equals("logo-image")) {
    249                         entry.setAttributionImage(accumulator.toString());
    250                     } else if (qName.equals("logo-url")) {
    251                         entry.setAttributionImageURL(accumulator.toString());
    252                     } else if (qName.equals("terms-of-use-text")) {
    253                         entry.setTermsOfUseText(accumulator.toString());
    254                     } else if (qName.equals("terms-of-use-url")) {
    255                         entry.setTermsOfUseURL(accumulator.toString());
    256                     } else if (qName.equals("country-code")) {
    257                         entry.setCountryCode(accumulator.toString());
    258                     } else if (qName.equals("icon")) {
    259                         entry.setIcon(accumulator.toString());
    260                     } else {
    261                     }
    262                     break;
    263                 case BOUNDS:
    264                     entry.setBounds(bounds);
    265                     bounds = null;
    266                     break;
    267                 case SHAPE:
    268                     bounds.addShape(shape);
    269                     shape = null;
    270                     break;
    271                 case CODE:
    272                     projections.add(accumulator.toString());
    273                     break;
    274                 case PROJECTIONS:
    275                     entry.setServerProjections(projections);
    276                     projections = null;
    277                     break;
     243                    }
     244                } else if (qName.equals("attribution-text")) {
     245                    entry.setAttributionText(accumulator.toString());
     246                } else if (qName.equals("attribution-url")) {
     247                    entry.setAttributionLinkURL(accumulator.toString());
     248                } else if (qName.equals("logo-image")) {
     249                    entry.setAttributionImage(accumulator.toString());
     250                } else if (qName.equals("logo-url")) {
     251                    entry.setAttributionImageURL(accumulator.toString());
     252                } else if (qName.equals("terms-of-use-text")) {
     253                    entry.setTermsOfUseText(accumulator.toString());
     254                } else if (qName.equals("terms-of-use-url")) {
     255                    entry.setTermsOfUseURL(accumulator.toString());
     256                } else if (qName.equals("country-code")) {
     257                    entry.setCountryCode(accumulator.toString());
     258                } else if (qName.equals("icon")) {
     259                    entry.setIcon(accumulator.toString());
     260                } else {
     261                }
     262                break;
     263            case BOUNDS:
     264                entry.setBounds(bounds);
     265                bounds = null;
     266                break;
     267            case SHAPE:
     268                bounds.addShape(shape);
     269                shape = null;
     270                break;
     271            case CODE:
     272                projections.add(accumulator.toString());
     273                break;
     274            case PROJECTIONS:
     275                entry.setServerProjections(projections);
     276                projections = null;
     277                break;
    278278            }
    279279        }
Note: See TracChangeset for help on using the changeset viewer.