Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public enum Feature {
- BIOFUEL("Biofuel"),
- GEOTHERMAL("Geothermal"),
- LEED_CERTIFICATION("LEED Certification"),
- LOW_IMPACT_MATERIALS("Low Impact Materials"),
- RAINWATER_RECOVERY("Rainwater Recovery"),
- SOLAR_ENERGY("Solar Energy"),
- SUSTAINABLE_AGRICULTURE("Sustainable Agriculture"),
- WASTEWATER_TREATMENT("Wastewater Treatment");
- private String displayName;
- public String getDisplayName() {
- return displayName;
- }
- @Override
- public String toString() {
- return getDisplayName();
- }
- private Feature(String displayName) {
- this.displayName = displayName;
- }
- public static Feature parseFeature(String displayName) {
- for (Feature f : Feature.values()) {
- if (f.getDisplayName().equals(displayName)) {
- return f;
- }
- }
- throw new IllegalArgumentException("No Feature with displayName: \"" + displayName + "\"");
- }
- }
- public class MapInfoParser extends DefaultHandler {
- // Store POI that have been parsed;
- private List<PointOfInterest> reg;
- // Store POI that we're currently parsing
- private PointOfInterest poi;
- // Remember information being parsed
- private StringBuffer accumulator;
- private Double latAccumulator;
- private List<Feature> featuresAccumulator = new ArrayList<Feature>();
- public MapInfoParser(List<PointOfInterest> reg) {
- this.reg = reg;
- }
- /**
- * Called at the start of the document (as the name suggests)
- */
- @Override
- public void startDocument() {
- // Just so you can visualize how the parser is working
- System.out.println("Start Document!");
- // Use accumulator to remember information parsed. Just initialize for
- // now.
- accumulator = new StringBuffer();
- }
- /**
- * Called when the parsing of an element starts.
- *
- * Lookup documentation to learn meanings of parameters.
- */
- @Override
- public void startElement(String namespaceURI, String localName,
- String qName, Attributes atts) {
- // Just so you can visualize how the parser is working
- System.out.println("StartElement: " + qName);
- // What are we parsing?
- if (qName.toLowerCase().equals("poi")) {
- // Its a POI! Create a poi object with the parsed Id and DisplayName
- poi = new PointOfInterest(atts.getValue("Id"), atts.getValue("DisplayName"));
- // Just so you can visualize how the parser is working
- System.out.println("Created a new poi with Id = " + atts.getValue("Id") + " and DisplayName = " + atts.getValue("DisplayName"));
- }
- // Let's start the accumulator
- // to remember the value that gets parsed
- accumulator.setLength(0);
- }
- /**
- * Called for values of elements
- *
- * Lookup documentation to learn meanings of parameters.
- */
- public void characters(char[] temp, int start, int length) {
- // Remember the value parsed
- accumulator.append(temp, start, length);
- }
- /**
- * Called when the end of an element is seen.
- *
- * Lookup documentation to learn meanings of parameters.
- */
- public void endElement(String uri, String localName, String qName) {
- String data = accumulator.toString().trim();
- // Just so you can visualize how the parser is working
- System.out.println("EndElement: " + qName + " value: " + data);
- if(qName.toLowerCase().equals("address")) {
- // just parsed an address
- poi.setAddress(data);
- // just parsed a lat but have to wait for long
- } else if (qName.toLowerCase().equals("lat")){
- latAccumulator = Double.parseDouble(data);
- // just parsed a long with the lat before
- } else if (qName.toLowerCase().equals("long")) {
- poi.setLatLong(new LatLong (latAccumulator, Double.parseDouble(data)));
- latAccumulator = null;
- // just parsed a description
- } else if (qName.toLowerCase().equals("description")) {
- poi.setDescription(data);
- // just added feature features
- } else if (qName.toLowerCase().equals("feature")) {
- poi.setFeatures(Feature.parseFeature(data));
- } else if (qName.toLowerCase().equals("poi")) {
- // just finished parsing a poi
- reg.add(poi);
- }
- // Reset the accumulator because we have seen the value
- accumulator.setLength(0);
- }
- /**
- * Called when the end of the document is reached
- */
- public void endDocument() {
- // Just so you can visualize how the parser is working
- System.out.println("End Document!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement