source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/structures/MyScriptHandler.java @ 7

Revision 7, 35.3 KB checked in by bmcguffin, 9 years ago (diff)

Renamed Interfaces in structures.events package from "*Interface" to "I_*"

Line 
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6package scriptbuilder.structures;
7
8import java.util.Random;
9import java.util.Stack;
10import java.util.TreeMap;
11import java.util.Vector;
12import java.util.logging.Level;
13import java.util.logging.Logger;
14import org.xml.sax.Attributes;
15import org.xml.sax.SAXParseException;
16import org.xml.sax.helpers.DefaultHandler;
17import scriptbuilder.structures.events.*;
18import scriptbuilder.structures.units.Unit;
19
20/**
21 * Script Handler class for the ScriptBuilder. Adapted from the TMCSim script
22 * handler written by Matthew Cechini.
23 *
24 * @author Bryan McGuffin
25 * @version 2017/07/03
26 */
27public class MyScriptHandler extends DefaultHandler
28{
29
30    /**
31     * Error Logger.
32     */
33    private Logger myScriptLogger = Logger.getLogger("scriptbuilder.structures");
34
35    /**
36     * The script which XML entities will be entered into.
37     */
38    private SimulationScript script;
39
40    /**
41     * Enumeration of every element which appears in the DTD as of 2017/07/01.
42     * They are in alphabetical order and broken up into 3 major sections.
43     */
44    private static enum ELEMENT
45    {
46
47        //These elements are the ones that have sub-elements in the DTD.
48        //They may or may not have character data attached.
49        TMC_SCRIPT,
50        ACTIVITY_LOG_EVALUATION,//EVENT
51        ADDITIONAL_INFO,
52        ATMS_EVALUATION,//EVENT
53        CAD_DATA,
54        CAD_EVALUATION,//EVENT
55        CAD_INCIDENT_EVENT,//EVENT
56        CARDFILE,
57        CHP_RADIO,//EVENT
58        CMS_EVALUATION,//EVENT
59        DIALOG,
60        FACILITATOR_EVALUATION,//EVENT
61        GENERAL,
62        GENERAL_INFO,
63        HEADER_INFO,
64        LOCATION,
65        LOCATION_INFO,
66        NEW_UNIT,
67        PARAMICS,//EVENT
68        RADIO_EVALUATION,
69        SAMPLE_MESSAGE,
70        SCRIPT_DATA,
71        SCRIPT_EVENT,
72        TELEPHONE,//EVENT
73        //These elements have no sub-elements, but all have character data.
74        ADDRESS,
75        AGY,
76        ALIAS,
77        AREA,
78        BADGE_NUM,
79        BEAT,
80        Beat,
81        CCTV_INFO,
82        CITY,
83        CMS_LINE,
84        COMMENT,
85        CURR_LOC,
86        DESTINATION,
87        DETAIL,
88        Direction,
89        EMS,
90        EXPECTED_ACTION,
91        FAX,
92        FIRE,
93        FullLoc,
94        ID,
95        INCIDENT,
96        Incident_type,
97        INSTRUCTOR,
98        Lane_number,
99        LAW,
100        LINE,
101        LOC,
102        Location_type,
103        MASTER_INC_NUM,
104        MAINTENANCE_RADIO,//EVENT
105        MISC_INFO,
106        NAME,
107        OFFICE,
108        OFFICER,
109        OOS,
110        P,
111        PHONE,
112        Postmile,
113        PRIMARY,
114        Route,
115        SERVICE,
116        STACK,
117        STATE,
118        STATUS,
119        Status,
120        STUDENT,
121        TEXT,
122        TIMER,
123        TIME_INDEX,
124        TITLE,
125        TMT_RADIO,//EVENT
126        TruncLoc,
127        TYPE,
128        Type,
129        TYPE_CODE,
130        UNIT_STATUS,
131        ZIP,
132        //These elements have no sub-elements or character data.
133        //They do have attributes.
134        AUDIO,//EVENT
135        TOW,//EVENT
136        UNIT,//EVENT
137        WITNESS;//EVENT
138
139        public String tag;
140
141        private ELEMENT()
142        {
143            this.tag = this.name();
144        }
145
146        /**
147         * Look up an element by its name.
148         *
149         * @param name the name of the element
150         * @return the element which has that name as its nametag, or null if
151         * none does
152         */
153        public static ELEMENT byName(String name)
154        {
155            for (ELEMENT e : ELEMENT.values())
156            {
157                if (e.tag.equals(name))
158                {
159                    return e;
160                }
161            }
162            return null;
163        }
164    }
165
166    /**
167     * Map containing all parsed values. Keys = XML Element. Values = parsed
168     * character data from XML file.
169     */
170    private TreeMap<ELEMENT, String> pcData;
171
172    /**
173     * Current element that we're in.
174     */
175    private ELEMENT currentElement;
176
177    /**
178     * Ordering of elements that we're within.
179     */
180    private Stack<ELEMENT> docPosition;
181
182    /**
183     * Map containing all parsed Incidents. Keys = Incident Log Number. Values =
184     * Incident object.
185     */
186    private TreeMap<Integer, ScriptIncident> incidentMap;
187
188    /**
189     * Map containing all Units. Keys = Unit number. Values = Unit object.
190     */
191    private TreeMap<String, Unit> unitMap;
192
193    /**
194     * Map containing all parsed script events. Keys = XML file level. Values =
195     * Event object.
196     */
197    private TreeMap<ELEMENT, I_ScriptEvent> eventMap;
198
199    /**
200     * Buffer used to hold parsed tag content.
201     */
202    private StringBuffer parsedValue = new StringBuffer();
203
204    /**
205     * *************************************************
206     * ~~~~~~~~Element attribute data.~~~~~~~~
207     * **************************************************
208     */
209    /**
210     * Log number for the current ScriptEvent being parsed.
211     */
212    private Integer incidentLogNumber = 0;
213
214    /**
215     * File path for the current audio file.
216     */
217    private String audioPath = "";
218
219    /**
220     * Length in seconds of the current audio file.
221     */
222    private Integer audioLength = 0;
223
224    /**
225     * Category name for the current cardfile.
226     */
227    private String cardfileCategory = "";
228
229    /**
230     * File path for the current CHP radio file.
231     */
232    private String CHPRadiofile = "";
233
234    /**
235     * ID number for the current CMS data.
236     */
237    private String CMSID = "";
238
239    /**
240     * Type for the current CMS data.
241     */
242    private String CMSType = "";
243
244    /**
245     * Role played by the instructor for the current dialog line.
246     */
247    private String InstructorRole = "";
248
249    /**
250     * Role played by the speaker for the current dialog line.
251     */
252    private String LineRole = "";
253
254    /**
255     * ID number for the current location info.
256     */
257    private String LocationInfoID = "";
258
259    /**
260     * ID number for the current unit.
261     */
262    private String NewUnitNum = "";
263
264    /**
265     * ID number for the current Paramics data.
266     */
267    private String ParamicsID = "";
268
269    /**
270     * Title of the script.
271     */
272    private String TMCTitle = "";
273
274    /**
275     * Beat for the current Tow data.
276     */
277    private String TowBeat = "";
278
279    /**
280     * Company name for the current Tow data.
281     */
282    private String TowCompany = "";
283
284    /**
285     * Confirmation phone number for the current Tow data.
286     */
287    private String TowConfNum = "";
288
289    /**
290     * Public telephone number for the current Tow data.
291     */
292    private String TowPubNum = "";
293
294    /**
295     * True/false value for whether the current unit is active.
296     */
297    private String UnitActive = "";
298
299    /**
300     * True/false value for whether the current unit is the primary unit.
301     */
302    private String UnitPrimary = "";
303
304    /**
305     * Status of the current unit.
306     */
307    private String UnitStatus = "";
308
309    /**
310     * ID number for the current unit.
311     */
312    private String UnitNum = "";
313
314    /**
315     * Street address of the current witness.
316     */
317    private String WitnessAddress = "";
318
319    /**
320     * Name of the current witness.
321     */
322    private String WitnessName = "";
323
324    /**
325     * Phone number of the current witness.
326     */
327    private String WitnessNum = "";
328
329    /**
330     * If true, the current CAD event is not empty.
331     */
332    private boolean foundSubEvents = false;
333
334    /**
335     * The most recent element which contains dialog or other sub-elements.
336     */
337    private I_ScriptEvent trackedEvent = null;
338
339    /**
340     * Incident description for the current ScriptEvent being parsed.
341     */
342    private String currentIncidentDesc = "";
343
344    /**
345     * Name of the current incident.
346     */
347    private String currentIncName = "";
348
349    /**
350     * Time index value (in seconds) for the current ScriptEvent being parsed.
351     */
352    private long currentEventTime = 0;
353
354    /**
355     * Number of levels deep we are. Used for debugging.
356     */
357    private int sublevels = 0;
358
359    /**
360     * The incident we are currently editing.
361     */
362    private ScriptIncident currInc = null;
363
364    /**
365     * Constructor. Initializes incident map.
366     */
367    public MyScriptHandler(SimulationScript s)
368    {
369        script = s;
370        incidentMap = new TreeMap<Integer, ScriptIncident>();
371        eventMap = new TreeMap<ELEMENT, I_ScriptEvent>();
372        pcData = new TreeMap<ELEMENT, String>();
373        docPosition = new Stack<ELEMENT>();
374    }
375
376    /**
377     * Get the list of incidents that have been parsed from the script file.
378     *
379     * @returns Vector of Incident objects.
380     */
381    public Vector<ScriptIncident> getIncidents()
382    {
383        return new Vector<ScriptIncident>(incidentMap.values());
384    }
385
386    /**
387     * SAX Handler method. Clear incident map and reset the error flag.
388     */
389    @Override
390    public void startDocument()
391    {
392        //System.out.println("STUB: Start the document");
393        for (ELEMENT e : ELEMENT.values())
394        {
395            pcData.put(e, null);
396        }
397    }
398
399    /**
400     * Clear all existing events, thereby resetting the script.
401     */
402    public void reset()
403    {
404        incidentMap.clear();
405        System.out.println("STUB: Restart the document");
406    }
407
408    /**
409     * SAX Handler method. Executes at the beginning of each XML element.
410     */
411    @Override
412    public void startElement(String uri, String localName, String qName, Attributes attributes)
413    {
414        /*System.out.println("Start_element;");
415         String t = "";
416         for (int i = 0; i < sublevels; i++)
417         {
418         t += "\t";
419         }
420         System.out.println(t + "New element " + qName);
421         if (attributes.getLength() > 0)
422         {
423         System.out.println(t + "Attributes:");
424         for (int i = 0; i < attributes.getLength(); i++)
425         {
426         System.out.print(t + "" + (i + 1) + ". " + attributes.getQName(i));
427         System.out.println(": '" + attributes.getValue(i) + "\'");
428         }
429         }
430         sublevels++;*/
431        docPosition.push(ELEMENT.byName(qName));
432
433        try
434        {
435            if (qName.equals(ELEMENT.ACTIVITY_LOG_EVALUATION.tag))
436            {
437                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.ACTIVITY_LOG_EVAL_EVENT));
438                trackedEvent = eventMap.get(docPosition.peek());
439            }
440            else if (qName.equals(ELEMENT.ATMS_EVALUATION.tag))
441            {
442                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.ATMS_EVAL_EVENT));
443                trackedEvent = eventMap.get(docPosition.peek());
444            }
445            else if (qName.equals(ELEMENT.AUDIO.tag))
446            {
447                foundSubEvents = true;
448                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.AUDIO_EVENT));
449                try
450                {
451                    audioLength = Integer.parseInt(attributes.getValue("Length"));
452                    audioPath = attributes.getValue("Path");
453                }
454                catch (Exception e)
455                {
456                    audioLength = 0;
457                    audioPath = "";
458                }
459            }
460            else if (qName.equals(ELEMENT.CARDFILE.tag))
461            {
462                try
463                {
464                    cardfileCategory = attributes.getValue("Category");
465                }
466                catch (Exception e)
467                {
468                    cardfileCategory = "";
469                }
470            }
471            else if (qName.equals(ELEMENT.CAD_EVALUATION.tag))
472            {
473                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.CAD_EVAL_EVENT));
474                trackedEvent = eventMap.get(docPosition.peek());
475            }
476            else if (qName.equals(ELEMENT.CAD_INCIDENT_EVENT.tag))
477            {
478                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.CAD_EVENT));
479            }
480            else if (qName.equals(ELEMENT.CCTV_INFO.tag))
481            {
482                foundSubEvents = true;
483                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.CCTV_EVENT));
484            }
485            else if (qName.equals(ELEMENT.CHP_RADIO.tag))
486            {
487                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.CHP_RADIO_EVENT));
488                trackedEvent = eventMap.get(docPosition.peek());
489                try
490                {
491                    CHPRadiofile = attributes.getValue("RadioFile");
492                }
493                catch (Exception e)
494                {
495                    CHPRadiofile = "";
496                }
497            }
498            else if (qName.equals(ELEMENT.CMS_EVALUATION.tag))
499            {
500                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.CMS_EVAL_EVENT));
501                trackedEvent = eventMap.get(docPosition.peek());
502                try
503                {
504                    CMSID = attributes.getValue("cmsID");
505                    CMSType = attributes.getValue("type");
506                }
507                catch (Exception e)
508                {
509                    CMSID = "";
510                    CMSType = "";
511                }
512            }
513            else if (qName.equals(ELEMENT.FACILITATOR_EVALUATION.tag))
514            {
515                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.FACILITATOR_EVAL_EVENT));
516                trackedEvent = eventMap.get(docPosition.peek());
517            }
518            else if (qName.equals(ELEMENT.INSTRUCTOR.tag))
519            {
520                try
521                {
522                    InstructorRole = attributes.getValue("Role");
523                }
524                catch (Exception e)
525                {
526                    System.out.println("SERIOUS PARSING ERROR IN \'Role\'");
527                    InstructorRole = "";
528                }
529            }
530            else if (qName.equals(ELEMENT.LINE.tag))
531            {
532                try
533                {
534                    LineRole = attributes.getValue("Role");
535                }
536                catch (Exception e)
537                {
538                    LineRole = "";
539                }
540            }
541            else if (qName.equals(ELEMENT.LOCATION_INFO.tag))
542            {
543                try
544                {
545                    LocationInfoID = attributes.getValue("ID");
546                }
547                catch (Exception e)
548                {
549                    LocationInfoID = "";
550                }
551            }
552            else if (qName.equals(ELEMENT.MAINTENANCE_RADIO.tag))
553            {
554                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.MAINTENANCE_RADIO_EVENT));
555            }
556            else if (qName.equals(ELEMENT.NEW_UNIT.tag))
557            {
558                try
559                {
560                    NewUnitNum = attributes.getValue("UnitNum");
561                }
562                catch (Exception e)
563                {
564                    NewUnitNum = "";
565                }
566            }
567            else if (qName.equals(ELEMENT.PARAMICS.tag))
568            {
569                foundSubEvents = true;
570                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.PARAMICS_EVENT));
571                trackedEvent = eventMap.get(docPosition.peek());
572                try
573                {
574                    ParamicsID = attributes.getValue("LocationID");
575                }
576                catch (Exception e)
577                {
578                    ParamicsID = "";
579                }
580            }
581            else if (qName.equals(ELEMENT.RADIO_EVALUATION.tag))
582            {
583                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.RADIO_EVAL_EVENT));
584                trackedEvent = eventMap.get(docPosition.peek());
585            }
586            else if (qName.equals(ELEMENT.TMC_SCRIPT.tag))
587            {
588                try
589                {
590                    TMCTitle = attributes.getValue("title");
591                }
592                catch (Exception e)
593                {
594                    TMCTitle = "";
595                }
596            }
597            else if (qName.equals(ELEMENT.TMT_RADIO.tag))
598            {
599                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.TMT_RADIO_EVENT));
600            }
601            else if (qName.equals(ELEMENT.TOW.tag))
602            {
603                foundSubEvents = true;
604                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.TOW_EVENT));
605                try
606                {
607                    TowBeat = attributes.getValue("Beat");
608                    TowCompany = attributes.getValue("Company");
609                    TowConfNum = attributes.getValue("ConfNum");
610                    TowPubNum = attributes.getValue("PubNum");
611                }
612                catch (Exception e)
613                {
614                    TowBeat = "";
615                    TowCompany = "";
616                    TowConfNum = "";
617                    TowPubNum = "";
618                }
619            }
620            else if (qName.equals(ELEMENT.TELEPHONE.tag))
621            {
622                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.TELEPHONE_EVENT));
623                trackedEvent = eventMap.get(docPosition.peek());
624            }
625            else if (qName.equals(ELEMENT.UNIT.tag))
626            {
627                foundSubEvents = true;
628                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.UNIT_EVENT));
629                try
630                {
631                    UnitActive = attributes.getValue("Active");
632                    UnitPrimary = attributes.getValue("Primary");
633                    UnitStatus = attributes.getValue("Status");
634                    UnitNum = attributes.getValue("UnitNum");
635                }
636                catch (Exception e)
637                {
638                    UnitActive = "";
639                    UnitPrimary = "";
640                    UnitStatus = "";
641                    UnitNum = "0-0";
642                }
643            }
644            else if (qName.equals(ELEMENT.WITNESS.tag))
645            {
646                foundSubEvents = true;
647                eventMap.put(docPosition.peek(), ScriptEvent.factoryByType(ScriptEvent.ScriptEventType.WITNESS_EVENT));
648                try
649                {
650                    WitnessAddress = attributes.getValue("Address");
651                    WitnessName = attributes.getValue("Name");
652                    WitnessNum = attributes.getValue("PhoneNum");
653                }
654                catch (Exception e)
655                {
656                    WitnessAddress = "";
657                    WitnessName = "";
658                    WitnessNum = "";
659                }
660            }
661            else if (qName.equals(ELEMENT.INCIDENT.tag))
662            {
663                try
664                {
665                    incidentLogNumber = Integer.parseInt(attributes.getValue(
666                            "LogNum"));
667                }
668                catch (Exception e)
669                {
670                    myScriptLogger.logp(Level.SEVERE, "ScriptHandler", "startElement",
671                            "Invalid LogNumber " + attributes.getValue(
672                                    "LogNum"), e);
673                    incidentLogNumber = 0;
674                }
675            }
676            else if (qName.equals(ELEMENT.UNIT.tag))
677            {
678                try
679                {
680                    UnitNum = attributes.getValue("UnitNum");
681                }
682                catch (Exception e)
683                {
684                    myScriptLogger.logp(Level.SEVERE, "ScriptHandler", "startElement",
685                            "Invalid Unit Number " + attributes.getValue(
686                                    "UnitNum"), e);
687                    UnitNum = "0-0";
688                }
689            }
690        }
691        catch (Exception e)
692        {
693            myScriptLogger.logp(Level.SEVERE, "ScriptHandler", "startElement",
694                    "Exception in starting element <" + qName + ">.", e);
695        }
696    }
697
698    /**
699     * SAX Handler method. Append read characters to local buffer.
700     */
701    @Override
702    public void characters(char[] ch, int start, int length)
703    {
704        String str = new String(ch, start, length).trim();
705        str = str.replace("\t ", "");
706        str = str.replace("\t", "");
707        str = str.replace("\n ", "\n");
708
709        parsedValue.append(str);
710        if (pcData.get(docPosition.peek()) == null || pcData.get(docPosition.peek()).equals(""))
711        {
712            pcData.put(docPosition.peek(), str);
713        }
714        else
715        {
716            pcData.put(docPosition.peek(), "" + (pcData.get(docPosition.peek())
717                    + "\n" + str));
718        }
719    }
720
721    /**
722     * SAX Handler method. Executes at the close of each XML element.
723     */
724    @Override
725    public void endElement(String uri, String localName, String qName)
726    {
727
728        currentElement = docPosition.pop();
729        I_ScriptEvent newEvent = null;
730        try
731        {
732            if (currentElement == ELEMENT.ATMS_EVALUATION)
733            {
734                newEvent = eventMap.remove(currentElement);
735
736                currInc.addNewEvent(newEvent, (int) currentEventTime);
737            }
738            else if (currentElement == ELEMENT.ACTIVITY_LOG_EVALUATION)
739            {
740                newEvent = eventMap.remove(currentElement);
741
742                currInc.addNewEvent(newEvent, (int) currentEventTime);
743            }
744            else if (currentElement == ELEMENT.AUDIO)
745            {
746                newEvent = eventMap.remove(currentElement);
747                ((AudioEvent) newEvent).audioLength = audioLength;
748                ((AudioEvent) newEvent).audioPath = audioPath;
749                ((AudioEvent) newEvent).length = audioLength;
750                currInc.addNewEvent(newEvent, (int) currentEventTime);
751            }
752            else if (currentElement == ELEMENT.CAD_EVALUATION)
753            {
754                newEvent = eventMap.remove(currentElement);
755
756                currInc.addNewEvent(newEvent, (int) currentEventTime);
757            }
758            else if (currentElement == ELEMENT.CAD_INCIDENT_EVENT)
759            {
760                newEvent = eventMap.remove(currentElement);
761                ((CADEvent) newEvent).detail = pcData.remove(ELEMENT.DETAIL);
762                ((CADEvent) newEvent).hasSubEvents = foundSubEvents;
763                if (null != ((CADEvent) newEvent).detail)
764                {
765                    if (!(((CADEvent) newEvent).detail.equals("")))
766                    {
767                        currInc.addNewEvent(newEvent, (int) currentEventTime);
768                    }
769                }
770                foundSubEvents = false;
771            }
772            else if (currentElement == ELEMENT.CCTV_INFO)
773            {
774                newEvent = eventMap.remove(currentElement);
775                ((CCTVEvent) newEvent).message = pcData.remove(ELEMENT.CCTV_INFO);
776                currInc.addNewEvent(newEvent, (int) currentEventTime);
777            }
778            else if (currentElement == ELEMENT.CHP_RADIO)
779            {
780                newEvent = eventMap.remove(currentElement);
781                ((CHPRadioEvent) newEvent).radioFile = CHPRadiofile;
782                currInc.addNewEvent(newEvent, (int) currentEventTime);
783            }
784
785            else if (currentElement == ELEMENT.CMS_EVALUATION)
786            {
787                newEvent = eventMap.remove(currentElement);
788                ((CMSEvaluationEvent) newEvent).cmsID = CMSID;
789                ((CMSEvaluationEvent) newEvent).cmsType = CMSType;
790                if (pcData.get(ELEMENT.LOCATION) != null)
791                {
792                    ((CMSEvaluationEvent) newEvent).location = pcData.remove(ELEMENT.LOCATION);
793                }
794
795                currInc.addNewEvent(newEvent, (int) currentEventTime);
796            }
797            else if (currentElement == ELEMENT.CMS_LINE)
798            {
799                if (trackedEvent instanceof CMSEvaluationEvent)
800                {
801                    ((CMSEvaluationEvent) trackedEvent).message.add(pcData.remove(ELEMENT.CMS_LINE));
802                }
803            }
804            else if (currentElement == ELEMENT.EXPECTED_ACTION)
805            {
806                if (trackedEvent instanceof I_EvaluationEvent)
807                {
808                    ((I_EvaluationEvent) trackedEvent).addAction(pcData.remove(ELEMENT.EXPECTED_ACTION));
809                }
810            }
811            else if (currentElement == ELEMENT.FACILITATOR_EVALUATION)
812            {
813                newEvent = eventMap.remove(currentElement);
814
815                currInc.addNewEvent(newEvent, (int) currentEventTime);
816            }
817            else if (currentElement == ELEMENT.INSTRUCTOR)
818            {
819                if (trackedEvent instanceof TelephoneEvent)
820                {
821                    ((TelephoneEvent) trackedEvent).lines.add(pcData.remove(ELEMENT.INSTRUCTOR));
822                    ((TelephoneEvent) trackedEvent).roles.add(InstructorRole);
823                }
824            }
825            else if (currentElement == ELEMENT.Lane_number)
826            {
827                if (trackedEvent instanceof ParamicsEvent)
828                {
829                    ((ParamicsEvent) trackedEvent).laneNums.add(Integer.parseInt(pcData.remove(ELEMENT.Lane_number)));
830                }
831            }
832            else if (currentElement == ELEMENT.LINE)
833            {
834                if (trackedEvent instanceof CHPRadioEvent)
835                {
836                    ((CHPRadioEvent) trackedEvent).lines.add(pcData.remove(ELEMENT.LINE));
837                    ((CHPRadioEvent) trackedEvent).roles.add(LineRole);
838                }
839            }
840            else if (currentElement == ELEMENT.MAINTENANCE_RADIO)
841            {
842                newEvent = eventMap.remove(currentElement);
843                ((MaintenanceRadioEvent) newEvent).message = pcData.remove(ELEMENT.MAINTENANCE_RADIO);
844                currInc.addNewEvent(newEvent, (int) currentEventTime);
845            }
846            else if (currentElement == ELEMENT.PARAMICS)
847            {
848                newEvent = eventMap.remove(currentElement);
849                ((ParamicsEvent) newEvent).locationID = ParamicsID;
850
851                if (pcData.get(ELEMENT.Status) != null)
852                {
853                    ((ParamicsEvent) newEvent).status = pcData.remove(ELEMENT.Status);
854                }
855                if (pcData.get(ELEMENT.Type) != null)
856                {
857                    ((ParamicsEvent) newEvent).type = pcData.remove(ELEMENT.Type);
858                }
859                currInc.addNewEvent(newEvent, (int) currentEventTime);
860            }
861            else if (currentElement == ELEMENT.RADIO_EVALUATION)
862            {
863                newEvent = eventMap.remove(currentElement);
864
865                currInc.addNewEvent(newEvent, (int) currentEventTime);
866            }
867            else if (currentElement == ELEMENT.STUDENT)
868            {
869                if (trackedEvent instanceof TelephoneEvent)
870                {
871                    ((TelephoneEvent) trackedEvent).lines.add(pcData.remove(ELEMENT.STUDENT));
872                    ((TelephoneEvent) trackedEvent).roles.add("Student");
873                }
874            }
875            else if (currentElement == ELEMENT.TEXT && docPosition.peek() == ELEMENT.GENERAL_INFO)
876            {
877                currInc.description = pcData.remove(ELEMENT.TEXT);
878            }
879            else if (currentElement == ELEMENT.TMT_RADIO)
880            {
881                newEvent = eventMap.remove(currentElement);
882                ((TMTRadioEvent) newEvent).message = pcData.remove(ELEMENT.TMT_RADIO);
883                currInc.addNewEvent(newEvent, (int) currentEventTime);
884            }
885            else if (currentElement == ELEMENT.TELEPHONE)
886            {
887                newEvent = eventMap.remove(currentElement);
888                currInc.addNewEvent(newEvent, (int) currentEventTime);
889            }
890            else if (currentElement == ELEMENT.TOW)
891            {
892                newEvent = eventMap.remove(currentElement);
893                ((TowEvent) newEvent).towBeat = TowBeat;
894                ((TowEvent) newEvent).towCompany = TowCompany;
895                ((TowEvent) newEvent).towConfNum = TowConfNum;
896                ((TowEvent) newEvent).towPubNum = TowPubNum;
897                currInc.addNewEvent(newEvent, (int) currentEventTime);
898            }
899            else if (currentElement == ELEMENT.UNIT)
900            {
901                newEvent = eventMap.remove(currentElement);
902                ((UnitEvent) newEvent).unitActive = UnitActive;
903                ((UnitEvent) newEvent).unitPrimary = UnitPrimary;
904                ((UnitEvent) newEvent).unitStatus = UnitStatus;
905                ((UnitEvent) newEvent).unitNum = UnitNum;
906                currInc.addNewEvent(newEvent, (int) currentEventTime);
907            }
908            else if (currentElement == ELEMENT.WITNESS)
909            {
910                newEvent = eventMap.remove(currentElement);
911                ((WitnessEvent) newEvent).witnessAddress = WitnessAddress;
912                ((WitnessEvent) newEvent).witnessName = WitnessName;
913                ((WitnessEvent) newEvent).witnessNum = WitnessNum;
914                currInc.addNewEvent(newEvent, (int) currentEventTime);
915            }
916
917            else if (qName.equals(ELEMENT.INCIDENT.tag))
918            {
919                currentIncName = parsedValue.toString();
920
921                if (incidentMap.get(incidentLogNumber) == null)
922                {
923                    incidentMap.put(incidentLogNumber,
924                            new ScriptIncident(SimulationScript.incidentColors[Math.abs(new Random().nextInt()) % SimulationScript.incidentColors.length],
925                                    incidentLogNumber, currentIncName, currentIncidentDesc,
926                                    script, (int) currentEventTime));
927                }
928                currInc = incidentMap.get(incidentLogNumber);
929
930            }
931            else if (qName.equals(ELEMENT.NEW_UNIT))
932            {
933                if (unitMap.get(NewUnitNum) == null)
934                {
935                    Unit unit = new Unit();
936                    unit.UnitNum = NewUnitNum;
937                    if (pcData.containsKey(ELEMENT.AGY))
938                    {
939                        unit.Agy = pcData.remove(ELEMENT.AGY);
940                    }
941                    if (pcData.containsKey(ELEMENT.ALIAS))
942                    {
943                        unit.Alias = pcData.remove(ELEMENT.ALIAS);
944                    }
945                    if (pcData.containsKey(ELEMENT.AREA))
946                    {
947                        unit.Area = pcData.remove(ELEMENT.AREA);
948                    }
949                    if (pcData.containsKey(ELEMENT.BADGE_NUM))
950                    {
951                        unit.Badge_Num = pcData.remove(ELEMENT.BADGE_NUM);
952                    }
953                    if (pcData.containsKey(ELEMENT.CURR_LOC))
954                    {
955                        unit.Curr_Loc = pcData.remove(ELEMENT.CURR_LOC);
956                    }
957                    if (pcData.containsKey(ELEMENT.DESTINATION))
958                    {
959                        unit.Destination = pcData.remove(ELEMENT.DESTINATION);
960                    }
961                    if (pcData.containsKey(ELEMENT.ID))
962                    {
963                        unit.ID = pcData.remove(ELEMENT.ID);
964                    }
965                    if (pcData.containsKey(ELEMENT.MASTER_INC_NUM))
966                    {
967                        unit.Master_Inc_Num = pcData.remove(ELEMENT.MASTER_INC_NUM);
968                    }
969                    if (pcData.containsKey(ELEMENT.MISC_INFO))
970                    {
971                        unit.Misc_Info = pcData.remove(ELEMENT.MISC_INFO);
972                    }
973                    if (pcData.containsKey(ELEMENT.OOS))
974                    {
975                        unit.OOS = pcData.remove(ELEMENT.OOS);
976                    }
977                    if (pcData.containsKey(ELEMENT.OFFICE))
978                    {
979                        unit.Office = pcData.remove(ELEMENT.OFFICE);
980                    }
981                    if (pcData.containsKey(ELEMENT.OFFICER))
982                    {
983                        unit.Officer = pcData.remove(ELEMENT.OFFICER);
984                    }
985                    if (pcData.containsKey(ELEMENT.P))
986                    {
987                        unit.P = pcData.remove(ELEMENT.P);
988                    }
989                    if (pcData.containsKey(ELEMENT.PRIMARY))
990                    {
991                        unit.Primary = pcData.remove(ELEMENT.PRIMARY);
992                    }
993                    if (pcData.containsKey(ELEMENT.STACK))
994                    {
995                        unit.Stack = pcData.remove(ELEMENT.STACK);
996                    }
997                    if (pcData.containsKey(ELEMENT.STATUS))
998                    {
999                        unit.Status = pcData.remove(ELEMENT.STATUS);
1000                    }
1001                    if (pcData.containsKey(ELEMENT.TIMER))
1002                    {
1003                        unit.Timer = pcData.remove(ELEMENT.TIMER);
1004                    }
1005                }
1006            }
1007            else if (qName.equals(ELEMENT.TIME_INDEX.tag))
1008            {
1009                currentEventTime = timeBytesToSeconds(parsedValue.toString().trim());
1010            }
1011
1012            parsedValue.setLength(0);
1013            eventMap.put(currentElement, null);
1014        }
1015        catch (Exception e)
1016        {
1017            myScriptLogger.logp(Level.SEVERE, "ScriptHandler", "endElement",
1018                    "Exception in ending element <" + qName + ">.", e);
1019        }
1020        /*
1021         String t = "";
1022         sublevels--;
1023         for (int i = 0; i < sublevels; i++)
1024         {
1025         t += "\t";
1026         }
1027         System.out.println(t + "End of element " + qName);*/
1028
1029    }
1030
1031    /**
1032     * SAX Handler method. End the document and close it out.
1033     */
1034    @Override
1035    public void endDocument()
1036    {
1037        //System.out.println("STUB: End the document");
1038    }
1039
1040    /**
1041     * Sax Handler method. Log normal errors.
1042     */
1043    @Override
1044    public void error(SAXParseException e)
1045    {
1046        myScriptLogger.logp(Level.SEVERE, "ScriptHandler", "error",
1047                "SAX Parsing error.", e);
1048    }
1049
1050    /**
1051     * SAX Handler method. Log fatal errors.
1052     */
1053    @Override
1054    public void fatalError(SAXParseException e)
1055    {
1056        myScriptLogger.logp(Level.SEVERE, "ScriptHandler", "fatalError",
1057                "SAX Parsing fatal error.", e);
1058    }
1059
1060    /**
1061     * SAX Handler method. Log warnings.
1062     */
1063    @Override
1064    public void warning(SAXParseException e)
1065    {
1066        myScriptLogger.logp(Level.SEVERE, "ScriptHandler", "warning",
1067                "SAX Parsing warning.", e);
1068    }
1069
1070    /**
1071     * Private method to convert a time object from format HH:MM:SS to a long
1072     * value of the corresponding number of seconds.
1073     *
1074     * @param time String time representation of format HH:MM:SS
1075     * @return long Number of seconds
1076     * @throws StringIndexOutOfBoundsException if the input parameter is not
1077     * valid
1078     */
1079    private long timeBytesToSeconds(String time)
1080            throws StringIndexOutOfBoundsException
1081    {
1082        long seconds = 0;
1083
1084        seconds = ((long) Character.digit(time.charAt(0), 10) * 36000
1085                + Character.digit(time.charAt(1), 10) * 3600
1086                + Character.digit(time.charAt(3), 10) * 600
1087                + Character.digit(time.charAt(4), 10) * 60
1088                + Character.digit(time.charAt(6), 10) * 10
1089                + Character.digit(time.charAt(7), 10));
1090
1091        return seconds;
1092    }
1093}
Note: See TracBrowser for help on using the repository browser.