Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.72 KB | None | 0 0
  1. public enum Feature {
  2.  
  3.     BIOFUEL("Biofuel"),
  4.     GEOTHERMAL("Geothermal"),
  5.     LEED_CERTIFICATION("LEED Certification"),
  6.     LOW_IMPACT_MATERIALS("Low Impact Materials"),
  7.     RAINWATER_RECOVERY("Rainwater Recovery"),
  8.     SOLAR_ENERGY("Solar Energy"),
  9.     SUSTAINABLE_AGRICULTURE("Sustainable Agriculture"),
  10.     WASTEWATER_TREATMENT("Wastewater Treatment");
  11.  
  12.     private String displayName;
  13.  
  14.     public String getDisplayName() {
  15.         return displayName;
  16.     }
  17.  
  18.     @Override
  19.     public String toString() {
  20.         return getDisplayName();
  21.     }
  22.  
  23.     private Feature(String displayName) {
  24.         this.displayName = displayName;
  25.     }
  26.    
  27.     public static Feature parseFeature(String displayName) {
  28.         for (Feature f : Feature.values()) {
  29.             if (f.getDisplayName().equals(displayName)) {
  30.                 return f;
  31.             }
  32.         }
  33.         throw new IllegalArgumentException("No Feature with displayName: \"" + displayName + "\"");
  34.     }
  35. }
  36.  
  37. public class MapInfoParser extends DefaultHandler {
  38.  
  39.     // Store POI that have been parsed;
  40.     private List<PointOfInterest> reg;
  41.     // Store POI that we're currently parsing
  42.     private PointOfInterest poi;
  43.     // Remember information being parsed
  44.     private StringBuffer accumulator;
  45.  
  46.     private Double latAccumulator;
  47.  
  48.     private List<Feature> featuresAccumulator = new ArrayList<Feature>();
  49.    
  50.     public MapInfoParser(List<PointOfInterest> reg) {
  51.         this.reg = reg;
  52.     }
  53.  
  54.     /**
  55.      * Called at the start of the document (as the name suggests)
  56.      */
  57.     @Override
  58.     public void startDocument() {
  59.         // Just so you can visualize how the parser is working
  60.         System.out.println("Start Document!");
  61.  
  62.         // Use accumulator to remember information parsed. Just initialize for
  63.         // now.
  64.         accumulator = new StringBuffer();
  65.     }
  66.  
  67.     /**
  68.      * Called when the parsing of an element starts.
  69.      *
  70.      * Lookup documentation to learn meanings of parameters.
  71.      */
  72.     @Override
  73.     public void startElement(String namespaceURI, String localName,
  74.                              String qName, Attributes atts) {
  75.  
  76.         // Just so you can visualize how the parser is working
  77.         System.out.println("StartElement: " + qName);
  78.  
  79.         // What are we parsing?
  80.         if (qName.toLowerCase().equals("poi")) {
  81.             // Its a POI! Create a poi object with the parsed Id and DisplayName
  82.             poi = new PointOfInterest(atts.getValue("Id"), atts.getValue("DisplayName"));
  83.  
  84.             // Just so you can visualize how the parser is working
  85.             System.out.println("Created a new poi with Id = " + atts.getValue("Id") + " and DisplayName = " + atts.getValue("DisplayName"));
  86.         }
  87.         // Let's start the accumulator
  88.         // to remember the value that gets parsed
  89.         accumulator.setLength(0);
  90.     }
  91.  
  92.     /**
  93.      * Called for values of elements
  94.      *
  95.      * Lookup documentation to learn meanings of parameters.
  96.      */
  97.     public void characters(char[] temp, int start, int length) {
  98.         // Remember the value parsed
  99.         accumulator.append(temp, start, length);
  100.     }
  101.  
  102.  
  103.  
  104.     /**
  105.      * Called when the end of an element is seen.
  106.      *
  107.      * Lookup documentation to learn meanings of parameters.
  108.      */
  109.     public void endElement(String uri, String localName, String qName) {
  110.         String data = accumulator.toString().trim();
  111.  
  112.         // Just so you can visualize how the parser is working
  113.         System.out.println("EndElement: " + qName + " value: " + data);
  114.  
  115.         if(qName.toLowerCase().equals("address")) {
  116.             // just parsed an address
  117.             poi.setAddress(data);
  118.  
  119.             // just parsed a lat but have to wait for long
  120.         } else if (qName.toLowerCase().equals("lat")){
  121.             latAccumulator = Double.parseDouble(data);
  122.  
  123.           // just parsed a long with the lat before
  124.         } else if (qName.toLowerCase().equals("long")) {
  125.  
  126.             poi.setLatLong(new LatLong (latAccumulator, Double.parseDouble(data)));
  127.             latAccumulator = null;
  128.  
  129.             // just parsed a description
  130.         } else if (qName.toLowerCase().equals("description")) {
  131.             poi.setDescription(data);
  132.  
  133.             // just added feature features
  134.         } else if (qName.toLowerCase().equals("feature")) {
  135.             poi.setFeatures(Feature.parseFeature(data));
  136.  
  137.         } else if (qName.toLowerCase().equals("poi")) {
  138.  
  139.             // just finished parsing a poi
  140.             reg.add(poi);
  141.  
  142.         }
  143.  
  144.         // Reset the accumulator because we have seen the value
  145.         accumulator.setLength(0);
  146.  
  147.     }
  148.  
  149.     /**
  150.      * Called when the end of the document is reached
  151.      */
  152.     public void endDocument() {
  153.         // Just so you can visualize how the parser is working
  154.         System.out.println("End Document!");
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement