001package jmri.jmrit.operations.locations; 002 003import java.io.File; 004 005import org.jdom2.Document; 006import org.jdom2.Element; 007import org.jdom2.ProcessingInstruction; 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011import jmri.InstanceManager; 012import jmri.InstanceManagerAutoDefault; 013import jmri.InstanceManagerAutoInitialize; 014import jmri.jmrit.operations.OperationsXml; 015import jmri.jmrit.operations.locations.divisions.DivisionManager; 016import jmri.jmrit.operations.locations.schedules.ScheduleManager; 017 018/** 019 * Load and stores locations and schedules for operations. 020 * 021 * @author Daniel Boudreau Copyright (C) 2008 2009 2010 022 */ 023public class LocationManagerXml extends OperationsXml implements InstanceManagerAutoDefault, InstanceManagerAutoInitialize { 024 025 public LocationManagerXml() { 026 } 027 028 @Override 029 public void writeFile(String name) throws java.io.FileNotFoundException, java.io.IOException { 030 log.debug("writeFile {}", name); 031 // This is taken in large part from "Java and XML" page 368 032 File file = findFile(name); 033 if (file == null) { 034 file = new File(name); 035 } 036 // create root element 037 Element root = new Element("operations-config"); // NOI18N 038 Document doc = newDocument(root, dtdLocation + "operations-locations.dtd"); // NOI18N 039 040 // add XSLT processing instruction 041 java.util.Map<String, String> m = new java.util.HashMap<String, String>(); 042 m.put("type", "text/xsl"); // NOI18N 043 m.put("href", xsltLocation + "operations-locations.xsl"); // NOI18N 044 ProcessingInstruction p = new ProcessingInstruction("xml-stylesheet", m); // NOI18N 045 doc.addContent(0, p); 046 047 InstanceManager.getDefault(DivisionManager.class).store(root); 048 InstanceManager.getDefault(LocationManager.class).store(root); 049 InstanceManager.getDefault(ScheduleManager.class).store(root); 050 051 writeXML(file, doc); 052 053 // done - location file now stored, so can't be dirty 054 setDirty(false); 055 } 056 057 /** 058 * Read the contents of a roster XML file into this object. Note that this 059 * does not clear any existing entries. 060 */ 061 @Override 062 public void readFile(String name) throws org.jdom2.JDOMException, java.io.IOException { 063 // suppress rootFromName(name) warning message by checking to see if file exists 064 if (findFile(name) == null) { 065 log.debug("{} file could not be found", name); 066 return; 067 } 068 // find root 069 Element root = rootFromName(name); 070 if (root == null) { 071 log.debug("{} file could not be read", name); 072 return; 073 } 074 075 if (!root.getName().equals("operations-config")) { 076 log.warn("OperationsPro location file corrupted"); 077 return; 078 } 079 080 InstanceManager.getDefault(DivisionManager.class).load(root); 081 InstanceManager.getDefault(LocationManager.class).load(root); 082 InstanceManager.getDefault(ScheduleManager.class).load(root); 083 084 setDirty(false); 085 log.debug("Locations have been loaded!"); 086 } 087 088 @Override 089 public void setOperationsFileName(String name) { 090 operationsFileName = name; 091 } 092 093 @Override 094 public String getOperationsFileName() { 095 return operationsFileName; 096 } 097 098 private String operationsFileName = "OperationsLocationRoster.xml"; // NOI18N 099 100 public void dispose() { 101 } 102 103 private final static Logger log = LoggerFactory.getLogger(LocationManagerXml.class); 104 105 @Override 106 public void initialize() { 107 this.load(); 108 } 109 110}