| 1 | package paramsim.paramicssimulator; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.io.FileReader; |
|---|
| 5 | import java.io.IOException; |
|---|
| 6 | import java.util.ArrayList; |
|---|
| 7 | import java.util.List; |
|---|
| 8 | import java.util.Scanner; |
|---|
| 9 | |
|---|
| 10 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| 11 | import javax.xml.parsers.ParserConfigurationException; |
|---|
| 12 | |
|---|
| 13 | import org.w3c.dom.Document; |
|---|
| 14 | import org.w3c.dom.Element; |
|---|
| 15 | import org.w3c.dom.NodeList; |
|---|
| 16 | import org.xml.sax.SAXException; |
|---|
| 17 | |
|---|
| 18 | import paramsim.paramicssimulator.SimulationCamera.ROAD_DIRECTION; |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * Creates a list of Simulation Cameras by parsing two configuration files: |
|---|
| 22 | * the camera_control file used by Paramics, and the dvdplayers.xml file |
|---|
| 23 | * used by the CAD Simulator. |
|---|
| 24 | * |
|---|
| 25 | * @author Greg Eddington |
|---|
| 26 | */ |
|---|
| 27 | public class CameraParser |
|---|
| 28 | { |
|---|
| 29 | /** Stopped DVD Title **/ |
|---|
| 30 | private static final String STOPPED_TITLE = "3"; |
|---|
| 31 | /** Slow DVD Titlee **/ |
|---|
| 32 | private static final String SLOW_TITLE = "2"; |
|---|
| 33 | /** Free Flow DVD Title **/ |
|---|
| 34 | private static final String FREE_FLOW_TITLE = "1"; |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * Enumeration containing XML tag names used to parse the XML |
|---|
| 38 | * document containing information about DVD Cameras. |
|---|
| 39 | * @author Matthew Cechini |
|---|
| 40 | * @author Greg Eddington |
|---|
| 41 | */ |
|---|
| 42 | private static enum CAMERA_TAGS |
|---|
| 43 | { |
|---|
| 44 | /** Top level tag. */ |
|---|
| 45 | DVD_PLAYERS ("DVD_PLAYERS"), |
|---|
| 46 | /** DVD player info. */ |
|---|
| 47 | DVD_PLAYER ("DVD_PLAYER"), |
|---|
| 48 | /** CCTV camera info. */ |
|---|
| 49 | CCTV ("CCTV"), |
|---|
| 50 | /** CCTV camera id. */ |
|---|
| 51 | ID ("id"), |
|---|
| 52 | /** CCTV camera direction. */ |
|---|
| 53 | DIRECTION ("dir"), |
|---|
| 54 | /** Speed range mapping to a title number. */ |
|---|
| 55 | RANGE ("RANGE"), |
|---|
| 56 | /** Minimum speed in a range. */ |
|---|
| 57 | MIN_SPEED ("min_speed"), |
|---|
| 58 | /** Maximum speed in a range. */ |
|---|
| 59 | MAX_SPEED ("max_speed"), |
|---|
| 60 | /** Title name. */ |
|---|
| 61 | TITLE ("title"); |
|---|
| 62 | |
|---|
| 63 | /** XML Tag name. */ |
|---|
| 64 | public String tag; |
|---|
| 65 | |
|---|
| 66 | /** Constructor **/ |
|---|
| 67 | private CAMERA_TAGS(String t) { tag = t; } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * Constructor. |
|---|
| 72 | */ |
|---|
| 73 | public CameraParser() |
|---|
| 74 | { |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * Parses two input files and creates a list of Simulation Cameras. |
|---|
| 79 | * |
|---|
| 80 | * @param dvdPlayersXml The XML file which contains DVD information for the CAD Simulator. |
|---|
| 81 | * @param cameraControlFile The camera_control file used by Paramics to map cameras to a simulation. |
|---|
| 82 | * @return A list of cameras configured from the parsed files. |
|---|
| 83 | * @throws IOException On error reading or parsing one of the files. |
|---|
| 84 | */ |
|---|
| 85 | public static List<SimulationCamera> loadCameras(String dvdPlayersXml, String cameraControlFile) |
|---|
| 86 | throws IOException |
|---|
| 87 | { |
|---|
| 88 | List<SimulationCamera> cameras = new ArrayList<SimulationCamera>(); |
|---|
| 89 | |
|---|
| 90 | // XML document |
|---|
| 91 | try |
|---|
| 92 | { |
|---|
| 93 | Document newDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder() |
|---|
| 94 | .parse(new File(dvdPlayersXml)); |
|---|
| 95 | |
|---|
| 96 | Element rootElement = newDoc.getDocumentElement(); |
|---|
| 97 | |
|---|
| 98 | NodeList players = rootElement.getElementsByTagName(CAMERA_TAGS.DVD_PLAYER.tag); |
|---|
| 99 | |
|---|
| 100 | for(int i = 0; i < players.getLength(); i++) |
|---|
| 101 | { |
|---|
| 102 | SimulationCamera camera = new SimulationCamera(); |
|---|
| 103 | |
|---|
| 104 | Element playerElement = (Element)players.item(i); |
|---|
| 105 | |
|---|
| 106 | Element cctvElement = (Element)playerElement.getElementsByTagName(CAMERA_TAGS.CCTV.tag).item(0); |
|---|
| 107 | camera.id = Integer.parseInt(cctvElement.getAttribute(CAMERA_TAGS.ID.tag)); |
|---|
| 108 | switch(cctvElement.getAttribute(CAMERA_TAGS.DIRECTION.tag).charAt(0)) |
|---|
| 109 | { |
|---|
| 110 | case 'N': |
|---|
| 111 | camera.direction = ROAD_DIRECTION.NORTH; |
|---|
| 112 | break; |
|---|
| 113 | case 'E': |
|---|
| 114 | camera.direction = ROAD_DIRECTION.EAST; |
|---|
| 115 | break; |
|---|
| 116 | case 'S': |
|---|
| 117 | camera.direction = ROAD_DIRECTION.SOUTH; |
|---|
| 118 | break; |
|---|
| 119 | default: |
|---|
| 120 | camera.direction = ROAD_DIRECTION.WEST; |
|---|
| 121 | break; |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | NodeList ranges = playerElement.getElementsByTagName(CAMERA_TAGS.RANGE.tag); |
|---|
| 125 | |
|---|
| 126 | for(int j = 0; j < ranges.getLength(); j++) |
|---|
| 127 | { |
|---|
| 128 | Element rangeElement = (Element)ranges.item(j); |
|---|
| 129 | |
|---|
| 130 | if (rangeElement.getAttribute(CAMERA_TAGS.TITLE.tag).contains(STOPPED_TITLE)) |
|---|
| 131 | { |
|---|
| 132 | camera.minStoppedSpeed = |
|---|
| 133 | Float.parseFloat(rangeElement.getAttribute(CAMERA_TAGS.MIN_SPEED.tag)); |
|---|
| 134 | camera.maxStoppedSpeed = |
|---|
| 135 | Float.parseFloat(rangeElement.getAttribute(CAMERA_TAGS.MAX_SPEED.tag)); |
|---|
| 136 | } |
|---|
| 137 | else if (rangeElement.getAttribute(CAMERA_TAGS.TITLE.tag).contains(SLOW_TITLE)) |
|---|
| 138 | { |
|---|
| 139 | camera.minSlowSpeed = |
|---|
| 140 | Float.parseFloat(rangeElement.getAttribute(CAMERA_TAGS.MIN_SPEED.tag)); |
|---|
| 141 | camera.maxSlowSpeed = |
|---|
| 142 | Float.parseFloat(rangeElement.getAttribute(CAMERA_TAGS.MAX_SPEED.tag)); |
|---|
| 143 | } |
|---|
| 144 | else if (rangeElement.getAttribute(CAMERA_TAGS.TITLE.tag).contains(FREE_FLOW_TITLE)) |
|---|
| 145 | { |
|---|
| 146 | camera.minFreeFlowSpeed = |
|---|
| 147 | Float.parseFloat(rangeElement.getAttribute(CAMERA_TAGS.MIN_SPEED.tag)); |
|---|
| 148 | camera.maxFreeFlowSpeed = |
|---|
| 149 | Float.parseFloat(rangeElement.getAttribute(CAMERA_TAGS.MAX_SPEED.tag)); |
|---|
| 150 | } |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | // Initialize Speed |
|---|
| 154 | camera.averageSpeedNE = camera.averageSpeedSW = |
|---|
| 155 | ((camera.minStoppedSpeed + camera.maxStoppedSpeed) / 2); |
|---|
| 156 | |
|---|
| 157 | cameras.add(camera); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | catch (ParserConfigurationException pce) |
|---|
| 161 | { |
|---|
| 162 | throw new IOException(pce.toString()); |
|---|
| 163 | } |
|---|
| 164 | catch (SAXException se) |
|---|
| 165 | { |
|---|
| 166 | throw new IOException(se.toString()); |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | // Camera Control File |
|---|
| 170 | Scanner f = new Scanner(new FileReader(cameraControlFile)); |
|---|
| 171 | |
|---|
| 172 | // Throw away the first line |
|---|
| 173 | f.nextLine(); |
|---|
| 174 | while (f.hasNextInt()) |
|---|
| 175 | { |
|---|
| 176 | int id = f.nextInt(); |
|---|
| 177 | |
|---|
| 178 | for (SimulationCamera camera : cameras) |
|---|
| 179 | { |
|---|
| 180 | if (camera.id == id) |
|---|
| 181 | { |
|---|
| 182 | // Route |
|---|
| 183 | camera.route = f.next(); |
|---|
| 184 | |
|---|
| 185 | // Direction |
|---|
| 186 | f.next(); |
|---|
| 187 | |
|---|
| 188 | // Postmile |
|---|
| 189 | camera.postmile = f.nextFloat(); |
|---|
| 190 | |
|---|
| 191 | break; |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | // Ignore the rest |
|---|
| 196 | f.nextLine(); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | return cameras; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|