| 1 | package atmsdriver.model; |
|---|
| 2 | |
|---|
| 3 | import atmsdriver.NetworkLoader; |
|---|
| 4 | import java.io.File; |
|---|
| 5 | import java.io.IOException; |
|---|
| 6 | import java.io.StringWriter; |
|---|
| 7 | import java.io.Writer; |
|---|
| 8 | import java.util.ArrayList; |
|---|
| 9 | import java.util.logging.Level; |
|---|
| 10 | import java.util.logging.Logger; |
|---|
| 11 | import javax.xml.parsers.DocumentBuilder; |
|---|
| 12 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| 13 | import javax.xml.transform.OutputKeys; |
|---|
| 14 | import javax.xml.transform.Transformer; |
|---|
| 15 | import javax.xml.transform.TransformerFactory; |
|---|
| 16 | import javax.xml.transform.dom.DOMSource; |
|---|
| 17 | import javax.xml.transform.stream.StreamResult; |
|---|
| 18 | import org.w3c.dom.Document; |
|---|
| 19 | import org.w3c.dom.Element; |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * |
|---|
| 23 | * @author andrew |
|---|
| 24 | */ |
|---|
| 25 | public class Network { |
|---|
| 26 | |
|---|
| 27 | final private ArrayList<FEPLine> lines; |
|---|
| 28 | final private HighwayStatusWriter hwyStatusWriter; |
|---|
| 29 | |
|---|
| 30 | public Network(File LDSFile, File loopFile, String hostName, int portNum) { |
|---|
| 31 | lines = (ArrayList<FEPLine>) new NetworkLoader(LDSFile, loopFile).getFEPLines(); |
|---|
| 32 | hwyStatusWriter = new HighwayStatusWriter(hostName, portNum); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | public void writeToFEP() { |
|---|
| 36 | try { |
|---|
| 37 | hwyStatusWriter.write(toXML()); |
|---|
| 38 | } catch (IOException ex) { |
|---|
| 39 | Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | public String toXML() { |
|---|
| 44 | String xml = null; |
|---|
| 45 | try { |
|---|
| 46 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
|---|
| 47 | DocumentBuilder builder = factory.newDocumentBuilder(); |
|---|
| 48 | Document theDoc = builder.newDocument(); |
|---|
| 49 | |
|---|
| 50 | Element networkElement = theDoc.createElement(XML_TAGS.NETWORK.tag); |
|---|
| 51 | theDoc.appendChild(networkElement); |
|---|
| 52 | |
|---|
| 53 | for (FEPLine line : lines) { |
|---|
| 54 | line.toXML(networkElement); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | Transformer tf = TransformerFactory.newInstance().newTransformer(); |
|---|
| 58 | |
|---|
| 59 | tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); |
|---|
| 60 | tf.setOutputProperty(OutputKeys.INDENT, "yes"); |
|---|
| 61 | tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); |
|---|
| 62 | |
|---|
| 63 | Writer out = new StringWriter(); |
|---|
| 64 | tf.transform(new DOMSource(theDoc), new StreamResult(out)); |
|---|
| 65 | xml = out.toString(); |
|---|
| 66 | out.close(); |
|---|
| 67 | } catch (Exception ex) { |
|---|
| 68 | Logger.getLogger(Network.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 69 | } |
|---|
| 70 | return xml; |
|---|
| 71 | |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | private static enum XML_TAGS { |
|---|
| 75 | |
|---|
| 76 | NETWORK("Network"); |
|---|
| 77 | |
|---|
| 78 | String tag; |
|---|
| 79 | |
|---|
| 80 | private XML_TAGS(String n) { |
|---|
| 81 | tag = n; |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | } |
|---|