Refactored simple SAX ElementHandler to be an interface.
This commit is contained in:
parent
88f359c25f
commit
b211266f45
@ -8,6 +8,7 @@ import java.util.HashMap;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import net.sf.openrocket.aerodynamics.WarningSet;
|
import net.sf.openrocket.aerodynamics.WarningSet;
|
||||||
|
import net.sf.openrocket.file.simplesax.AbstractElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.ElementHandler;
|
import net.sf.openrocket.file.simplesax.ElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.NullElementHandler;
|
import net.sf.openrocket.file.simplesax.NullElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.PlainTextHandler;
|
import net.sf.openrocket.file.simplesax.PlainTextHandler;
|
||||||
@ -77,7 +78,7 @@ public class RockSimMotorLoader extends AbstractMotorLoader {
|
|||||||
/**
|
/**
|
||||||
* Initial handler for the RockSim engine files.
|
* Initial handler for the RockSim engine files.
|
||||||
*/
|
*/
|
||||||
private static class RSEHandler extends ElementHandler {
|
private static class RSEHandler extends AbstractElementHandler {
|
||||||
private final List<Motor> motors = new ArrayList<Motor>();
|
private final List<Motor> motors = new ArrayList<Motor>();
|
||||||
|
|
||||||
private RSEMotorHandler motorHandler;
|
private RSEMotorHandler motorHandler;
|
||||||
@ -124,7 +125,7 @@ public class RockSimMotorLoader extends AbstractMotorLoader {
|
|||||||
/**
|
/**
|
||||||
* Handler for a RockSim engine file <motor> element.
|
* Handler for a RockSim engine file <motor> element.
|
||||||
*/
|
*/
|
||||||
private static class RSEMotorHandler extends ElementHandler {
|
private static class RSEMotorHandler extends AbstractElementHandler {
|
||||||
|
|
||||||
private final String manufacturer;
|
private final String manufacturer;
|
||||||
private final String designation;
|
private final String designation;
|
||||||
@ -390,7 +391,7 @@ public class RockSimMotorLoader extends AbstractMotorLoader {
|
|||||||
/**
|
/**
|
||||||
* Handler for the <data> element in a RockSim engine file motor definition.
|
* Handler for the <data> element in a RockSim engine file motor definition.
|
||||||
*/
|
*/
|
||||||
private static class RSEMotorDataHandler extends ElementHandler {
|
private static class RSEMotorDataHandler extends AbstractElementHandler {
|
||||||
|
|
||||||
private final List<Double> time = new ArrayList<Double>();
|
private final List<Double> time = new ArrayList<Double>();
|
||||||
private final List<Double> force = new ArrayList<Double>();
|
private final List<Double> force = new ArrayList<Double>();
|
||||||
|
|||||||
@ -20,6 +20,7 @@ import net.sf.openrocket.document.StorageOptions;
|
|||||||
import net.sf.openrocket.file.AbstractRocketLoader;
|
import net.sf.openrocket.file.AbstractRocketLoader;
|
||||||
import net.sf.openrocket.file.MotorFinder;
|
import net.sf.openrocket.file.MotorFinder;
|
||||||
import net.sf.openrocket.file.RocketLoadException;
|
import net.sf.openrocket.file.RocketLoadException;
|
||||||
|
import net.sf.openrocket.file.simplesax.AbstractElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.ElementHandler;
|
import net.sf.openrocket.file.simplesax.ElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.PlainTextHandler;
|
import net.sf.openrocket.file.simplesax.PlainTextHandler;
|
||||||
import net.sf.openrocket.file.simplesax.SimpleSAX;
|
import net.sf.openrocket.file.simplesax.SimpleSAX;
|
||||||
@ -545,7 +546,7 @@ class DocumentConfig {
|
|||||||
* The starting point of the handlers. Accepts a single <openrocket> element and hands
|
* The starting point of the handlers. Accepts a single <openrocket> element and hands
|
||||||
* the contents to be read by a OpenRocketContentsHandler.
|
* the contents to be read by a OpenRocketContentsHandler.
|
||||||
*/
|
*/
|
||||||
class OpenRocketHandler extends ElementHandler {
|
class OpenRocketHandler extends AbstractElementHandler {
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private OpenRocketContentHandler handler = null;
|
private OpenRocketContentHandler handler = null;
|
||||||
|
|
||||||
@ -636,7 +637,7 @@ class OpenRocketHandler extends ElementHandler {
|
|||||||
/**
|
/**
|
||||||
* Handles the content of the <openrocket> tag.
|
* Handles the content of the <openrocket> tag.
|
||||||
*/
|
*/
|
||||||
class OpenRocketContentHandler extends ElementHandler {
|
class OpenRocketContentHandler extends AbstractElementHandler {
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private final OpenRocketDocument doc;
|
private final OpenRocketDocument doc;
|
||||||
private final Rocket rocket;
|
private final Rocket rocket;
|
||||||
@ -696,7 +697,7 @@ class OpenRocketContentHandler extends ElementHandler {
|
|||||||
* A handler that creates components from the corresponding elements. The control of the
|
* A handler that creates components from the corresponding elements. The control of the
|
||||||
* contents is passed on to ComponentParameterHandler.
|
* contents is passed on to ComponentParameterHandler.
|
||||||
*/
|
*/
|
||||||
class ComponentHandler extends ElementHandler {
|
class ComponentHandler extends AbstractElementHandler {
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private final RocketComponent parent;
|
private final RocketComponent parent;
|
||||||
|
|
||||||
@ -740,7 +741,7 @@ class ComponentHandler extends ElementHandler {
|
|||||||
* This uses the setters, or delegates the handling to another handler for specific
|
* This uses the setters, or delegates the handling to another handler for specific
|
||||||
* elements.
|
* elements.
|
||||||
*/
|
*/
|
||||||
class ComponentParameterHandler extends ElementHandler {
|
class ComponentParameterHandler extends AbstractElementHandler {
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private final RocketComponent component;
|
private final RocketComponent component;
|
||||||
|
|
||||||
@ -821,7 +822,7 @@ class ComponentParameterHandler extends ElementHandler {
|
|||||||
* A handler that reads the <point> specifications within the freeformfinset's
|
* A handler that reads the <point> specifications within the freeformfinset's
|
||||||
* <finpoints> elements.
|
* <finpoints> elements.
|
||||||
*/
|
*/
|
||||||
class FinSetPointHandler extends ElementHandler {
|
class FinSetPointHandler extends AbstractElementHandler {
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private final FreeformFinSet finset;
|
private final FreeformFinSet finset;
|
||||||
@ -873,7 +874,7 @@ class FinSetPointHandler extends ElementHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class MotorMountHandler extends ElementHandler {
|
class MotorMountHandler extends AbstractElementHandler {
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private final MotorMount mount;
|
private final MotorMount mount;
|
||||||
private MotorHandler motorHandler;
|
private MotorHandler motorHandler;
|
||||||
@ -969,7 +970,7 @@ class MotorMountHandler extends ElementHandler {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
class MotorConfigurationHandler extends ElementHandler {
|
class MotorConfigurationHandler extends AbstractElementHandler {
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private final Rocket rocket;
|
private final Rocket rocket;
|
||||||
@ -1028,7 +1029,7 @@ class MotorConfigurationHandler extends ElementHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class MotorHandler extends ElementHandler {
|
class MotorHandler extends AbstractElementHandler {
|
||||||
/** File version where latest digest format was introduced */
|
/** File version where latest digest format was introduced */
|
||||||
private static final int MOTOR_DIGEST_VERSION = 104;
|
private static final int MOTOR_DIGEST_VERSION = 104;
|
||||||
|
|
||||||
@ -1162,7 +1163,7 @@ class MotorHandler extends ElementHandler {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SimulationsHandler extends ElementHandler {
|
class SimulationsHandler extends AbstractElementHandler {
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private final OpenRocketDocument doc;
|
private final OpenRocketDocument doc;
|
||||||
private SingleSimulationHandler handler;
|
private SingleSimulationHandler handler;
|
||||||
@ -1195,7 +1196,7 @@ class SimulationsHandler extends ElementHandler {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class SingleSimulationHandler extends ElementHandler {
|
class SingleSimulationHandler extends AbstractElementHandler {
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
|
|
||||||
private final OpenRocketDocument doc;
|
private final OpenRocketDocument doc;
|
||||||
@ -1290,7 +1291,7 @@ class SingleSimulationHandler extends ElementHandler {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
class SimulationConditionsHandler extends ElementHandler {
|
class SimulationConditionsHandler extends AbstractElementHandler {
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private SimulationOptions conditions;
|
private SimulationOptions conditions;
|
||||||
private AtmosphereHandler atmosphereHandler;
|
private AtmosphereHandler atmosphereHandler;
|
||||||
@ -1402,7 +1403,7 @@ class SimulationConditionsHandler extends ElementHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class AtmosphereHandler extends ElementHandler {
|
class AtmosphereHandler extends AbstractElementHandler {
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private final String model;
|
private final String model;
|
||||||
@ -1467,7 +1468,7 @@ class AtmosphereHandler extends ElementHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class FlightDataHandler extends ElementHandler {
|
class FlightDataHandler extends AbstractElementHandler {
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
|
|
||||||
private FlightDataBranchHandler dataHandler;
|
private FlightDataBranchHandler dataHandler;
|
||||||
@ -1589,7 +1590,7 @@ class FlightDataHandler extends ElementHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class FlightDataBranchHandler extends ElementHandler {
|
class FlightDataBranchHandler extends AbstractElementHandler {
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
private final DocumentLoadingContext context;
|
private final DocumentLoadingContext context;
|
||||||
private final FlightDataType[] types;
|
private final FlightDataType[] types;
|
||||||
|
|||||||
@ -5,6 +5,7 @@ package net.sf.openrocket.file.rocksim.importt;
|
|||||||
|
|
||||||
import net.sf.openrocket.aerodynamics.WarningSet;
|
import net.sf.openrocket.aerodynamics.WarningSet;
|
||||||
import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
|
import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
|
||||||
|
import net.sf.openrocket.file.simplesax.AbstractElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.ElementHandler;
|
import net.sf.openrocket.file.simplesax.ElementHandler;
|
||||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
||||||
|
|
||||||
@ -13,7 +14,7 @@ import java.util.HashMap;
|
|||||||
/**
|
/**
|
||||||
* A SAX handler for the Rocksim AttachedParts XML type.
|
* A SAX handler for the Rocksim AttachedParts XML type.
|
||||||
*/
|
*/
|
||||||
class AttachedPartsHandler extends ElementHandler {
|
class AttachedPartsHandler extends AbstractElementHandler {
|
||||||
/** The parent component. */
|
/** The parent component. */
|
||||||
private final RocketComponent component;
|
private final RocketComponent component;
|
||||||
|
|
||||||
|
|||||||
@ -6,7 +6,7 @@ package net.sf.openrocket.file.rocksim.importt;
|
|||||||
import net.sf.openrocket.aerodynamics.WarningSet;
|
import net.sf.openrocket.aerodynamics.WarningSet;
|
||||||
import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
|
import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
|
||||||
import net.sf.openrocket.file.rocksim.RocksimDensityType;
|
import net.sf.openrocket.file.rocksim.RocksimDensityType;
|
||||||
import net.sf.openrocket.file.simplesax.ElementHandler;
|
import net.sf.openrocket.file.simplesax.AbstractElementHandler;
|
||||||
import net.sf.openrocket.material.Material;
|
import net.sf.openrocket.material.Material;
|
||||||
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
import net.sf.openrocket.rocketcomponent.RocketComponent;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
@ -20,7 +20,7 @@ import java.util.HashMap;
|
|||||||
*
|
*
|
||||||
* @param <C> the specific RocketComponent subtype for which the concrete handler can create
|
* @param <C> the specific RocketComponent subtype for which the concrete handler can create
|
||||||
*/
|
*/
|
||||||
public abstract class BaseHandler<C extends RocketComponent> extends ElementHandler {
|
public abstract class BaseHandler<C extends RocketComponent> extends AbstractElementHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Prepend rocksim materials.
|
* Prepend rocksim materials.
|
||||||
|
|||||||
@ -7,6 +7,7 @@ import net.sf.openrocket.aerodynamics.WarningSet;
|
|||||||
import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
|
import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
|
||||||
import net.sf.openrocket.file.rocksim.RocksimFinishCode;
|
import net.sf.openrocket.file.rocksim.RocksimFinishCode;
|
||||||
import net.sf.openrocket.file.rocksim.RocksimLocationMode;
|
import net.sf.openrocket.file.rocksim.RocksimLocationMode;
|
||||||
|
import net.sf.openrocket.file.simplesax.AbstractElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.ElementHandler;
|
import net.sf.openrocket.file.simplesax.ElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.PlainTextHandler;
|
import net.sf.openrocket.file.simplesax.PlainTextHandler;
|
||||||
import net.sf.openrocket.material.Material;
|
import net.sf.openrocket.material.Material;
|
||||||
@ -31,7 +32,7 @@ import java.util.List;
|
|||||||
* characteristics are kept here until the closing FinSet tag. At that point, <code>asOpenRocket</code> method is called
|
* characteristics are kept here until the closing FinSet tag. At that point, <code>asOpenRocket</code> method is called
|
||||||
* to construct the corresponding OpenRocket FinSet.
|
* to construct the corresponding OpenRocket FinSet.
|
||||||
*/
|
*/
|
||||||
class FinSetHandler extends ElementHandler {
|
class FinSetHandler extends AbstractElementHandler {
|
||||||
/**
|
/**
|
||||||
* The parent component.
|
* The parent component.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -8,6 +8,7 @@ import net.sf.openrocket.aerodynamics.Warning;
|
|||||||
import net.sf.openrocket.aerodynamics.WarningSet;
|
import net.sf.openrocket.aerodynamics.WarningSet;
|
||||||
import net.sf.openrocket.document.OpenRocketDocument;
|
import net.sf.openrocket.document.OpenRocketDocument;
|
||||||
import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
|
import net.sf.openrocket.file.rocksim.RocksimCommonConstants;
|
||||||
|
import net.sf.openrocket.file.simplesax.AbstractElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.ElementHandler;
|
import net.sf.openrocket.file.simplesax.ElementHandler;
|
||||||
import net.sf.openrocket.file.simplesax.PlainTextHandler;
|
import net.sf.openrocket.file.simplesax.PlainTextHandler;
|
||||||
import net.sf.openrocket.rocketcomponent.Rocket;
|
import net.sf.openrocket.rocketcomponent.Rocket;
|
||||||
@ -24,7 +25,7 @@ import java.util.HashMap;
|
|||||||
* <p/>
|
* <p/>
|
||||||
* Limitations: Rocksim flight simulations are not imported; tube fins are not supported; Rocksim 'pods' are not supported.
|
* Limitations: Rocksim flight simulations are not imported; tube fins are not supported; Rocksim 'pods' are not supported.
|
||||||
*/
|
*/
|
||||||
public class RocksimHandler extends ElementHandler {
|
public class RocksimHandler extends AbstractElementHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main content handler.
|
* The main content handler.
|
||||||
@ -67,7 +68,7 @@ public class RocksimHandler extends ElementHandler {
|
|||||||
/**
|
/**
|
||||||
* Handles the content of the <DesignInformation> tag.
|
* Handles the content of the <DesignInformation> tag.
|
||||||
*/
|
*/
|
||||||
class RocksimContentHandler extends ElementHandler {
|
class RocksimContentHandler extends AbstractElementHandler {
|
||||||
/**
|
/**
|
||||||
* The OpenRocketDocument that is the container for the rocket.
|
* The OpenRocketDocument that is the container for the rocket.
|
||||||
*/
|
*/
|
||||||
@ -145,7 +146,7 @@ class RocksimContentHandler extends ElementHandler {
|
|||||||
* Correct functioning of this handler is predicated on the stage count element appearing before the actual stage parts
|
* Correct functioning of this handler is predicated on the stage count element appearing before the actual stage parts
|
||||||
* structures. If that invariant is not true, then behavior will be unpredictable.
|
* structures. If that invariant is not true, then behavior will be unpredictable.
|
||||||
*/
|
*/
|
||||||
class RocketDesignHandler extends ElementHandler {
|
class RocketDesignHandler extends AbstractElementHandler {
|
||||||
/**
|
/**
|
||||||
* The parent component.
|
* The parent component.
|
||||||
*/
|
*/
|
||||||
@ -309,7 +310,7 @@ class RocketDesignHandler extends ElementHandler {
|
|||||||
/**
|
/**
|
||||||
* A SAX handler for a Rocksim stage.
|
* A SAX handler for a Rocksim stage.
|
||||||
*/
|
*/
|
||||||
class StageHandler extends ElementHandler {
|
class StageHandler extends AbstractElementHandler {
|
||||||
/**
|
/**
|
||||||
* The parent OpenRocket component.
|
* The parent OpenRocket component.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -0,0 +1,54 @@
|
|||||||
|
package net.sf.openrocket.file.simplesax;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import net.sf.openrocket.aerodynamics.Warning;
|
||||||
|
import net.sf.openrocket.aerodynamics.WarningSet;
|
||||||
|
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An abstract base class for creating an ElementHandler. This implements the close
|
||||||
|
* methods so that warnings are generated for spurious content.
|
||||||
|
*
|
||||||
|
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
||||||
|
*/
|
||||||
|
public abstract class AbstractElementHandler implements ElementHandler {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract ElementHandler openElement(String element,
|
||||||
|
HashMap<String, String> attributes, WarningSet warnings) throws SAXException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
* <p>
|
||||||
|
* The default implementation is to add warnings for any textual content or attributes.
|
||||||
|
* This is useful for generating warnings for unknown XML attributes.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void closeElement(String element, HashMap<String, String> attributes,
|
||||||
|
String content, WarningSet warnings) throws SAXException {
|
||||||
|
|
||||||
|
if (!content.trim().equals("")) {
|
||||||
|
warnings.add(Warning.fromString("Unknown text in element '" + element
|
||||||
|
+ "', ignoring."));
|
||||||
|
}
|
||||||
|
if (!attributes.isEmpty()) {
|
||||||
|
warnings.add(Warning.fromString("Unknown attributes in element '" + element
|
||||||
|
+ "', ignoring."));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritDoc}
|
||||||
|
* <p>
|
||||||
|
* The default implementation is a no-op.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void endHandler(String element, HashMap<String, String> attributes,
|
||||||
|
String content, WarningSet warnings) throws SAXException {
|
||||||
|
// No-op
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -13,7 +13,7 @@ import org.xml.sax.helpers.DefaultHandler;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* The actual SAX handler class. Contains the necessary methods for parsing the SAX source.
|
* The actual SAX handler class. Contains the necessary methods for parsing the SAX source.
|
||||||
* Delegates the actual content parsing to {@link ElementHandler} objects.
|
* Delegates the actual content parsing to {@link AbstractElementHandler} objects.
|
||||||
*/
|
*/
|
||||||
class DelegatorHandler extends DefaultHandler {
|
class DelegatorHandler extends DefaultHandler {
|
||||||
private final WarningSet warnings;
|
private final WarningSet warnings;
|
||||||
@ -22,12 +22,12 @@ class DelegatorHandler extends DefaultHandler {
|
|||||||
private final Deque<StringBuilder> elementData = new ArrayDeque<StringBuilder>();
|
private final Deque<StringBuilder> elementData = new ArrayDeque<StringBuilder>();
|
||||||
private final Deque<HashMap<String, String>> elementAttributes = new ArrayDeque<HashMap<String, String>>();
|
private final Deque<HashMap<String, String>> elementAttributes = new ArrayDeque<HashMap<String, String>>();
|
||||||
|
|
||||||
|
|
||||||
// Ignore all elements as long as ignore > 0
|
// Ignore all elements as long as ignore > 0
|
||||||
private int ignore = 0;
|
private int ignore = 0;
|
||||||
|
|
||||||
|
|
||||||
public DelegatorHandler(ElementHandler initialHandler, WarningSet warnings) {
|
public DelegatorHandler(AbstractElementHandler initialHandler, WarningSet warnings) {
|
||||||
this.warnings = warnings;
|
this.warnings = warnings;
|
||||||
handlerStack.add(initialHandler);
|
handlerStack.add(initialHandler);
|
||||||
elementData.add(new StringBuilder()); // Just in case
|
elementData.add(new StringBuilder()); // Just in case
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package net.sf.openrocket.file.simplesax;
|
|||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
import net.sf.openrocket.aerodynamics.Warning;
|
|
||||||
import net.sf.openrocket.aerodynamics.WarningSet;
|
import net.sf.openrocket.aerodynamics.WarningSet;
|
||||||
|
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
@ -18,30 +17,23 @@ import org.xml.sax.SAXException;
|
|||||||
*
|
*
|
||||||
* and the initial handler is initHandler, then the following methods will be called:
|
* and the initial handler is initHandler, then the following methods will be called:
|
||||||
*
|
*
|
||||||
* 1. initHandler.{@link #openElement(String, HashMap, WarningSet)} is called for
|
* 1. initHandler.openElement(String, HashMap, WarningSet) is called for the opening element <foo>, which returns fooHandler
|
||||||
* the opening element <bar>, which returns fooHandler
|
* 2. fooHandler.openElement(String, HashMap, WarningSet) is called for the opening element <bar>, which returns barHandler
|
||||||
* 2. fooHandler.{@link #openElement(String, HashMap, WarningSet)} is called for
|
* 3. barHandler.endHandler(String, HashMap, String, WarningSet) is called for the closing element </bar>
|
||||||
* the opening element <bar>, which returns barHandler
|
* 4. fooHandler.closeElement(String, HashMap, String, WarningSet) is called for the closing element </bar>
|
||||||
* 3. barHandler.{@link #endHandler(String, HashMap, String, WarningSet)} is called for
|
* 5. fooHandler.endHandler(String, HashMap, String, WarningSet) is called for the closing element </foo>
|
||||||
* the closing element </bar>
|
* 6. initHandler.closeElement(String, HashMap, String, WarningSet) is called for the closing element </foo>
|
||||||
* 4. fooHandler.{@link #closeElement(String, HashMap, String, WarningSet)} is called for
|
|
||||||
* the closing element </bar>
|
|
||||||
* 5. fooHandler.{@link #endHandler(String, HashMap, String, WarningSet)} is called for
|
|
||||||
* the closing element </foo>
|
|
||||||
* 6. initHandler.{@link #closeElement(String, HashMap, String, WarningSet)} is called for
|
|
||||||
* the closing element </foo>
|
|
||||||
*
|
*
|
||||||
* Note that {@link #endHandler(String, HashMap, String, WarningSet)} is not called for
|
* Note that endHandler(String, HashMap, String, WarningSet) is not called for the initial handler.
|
||||||
* the initial handler.
|
|
||||||
*
|
*
|
||||||
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
||||||
*/
|
*/
|
||||||
public abstract class ElementHandler {
|
public interface ElementHandler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when an opening element is encountered. Returns the handler that will handle
|
* Called when an opening tag of a contained element is encountered. Returns the handler
|
||||||
* the elements within that element, or <code>null</code> if the element and all of
|
* that will handle the elements within that element, or <code>null</code> if the element
|
||||||
* its contents is to be ignored.
|
* and all of its contents is to be ignored.
|
||||||
* <p>
|
* <p>
|
||||||
* Note that this method may also return <code>this</code>, in which case this
|
* Note that this method may also return <code>this</code>, in which case this
|
||||||
* handler will also handle the subelement.
|
* handler will also handle the subelement.
|
||||||
@ -52,43 +44,29 @@ public abstract class ElementHandler {
|
|||||||
* @return the handler that handles elements encountered within this element,
|
* @return the handler that handles elements encountered within this element,
|
||||||
* or <code>null</code> if the element is to be ignored.
|
* or <code>null</code> if the element is to be ignored.
|
||||||
*/
|
*/
|
||||||
public abstract ElementHandler openElement(String element,
|
public ElementHandler openElement(String element, HashMap<String, String> attributes,
|
||||||
HashMap<String, String> attributes, WarningSet warnings) throws SAXException;
|
WarningSet warnings) throws SAXException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when an element is closed. The default implementation checks whether there is
|
* Called when a closing tag of a contained element is encountered.
|
||||||
* any non-space text within the element and if there exists any attributes, and adds
|
* <p>
|
||||||
* a warning of both. This can be used at the and of the method to check for
|
* This method can be used to handle the textual content of the element for simple text
|
||||||
* spurious data.
|
* elements, which is passed in as the "content" parameter.
|
||||||
*
|
*
|
||||||
* @param element the element name.
|
* @param element the element name.
|
||||||
* @param attributes attributes of the element.
|
* @param attributes attributes of the element.
|
||||||
* @param content the textual content of the element.
|
* @param content the textual content of the element.
|
||||||
* @param warnings the warning set to store warnings in.
|
* @param warnings the warning set to store warnings in.
|
||||||
*/
|
*/
|
||||||
public void closeElement(String element, HashMap<String, String> attributes,
|
public abstract void closeElement(String element, HashMap<String, String> attributes,
|
||||||
String content, WarningSet warnings) throws SAXException {
|
String content, WarningSet warnings) throws SAXException;
|
||||||
|
|
||||||
if (!content.trim().equals("")) {
|
|
||||||
warnings.add(Warning.fromString("Unknown text in element '" + element
|
|
||||||
+ "', ignoring."));
|
|
||||||
}
|
|
||||||
if (!attributes.isEmpty()) {
|
|
||||||
warnings.add(Warning.fromString("Unknown attributes in element '" + element
|
|
||||||
+ "', ignoring."));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the element block that this handler is handling ends.
|
* Called when the current element that this handler is handling is closed.
|
||||||
* The default implementation is a no-op.
|
|
||||||
*
|
*
|
||||||
* @param warnings the warning set to store warnings in.
|
* @param warnings the warning set to store warnings in.
|
||||||
*/
|
*/
|
||||||
public void endHandler(String element, HashMap<String, String> attributes,
|
public abstract void endHandler(String element, HashMap<String, String> attributes,
|
||||||
String content, WarningSet warnings) throws SAXException {
|
String content, WarningSet warnings) throws SAXException;
|
||||||
// No-op
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -16,7 +16,7 @@ import org.xml.sax.SAXException;
|
|||||||
*
|
*
|
||||||
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
||||||
*/
|
*/
|
||||||
public class NullElementHandler extends ElementHandler {
|
public class NullElementHandler extends AbstractElementHandler {
|
||||||
public static final NullElementHandler INSTANCE = new NullElementHandler();
|
public static final NullElementHandler INSTANCE = new NullElementHandler();
|
||||||
|
|
||||||
private static final HashMap<String, String> EMPTY_MAP = new HashMap<String,String>();
|
private static final HashMap<String, String> EMPTY_MAP = new HashMap<String,String>();
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import net.sf.openrocket.aerodynamics.WarningSet;
|
|||||||
* An element handler that does not allow any sub-elements. If any are encountered
|
* An element handler that does not allow any sub-elements. If any are encountered
|
||||||
* a warning is generated and they are ignored.
|
* a warning is generated and they are ignored.
|
||||||
*/
|
*/
|
||||||
public class PlainTextHandler extends ElementHandler {
|
public class PlainTextHandler extends AbstractElementHandler {
|
||||||
public static final PlainTextHandler INSTANCE = new PlainTextHandler();
|
public static final PlainTextHandler INSTANCE = new PlainTextHandler();
|
||||||
|
|
||||||
private PlainTextHandler() {
|
private PlainTextHandler() {
|
||||||
|
|||||||
@ -16,8 +16,8 @@ import org.xml.sax.helpers.XMLReaderFactory;
|
|||||||
* both. This holds true for both the OpenRocket and RockSim design formats and the
|
* both. This holds true for both the OpenRocket and RockSim design formats and the
|
||||||
* RockSim engine definition format.
|
* RockSim engine definition format.
|
||||||
* <p>
|
* <p>
|
||||||
* The actual handling is performed by subclasses of {@link ElementHandler}. The
|
* The actual handling is performed by subclasses of {@link AbstractElementHandler}. The
|
||||||
* initial handler is provided to the {@link #readXML(InputSource, ElementHandler, WarningSet)}
|
* initial handler is provided to the {@link #readXML(InputSource, AbstractElementHandler, WarningSet)}
|
||||||
* method.
|
* method.
|
||||||
*
|
*
|
||||||
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
* @author Sampo Niskanen <sampo.niskanen@iki.fi>
|
||||||
@ -33,7 +33,7 @@ public class SimpleSAX {
|
|||||||
* @throws IOException if an I/O exception occurs while reading.
|
* @throws IOException if an I/O exception occurs while reading.
|
||||||
* @throws SAXException if e.g. malformed XML is encountered.
|
* @throws SAXException if e.g. malformed XML is encountered.
|
||||||
*/
|
*/
|
||||||
public static void readXML(InputSource source, ElementHandler initialHandler,
|
public static void readXML(InputSource source, AbstractElementHandler initialHandler,
|
||||||
WarningSet warnings) throws IOException, SAXException {
|
WarningSet warnings) throws IOException, SAXException {
|
||||||
|
|
||||||
DelegatorHandler xmlhandler = new DelegatorHandler(initialHandler, warnings);
|
DelegatorHandler xmlhandler = new DelegatorHandler(initialHandler, warnings);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user