001package jmri.configurexml.turnoutoperations; 002 003import java.lang.reflect.Constructor; 004import java.lang.reflect.InvocationTargetException; 005import jmri.CommonTurnoutOperation; 006import jmri.TurnoutOperation; 007import org.jdom2.Element; 008import org.slf4j.Logger; 009import org.slf4j.LoggerFactory; 010 011/** 012 * Concrete subclass to save/restore NoFeedbackTurnoutOperation object to/from 013 * XML. 014 * 015 * @author John Harper Copyright 2005 016 */ 017public abstract class CommonTurnoutOperationXml extends TurnoutOperationXml { 018 019 @Override 020 public Element store(Object op) { 021 CommonTurnoutOperation myOp = (CommonTurnoutOperation) op; 022 Element elem = super.store(op); 023 elem.setAttribute("interval", String.valueOf(myOp.getInterval())); 024 elem.setAttribute("maxtries", String.valueOf(myOp.getMaxTries())); 025 return elem; 026 } 027 028 /** 029 * called for a newly-constructed object to load it from an XML element 030 * 031 * @param e the XML element of type "turnoutOperation" 032 * @param constr constructor of subclass of TurnoutOperation to create 033 * @param di default interval 034 * @param dmt default max tries 035 * @return a TurnoutOperation or null if unable to load from e 036 */ 037 public TurnoutOperation loadOne(Element e, Constructor<?> constr, int di, int dmt) { 038 int interval = di; 039 int maxTries = dmt; 040// boolean noDelete = false; 041 TurnoutOperation result = null; 042 if (e.getAttribute("name") == null) { 043 log.warn("unexpected null in name {} {}", e, e.getAttributes()); 044 return null; 045 } 046 String name = e.getAttribute("name").getValue(); 047 if (e.getAttribute("interval") != null) { 048 try { 049 interval = Integer.parseInt(e.getAttribute("interval").getValue()); 050 } catch (NumberFormatException ex) { 051 } 052 } 053 if (e.getAttribute("maxtries") != null) { 054 try { 055 maxTries = Integer.parseInt(e.getAttribute("maxtries").getValue()); 056 } catch (NumberFormatException ex) { 057 } 058 } 059 // constructor takes care of enrolling the new operation 060 try { 061 result = (TurnoutOperation) constr.newInstance(new Object[]{name, interval, maxTries}); 062 } catch (InstantiationException | IllegalAccessException | InvocationTargetException e1) { 063 log.error("while creating CommonTurnoutOperation", e1); 064 return null; 065 } 066 if (log.isDebugEnabled()) { 067 log.debug("create turnout operation: ({})", name); 068 } 069 return result; 070 } 071 072 private final static Logger log = LoggerFactory.getLogger(CommonTurnoutOperationXml.class); 073 074}