view src/de/mpiwg/anteater/xml/impl/PlacemakerXMLParser.java @ 0:036535fcd179

anteater
author jdamerow
date Fri, 14 Sep 2012 10:30:43 +0200
parents
children
line wrap: on
line source

package de.mpiwg.anteater.xml.impl;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jdom2.Element;
import org.jdom2.Namespace;

import de.mpiwg.anteater.places.Place;
import de.mpiwg.anteater.places.PlaceInformation;
import de.mpiwg.anteater.xml.IPlaceFinderParser;

public class PlacemakerXMLParser extends JDOMParser implements IPlaceFinderParser {

	public PlacemakerXMLParser(String content) {
		super(content, false);
	}	
	
	public PlacemakerXMLParser(InputStream stream) {
		super(stream);
	}
	
	@Override
	public List<PlaceInformation> parsePlaces() {
		List<PlaceInformation> placeInfos = new ArrayList<PlaceInformation>();
		List<Element> references = executeXPath("/default:contentlocation/default:document/default:referenceList/default:reference", "default");
		if (references == null || references.isEmpty())
			return placeInfos;
		
		List<Element> placeNodes = executeXPath("/default:contentlocation/default:document/default:placeDetails", "default");

		// get all places
		Map<String, Place> places = new HashMap<String, Place>();
		Namespace ns = Namespace.getNamespace("http://wherein.yahooapis.com/v1/schema");
		for (Element placeDetailNode : placeNodes) {
			Element placeNode = placeDetailNode.getChild("place",ns);
			
			if (placeNode != null) {
				Place place = new Place();
				
				
				place.setWoeId(placeNode.getChildText("woeId", ns));
				place.setType(placeNode.getChildText("type", ns));
				place.setName(placeNode.getChildText("name", ns));
				
				Element centroid = placeNode.getChild("centroid", ns);
				if (centroid != null) {
					String latitute = centroid.getChildText("latitude", ns);
					if (latitute != null)
						place.setLatitude(new Float(latitute));
					
					String longitude = centroid.getChildText("longitude", ns);
					if (longitude != null)
						place.setLongitude(new Float(longitude));
				}
				
				String id = placeDetailNode.getChildText("placeId", ns);
				if (id != null) {
					places.put(id, place);
				}
			}	
		}
		
		// get all references
		for (Element reference : references) {
			PlaceInformation pInf = new PlaceInformation();
			
			String start = reference.getChildText("start", ns);
			if (start != null)
				pInf.setStart(new Integer(start));
			
			String end = reference.getChildText("end", ns);
			if (end != null) {
				pInf.setLength((new Integer(end)) - pInf.getStart());
			}
			
			pInf.setReferenceInText(reference.getChildText("text", ns));
			
			String placeIdString = reference.getChildText("placeIds", ns);
			String[] placeIds = placeIdString.split(" ");
			pInf.setPlaces(new ArrayList<Place>());
			
			for (String placeId : placeIds) {
				Place p = places.get(placeId);
				if (p != null)
					pInf.getPlaces().add(p);
			}
			
			placeInfos.add(pInf);
		}
		
		return placeInfos;
	}

}