| 1 | package tmcsim.common; |
|---|
| 2 | |
|---|
| 3 | import java.io.Serializable; |
|---|
| 4 | import java.util.Vector; |
|---|
| 5 | |
|---|
| 6 | import org.w3c.dom.Document; |
|---|
| 7 | import org.w3c.dom.Element; |
|---|
| 8 | |
|---|
| 9 | /** |
|---|
| 10 | * XMLIncident contains incident information used to create the XML file that |
|---|
| 11 | * is transmitted to paramics. It's methods construct an XML element with the |
|---|
| 12 | * incident data. |
|---|
| 13 | * |
|---|
| 14 | * @author Matthew Cechini (mcechini@calpoly.edu) |
|---|
| 15 | * @version $Date: 2006/06/06 20:46:41 $ $Revision: 1.4 $ |
|---|
| 16 | */ |
|---|
| 17 | @SuppressWarnings("serial") |
|---|
| 18 | public class XMLIncident implements Serializable { |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * Enumeration with XML tag names. |
|---|
| 22 | * @author Matthew Cechini |
|---|
| 23 | */ |
|---|
| 24 | private static enum XML_TAGS { |
|---|
| 25 | /** Current Incident info. */ |
|---|
| 26 | INCIDENT ("Incident"), |
|---|
| 27 | /** Incident's ID. */ |
|---|
| 28 | ID ("Identifier"), |
|---|
| 29 | /** Incident's status. */ |
|---|
| 30 | STATUS ("Status"), |
|---|
| 31 | /** Incident Location. */ |
|---|
| 32 | LOCATION ("Location"), |
|---|
| 33 | /** Incident's route location. */ |
|---|
| 34 | ROUTE ("Route"), |
|---|
| 35 | /** Incident's route direction. */ |
|---|
| 36 | DIRECTION ("Direction"), |
|---|
| 37 | /** Incident's route location type. */ |
|---|
| 38 | LOC_TYPE ("Location_type"), |
|---|
| 39 | /** Incident's route postmile. */ |
|---|
| 40 | POSTMILE ("Postmile"), |
|---|
| 41 | /** Incident's type. */ |
|---|
| 42 | INC_TYPE ("Incident_type"), |
|---|
| 43 | /** Incident lanes. */ |
|---|
| 44 | LANES ("Lanes"), |
|---|
| 45 | /** Lane number. */ |
|---|
| 46 | LANE_NUM ("Lane_number"); |
|---|
| 47 | |
|---|
| 48 | public String tag; |
|---|
| 49 | |
|---|
| 50 | private XML_TAGS(String t) { |
|---|
| 51 | tag = t; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Enumeration containing possible incident status values. |
|---|
| 57 | */ |
|---|
| 58 | public static enum INCIDENT_STATUS { |
|---|
| 59 | NEW ("NEW"), |
|---|
| 60 | CHANGED ("CHANGED"), |
|---|
| 61 | ON_GOING ("ON_GOING"), |
|---|
| 62 | CLEARED ("CLEARED"); |
|---|
| 63 | |
|---|
| 64 | public String status; |
|---|
| 65 | |
|---|
| 66 | private INCIDENT_STATUS(String s) { |
|---|
| 67 | status = s; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Returns the INCIDENT_STATUS enumeration value which has a status |
|---|
| 72 | * value that matches the parameter value. |
|---|
| 73 | * @param val Incident status. |
|---|
| 74 | * @throws ScriptException if the parameter value is invalid. |
|---|
| 75 | * @return INCIDENT_STATUS for the parameter value. |
|---|
| 76 | */ |
|---|
| 77 | public static INCIDENT_STATUS fromValue(String val) throws ScriptException { |
|---|
| 78 | |
|---|
| 79 | for(INCIDENT_STATUS incStatus : values()) { |
|---|
| 80 | if(incStatus.status.equals(val)) |
|---|
| 81 | return incStatus; |
|---|
| 82 | } |
|---|
| 83 | throw new ScriptException(ScriptException.INVALID_ENUM, val); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | }; |
|---|
| 87 | |
|---|
| 88 | /** |
|---|
| 89 | * Enumeration containing possible incident type values. |
|---|
| 90 | */ |
|---|
| 91 | public static enum INCIDENT_TYPE { |
|---|
| 92 | LANE_BREAKDOWN ("LANE_BREAKDOWN"); |
|---|
| 93 | |
|---|
| 94 | public String type; |
|---|
| 95 | |
|---|
| 96 | private INCIDENT_TYPE(String s) { |
|---|
| 97 | type = s; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | * Returns the INCIDENT_STATUS enumeration value which has a status |
|---|
| 102 | * value that matches the parameter value. |
|---|
| 103 | * @param val Incident status. |
|---|
| 104 | * @throws ScriptException if the parameter value is invalid. |
|---|
| 105 | * @return INCIDENT_STATUS for the parameter value. |
|---|
| 106 | */ |
|---|
| 107 | public static INCIDENT_TYPE fromValue(String val) throws ScriptException { |
|---|
| 108 | |
|---|
| 109 | for(INCIDENT_TYPE incType : values()) { |
|---|
| 110 | if(incType.type.equals(val)) |
|---|
| 111 | return incType; |
|---|
| 112 | } |
|---|
| 113 | throw new ScriptException(ScriptException.INVALID_ENUM, val); |
|---|
| 114 | } |
|---|
| 115 | }; |
|---|
| 116 | |
|---|
| 117 | /** Incident unique ID. */ |
|---|
| 118 | private String incidentID = null; |
|---|
| 119 | |
|---|
| 120 | /** Incident status. */ |
|---|
| 121 | private INCIDENT_STATUS incidentStatus = null; |
|---|
| 122 | |
|---|
| 123 | /** Incident type. */ |
|---|
| 124 | private INCIDENT_TYPE incidentType = null; |
|---|
| 125 | |
|---|
| 126 | /** Lane numbers affected incident. */ |
|---|
| 127 | private Vector<String> lanes = null; |
|---|
| 128 | |
|---|
| 129 | /** Incident location object. */ |
|---|
| 130 | private ParamicsLocation theLocation = null; |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | /** |
|---|
| 134 | * Constructor. Initialize data members. |
|---|
| 135 | * |
|---|
| 136 | * @param id Incident ID. |
|---|
| 137 | * @param newLocation Incident location. |
|---|
| 138 | */ |
|---|
| 139 | public XMLIncident(String id, ParamicsLocation newLocation) { |
|---|
| 140 | |
|---|
| 141 | incidentID = id; |
|---|
| 142 | theLocation = newLocation; |
|---|
| 143 | incidentStatus = INCIDENT_STATUS.CLEARED; |
|---|
| 144 | incidentType = INCIDENT_TYPE.LANE_BREAKDOWN; |
|---|
| 145 | lanes = new Vector<String>(); |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /** |
|---|
| 149 | * Get the incident ID. |
|---|
| 150 | * @return Incident ID. |
|---|
| 151 | */ |
|---|
| 152 | public String getIdentifier() { |
|---|
| 153 | return incidentID; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | /** |
|---|
| 158 | * Receive the tag name and data from XML parsing. |
|---|
| 159 | * @param name XML Tag name. |
|---|
| 160 | * @param value XML Tag value. |
|---|
| 161 | * @throws ScriptException if there is an error in parsing the node data. |
|---|
| 162 | */ |
|---|
| 163 | public void readXMLNode(String name, String value) throws ScriptException { |
|---|
| 164 | |
|---|
| 165 | if(name.equals(XML_TAGS.STATUS.tag)) |
|---|
| 166 | incidentStatus = INCIDENT_STATUS.fromValue(value); |
|---|
| 167 | else if(name.equals(XML_TAGS.INC_TYPE.tag)) |
|---|
| 168 | incidentType = INCIDENT_TYPE.fromValue(value); |
|---|
| 169 | else if(name.equals(XML_TAGS.LANE_NUM.tag)) |
|---|
| 170 | lanes.add(value); |
|---|
| 171 | |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | /** |
|---|
| 175 | * Adds XML tags to the parameter Element for this Incident with the |
|---|
| 176 | * following schema: <br/> |
|---|
| 177 | * <Incident><br/> |
|---|
| 178 | * <Identifier/><br/> |
|---|
| 179 | * <Status/><br/> |
|---|
| 180 | * <Location><br/> |
|---|
| 181 | * <Route/><br/> |
|---|
| 182 | * <Direction/><br/> |
|---|
| 183 | * <Location_type/><br/> |
|---|
| 184 | * <Postmile/><br/> |
|---|
| 185 | * </Location><br/> |
|---|
| 186 | * <Incident_type/><br/> |
|---|
| 187 | * <Lanes><br/> |
|---|
| 188 | * <Lane_number/><br/> |
|---|
| 189 | * </Lanes><br/> |
|---|
| 190 | * </Incident><br/> |
|---|
| 191 | * |
|---|
| 192 | * @param currElem XML Element used as a root for XML tag appending. |
|---|
| 193 | */ |
|---|
| 194 | public void toXML(Element currElem) { |
|---|
| 195 | |
|---|
| 196 | Document theDoc = currElem.getOwnerDocument(); |
|---|
| 197 | |
|---|
| 198 | Element incidentElem = theDoc.createElement(XML_TAGS.INCIDENT.tag); |
|---|
| 199 | currElem.appendChild(incidentElem); |
|---|
| 200 | |
|---|
| 201 | Element idElement = theDoc.createElement(XML_TAGS.ID.tag); |
|---|
| 202 | idElement.appendChild(theDoc.createTextNode(incidentID)); |
|---|
| 203 | incidentElem.appendChild(idElement); |
|---|
| 204 | |
|---|
| 205 | Element statusElement = theDoc.createElement(XML_TAGS.STATUS.tag); |
|---|
| 206 | statusElement.appendChild(theDoc.createTextNode(incidentStatus.status)); |
|---|
| 207 | incidentElem.appendChild(statusElement); |
|---|
| 208 | |
|---|
| 209 | writeLocationXML(incidentElem); |
|---|
| 210 | |
|---|
| 211 | Element typeElement = theDoc.createElement(XML_TAGS.INC_TYPE.tag); |
|---|
| 212 | typeElement.appendChild(theDoc.createTextNode(incidentType.type)); |
|---|
| 213 | incidentElem.appendChild(typeElement); |
|---|
| 214 | |
|---|
| 215 | writeLanesXML(incidentElem); |
|---|
| 216 | |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /** |
|---|
| 220 | * |
|---|
| 221 | * Write the location information for the incident with the following |
|---|
| 222 | * XML Schema: <br> |
|---|
| 223 | * <Location><br> |
|---|
| 224 | * <Route/><br> |
|---|
| 225 | * <Direction/><br> |
|---|
| 226 | * <Location_type/><br> |
|---|
| 227 | * <Postmile/><br> |
|---|
| 228 | * </Location><br> |
|---|
| 229 | */ |
|---|
| 230 | protected void writeLocationXML(Element currElem) { |
|---|
| 231 | |
|---|
| 232 | Document theDoc = currElem.getOwnerDocument(); |
|---|
| 233 | |
|---|
| 234 | Element locationElement = theDoc.createElement(XML_TAGS.LOCATION.tag); |
|---|
| 235 | currElem.appendChild(locationElement); |
|---|
| 236 | |
|---|
| 237 | Element routeElement = theDoc.createElement(XML_TAGS.ROUTE.tag); |
|---|
| 238 | routeElement.appendChild(theDoc.createTextNode(theLocation.incidentRoute)); |
|---|
| 239 | locationElement.appendChild(routeElement); |
|---|
| 240 | |
|---|
| 241 | Element directionElement = theDoc.createElement(XML_TAGS.DIRECTION.tag); |
|---|
| 242 | directionElement.appendChild(theDoc.createTextNode(theLocation.incidentDirection)); |
|---|
| 243 | locationElement.appendChild(directionElement); |
|---|
| 244 | |
|---|
| 245 | Element typeElement = theDoc.createElement(XML_TAGS.LOC_TYPE.tag); |
|---|
| 246 | typeElement.appendChild(theDoc.createTextNode(theLocation.incidentLocType)); |
|---|
| 247 | locationElement.appendChild(typeElement); |
|---|
| 248 | |
|---|
| 249 | Element postmileElement = theDoc.createElement(XML_TAGS.POSTMILE.tag); |
|---|
| 250 | postmileElement.appendChild(theDoc.createTextNode(theLocation.incidentPostmile)); |
|---|
| 251 | locationElement.appendChild(postmileElement); |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | /** |
|---|
| 255 | * Write the lanes for the incident with the following XML Schema.<br/> |
|---|
| 256 | * <Lanes><br> |
|---|
| 257 | * <Lane_number/><br> |
|---|
| 258 | * <Lane_number/><br> |
|---|
| 259 | * </Lanes><br> |
|---|
| 260 | */ |
|---|
| 261 | protected void writeLanesXML(Element currElem) { |
|---|
| 262 | |
|---|
| 263 | Document theDoc = currElem.getOwnerDocument(); |
|---|
| 264 | |
|---|
| 265 | Element lanesElement = theDoc.createElement(XML_TAGS.LANES.tag); |
|---|
| 266 | currElem.appendChild(lanesElement); |
|---|
| 267 | |
|---|
| 268 | Element laneElement = null; |
|---|
| 269 | |
|---|
| 270 | for(String l : lanes) { |
|---|
| 271 | laneElement = theDoc.createElement(XML_TAGS.LANE_NUM.tag); |
|---|
| 272 | laneElement.appendChild(theDoc.createTextNode(l)); |
|---|
| 273 | lanesElement.appendChild(laneElement); |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | /** |
|---|
| 278 | * This method is used to set the status of this incident object to |
|---|
| 279 | * "ON_GOING" if the incident has not been cleard. |
|---|
| 280 | */ |
|---|
| 281 | public void update() { |
|---|
| 282 | |
|---|
| 283 | if(!isCleared()) |
|---|
| 284 | incidentStatus = INCIDENT_STATUS.ON_GOING; |
|---|
| 285 | |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | /** |
|---|
| 289 | * Method is called to determine if this incident update object is clearing |
|---|
| 290 | * an incident. This is determined by checking if the status string is "CLEARED". |
|---|
| 291 | * |
|---|
| 292 | * @return true if object is clearing an incident, false if not. |
|---|
| 293 | */ |
|---|
| 294 | public boolean isCleared() { |
|---|
| 295 | return (incidentStatus == INCIDENT_STATUS.CLEARED); |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | } |
|---|