Mercurial > hg > anteater
view src/de/mpiwg/anteater/xml/impl/EventXMLManager.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.File; import java.io.InputStream; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Element; import de.mpiwg.anteater.events.Applicant; import de.mpiwg.anteater.events.Location; import de.mpiwg.anteater.events.ResearchEvent; import de.mpiwg.anteater.events.Species; import de.mpiwg.anteater.places.Place; public class EventXMLManager extends JDOMParser { private String filepath; public EventXMLManager(String filepath) { super(); this.filepath = filepath; File resultFile = new File(filepath); if (resultFile.exists()) init(filepath); else { InputStream stream = getClass().getResourceAsStream( "templates/eventsFile.xml"); init(stream); } } public void addEvent(ResearchEvent event) { Element eventNode = new Element("event"); File file = new File(event.getTextInformation().getFilepath()); eventNode.setAttribute(new Attribute("text", file.getName())); eventNode.setAttribute(new Attribute("date_filed", event.getDate())); Element applicantsNode = new Element("applicants"); eventNode.addContent(applicantsNode); // add applicants for (Applicant applicant : event.getApplicants()) { Element applicantNode = new Element("applicant"); applicantsNode.addContent(applicantNode); // name Element name = new Element("name"); applicantNode.addContent(name); name.setText(applicant.getApplicant().getReferenceInText()); // appliant institution Element appInstNode = new Element("applicant_institutions"); applicantNode.addContent(appInstNode); for (Location location : applicant.getApplicantInstitution()) { Element appInst = new Element("applicant_institution"); appInstNode.addContent(appInst); createPlace(location, appInst); } Element appLocs = new Element("applicant_locations"); applicantNode.addContent(appLocs); for (Location location : applicant.getLocation()) { Element appLoc = new Element("applicant_locations"); appLocs.addContent(appLoc); createPlace(location, appLoc); } } Element researchLocsNode = new Element("research_locations"); eventNode.addContent(researchLocsNode); // add research location for (Location location : event.getResearchLocations()) { Element researchLoc = new Element("research_location"); researchLocsNode.addContent(researchLoc); createPlace(location, researchLoc); } Element speciesNode = new Element("researched_species"); eventNode.addContent(speciesNode); // add species for (Species species : event.getResearchedSpecies()) { Element spNode = new Element("species"); speciesNode.addContent(spNode); spNode.setText(species.getSpeciesName().getReferenceInText()); spNode.setAttribute(new Attribute("identified_name", species.getSpeciesName().getIdentifiedName())); } this.addElementToDoc(eventNode, "/events"); } public void createPlace(Location location, Element parent) { Element instname = new Element("name"); instname.setText(location.getLocation().getReferenceInText()); parent.addContent(instname); for (Place place : location.getLocation().getPlaces()) { Element placeInfo = new Element("place_information"); parent.addContent(placeInfo); placeInfo.setAttribute(new Attribute("type", place.getType())); placeInfo.setText(place.getName()); placeInfo.setAttribute(new Attribute("woeId", place.getWoeId())); placeInfo.setAttribute(new Attribute("latitude", place.getLatitude() + "")); placeInfo.setAttribute(new Attribute("longitude", place.getLongitude() + "")); } } private void addElementToDoc(Element element, String xpath) { List<Element> nodes = executeXPath(xpath, null); if (nodes != null && nodes.size() > 0) { nodes.get(0).addContent(element); } save(filepath, false); } }