WEPSOperation.java [src/nodes] Revision: default Date:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package nodes;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.apache.commons.lang3.math.NumberUtils;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import static utils.Constants.*;
import utils.TranslatorException;
/**
*
* @author Brad
*/
public class WEPSOperation extends Operation {
private static final String OPERATIONDTD = "../config/data/operation_db.dtd";
private final List<WEPSAction> actions;
public WEPSOperation() {
super();
actions = new ArrayList<>();
}
public WEPSOperation(String name) {
actions = new ArrayList<>();
this.name = name;
}
public List<WEPSAction> getActions() {
return actions;
}
public boolean containsAction(String code, int id) {
WEPSAction tempAction = new WEPSAction(code, id);
return actions.contains(tempAction);
}
public void addAction(WEPSAction action) {
actions.add(action);
}
@Override
public void addProperty(String name, Object value) {
if (name.equals("action")) {
actions.add(( WEPSAction ) value);
} else
super.addProperty(name, value);
}
@Override
public void parseLMODData(JSONObject data) throws XMLStreamException, ParserConfigurationException, SAXException, FileNotFoundException, IOException, TranslatorException, JSONException {
XMLEvent event;
StartElement startElement;
WEPSAction action;
String cropName = null;
String xmlData = data.getString(WEPS_OP_PARAM_SET);
InputStream stream = new ByteArrayInputStream(xmlData.getBytes());//IOUtils.toInputStream(xmlData, Charset.defaultCharset());
URL url = getClass().getResource(OPERATIONDTD);
xmlData = xmlData.replace("operation_db.dtd", url.toString());
// Set up xml stream
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader reader = factory.createXMLEventReader(stream);
//validate the xml
SAXParserFactory saxfactory = SAXParserFactory.newInstance();
saxfactory.setValidating(true);
SAXParser parser = saxfactory.newSAXParser();
XMLReader validreader = parser.getXMLReader();
validreader.setErrorHandler(
new ErrorHandler() {
@Override
public void warning(SAXParseException e) throws SAXException {
System.out.println("WARNING : " + e.getMessage()); // do nothing
}
@Override
public void error(SAXParseException e) throws SAXException {
System.out.println("ERROR : " + e.getMessage());
throw e;
}
@Override
public void fatalError(SAXParseException e) throws SAXException {
System.out.println("FATAL : " + e.getMessage());
throw e;
}
}
);
validreader.parse(new InputSource(new StringReader(xmlData)));
// start parsing
while (reader.hasNext()) {
event = reader.nextEvent();
switch (event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
startElement = event.asStartElement();
if (startElement.getName().getLocalPart().equals(OPERATIONNAME)) {
if (!reader.getElementText().equals(name))
throw new TranslatorException("Operation: " + name + " does not match the LMOD operation data.");
break;
}
if (startElement.getName().getLocalPart().equals(ACTIONVALUE)) {
action = new WEPSAction();
if (properties.get("crop") != null) {
cropName = (( WEPSCrop ) properties.get("crop")).getName();
}
if (properties.get("residue") != null) {
cropName = (( WEPSResidue ) properties.get("residue")).getName();
}
action.parseXMLData(reader, cropName, rowDirection, residueAmount);
actions.add(action);
break;
}
}
}
}
public double calculateFuel() {
double totalfuel = 0;
double fuel;
for (WEPSAction action : actions) {
fuel = NumberUtils.toDouble(action.getParameter("oenergyarea"), 0);
totalfuel += fuel;
}
return totalfuel;
}
}