Warning: Can't use blame annotator:
svn blame failed on trunk/src/tmcsim/client/cadclientgui/ScriptHandler.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator/trunk/src/tmcsim/client/cadclientgui/ScriptHandler.java @ 3

Revision 3, 17.4 KB checked in by jdalbey, 10 years ago (diff)

Initial Import of project files - cadclientgui

RevLine 
1package tmcsim.client.cadclientgui;
2
3import java.util.TreeMap;
4import java.util.Vector;
5import java.util.logging.Level;
6import java.util.logging.Logger;
7
8import org.xml.sax.Attributes;
9import org.xml.sax.SAXParseException;
10import org.xml.sax.helpers.DefaultHandler;
11
12import tmcsim.client.cadclientgui.data.Incident;
13import tmcsim.client.cadclientgui.data.IncidentEvent;
14import tmcsim.client.cadclientgui.data.Unit;
15import tmcsim.cadmodels.IncidentInquiryDetails;
16import tmcsim.cadmodels.IncidentInquiryHeader;
17import tmcsim.cadmodels.IncidentInquiryModel_obj;
18import tmcsim.cadmodels.IncidentInquiryServices;
19import tmcsim.cadmodels.IncidentInquiryTows;
20import tmcsim.cadmodels.IncidentInquiryUnitsAssigned;
21import tmcsim.cadmodels.IncidentInquiryWitnesses;
22import tmcsim.client.cadclientgui.enums.CADScriptTags.SCRIPT_LEVEL_TAGS;
23import tmcsim.client.cadclientgui.enums.CADScriptTags.UNIT_TAGS;
24import tmcsim.client.cadclientgui.enums.CADScriptTags.SCRIPT_EVENT_TAGS;
25import tmcsim.common.CADScriptTags.AUDIO_TAGS;
26import tmcsim.common.CADScriptTags.CAD_INCIDENT_DATA_TAGS;
27import tmcsim.common.CADScriptTags.CCTV_TAGS;
28import tmcsim.common.CADScriptTags.INCIDENT_HEADER_TAGS;
29import tmcsim.common.CADScriptTags.LOCATION_INFO_TAGS;
30import tmcsim.common.CADScriptTags.PARAMICS_TAGS;
31import tmcsim.common.CADScriptTags.SERVICE_TAGS;
32import tmcsim.common.CADScriptTags.TOW_TAGS;
33import tmcsim.common.CADScriptTags.WITNESS_TAGS;
34import tmcsim.common.CCTVDirections;
35import tmcsim.common.CCTVInfo;
36import tmcsim.common.ParamicsLocation;
37import tmcsim.common.XMLIncident;
38
39
40/**
41 * SAX Handler that parses a script file and creates a list of Incident objects
42 * containing all script information that will be used by the CAD.
43 *
44 * @author Matthew Cechini
45 * @version
46 */
47public class ScriptHandler extends DefaultHandler { 
48
49    /** Error Logger. */
50    private Logger scriptLogger = Logger.getLogger("tmcsim.common");
51   
52    /**
53     * Enumeration used to keep track of the current tab level that the
54     * parser is reading data in.
55     */
56    private static enum LEVEL { NONE, TMC_SCRIPT, SCRIPT_EVENT, SCRIPT_DATA,
57        NEW_UNIT, CAD_DATA, LOCATION_INFO, CAD_INCIDENT_EVENT, PARAMICS, HEADER_INFO};
58   
59    /** Buffer used to hold parsed tag content */
60    private StringBuffer parsedValue = new StringBuffer();
61   
62    /** Current Tag level within the script that is being parsed */
63    private LEVEL currentLevel       = LEVEL.NONE;
64   
65    /** Log number for the current ScriptEvent being parsed */
66    private Integer currentLogNumber     = 0;
67   
68    /** Incident description for the current ScriptEvent being parsed */
69    private String  currentIncidentDesc  = "";
70   
71    /** Time index value (in seconds) for the current ScriptEvent being parsed */
72    private long    currentEventTime     = 0;
73   
74    /** ParamicsLocation object for current script event */
75    private ParamicsLocation currLoc      = null;
76   
77    /** IncidentInquiryHeader object for current script event */
78    private IncidentInquiryHeader currIIH = null;
79   
80    /** IncidentEvent object for current script event */
81    private IncidentEvent currEvent       = null;
82   
83    /** XMLIncident object for current script event */
84    private XMLIncident currXMLInc        = null;
85   
86    private TreeMap<String, Unit> unitMap;
87   
88    private TreeMap<Integer, Incident> incidentMap;
89   
90    private String currUnitNum;
91   
92    private Unit currUnit;
93   
94    private Incident currentIncident;
95   
96    private long currentIncidentTime;
97   
98    private String cadDataTag;
99   
100   
101   
102    /** Constructor.  Initializes incident map. */
103    public ScriptHandler() {
104        unitMap = new TreeMap<String, Unit>();
105        incidentMap = new TreeMap<Integer, Incident>();
106    }
107   
108    public Vector<Unit> getUnits() {
109        return new Vector<Unit>(unitMap.values());
110    }   
111   
112    /** 
113     * Get the list of incidents that have been parsed from the script file.
114     *
115     * @returns Vector of Incident objects.
116     */
117    public Vector<Incident> getIncidents() {
118        return new Vector<Incident>(incidentMap.values());
119    }       
120   
121    /** SAX Handler method.  Clear incident map and reset the error flag. */
122    public void startDocument() {       
123        unitMap.clear();
124        incidentMap.clear();       
125    }   
126   
127    /** SAX Handler method.   */
128    public void startElement(String uri, String localName, String qName, Attributes attributes)  {
129       
130        try {
131            if(qName.equals(SCRIPT_LEVEL_TAGS.TMC_SCRIPT.tag)) {
132                currentLevel = LEVEL.TMC_SCRIPT;
133            }   
134            else if(qName.equals(SCRIPT_LEVEL_TAGS.SCRIPT_DATA.tag))
135            {
136                currentLevel = LEVEL.SCRIPT_DATA;
137            }
138            else if(qName.equals(SCRIPT_LEVEL_TAGS.NEW_UNIT.tag))
139            {
140                currUnitNum = attributes.getValue(UNIT_TAGS.UNIT_NUM.tag);
141                currentLevel = LEVEL.NEW_UNIT;
142                if(unitMap.containsKey(currUnitNum))
143                {
144                    currUnit = unitMap.get(currUnitNum);
145                }
146                else
147                {
148                    currUnit = new Unit(currUnitNum);
149                }
150            }
151            else if(qName.equals(SCRIPT_LEVEL_TAGS.SCRIPT_EVENT.tag)) {
152                currentLevel = LEVEL.SCRIPT_EVENT;
153            }
154            else if(qName.equals(SCRIPT_LEVEL_TAGS.CAD_DATA.tag)) {
155                currentLevel = LEVEL.CAD_DATA;             
156            }
157            else if(qName.equals(SCRIPT_LEVEL_TAGS.HEADER_INFO.tag)) {
158                currIIH           = new IncidentInquiryHeader();
159                currIIH.logNumber = currentLogNumber;
160               
161                currentLevel = LEVEL.HEADER_INFO;               
162            }
163            else if(qName.equals(SCRIPT_LEVEL_TAGS.LOCATION_INFO.tag)) {
164                currLoc      = new ParamicsLocation(attributes.getValue(
165                        LOCATION_INFO_TAGS.ID.tag));
166                currentLevel = LEVEL.LOCATION_INFO;             
167            }
168            else if(qName.equals(SCRIPT_LEVEL_TAGS.CAD_INCIDENT_EVENT.tag)) {
169                currEvent    = new IncidentEvent(currentEventTime - incidentMap.get(
170                        currentLogNumber).getSecondsToStart());         
171                currentLevel = LEVEL.CAD_INCIDENT_EVENT;
172            }
173            else if(qName.equals(SCRIPT_LEVEL_TAGS.PARAMICS.tag)) {
174                String locationID = attributes.getValue(
175                        PARAMICS_TAGS.LOCATION_ID.tag);
176               
177                currXMLInc = new XMLIncident(locationID, incidentMap.get(
178                        currentLogNumber).locationMap.get(locationID));
179                currentLevel = LEVEL.PARAMICS;
180            }
181            else if(qName.equals(SCRIPT_EVENT_TAGS.INCIDENT.tag)) {
182                try {
183                    currentLogNumber = Integer.parseInt(attributes.getValue(
184                            SCRIPT_EVENT_TAGS.LOG_NUMBER.tag));
185                }
186                catch (Exception e)  { 
187                    scriptLogger.logp(Level.SEVERE, "ScriptHandler", "startElement", 
188                            "Invalid LogNumber " + attributes.getValue(
189                                    SCRIPT_EVENT_TAGS.LOG_NUMBER.tag), e);
190                    currentLogNumber = 0;
191                }               
192            }
193            else if(qName.equals(CAD_INCIDENT_DATA_TAGS.UNIT.tag)) {
194                IncidentInquiryUnitsAssigned iiu = new IncidentInquiryUnitsAssigned(IncidentInquiryModel_obj.SCRIPT_POS_INFO);
195               
196                iiu.beat = attributes.getValue(tmcsim.common.CADScriptTags.UNIT_TAGS.UNIT_NUMBER.tag);
197                iiu.statusType = attributes.getValue(tmcsim.common.CADScriptTags.UNIT_TAGS.UNIT_STATUS.tag);
198                iiu.isPrimary  = new Boolean(attributes.getValue(tmcsim.common.CADScriptTags.UNIT_TAGS.UNIT_PRIMARY.tag)).booleanValue();
199                iiu.isActive   = new Boolean(attributes.getValue(tmcsim.common.CADScriptTags.UNIT_TAGS.UNIT_ACTIVE.tag)).booleanValue();
200               
201                currEvent.eventInfo.addUnit(iiu);
202            }
203            else if(qName.equals(CAD_INCIDENT_DATA_TAGS.WITNESS.tag)) {
204                IncidentInquiryWitnesses iiw = new IncidentInquiryWitnesses(IncidentInquiryModel_obj.SCRIPT_POS_INFO);
205   
206                iiw.reportingParty = attributes.getValue(WITNESS_TAGS.WITNESS_NAME.tag);
207                iiw.telephoneNum   = attributes.getValue(WITNESS_TAGS.WITNESS_PHONE.tag);
208                iiw.address        = attributes.getValue(WITNESS_TAGS.WITNESS_ADDRESS.tag);
209                               
210                currEvent.eventInfo.addWitness(iiw);
211            }
212            else if(qName.equals(CAD_INCIDENT_DATA_TAGS.TOW.tag)) {
213                IncidentInquiryTows iit = new IncidentInquiryTows(IncidentInquiryModel_obj.SCRIPT_POS_INFO);
214               
215                iit.towCompany     = attributes.getValue(TOW_TAGS.TOW_COMPANY_NAME.tag);
216                iit.confPhoneNum   = attributes.getValue(TOW_TAGS.CONF_PHONE_NUM.tag);
217                iit.publicPhoneNum = attributes.getValue(TOW_TAGS.PUBLIC_PHONE_NUM.tag);
218                iit.beat           = attributes.getValue(TOW_TAGS.BEAT.tag);
219               
220                currEvent.eventInfo.addTow(iit);               
221            }
222            else if(qName.equals(CAD_INCIDENT_DATA_TAGS.SERVICE.tag)) {
223                IncidentInquiryServices iis = new IncidentInquiryServices(IncidentInquiryModel_obj.SCRIPT_POS_INFO);
224               
225                iis.serviceName    = attributes.getValue(SERVICE_TAGS.SERVICE_NAME.tag);
226                iis.confPhoneNum   = attributes.getValue(SERVICE_TAGS.CONF_PHONE_NUM.tag);
227                iis.publicPhoneNum = attributes.getValue(SERVICE_TAGS.PUBLIC_PHONE_NUM.tag);
228               
229                currEvent.eventInfo.addService(iis);       
230            }       
231            else if(qName.equals(CAD_INCIDENT_DATA_TAGS.AUDIO.tag)) {
232                currEvent.waveFile   = attributes.getValue(AUDIO_TAGS.FILE_PATH.tag);
233                currEvent.waveLength = new Integer(attributes.getValue(AUDIO_TAGS.FILE_LENGTH.tag)).intValue();
234            }
235            else if(qName.equals(CAD_INCIDENT_DATA_TAGS.CCTV_INFO.tag)) {
236                CCTVInfo newInfo = new CCTVInfo();
237               
238                try {
239                    newInfo.cctv_id   = Integer.parseInt(attributes.getValue(CCTV_TAGS.CCTV_ID.tag));
240                    newInfo.direction = CCTVDirections.fromChar(attributes.getValue(CCTV_TAGS.CCTV_DIR.tag).charAt(0));
241                    newInfo.toggle    = Boolean.parseBoolean(attributes.getValue(CCTV_TAGS.CCTV_TOGGLE.tag));
242               
243                    currEvent.cctvInfos.add(newInfo);
244                } 
245                catch (Exception e) {
246                    scriptLogger.logp(Level.SEVERE, "ScriptHandler", "startElement", 
247                            "Exception in parsing CCTV_INFO node.", e);
248                }
249                           
250            }
251            else if(currentLevel == LEVEL.CAD_DATA)
252            {
253                if(cadDataTag == null)
254                {
255                    cadDataTag = qName;
256                }
257            }
258
259        } catch (Exception e) {
260            scriptLogger.logp(Level.SEVERE, "ScriptHandler", "startElement", 
261                    "Exception in starting element <" + qName + ">.", e);
262        }
263    }
264
265    /** SAX Handler method.  Append read characters to local buffer. */
266    public void characters(char[] ch, int start, int length) {     
267        parsedValue.append(new String(ch, start, length).trim());               
268    }
269
270    /** SAX Handler method. */
271    public void endElement(String uri, String localName, String qName)  {
272
273        try {
274            if(qName.equals(SCRIPT_LEVEL_TAGS.SCRIPT_DATA.tag))
275            {
276                currentLevel = LEVEL.TMC_SCRIPT;
277            }
278            else if(qName.equals(SCRIPT_LEVEL_TAGS.NEW_UNIT.tag))
279            {
280                unitMap.put(currUnitNum, currUnit);
281                currentLevel = LEVEL.SCRIPT_DATA;
282            }
283            else if(qName.equals(SCRIPT_LEVEL_TAGS.SCRIPT_EVENT.tag)) {
284                currentLevel = LEVEL.TMC_SCRIPT;
285            }
286            else if(qName.equals(SCRIPT_LEVEL_TAGS.CAD_DATA.tag)) {
287                currentLevel = LEVEL.SCRIPT_EVENT;
288            }
289            else if(qName.equals(SCRIPT_LEVEL_TAGS.HEADER_INFO.tag)) {
290                incidentMap.get(currentLogNumber).header = currIIH;
291                currentLevel = LEVEL.CAD_DATA;
292            }
293            else if(qName.equals(SCRIPT_LEVEL_TAGS.LOCATION_INFO.tag)) {
294                incidentMap.get(currentLogNumber).locationMap.put(currLoc.locationID, currLoc);
295                currentLevel = LEVEL.CAD_DATA;
296            }
297            else if(qName.equals(SCRIPT_LEVEL_TAGS.CAD_INCIDENT_EVENT.tag)) {
298                incidentMap.get(currentLogNumber).addEvent(currEvent);
299                currentLevel = LEVEL.CAD_DATA;
300            }
301            else if(qName.equals(SCRIPT_LEVEL_TAGS.PARAMICS.tag)) {     
302                currEvent.XMLIncidents.add(currXMLInc);
303                currentLevel = LEVEL.CAD_INCIDENT_EVENT;
304            }
305            else if(qName.equals(SCRIPT_EVENT_TAGS.INCIDENT.tag)) {
306                currentIncidentDesc = parsedValue.toString();
307               
308                if(incidentMap.get(currentLogNumber) == null) {             
309               
310                    incidentMap.put(currentLogNumber, 
311                            new Incident(currentLogNumber, 
312                                         currentIncidentDesc, 
313                                         currentEventTime));
314                }
315                currentIncident = incidentMap.get(currentLogNumber);
316            }
317            else if(qName.equals(SCRIPT_EVENT_TAGS.TIME_INDEX.tag)) {
318                currentEventTime = timeBytesToSeconds(parsedValue.toString().trim());
319            }
320            else if(qName.equals(INCIDENT_HEADER_TAGS.TYPE.tag)){
321                currIIH.type = parsedValue.toString();
322            }
323            else if(qName.equals(INCIDENT_HEADER_TAGS.BEAT.tag)){
324                currIIH.beat = parsedValue.toString();
325            }
326            else if(qName.equals(INCIDENT_HEADER_TAGS.FULL_LOCATION.tag)){
327                currIIH.fullLocation = parsedValue.toString();
328            }
329            else if(qName.equals(INCIDENT_HEADER_TAGS.TRUNC_LOCATION.tag)){
330                currIIH.truncLocation = parsedValue.toString();
331            }
332            else if(qName.equals(INCIDENT_HEADER_TAGS.LOG_NUMBER.tag)){
333                currIIH.logNumber = Integer.parseInt(parsedValue.toString());
334            }
335            else if(qName.equals(CAD_INCIDENT_DATA_TAGS.DETAIL.tag)) {
336                currEvent.eventInfo.addDetail(new IncidentInquiryDetails(
337                        IncidentInquiryModel_obj.SCRIPT_POS_INFO, 
338                        parsedValue.toString(), false));
339            }   
340            else if(currentLevel == LEVEL.PARAMICS) {
341                try {
342                    currXMLInc.readXMLNode(qName, parsedValue.toString());
343                }
344                catch (Exception e) {
345                    scriptLogger.logp(Level.SEVERE, "ScriptHandler", "endElement", 
346                            "Exception in parsing PARAMICS node.", e);
347                }
348            }
349            else if(currentLevel == LEVEL.LOCATION_INFO) {
350                currLoc.readXMLNode(qName, parsedValue.toString());
351            }
352            else if(currentLevel == LEVEL.NEW_UNIT)
353            {
354                currUnit.readXMLNode(qName, parsedValue.toString());
355            }
356            else if(currentLevel == LEVEL.CAD_DATA)
357            {
358                currentIncident.readXMLNode(cadDataTag, qName, parsedValue.toString());
359            }
360            if(qName.equals(cadDataTag)) 
361            {
362                cadDataTag = null;
363            }
364   
365            parsedValue.setLength(0);
366        } catch (Exception e) {
367            scriptLogger.logp(Level.SEVERE, "ScriptHandler", "endElement", 
368                    "Exception in ending element <" + qName + ">.", e);
369        }
370    }   
371   
372    public void endDocument() {
373    }
374   
375    public void error(SAXParseException e) {
376        scriptLogger.logp(Level.SEVERE, "ScriptHandler", "error", 
377                "SAX Parsing error.", e);
378    }
379   
380    public void fatalError(SAXParseException e) {
381        scriptLogger.logp(Level.SEVERE, "ScriptHandler", "fatalError", 
382                "SAX Parsing fatal error.", e);
383    }
384   
385    public void warning(SAXParseException e) {
386        scriptLogger.logp(Level.SEVERE, "ScriptHandler", "warning", 
387                "SAX Parsing warning.", e);
388    }
389   
390
391    /**
392     * Private method to convert a time object from format HH:MM:SS to a long value of the
393     * corresponding number of seconds.
394     *
395     * @param time String time representation of format HH:MM:SS
396     * @return long Number of seconds
397     * @throws StringIndexOutOfBoundsException if the input parameter is not valid
398     */
399    private long timeBytesToSeconds(String time) 
400        throws StringIndexOutOfBoundsException
401    {
402        long seconds = 0;
403
404        seconds =  ((long) Character.digit(time.charAt(0), 10) * 36000 +
405                        Character.digit(time.charAt(1), 10) * 3600 +
406                        Character.digit(time.charAt(3), 10) * 600 + 
407                        Character.digit(time.charAt(4), 10) * 60 + 
408                        Character.digit(time.charAt(6), 10) * 10 +
409                        Character.digit(time.charAt(7), 10));
410
411             
412        return seconds;
413    }       
414   
415   
416}
Note: See TracBrowser for help on using the repository browser.