| 1 | package atmsdriver.model; |
|---|
| 2 | |
|---|
| 3 | import java.util.ArrayList; |
|---|
| 4 | import java.util.List; |
|---|
| 5 | import org.w3c.dom.Document; |
|---|
| 6 | import org.w3c.dom.Element; |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | * An FEPLine is a simulated line of communication from the FEP to |
|---|
| 10 | * Stations in the highways network. |
|---|
| 11 | * |
|---|
| 12 | * An FEPLine contains static line meta data and a list of Stations. A |
|---|
| 13 | * single FEPLine contains multiple Stations. |
|---|
| 14 | * |
|---|
| 15 | * @author John A. Torres |
|---|
| 16 | * @version 09/10/2017 |
|---|
| 17 | */ |
|---|
| 18 | final public class FEPLine |
|---|
| 19 | { |
|---|
| 20 | /* Static FEPLine meta data */ |
|---|
| 21 | final public int lineID; |
|---|
| 22 | final public List<Station> stations; |
|---|
| 23 | final private int count; |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Constructs an FEPLine from given line number, list of stations, and count. |
|---|
| 27 | * |
|---|
| 28 | * @param lineNum |
|---|
| 29 | * @param stations |
|---|
| 30 | * @param count |
|---|
| 31 | */ |
|---|
| 32 | FEPLine(int lineID, ArrayList<Station> stations, int count) |
|---|
| 33 | { |
|---|
| 34 | this.lineID = lineID; |
|---|
| 35 | this.stations = stations; |
|---|
| 36 | this.count = count; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | /** Returns a string of highways data. If MetaDataOnly is true, you get a full |
|---|
| 40 | * dump of the highways meta data, which does not include dynamic loop values, |
|---|
| 41 | * and does include the string location names. If MetaDataOnly is false, |
|---|
| 42 | * dynamic loop values are included, and unnecessary information like string |
|---|
| 43 | * location values are included. |
|---|
| 44 | * |
|---|
| 45 | * The FEPSimulator takes in the toCondensedFormat() output, with a MetaDataOnly |
|---|
| 46 | * value of false, over the socket. |
|---|
| 47 | * |
|---|
| 48 | * The MetaDataOnly flag should be used to get a full dump of the highways |
|---|
| 49 | * information. This was used to get the highways_fullmap.txt output. |
|---|
| 50 | * |
|---|
| 51 | * @param MetaDataOnly Whether you want meta data, or a full dump for FEPSim |
|---|
| 52 | * @return String, highways data in condensed format |
|---|
| 53 | */ |
|---|
| 54 | public String toCondensedFormat(boolean MetaDataOnly) |
|---|
| 55 | { |
|---|
| 56 | StringBuilder build = new StringBuilder(); |
|---|
| 57 | build.append(Integer.toString(this.lineID)); |
|---|
| 58 | build.append(" "); |
|---|
| 59 | build.append(Integer.toString(this.count)); |
|---|
| 60 | build.append(" "); |
|---|
| 61 | build.append(Integer.toString(this.stations.size())); |
|---|
| 62 | build.append("\n"); |
|---|
| 63 | for(Station station : stations) |
|---|
| 64 | { |
|---|
| 65 | build.append(station.toCondensedFormat(MetaDataOnly)); |
|---|
| 66 | } |
|---|
| 67 | return build.toString(); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Returns the FEPLine data in XMLFormat |
|---|
| 72 | * |
|---|
| 73 | * @param currElem The current XML <Line> element |
|---|
| 74 | */ |
|---|
| 75 | public void toXML(Element currElem) |
|---|
| 76 | { |
|---|
| 77 | Document theDoc = currElem.getOwnerDocument(); |
|---|
| 78 | |
|---|
| 79 | Element lineElement = theDoc.createElement(XML_TAGS.LINE.tag); |
|---|
| 80 | currElem.appendChild(lineElement); |
|---|
| 81 | |
|---|
| 82 | Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag); |
|---|
| 83 | lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineID))); |
|---|
| 84 | lineElement.appendChild(lineNumElement); |
|---|
| 85 | |
|---|
| 86 | Element countElement = theDoc.createElement(XML_TAGS.COUNT.tag); |
|---|
| 87 | countElement.appendChild(theDoc.createTextNode(String.valueOf(this.count))); |
|---|
| 88 | lineElement.appendChild(countElement); |
|---|
| 89 | |
|---|
| 90 | Element stationsElement = theDoc.createElement(XML_TAGS.STATIONS.tag); |
|---|
| 91 | lineElement.appendChild(stationsElement); |
|---|
| 92 | for (Station station : stations) |
|---|
| 93 | { |
|---|
| 94 | station.toXML(stationsElement); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * XML Tags used in toXML() |
|---|
| 100 | */ |
|---|
| 101 | private static enum XML_TAGS |
|---|
| 102 | { |
|---|
| 103 | |
|---|
| 104 | LINE("Line"), |
|---|
| 105 | LINE_NUM("Line_Num"), |
|---|
| 106 | STATIONS("Stations"), |
|---|
| 107 | COUNT("Count"); |
|---|
| 108 | |
|---|
| 109 | String tag; |
|---|
| 110 | |
|---|
| 111 | private XML_TAGS(String n) |
|---|
| 112 | { |
|---|
| 113 | tag = n; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | } |
|---|