source: tmcsimulator/branches/trunk/src/atmsdriver/model/Network.java @ 82

Revision 82, 2.5 KB checked in by jtorres, 9 years ago (diff)

Added socket client to ATMSDriver. Added socket server to FEPSim

Line 
1package atmsdriver.model;
2
3import atmsdriver.NetworkLoader;
4import java.io.File;
5import java.io.IOException;
6import java.io.StringWriter;
7import java.io.Writer;
8import java.util.ArrayList;
9import java.util.logging.Level;
10import java.util.logging.Logger;
11import javax.xml.parsers.DocumentBuilder;
12import javax.xml.parsers.DocumentBuilderFactory;
13import javax.xml.transform.OutputKeys;
14import javax.xml.transform.Transformer;
15import javax.xml.transform.TransformerFactory;
16import javax.xml.transform.dom.DOMSource;
17import javax.xml.transform.stream.StreamResult;
18import org.w3c.dom.Document;
19import org.w3c.dom.Element;
20
21/**
22 *
23 * @author andrew
24 */
25public 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}
Note: See TracBrowser for help on using the repository browser.