|
| 1 | +package org.sofwerx.ogc.sos; |
| 2 | + |
| 3 | +import android.util.Log; |
| 4 | + |
| 5 | +import org.w3c.dom.Document; |
| 6 | +import org.w3c.dom.Element; |
| 7 | +import org.xml.sax.InputSource; |
| 8 | +import org.xml.sax.SAXException; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.io.StringReader; |
| 12 | + |
| 13 | +import javax.xml.parsers.DocumentBuilder; |
| 14 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 15 | +import javax.xml.parsers.ParserConfigurationException; |
| 16 | + |
| 17 | +public abstract class AbstractSosOperation { |
| 18 | + private static SosSensor defaultSensor; |
| 19 | + |
| 20 | + public static AbstractSosOperation newFromXmlString(String text) { |
| 21 | + if (text != null) { |
| 22 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 23 | + try { |
| 24 | + DocumentBuilder builder = null; |
| 25 | + builder = factory.newDocumentBuilder(); |
| 26 | + Document doc = builder.parse(new InputSource(new StringReader(text))); |
| 27 | + return newFromXML(doc); |
| 28 | + } catch (ParserConfigurationException | IOException | SAXException e) { |
| 29 | + e.printStackTrace(); |
| 30 | + } |
| 31 | + } |
| 32 | + return null; |
| 33 | + } |
| 34 | + |
| 35 | + public static AbstractSosOperation newFromXML(Document doc) { |
| 36 | + AbstractSosOperation operation = null; |
| 37 | + if (doc != null) { |
| 38 | + Element element = doc.getDocumentElement(); |
| 39 | + if (element != null) { |
| 40 | + String tagName = element.getTagName(); |
| 41 | + if (tagName != null) { |
| 42 | + if (tagName.contains(OperationInsertSensorResponse.NAMESPACE)) |
| 43 | + operation = new OperationInsertSensorResponse(); |
| 44 | + else if (tagName.contains(OperationInsertSensor.NAMESPACE)) |
| 45 | + operation = new OperationInsertSensor(); |
| 46 | + else if (tagName.contains(OperationInsertResultTemplateResponse.NAMESPACE)) |
| 47 | + operation = new OperationInsertResultTemplateResponse(); |
| 48 | + else if (tagName.contains(OperationInsertResultTemplate.NAMESPACE)) |
| 49 | + operation = new OperationInsertResultTemplate(); |
| 50 | + else if (tagName.contains(OperationInsertResultResponse.NAMESPACE)) |
| 51 | + operation = new OperationInsertResultResponse(); |
| 52 | + else if (tagName.contains(OperationInsertResult.NAMESPACE)) |
| 53 | + operation = new OperationInsertResult(defaultSensor); |
| 54 | + if (operation != null) |
| 55 | + operation.parse(element); |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + return operation; |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Used so that templates requiring some context can have that context available |
| 64 | + * @return |
| 65 | + */ |
| 66 | + public static SosSensor getDefaultSensor() { |
| 67 | + return defaultSensor; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Used so that templates requiring some context can have that context available |
| 72 | + * @param defaultSensor |
| 73 | + */ |
| 74 | + public static void setDefaultSensor(SosSensor defaultSensor) { |
| 75 | + AbstractSosOperation.defaultSensor = defaultSensor; |
| 76 | + Log.d(SosIpcTransceiver.TAG,"Default sensor is now: "+((defaultSensor==null)?"null":defaultSensor.getUniqueId())); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Does this operation contain all the information it needs |
| 81 | + * @return |
| 82 | + */ |
| 83 | + public abstract boolean isValid(); |
| 84 | + |
| 85 | + protected abstract void parse(Element element); |
| 86 | + public Document toXML() throws ParserConfigurationException { |
| 87 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 88 | + DocumentBuilder parser = factory.newDocumentBuilder(); |
| 89 | + return parser.newDocument(); |
| 90 | + } |
| 91 | +} |
0 commit comments