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

Revision 79, 2.6 KB checked in by jtorres, 9 years ago (diff)

new atmsdriver package in trunk. fep_rpc_client dynamic data packing added in branches. fep_rpc_client refactored data packing code into static DataPacker? class. Added vds_data folder into config/. Added new project configuration for ATMSDriver.

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