001package jmri.jmrit.dispatcher; 002 003import java.io.File; 004import java.io.IOException; 005import java.util.ArrayList; 006import java.util.Arrays; 007import java.util.List; 008import java.util.regex.Matcher; 009import java.util.regex.Pattern; 010import jmri.util.FileUtil; 011import jmri.util.XmlFilenameFilter; 012import org.jdom2.Document; 013import org.jdom2.Element; 014import org.slf4j.Logger; 015import org.slf4j.LoggerFactory; 016 017import jmri.InstanceManager; 018import jmri.configurexml.AbstractXmlAdapter.EnumIO; 019import jmri.configurexml.AbstractXmlAdapter.EnumIoNamesNumbers; 020import jmri.jmrit.dispatcher.ActiveTrain.TrainDetection; 021 022/** 023 * Handles reading and writing of TrainInfo files to disk as an XML file to/from 024 * the dispatcher/traininfo/ directory in the user's preferences area 025 * <p> 026 * This class manipulates the files conforming to the dispatcher-traininfo DTD 027 * <p> 028 * The file is written when the user requests that train information be saved. A 029 * TrainInfo file is read when the user request it in the Activate New Train 030 * window 031 * 032 * <p> 033 * This file is part of JMRI. 034 * <p> 035 * JMRI is open source software; you can redistribute it and/or modify it under 036 * the terms of version 2 of the GNU General Public License as published by the 037 * Free Software Foundation. See the "COPYING" file for a copy of this license. 038 * <p> 039 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY 040 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 041 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 042 * 043 * @author Dave Duchamp Copyright (C) 2009 044 */ 045public class TrainInfoFile extends jmri.jmrit.XmlFile { 046 047 public TrainInfoFile() { 048 super(); 049 } 050 // operational variables 051 private String fileLocation = FileUtil.getUserFilesPath() 052 + "dispatcher" + File.separator + "traininfo" + File.separator; 053 054 public void setFileLocation(String testLocation) { 055 fileLocation = testLocation; 056 } 057 private Document doc = null; 058 private Element root = null; 059 060 static final EnumIO<ActiveTrain.TrainDetection> trainsdectionFromEnumMap = new EnumIoNamesNumbers<>(ActiveTrain.TrainDetection.class); 061 static final EnumIO<ActiveTrain.TrainLengthUnits> trainlengthFromEnumMap = new EnumIoNamesNumbers<>(ActiveTrain.TrainLengthUnits.class); 062 063 /* 064 * Reads Dispatcher TrainInfo from a file in the user's preferences directory 065 * If the file containing Dispatcher TrainInfo does not exist this routine returns quietly. 066 * "name" is assumed to have the .xml or .XML extension already included 067 */ 068 public TrainInfo readTrainInfo(String name) throws org.jdom2.JDOMException, java.io.IOException { 069 log.debug("entered readTrainInfo for {}", name); 070 TrainInfo tInfo = null; 071 int version = 1; 072 // check if file exists 073 if (checkFile(fileLocation + name)) { 074 // file is present. 075 tInfo = new TrainInfo(); 076 root = rootFromName(fileLocation + name); 077 if (root != null) { 078 // there is a file 079 Element traininfo = root.getChild("traininfo"); 080 if (traininfo != null) { 081 // get version so we dont look for missing fields 082 if (traininfo.getAttribute("version") != null ) { 083 try { 084 version = traininfo.getAttribute("version").getIntValue(); 085 } 086 catch(Exception ex) { 087 log.error("Traininfo file version number not an integer: assuming version 1"); 088 version = 1; 089 } 090 } else { 091 version = 1; 092 } 093 tInfo.setVersion(version); 094 // there are train info options defined, read them 095 if (traininfo.getAttribute("transitname") != null) { 096 // there is a transit name selected 097 tInfo.setTransitName(traininfo.getAttribute("transitname").getValue()); 098 } else { 099 log.error("Transit name missing when reading TrainInfoFile {}", name); 100 } 101 if (traininfo.getAttribute("dynamictransit") != null ) { 102 tInfo.setDynamicTransit(traininfo.getAttribute("dynamictransit").getValue().equals("yes")); 103 } 104 if (traininfo.getAttribute("dynamictransitcloseloop") != null ) { 105 tInfo.setDynamicTransitCloseLoopIfPossible(traininfo.getAttribute("dynamictransitcloseloop").getValue().equals("yes")); 106 } 107 if (version < 6) { 108 if (traininfo.getAttribute("trainname") != null) { 109 tInfo.setTrainName(traininfo.getAttribute("trainname").getValue()); 110 tInfo.setRosterId(traininfo.getAttribute("trainname").getValue()); 111 tInfo.setTrainUserName(traininfo.getAttribute("trainname").getValue()); 112 } else { 113 log.error("Train name missing when reading TrainInfoFile {}", name); 114 } 115 } else { 116 if (traininfo.getAttribute("trainname") != null) { 117 tInfo.setRosterId(traininfo.getAttribute("trainname").getValue()); 118 } 119 if (traininfo.getAttribute("rosterid") != null) { 120 tInfo.setRosterId(traininfo.getAttribute("rosterid").getValue()); 121 } 122 if (traininfo.getAttribute("trainusername") != null) { 123 tInfo.setTrainUserName(traininfo.getAttribute("trainusername").getValue()); 124 } 125 } 126 if (traininfo.getAttribute("dccaddress") != null) { 127 tInfo.setDccAddress(traininfo.getAttribute("dccaddress").getValue()); 128 } else { 129 log.error("DCC Address missing when reading TrainInfoFile {}", name); 130 } 131 if (traininfo.getAttribute("trainintransit") != null) { 132 tInfo.setTrainInTransit(true); 133 if (traininfo.getAttribute("trainintransit").getValue().equals("no")) { 134 tInfo.setTrainInTransit(false); 135 } 136 } else { 137 log.error("Train in Transit check box missing when reading TrainInfoFile {}", name); 138 } 139 if (traininfo.getAttribute("startblockname") != null) { 140 // there is a transit name selected 141 tInfo.setStartBlockName(traininfo.getAttribute("startblockname").getValue()); 142 } else { 143 log.error("Start block name missing when reading TrainInfoFile {}", name); 144 } 145 if (traininfo.getAttribute("endblockname") != null) { 146 // there is a transit name selected 147 tInfo.setDestinationBlockName(traininfo.getAttribute("endblockname").getValue()); 148 } else { 149 log.error("Destination block name missing when reading TrainInfoFile {}", name); 150 } 151 if (tInfo.getDynamicTransit()) { 152 if (traininfo.getAttribute("viablockname") != null) { 153 // there is a transit name selected 154 tInfo.setViaBlockName(traininfo.getAttribute("viablockname").getValue()); 155 } else { 156 log.error("Via block name missing for dynamic trainsit when reading TrainInfoFile {}", name); 157 } 158 } 159 if (traininfo.getAttribute("trainfromroster") != null) { 160 tInfo.setTrainFromRoster(true); 161 if (traininfo.getAttribute("trainfromroster").getValue().equals("no")) { 162 tInfo.setTrainFromRoster(false); 163 } 164 } 165 if (traininfo.getAttribute("trainfromtrains") != null) { 166 tInfo.setTrainFromTrains(true); 167 if (traininfo.getAttribute("trainfromtrains").getValue().equals("no")) { 168 tInfo.setTrainFromTrains(false); 169 } 170 } 171 if (traininfo.getAttribute("trainfromuser") != null) { 172 tInfo.setTrainFromUser(true); 173 if (traininfo.getAttribute("trainfromuser").getValue().equals("no")) { 174 tInfo.setTrainFromUser(false); 175 } 176 } 177 if (traininfo.getAttribute("trainfromsetlater") != null) { 178 tInfo.setTrainFromSetLater(true); 179 if (traininfo.getAttribute("trainfromsetlater").getValue().equals("no")) { 180 tInfo.setTrainFromSetLater(false); 181 } 182 } 183 if (traininfo.getAttribute("priority") != null) { 184 tInfo.setPriority(Integer.parseInt(traininfo.getAttribute("priority").getValue())); 185 } else { 186 log.error("Priority missing when reading TrainInfoFile {}", name); 187 } 188 if (traininfo.getAttribute("allocatealltheway") != null) { 189 if (traininfo.getAttribute("allocatealltheway").getValue().equals("yes")) { 190 tInfo.setAllocateAllTheWay(true); 191 } 192 } 193 if (traininfo.getAttribute("allocationmethod") != null) { 194 tInfo.setAllocationMethod(traininfo.getAttribute("allocationmethod").getIntValue()); 195 } 196 if (traininfo.getAttribute("nexttrain") != null) { 197 tInfo.setNextTrain(traininfo.getAttribute("nexttrain").getValue()); 198 } 199 if (traininfo.getAttribute("resetwhendone") != null) { 200 if (traininfo.getAttribute("resetwhendone").getValue().equals("yes")) { 201 tInfo.setResetWhenDone(true); 202 } 203 if (traininfo.getAttribute("delayedrestart") != null) { 204 // for older files that didnot have seperate restart details for to and fro 205 // we default that data to this data. 206 switch (traininfo.getAttribute("delayedrestart").getValue()) { 207 case "no": 208 tInfo.setDelayedRestart(ActiveTrain.NODELAY); 209 tInfo.setReverseDelayedRestart(ActiveTrain.NODELAY); 210 break; 211 case "sensor": 212 tInfo.setDelayedRestart(ActiveTrain.SENSORDELAY); 213 tInfo.setReverseDelayedRestart(ActiveTrain.SENSORDELAY); 214 if (traininfo.getAttribute("delayedrestartsensor") != null) { 215 tInfo.setRestartSensorName(traininfo.getAttribute("delayedrestartsensor").getValue()); 216 tInfo.setReverseRestartSensorName(traininfo.getAttribute("delayedrestartsensor").getValue()); 217 } 218 if (traininfo.getAttribute("resetrestartsensor") != null) { 219 tInfo.setResetRestartSensor(traininfo.getAttribute("resetrestartsensor").getValue().equals("yes")); 220 tInfo.setReverseResetRestartSensor(traininfo.getAttribute("resetrestartsensor").getValue().equals("yes")); 221 } 222 break; 223 case "timed": 224 tInfo.setDelayedRestart(ActiveTrain.TIMEDDELAY); 225 tInfo.setReverseDelayedRestart(ActiveTrain.TIMEDDELAY); 226 if (traininfo.getAttribute("delayedrestarttime") != null) { 227 tInfo.setRestartDelayMin((int) traininfo.getAttribute("delayedrestarttime").getLongValue()); 228 tInfo.setReverseRestartDelayMin((int) traininfo.getAttribute("delayedrestarttime").getLongValue()); 229 } break; 230 default: 231 break; 232 } 233 } 234 } 235 if (traininfo.getAttribute("reverseatend") != null) { 236 tInfo.setReverseAtEnd(true); 237 if (traininfo.getAttribute("reverseatend").getValue().equals("no")) { 238 tInfo.setReverseAtEnd(false); 239 } 240 if (version > 3) { 241 // fro delays are independent from to delays 242 if (traininfo.getAttribute("reversedelayedrestart") != null) { 243 switch (traininfo.getAttribute("reversedelayedrestart").getValue()) { 244 case "no": 245 tInfo.setReverseDelayedRestart(ActiveTrain.NODELAY); 246 break; 247 case "sensor": 248 tInfo.setReverseDelayedRestart(ActiveTrain.SENSORDELAY); 249 if (traininfo.getAttribute("reversedelayedrestartsensor") != null) { 250 tInfo.setReverseRestartSensorName( 251 traininfo.getAttribute("reversedelayedrestartsensor").getValue()); 252 } 253 if (traininfo.getAttribute("reverseresetrestartsensor") != null) { 254 tInfo.setReverseResetRestartSensor( 255 traininfo.getAttribute("reverseresetrestartsensor").getValue() 256 .equals("yes")); 257 } 258 break; 259 case "timed": 260 tInfo.setReverseDelayedRestart(ActiveTrain.TIMEDDELAY); 261 if (traininfo.getAttribute("reversedelayedrestarttime") != null) { 262 tInfo.setReverseRestartDelayMin((int) traininfo 263 .getAttribute("reversedelayedrestarttime").getLongValue()); 264 } 265 break; 266 default: 267 break; 268 } 269 } 270 } 271 } 272 if (traininfo.getAttribute("delayedstart") != null) { 273 switch (traininfo.getAttribute("delayedstart").getValue()) { 274 case "no": 275 tInfo.setDelayedStart(ActiveTrain.NODELAY); 276 break; 277 case "sensor": 278 tInfo.setDelayedStart(ActiveTrain.SENSORDELAY); 279 break; 280 default: 281 //This covers the old versions of the file with "yes" 282 tInfo.setDelayedStart(ActiveTrain.TIMEDDELAY); 283 break; 284 } 285 } 286 if (traininfo.getAttribute("departuretimehr") != null) { 287 tInfo.setDepartureTimeHr(Integer.parseInt(traininfo.getAttribute("departuretimehr").getValue())); 288 } 289 if (traininfo.getAttribute("departuretimemin") != null) { 290 tInfo.setDepartureTimeMin(Integer.parseInt(traininfo.getAttribute("departuretimemin").getValue())); 291 } 292 if (traininfo.getAttribute("delayedSensor") != null) { 293 tInfo.setDelaySensorName(traininfo.getAttribute("delayedSensor").getValue()); 294 } 295 if (traininfo.getAttribute("resetstartsensor") != null) { 296 tInfo.setResetStartSensor(traininfo.getAttribute("resetstartsensor").getValue().equals("yes")); 297 } 298 if (traininfo.getAttribute("traintype") != null) { 299 tInfo.setTrainType(traininfo.getAttribute("traintype").getValue()); 300 } 301 if (traininfo.getAttribute("autorun") != null) { 302 tInfo.setAutoRun(true); 303 if (traininfo.getAttribute("autorun").getValue().equals("no")) { 304 tInfo.setAutoRun(false); 305 } 306 } 307 if (traininfo.getAttribute("loadatstartup") != null) { 308 tInfo.setLoadAtStartup(true); 309 if (traininfo.getAttribute("loadatstartup").getValue().equals("no")) { 310 tInfo.setLoadAtStartup(false); 311 } 312 } 313 // here retrieve items related only to automatically run trains if present 314 if (traininfo.getAttribute("speedfactor") != null) { 315 tInfo.setSpeedFactor(Float.parseFloat(traininfo.getAttribute("speedfactor").getValue())); 316 } 317 if (traininfo.getAttribute("maxspeed") != null) { 318 tInfo.setMaxSpeed(Float.parseFloat(traininfo.getAttribute("maxspeed").getValue())); 319 } 320 if (traininfo.getAttribute("minreliableoperatingspeed") != null) { 321 tInfo.setMinReliableOperatingSpeed(Float.parseFloat(traininfo.getAttribute("minreliableoperatingspeed").getValue())); 322 } 323 if (traininfo.getAttribute("ramprate") != null) { 324 tInfo.setRampRate(traininfo.getAttribute("ramprate").getValue()); 325 } 326 tInfo.setTrainDetection(TrainDetection.TRAINDETECTION_WHOLETRAIN); 327 if (version > 4) { 328 if (traininfo.getAttribute("traindetection") != null) { 329 tInfo.setTrainDetection(trainsdectionFromEnumMap.inputFromAttribute(traininfo.getAttribute("traindetection"))); 330 } 331 } 332 else { 333 if (traininfo.getAttribute("resistancewheels").getValue().equals("no")) { 334 tInfo.setTrainDetection(TrainDetection.TRAINDETECTION_HEADONLY); 335 } 336 } 337 if (traininfo.getAttribute("runinreverse") != null) { 338 tInfo.setRunInReverse(true); 339 if (traininfo.getAttribute("runinreverse").getValue().equals("no")) { 340 tInfo.setRunInReverse(false); 341 } 342 } 343 if (traininfo.getAttribute("sounddecoder") != null) { 344 tInfo.setSoundDecoder(true); 345 if (traininfo.getAttribute("sounddecoder").getValue().equals("no")) { 346 tInfo.setSoundDecoder(false); 347 } 348 } 349 if (version > 5) { 350 if (traininfo.getAttribute("trainlengthunits") != null) { 351 tInfo.setTrainLengthUnits(trainlengthFromEnumMap.inputFromAttribute(traininfo.getAttribute("trainlengthunits"))); 352 } 353 } 354 if (traininfo.getAttribute("maxtrainlengthMeters") != null) { 355 tInfo.setMaxTrainLengthScaleMeters(Float.parseFloat(traininfo.getAttribute("maxtrainlengthscalemeters").getValue())); 356 } else { 357 if (traininfo.getAttribute("maxtrainlength") != null) { 358 if (InstanceManager.getDefault(DispatcherFrame.class).getUseScaleMeters()) { 359 tInfo.setMaxTrainLengthScaleMeters(Float.parseFloat(traininfo.getAttribute("maxtrainlength").getValue())); 360 } else { 361 tInfo.setMaxTrainLengthScaleFeet(Float.parseFloat(traininfo.getAttribute("maxtrainlength").getValue())); 362 } 363 } 364 } 365 if (traininfo.getAttribute("terminatewhendone") != null) { 366 tInfo.setTerminateWhenDone(false); 367 if (traininfo.getAttribute("terminatewhendone").getValue().equals("yes")) { 368 tInfo.setTerminateWhenDone(true); 369 } 370 } 371 if (traininfo.getAttribute("usespeedprofile") != null) { 372 tInfo.setUseSpeedProfile(false); 373 if (traininfo.getAttribute("usespeedprofile").getValue().equals("yes")) { 374 tInfo.setUseSpeedProfile(true); 375 } 376 } 377 if (traininfo.getAttribute("stopbyspeedprofile") != null) { 378 tInfo.setStopBySpeedProfile(false); 379 if (traininfo.getAttribute("stopbyspeedprofile").getValue().equals("yes")) { 380 tInfo.setStopBySpeedProfile(true); 381 } 382 } 383 if (traininfo.getAttribute("stopbyspeedprofileadjust") != null) { 384 tInfo.setStopBySpeedProfileAdjust(traininfo.getAttribute("stopbyspeedprofileadjust").getFloatValue()); 385 } 386 if (traininfo.getAttribute("waittime") != null) { 387 tInfo.setWaitTime(traininfo.getAttribute("waittime").getFloatValue()); 388 } 389 if (traininfo.getAttribute("blockname") != null) { 390 tInfo.setBlockName(traininfo.getAttribute("blockname").getValue()); 391 } 392 393 if (version == 1) { 394 String parseArray[]; 395 // If you only have a systemname then its everything before the dash 396 tInfo.setStartBlockId(tInfo.getStartBlockName().split("-")[0]); 397 // If you have a systemname and username you want everything before the open bracket 398 tInfo.setStartBlockId(tInfo.getStartBlockId().split("\\(")[0]); 399 // to guard against a dash in the names, we need the last part, not just [1] 400 parseArray = tInfo.getStartBlockName().split("-"); 401 tInfo.setStartBlockSeq(-1); // default value 402 if (parseArray.length > 0) { 403 try { 404 tInfo.setStartBlockSeq(Integer.parseInt(parseArray[parseArray.length -1])); 405 } 406 catch (Exception Ex) { 407 log.error("Invalid StartBlockSequence{}",parseArray[parseArray.length -1]); 408 } 409 } 410 // repeat for destination 411 tInfo.setDestinationBlockId(tInfo.getDestinationBlockName().split("-")[0]); 412 tInfo.setDestinationBlockId(tInfo.getDestinationBlockId().split("\\(")[0]); 413 parseArray = tInfo.getDestinationBlockName().split("-"); 414 tInfo.setDestinationBlockSeq(-1); 415 if (parseArray.length > 0) { 416 try { 417 tInfo.setDestinationBlockSeq(Integer.parseInt(parseArray[parseArray.length -1])); 418 } 419 catch (Exception Ex) { 420 log.error("Invalid StartBlockSequence{}",parseArray[parseArray.length -1]); 421 } 422 } 423 // Transit we need the whole thing or the bit before the first open bracket 424 tInfo.setTransitId(tInfo.getTransitName().split("\\(")[0]); 425 log.debug("v1: t = {}, bs = {}, be = {}", tInfo.getTransitName(), tInfo.getStartBlockName(), tInfo.getDestinationBlockName()); 426 } 427 if ( version > 1 ) { 428 if (traininfo.getAttribute("transitid") != null) { 429 // there is a transit name selected 430 tInfo.setTransitId(traininfo.getAttribute("transitid").getValue()); 431 } else { 432 log.error("Transit id missing when reading TrainInfoFile {}", name); 433 } 434 if (traininfo.getAttribute("startblockid") != null) { 435 // there is a transit name selected 436 tInfo.setStartBlockId(traininfo.getAttribute("startblockid").getValue()); 437 } else { 438 log.error("Start block Id missing when reading TrainInfoFile {}", name); 439 } 440 if (traininfo.getAttribute("endblockid") != null) { 441 // there is a transit name selected 442 tInfo.setDestinationBlockId(traininfo.getAttribute("endblockid").getValue()); 443 } else { 444 log.error("Destination block Id missing when reading TrainInfoFile {}", name); 445 } 446 if (traininfo.getAttribute("startblockseq") != null) { 447 // there is a transit name selected 448 try { 449 tInfo.setStartBlockSeq(traininfo.getAttribute("startblockseq").getIntValue()); 450 } 451 catch (Exception ex) { 452 log.error("Start block sequence invalid when reading TrainInfoFile"); 453 } 454 } else { 455 log.error("Start block sequence missing when reading TrainInfoFile {}", name); 456 } 457 if (traininfo.getAttribute("endblockseq") != null) { 458 // there is a transit name selected 459 try { 460 tInfo.setDestinationBlockSeq(traininfo.getAttribute("endblockseq").getIntValue()); 461 } 462 catch (Exception ex) { 463 log.error("Destination block sequence invalid when reading TrainInfoFile {}", name); 464 } 465 } else { 466 log.error("Destination block sequence missing when reading TrainInfoFile {}", name); 467 } 468 } 469 if ( version == 1 || version == 2) { 470 // Change transit and block names from sysName(userName) to displayName 471 tInfo.setTransitName(convertName(tInfo.getTransitName())); 472 tInfo.setStartBlockName(convertName(tInfo.getStartBlockName())); 473 tInfo.setDestinationBlockName(convertName(tInfo.getDestinationBlockName())); 474 } 475 } 476 } 477 } 478 return tInfo; 479 } 480 481 public String convertName(String name) { 482 // transit: sys(user), block: sys(user)-n 483 String newName = name; 484 485 Pattern p = Pattern.compile(".+\\((.+)\\)(-\\d+)*"); 486 Matcher m = p.matcher(name); 487 if (m.matches()) { 488 log.debug("regex: name = '{}', group 1 = '{}', group 2 = '{}'", name, m.group(1), m.group(2)); 489 if (m.group(1) != null) { 490 newName = m.group(1).trim(); 491 if (m.group(2) != null) { 492 newName = newName + m.group(2).trim(); 493 } 494 } 495 } 496 497 log.debug("convertName: old = '{}', new = '{}'", name, newName); 498 return newName; 499 } 500 501 /* 502 * Writes out Dispatcher options to a file in the user's preferences directory 503 */ 504 public void writeTrainInfo(TrainInfo tf, String name) throws java.io.IOException { 505 log.debug("entered writeTrainInfo"); 506 root = new Element("traininfofile"); 507 doc = newDocument(root, dtdLocation + "dispatcher-traininfo.dtd"); 508 // add XSLT processing instruction 509 // <?xml-stylesheet type="text/xsl" href="XSLT/block-values.xsl"?> 510 java.util.Map<String, String> m = new java.util.HashMap<>(); 511 m.put("type", "text/xsl"); 512 m.put("href", xsltLocation + "dispatcher-traininfo.xsl"); 513 org.jdom2.ProcessingInstruction p = new org.jdom2.ProcessingInstruction("xml-stylesheet", m); 514 doc.addContent(0, p); 515 516 // save Dispatcher TrainInfo in xml format 517 Element traininfo = new Element("traininfo"); 518 // write version number 519 traininfo.setAttribute("version", "7"); 520 traininfo.setAttribute("transitname", tf.getTransitName()); 521 traininfo.setAttribute("transitid", tf.getTransitId()); 522 traininfo.setAttribute("dynamictransit", (tf.getDynamicTransit() ? "yes" : "no")); 523 traininfo.setAttribute("trainname", tf.getTrainName()); 524 traininfo.setAttribute("trainusername", tf.getTrainUserName()); 525 traininfo.setAttribute("rosterid", tf.getRosterId()); 526 traininfo.setAttribute("dccaddress", tf.getDccAddress()); 527 traininfo.setAttribute("trainintransit", "" + (tf.getTrainInTransit() ? "yes" : "no")); 528 traininfo.setAttribute("startblockname", tf.getStartBlockName()); 529 traininfo.setAttribute("startblockid", tf.getStartBlockId()); 530 traininfo.setAttribute("startblockseq", Integer.toString(tf.getStartBlockSeq())); 531 traininfo.setAttribute("endblockname", tf.getDestinationBlockName()); 532 traininfo.setAttribute("endblockid", tf.getDestinationBlockId()); 533 traininfo.setAttribute("endblockseq", Integer.toString(tf.getDestinationBlockSeq())); 534 traininfo.setAttribute("viablockname", tf.getViaBlockName()); 535 traininfo.setAttribute("trainfromroster", "" + (tf.getTrainFromRoster() ? "yes" : "no")); 536 traininfo.setAttribute("trainfromtrains", "" + (tf.getTrainFromTrains() ? "yes" : "no")); 537 traininfo.setAttribute("trainfromuser", "" + (tf.getTrainFromUser() ? "yes" : "no")); 538 traininfo.setAttribute("trainfromsetlater", "" + (tf.getTrainFromSetLater() ? "yes" : "no")); 539 traininfo.setAttribute("priority", Integer.toString(tf.getPriority())); 540 traininfo.setAttribute("traindetection", trainsdectionFromEnumMap.outputFromEnum(tf.getTrainDetection())); 541 traininfo.setAttribute("resetwhendone", "" + (tf.getResetWhenDone() ? "yes" : "no")); 542 switch (tf.getDelayedRestart()) { 543 case ActiveTrain.SENSORDELAY: 544 traininfo.setAttribute("delayedrestart", "sensor"); 545 traininfo.setAttribute("delayedrestartsensor", tf.getRestartSensorName()); 546 traininfo.setAttribute("resetrestartsensor", "" + (tf.getResetRestartSensor() ? "yes" : "no")); 547 break; 548 case ActiveTrain.TIMEDDELAY: 549 traininfo.setAttribute("delayedrestart", "timed"); 550 traininfo.setAttribute("delayedrestarttime", Integer.toString(tf.getRestartDelayMin())); 551 break; 552 default: 553 traininfo.setAttribute("delayedrestart", "no"); 554 break; 555 } 556 557 traininfo.setAttribute("reverseatend", "" + (tf.getReverseAtEnd() ? "yes" : "no")); 558 switch (tf.getReverseDelayedRestart()) { 559 case ActiveTrain.SENSORDELAY: 560 traininfo.setAttribute("reversedelayedrestart", "sensor"); 561 traininfo.setAttribute("reversedelayedrestartsensor", tf.getReverseRestartSensorName()); 562 traininfo.setAttribute("reverseresetrestartsensor", "" + (tf.getReverseResetRestartSensor() ? "yes" : "no")); 563 break; 564 case ActiveTrain.TIMEDDELAY: 565 traininfo.setAttribute("reversedelayedrestart", "timed"); 566 traininfo.setAttribute("reversedelayedrestarttime", Integer.toString(tf.getReverseRestartDelayMin())); 567 break; 568 default: 569 traininfo.setAttribute("reversedelayedrestart", "no"); 570 break; 571 } 572 if (tf.getDelayedStart() == ActiveTrain.TIMEDDELAY) { 573 traininfo.setAttribute("delayedstart", "timed"); 574 } else if (tf.getDelayedStart() == ActiveTrain.SENSORDELAY) { 575 traininfo.setAttribute("delayedstart", "sensor"); 576 if (tf.getDelaySensorName() != null) { 577 traininfo.setAttribute("delayedSensor", tf.getDelaySensorName()); 578 traininfo.setAttribute("resetstartsensor", "" + (tf.getResetStartSensor() ? "yes" : "no")); 579 } 580 } 581 582 traininfo.setAttribute("terminatewhendone", (tf.getTerminateWhenDone() ? "yes" : "no")); 583 traininfo.setAttribute("departuretimehr", Integer.toString(tf.getDepartureTimeHr())); 584 traininfo.setAttribute("departuretimemin", Integer.toString(tf.getDepartureTimeMin())); 585 traininfo.setAttribute("traintype", tf.getTrainType()); 586 traininfo.setAttribute("autorun", "" + (tf.getAutoRun() ? "yes" : "no")); 587 traininfo.setAttribute("loadatstartup", "" + (tf.getLoadAtStartup() ? "yes" : "no")); 588 traininfo.setAttribute("allocatealltheway", "" + (tf.getAllocateAllTheWay() ? "yes" : "no")); 589 traininfo.setAttribute("allocationmethod", Integer.toString(tf.getAllocationMethod())); 590 traininfo.setAttribute("nexttrain", tf.getNextTrain()); 591 // here save items related to automatically running active trains 592 traininfo.setAttribute("speedfactor", Float.toString(tf.getSpeedFactor())); 593 traininfo.setAttribute("maxspeed", Float.toString(tf.getMaxSpeed())); 594 traininfo.setAttribute("minreliableoperatingspeed", Float.toString(tf.getMinReliableOperatingSpeed())); 595 traininfo.setAttribute("ramprate", tf.getRampRate()); 596 traininfo.setAttribute("runinreverse", "" + (tf.getRunInReverse() ? "yes" : "no")); 597 traininfo.setAttribute("sounddecoder", "" + (tf.getSoundDecoder() ? "yes" : "no")); 598 traininfo.setAttribute("maxtrainlengthscalemeters", Float.toString(tf.getMaxTrainLengthScaleMeters())); 599 traininfo.setAttribute("trainlengthunits", trainlengthFromEnumMap.outputFromEnum(tf.getTrainLengthUnits())); 600 traininfo.setAttribute("usespeedprofile", "" + (tf.getUseSpeedProfile() ? "yes" : "no")); 601 traininfo.setAttribute("stopbyspeedprofile", "" + (tf.getStopBySpeedProfile() ? "yes" : "no")); 602 traininfo.setAttribute("stopbyspeedprofileadjust", Float.toString(tf.getStopBySpeedProfileAdjust())); 603 traininfo.setAttribute("waittime", Float.toString(tf.getWaitTime())); 604 traininfo.setAttribute("blockname", tf.getBlockName()); 605 606 root.addContent(traininfo); 607 608 // write out the file 609 try { 610 if (!checkFile(fileLocation + name)) { 611 // file does not exist, create it 612 File file = new File(fileLocation + name); 613 if (!file.createNewFile()) // create file and check result 614 { 615 log.error("createNewFile failed"); 616 } 617 } 618 // write content to file 619 writeXML(findFile(fileLocation + name), doc); 620 } catch (java.io.IOException ioe) { 621 log.error("IO Exception writing", ioe); 622 throw (ioe); 623 } 624 } 625 626 /** 627 * Get the names of all current TrainInfo files. Returns names as an array 628 * of Strings. Returns an empty array if no files are present. Note: Fill 629 * names still end with .xml or .XML. (Modeled after a method in 630 * RecreateRosterAction.java by Bob Jacobsen) 631 * 632 * @return names as an array or an empty array if none present 633 */ 634 public String[] getTrainInfoFileNames() { 635 // ensure preferences will be found for read 636 FileUtil.createDirectory(fileLocation); 637 // create an array of file names from roster dir in preferences, count entries 638 List<String> names = new ArrayList<>(); 639 log.debug("directory of TrainInfoFiles is {}", fileLocation); 640 File fp = new File(fileLocation); 641 if (fp.exists()) { 642 String[] xmlList = fp.list(new XmlFilenameFilter()); 643 if (xmlList!=null) { 644 names.addAll(Arrays.asList(xmlList)); 645 } 646 } 647 // Sort the resulting array 648 names.sort((s1, s2) -> { 649 return s1.compareTo(s2); 650 }); 651 return names.toArray(new String[names.size()]); 652 } 653 654 /** 655 * Get the names of all current TrainInfo files. Returns list 656 * of files and some basic details for each file 657 *. 658 * @return names as an array or an empty array if none present 659 */ 660 public List<TrainInfoFileSummary> getTrainInfoFileSummaries() { 661 List<TrainInfoFileSummary> summaries = new ArrayList<>(); 662 for (String fileName : getTrainInfoFileNames()) { 663 try { 664 TrainInfo ti = readTrainInfo(fileName); 665 summaries.add(new TrainInfoFileSummary(fileName, ti.getTransitName(), ti.getTrainName(), 666 ti.getStartBlockName(), ti.getDestinationBlockName(), ti.getDccAddress())); 667 } catch (org.jdom2.JDOMException ex) { 668 summaries.add(new TrainInfoFileSummary(fileName)); 669 } catch (IOException ex) { 670 summaries.add(new TrainInfoFileSummary(fileName)); 671 } 672 } 673 return summaries; 674 } 675 676 /** 677 * Delete a specified TrainInfo file. 678 * 679 * @param name the file to delete 680 */ 681 public void deleteTrainInfoFile(String name) { 682 // locate the file and delete it if it exists 683 File f = new File(fileLocation + name); 684 if (!f.delete()) { // delete file and check success 685 log.error("failed to delete TrainInfo file - {}", name); 686 } 687 } 688 689 private final static Logger log = LoggerFactory.getLogger(TrainInfoFile.class); 690}