source: tmcsimulator/branches/ATMSDriver/src/atmsdriver/network/model/Network.java @ 77

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

fep_rpc_client: NetworkReader? complete. All cpp code is commented and cleaned. Added tinyxml lib.

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