| 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 lineNum; |
|---|
| 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 lineNum, ArrayList<Station> stations, int count) |
|---|
| 33 | { |
|---|
| 34 | this.lineNum = lineNum; |
|---|
| 35 | this.stations = stations; |
|---|
| 36 | this.count = count; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * Returns the FEPLine meta data in string format |
|---|
| 41 | * @return FEPLine metadata |
|---|
| 42 | */ |
|---|
| 43 | public String getLineMeta() |
|---|
| 44 | { |
|---|
| 45 | StringBuilder build = new StringBuilder(); |
|---|
| 46 | build.append(Integer.toString(this.lineNum)); |
|---|
| 47 | build.append(" "); |
|---|
| 48 | build.append(Integer.toString(this.count)); |
|---|
| 49 | build.append(" "); |
|---|
| 50 | build.append(Integer.toString(this.stations.size())); |
|---|
| 51 | build.append("\n"); |
|---|
| 52 | for (Station station : stations) |
|---|
| 53 | { |
|---|
| 54 | build.append(station.getStationMeta()); |
|---|
| 55 | } |
|---|
| 56 | return build.toString(); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * Returns the FEPLine data in XMLFormat |
|---|
| 61 | * |
|---|
| 62 | * @param currElem The current XML <Line> element |
|---|
| 63 | */ |
|---|
| 64 | public void toXML(Element currElem) |
|---|
| 65 | { |
|---|
| 66 | Document theDoc = currElem.getOwnerDocument(); |
|---|
| 67 | |
|---|
| 68 | Element lineElement = theDoc.createElement(XML_TAGS.LINE.tag); |
|---|
| 69 | currElem.appendChild(lineElement); |
|---|
| 70 | |
|---|
| 71 | Element lineNumElement = theDoc.createElement(XML_TAGS.LINE_NUM.tag); |
|---|
| 72 | lineNumElement.appendChild(theDoc.createTextNode(String.valueOf(this.lineNum))); |
|---|
| 73 | lineElement.appendChild(lineNumElement); |
|---|
| 74 | |
|---|
| 75 | Element countElement = theDoc.createElement(XML_TAGS.COUNT.tag); |
|---|
| 76 | countElement.appendChild(theDoc.createTextNode(String.valueOf(this.count))); |
|---|
| 77 | lineElement.appendChild(countElement); |
|---|
| 78 | |
|---|
| 79 | Element stationsElement = theDoc.createElement(XML_TAGS.STATIONS.tag); |
|---|
| 80 | lineElement.appendChild(stationsElement); |
|---|
| 81 | for (Station station : stations) |
|---|
| 82 | { |
|---|
| 83 | station.toXML(stationsElement); |
|---|
| 84 | } |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * XML Tags used in toXML() |
|---|
| 89 | */ |
|---|
| 90 | private static enum XML_TAGS |
|---|
| 91 | { |
|---|
| 92 | |
|---|
| 93 | LINE("Line"), |
|---|
| 94 | LINE_NUM("Line_Num"), |
|---|
| 95 | STATIONS("Stations"), |
|---|
| 96 | COUNT("Count"); |
|---|
| 97 | |
|---|
| 98 | String tag; |
|---|
| 99 | |
|---|
| 100 | private XML_TAGS(String n) |
|---|
| 101 | { |
|---|
| 102 | tag = n; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | } |
|---|