+class DatatypeHandler extends AbstractElementHandler {
+ private final DocumentLoadingContext context;
+ private final OpenRocketContentHandler contentHandler;
+ private CustomExpressionHandler customExpressionHandler = null;
+
+ public DatatypeHandler(OpenRocketContentHandler contentHandler, DocumentLoadingContext context) {
+ this.context = context;
+ this.contentHandler = contentHandler;
+ }
+
+ @Override
+ public ElementHandler openElement(String element,
+ HashMap<String, String> attributes, WarningSet warnings)
+ throws SAXException {
+
+ if (element.equals("type") && attributes.get("source").equals("customexpression") ){
+ customExpressionHandler = new CustomExpressionHandler(contentHandler, context);
+ return customExpressionHandler;
+ }
+ else {
+ warnings.add(Warning.fromString("Unknown datatype " + element + " defined, ignoring"));
+ }
+
+ return this;
+ }
+
+ @Override
+ public void closeElement(String element, HashMap<String, String> attributes, String content, WarningSet warnings) throws SAXException {
+ attributes.remove("source");
+ super.closeElement(element, attributes, content, warnings);
+
+ if (customExpressionHandler != null){
+ contentHandler.getDocument().addCustomExpression(customExpressionHandler.currentExpression);
+ }
+
+ }
+
+}
+
+class CustomExpressionHandler extends AbstractElementHandler{
+ private final DocumentLoadingContext context;
+ private final OpenRocketContentHandler contentHandler;
+ public CustomExpression currentExpression;
+
+ public CustomExpressionHandler(OpenRocketContentHandler contentHandler, DocumentLoadingContext context) {
+ this.context = context;
+ this.contentHandler = contentHandler;
+ currentExpression = new CustomExpression(contentHandler.getDocument());
+
+ }
+
+ @Override
+ public ElementHandler openElement(String element,
+ HashMap<String, String> attributes, WarningSet warnings)
+ throws SAXException {
+
+ return this;
+ }
+
+ @Override
+ public void closeElement(String element, HashMap<String, String> attributes,
+ String content, WarningSet warnings) throws SAXException {
+
+ if (element.equals("type")) {
+ contentHandler.getDocument().addCustomExpression(currentExpression);
+ }
+
+ if (element.equals("name")) {
+ currentExpression.setName(content);
+ }
+
+ if (element.equals("symbol")) {
+ currentExpression.setSymbol(content);
+ }
+
+ if (element.equals("unit") && attributes.get("unittype").equals("auto")) {
+ currentExpression.setUnit(content);
+ }
+
+ if (element.equals("expression")){
+ currentExpression.setExpression(content);
+ }
+ }
+}