commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject svn commit: r1634234 - in /commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2: ./ env/groovy/ env/javascript/ env/jexl/ env/xpath/ io/ model/
Date Sat, 25 Oct 2014 15:16:31 GMT
Author: ate
Date: Sat Oct 25 15:16:30 2014
New Revision: 1634234

URL: http://svn.apache.org/r1634234
Log:
SCXML-212: Add support for SCXML datamodel type and default Evaluator instantiation

Added:
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorFactory.java
  (with props)
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorProvider.java
  (with props)
Modified:
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/Evaluator.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutionContext.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/groovy/GroovyEvaluator.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/javascript/JSEvaluator.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/jexl/JexlEvaluator.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/xpath/XPathEvaluator.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLWriter.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/Evaluator.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/Evaluator.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/Evaluator.java (original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/Evaluator.java Sat
Oct 25 15:16:30 2014
@@ -26,6 +26,12 @@ import org.w3c.dom.Node;
 public interface Evaluator {
 
     /**
+     * Get the datamodel type supported by this Evaluator
+     * @return The supported datamodel type
+     */
+    String getSupportedDatamodel();
+
+    /**
      * Evaluate an expression.
      *
      * @param ctx variable context

Added: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorFactory.java?rev=1634234&view=auto
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorFactory.java
(added)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorFactory.java
Sat Oct 25 15:16:30 2014
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.scxml2;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.commons.scxml2.env.groovy.GroovyEvaluator;
+import org.apache.commons.scxml2.env.javascript.JSEvaluator;
+import org.apache.commons.scxml2.env.jexl.JexlEvaluator;
+import org.apache.commons.scxml2.env.xpath.XPathEvaluator;
+import org.apache.commons.scxml2.model.ModelException;
+import org.apache.commons.scxml2.model.SCXML;
+
+/**
+ * A static singleton factory for {@link EvaluatorProvider}s by supported SCXML datamodel
type.
+ * <p>
+ *  The EvaluatorFactory is used to automatically create an {@link Evaluator} instance for
an SCXML
+ *  statemachine when none has been pre-defined and configured for the {@link SCXMLExecutor}.
+ * </p>
+ * <p>
+ *  The builtin supported providers are:
+ *  <ul>
+ *      <li>no datamodel (default) or datamodel="jexl": {@link JexlEvaluator.JexlEvaluatorProvider}</li>
+ *      <li>datamodel="ecmascript": {@link JSEvaluator.JSEvaluatorProvider}</li>
+ *      <li>datamodel="groovy": {@link GroovyEvaluator.GroovyEvaluatorProvider}</li>
+ *      <li>datamodel="xpath": {@link XPathEvaluator.XPathEvaluatorProvider}</li>
+ *  </ul>
+ *  </p>
+ *  <p>
+ *  For adding additional or overriding the builtin Evaluator implementations use
+ *  {@link #registerEvaluatorProvider(EvaluatorProvider)} or {@link #unregisterEvaluatorProvider(String)}.
+ *  </p>
+ *  <p>
+ *  The default provider can be overridden using the {@link #setDefaultProvider(EvaluatorProvider)}
which will
+ *  register the provider under an empty ("") value for the datamodel.<br/>
+ *  Note: this is <em>not</em> the same as datamodel="null" (which currently
is not (yet) supported)!
+ * </p>
+ */
+public class EvaluatorFactory {
+
+    private static EvaluatorFactory INSTANCE = new EvaluatorFactory();
+
+    private final Map<String, EvaluatorProvider> providers = new ConcurrentHashMap<String,
EvaluatorProvider>();
+
+    private EvaluatorFactory() {
+        providers.put("xpath", new XPathEvaluator.XPathEvaluatorProvider());
+        providers.put("ecmascript", new JSEvaluator.JSEvaluatorProvider());
+        providers.put("groovy", new GroovyEvaluator.GroovyEvaluatorProvider());
+        providers.put("jexl", new JexlEvaluator.JexlEvaluatorProvider());
+        providers.put("", providers.get("jexl"));
+    }
+
+    public static void setDefaultProvider(EvaluatorProvider defaultProvider) {
+        INSTANCE.providers.put("", defaultProvider);
+    }
+
+    public static EvaluatorProvider getDefaultProvider() {
+        return INSTANCE.providers.get("");
+    }
+
+    public static EvaluatorProvider getEvaluatorProvider(String datamodelType) {
+        return INSTANCE.providers.get(datamodelType == null ? "" : datamodelType);
+    }
+
+    public static void registerEvaluatorProvider(EvaluatorProvider provider) {
+        INSTANCE.providers.put(provider.getSupportedDatamodel(), provider);
+    }
+
+    public static void unregisterEvaluatorProvider(String datamodelType) {
+        INSTANCE.providers.remove(datamodelType == null ? "" : datamodelType);
+    }
+
+    /**
+     * Returns a dedicated Evaluator instance for a specific SCXML document its documentmodel
type.
+     * <p>If no SCXML document is provided a default Evaluator will be returned.</p>
+     * @param document The document to return a dedicated Evaluator for. May be null to retrieve
the default Evaluator.
+     * @return a new and not sharable Evaluator instance for the provided document, or a
default Evaluator otherwise
+     * @throws ModelException If the SCXML document datamodel type is not supported.
+     */
+    public static Evaluator getEvaluator(SCXML document) throws ModelException {
+        String datamodelType = document != null ? document.getDatamodelType() : null;
+        EvaluatorProvider provider = INSTANCE.providers.get(datamodelType == null ? "" :
datamodelType);
+        if (provider == null) {
+            throw new ModelException("Unsupported SCXML document datamodel type \""+(datamodelType)+"\"");
+        }
+        return document != null ? provider.getEvaluator(document) : provider.getEvaluator();
+    }
+}

Propchange: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorFactory.java
------------------------------------------------------------------------------
    svn:keywords = Id

Added: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorProvider.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorProvider.java?rev=1634234&view=auto
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorProvider.java
(added)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorProvider.java
Sat Oct 25 15:16:30 2014
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.scxml2;
+
+import org.apache.commons.scxml2.model.SCXML;
+
+/**
+ * An EvaluatorProvider provides an {@link Evaluator} instance for a specific SCXML datamodel
type.
+ */
+public interface EvaluatorProvider {
+
+    /**
+     * @return The SCXML datamodel type this provider supports
+     */
+    String getSupportedDatamodel();
+
+    /**
+     * @return a default or generic {@link Evaluator} supporting the {@link #getSupportedDatamodel()}
+     */
+    Evaluator getEvaluator();
+
+    /**
+     * Factory method to return a dedicated and optimized {@link Evaluator} instance for
a specific SCXML document.
+     * <p>
+     * As the returned Evaluator <em>might</em> be optimized and dedicated for
the SCXML document instance, the
+     * Evaluator may not be shareable and reusable for other SCXML documents.
+     * </p>
+     * @param document the SCXML document
+     * @return a new and not sharable Evaluator instance
+     */
+    Evaluator getEvaluator(SCXML document);
+}

Propchange: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorProvider.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/EvaluatorProvider.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java (original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCInstance.java Sat
Oct 25 15:16:30 2014
@@ -53,11 +53,6 @@ public class SCInstance implements Seria
     private static final String ERR_NO_STATE_MACHINE = "SCInstance: State machine not set";
 
     /**
-     * SCInstance cannot be initialized without setting an evaluator.
-     */
-    private static final String ERR_NO_EVALUATOR = "SCInstance: Evaluator not set";
-
-    /**
      * SCInstance cannot be initialized without setting an error reporter.
      */
     private static final String ERR_NO_ERROR_REPORTER = "SCInstance: ErrorReporter not set";
@@ -147,7 +142,11 @@ public class SCInstance implements Seria
             throw new ModelException(ERR_NO_STATE_MACHINE);
         }
         if (evaluator == null) {
-            throw new ModelException(ERR_NO_EVALUATOR);
+            evaluator = EvaluatorFactory.getEvaluator(stateMachine);
+        }
+        if (stateMachine.getDatamodelType() != null && !stateMachine.getDatamodelType().equals(evaluator.getSupportedDatamodel()))
{
+            throw new ModelException("Incompatible SCXML document datamodel type \""+stateMachine.getDatamodelType()+"\""
+                    + " for evaluator "+evaluator.getClass().getName()+" supported datamodel
\""+evaluator.getSupportedDatamodel()+"\"");
         }
         if (errorReporter == null) {
             throw new ModelException(ERR_NO_ERROR_REPORTER);
@@ -186,27 +185,32 @@ public class SCInstance implements Seria
     /**
      * Set or re-attach the evaluator
      * <p>
-     * If this state machine instance has been initialized before, it will be initialized
again, destroying all existing
-     * state!
+     * If not re-attaching and this state machine instance has been initialized before,
+     * it will be initialized again, destroying all existing state!
      * </p>
      * @param evaluator The evaluator for this state machine instance.
-     * @throws ModelException if an attempt is made to set a null value for the evaluator
      */
-    protected void setEvaluator(Evaluator evaluator) throws ModelException {
-        if (evaluator == null) {
-            throw new ModelException(ERR_NO_EVALUATOR);
-        }
-        if (this.evaluator != null && initialized) {
-            this.evaluator = evaluator;
-            // change of evaluator after initialization: re-initialize
-            initialize();
-        }
-        else {
-            this.evaluator = evaluator;
+    protected void setEvaluator(Evaluator evaluator, boolean reAttach) throws ModelException
{
+        this.evaluator = evaluator;
+        if (initialized) {
+            if (!reAttach) {
+                // change of evaluator after initialization: re-initialize
+                initialize();
+            }
+            else if (evaluator == null) {
+                throw new ModelException("SCInstance: re-attached without Evaluator");
+            }
         }
     }
 
     /**
+     * @return Return the current evaluator
+     */
+    protected Evaluator getEvaluator() {
+        return evaluator;
+    }
+
+    /**
      * Set or re-attach the error reporter
      * @param errorReporter The error reporter for this state machine instance.
      * @throws ModelException if an attempt is made to set a null value for the error reporter
@@ -238,14 +242,8 @@ public class SCInstance implements Seria
         if (stateMachine == null) {
             throw new ModelException(ERR_NO_STATE_MACHINE);
         }
-        if (this.stateMachine != null && initialized) {
-            this.stateMachine = stateMachine;
-            // change of state machine after initialization: re-initialize
-            initialize();
-        }
-        else {
-            this.stateMachine = stateMachine;
-        }
+        this.stateMachine = stateMachine;
+        initialize();
     }
 
     /**

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutionContext.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutionContext.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutionContext.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/SCXMLExecutionContext.java
Sat Oct 25 15:16:30 2014
@@ -190,6 +190,8 @@ public class SCXMLExecutionContext imple
      */
     protected void setStateMachine(SCXML stateMachine) throws ModelException {
         scInstance.setStateMachine(stateMachine);
+        // synchronize possible derived evaluator
+        this.evaluator = scInstance.getEvaluator();
     }
 
     /**
@@ -216,8 +218,9 @@ public class SCXMLExecutionContext imple
      * @throws ModelException if attempting to set a null value or the state machine instance
failed to re-initialize
      */
     protected void setEvaluator(Evaluator evaluator) throws ModelException {
-        scInstance.setEvaluator(evaluator);
-        this.evaluator = evaluator;
+        scInstance.setEvaluator(evaluator, false);
+        // synchronize possible derived evaluator
+        this.evaluator = scInstance.getEvaluator();
     }
 
     /**
@@ -294,7 +297,7 @@ public class SCXMLExecutionContext imple
         if (scInstance != null) {
             scInstance.detach();
             try {
-                scInstance.setEvaluator(evaluator);
+                scInstance.setEvaluator(evaluator, true);
                 scInstance.setErrorReporter(errorReporter);
             }
             catch (ModelException me) {

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/groovy/GroovyEvaluator.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/groovy/GroovyEvaluator.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/groovy/GroovyEvaluator.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/groovy/GroovyEvaluator.java
Sat Oct 25 15:16:30 2014
@@ -27,9 +27,11 @@ import java.util.regex.Pattern;
 
 import org.apache.commons.scxml2.Context;
 import org.apache.commons.scxml2.Evaluator;
+import org.apache.commons.scxml2.EvaluatorProvider;
 import org.apache.commons.scxml2.SCXMLExpressionException;
 import org.apache.commons.scxml2.SCXMLSystemContext;
 import org.apache.commons.scxml2.env.EffectiveContextMap;
+import org.apache.commons.scxml2.model.SCXML;
 import org.w3c.dom.Node;
 
 /**
@@ -43,6 +45,26 @@ public class GroovyEvaluator implements 
     /** Serial version UID. */
     private static final long serialVersionUID = 1L;
 
+    private static final String SUPPORTED_DATAMODEL = "groovy";
+
+    public static class GroovyEvaluatorProvider implements EvaluatorProvider {
+
+        @Override
+        public String getSupportedDatamodel() {
+            return SUPPORTED_DATAMODEL;
+        }
+
+        @Override
+        public Evaluator getEvaluator() {
+            return new GroovyEvaluator();
+        }
+
+        @Override
+        public Evaluator getEvaluator(final SCXML document) {
+            return new GroovyEvaluator();
+        }
+    }
+
     /** Error message if evaluation context is not a GroovyContext. */
     private static final String ERR_CTX_TYPE = "Error evaluating Groovy "
             + "expression, Context must be a org.apache.commons.scxml2.env.groovy.GroovyContext";
@@ -128,6 +150,12 @@ public class GroovyEvaluator implements 
 
     /* SCXMLEvaluator implementation methods */
 
+
+    @Override
+    public String getSupportedDatamodel() {
+        return SUPPORTED_DATAMODEL;
+    }
+
     /**
      * Evaluate an expression.
      *

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/javascript/JSEvaluator.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/javascript/JSEvaluator.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/javascript/JSEvaluator.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/javascript/JSEvaluator.java
Sat Oct 25 15:16:30 2014
@@ -27,8 +27,10 @@ import javax.script.ScriptEngineManager;
 import org.apache.commons.scxml2.Builtin;
 import org.apache.commons.scxml2.Context;
 import org.apache.commons.scxml2.Evaluator;
+import org.apache.commons.scxml2.EvaluatorProvider;
 import org.apache.commons.scxml2.SCXMLExpressionException;
 import org.apache.commons.scxml2.SCXMLSystemContext;
+import org.apache.commons.scxml2.model.SCXML;
 import org.w3c.dom.Node;
 
 /**
@@ -47,6 +49,26 @@ public class JSEvaluator implements Eval
 
     // CONSTANTS
 
+    private static final String SUPPORTED_DATAMODEL = "ecmascript";
+
+    public static class JSEvaluatorProvider implements EvaluatorProvider {
+
+        @Override
+        public String getSupportedDatamodel() {
+            return SUPPORTED_DATAMODEL;
+        }
+
+        @Override
+        public Evaluator getEvaluator() {
+            return new JSEvaluator();
+        }
+
+        @Override
+        public Evaluator getEvaluator(final SCXML document) {
+            return new JSEvaluator();
+        }
+    }
+
     /** Pattern for recognizing the SCXML In() special predicate. */
     private static final Pattern IN_FN = Pattern.compile("In\\(");
     /** Pattern for recognizing the Commons SCXML Data() builtin function. */
@@ -68,6 +90,12 @@ public class JSEvaluator implements Eval
 
     // INSTANCE METHODS
 
+    @Override
+    public String getSupportedDatamodel() {
+        return SUPPORTED_DATAMODEL;
+    }
+
+
     /**
      * Creates a child context.
      *

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/jexl/JexlEvaluator.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/jexl/JexlEvaluator.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/jexl/JexlEvaluator.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/jexl/JexlEvaluator.java
Sat Oct 25 15:16:30 2014
@@ -25,8 +25,10 @@ import org.apache.commons.jexl2.JexlEngi
 import org.apache.commons.jexl2.Script;
 import org.apache.commons.scxml2.Context;
 import org.apache.commons.scxml2.Evaluator;
+import org.apache.commons.scxml2.EvaluatorProvider;
 import org.apache.commons.scxml2.SCXMLExpressionException;
 import org.apache.commons.scxml2.env.EffectiveContextMap;
+import org.apache.commons.scxml2.model.SCXML;
 import org.w3c.dom.Node;
 
 /**
@@ -42,10 +44,32 @@ public class JexlEvaluator implements Ev
     /** Serial version UID. */
     private static final long serialVersionUID = 1L;
 
+    private static final String SUPPORTED_DATAMODEL = "jexl";
+
+    public static class JexlEvaluatorProvider implements EvaluatorProvider {
+
+        @Override
+        public String getSupportedDatamodel() {
+            return SUPPORTED_DATAMODEL;
+        }
+
+        @Override
+        public Evaluator getEvaluator() {
+            return new JexlEvaluator();
+        }
+
+        @Override
+        public Evaluator getEvaluator(final SCXML document) {
+            return new JexlEvaluator();
+        }
+    }
+
     /** Error message if evaluation context is not a JexlContext. */
     private static final String ERR_CTX_TYPE = "Error evaluating JEXL "
         + "expression, Context must be a org.apache.commons.scxml2.env.jexl.JexlContext";
 
+
+
     /** The internal JexlEngine instance to use. */
     private transient volatile JexlEngine jexlEngine;
 
@@ -108,6 +132,11 @@ public class JexlEvaluator implements Ev
         }
     }
 
+    @Override
+    public String getSupportedDatamodel() {
+        return SUPPORTED_DATAMODEL;
+    }
+
     /**
      * Evaluate an expression.
      *

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/xpath/XPathEvaluator.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/xpath/XPathEvaluator.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/xpath/XPathEvaluator.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/env/xpath/XPathEvaluator.java
Sat Oct 25 15:16:30 2014
@@ -33,8 +33,10 @@ import javax.xml.xpath.XPathFunction;
 
 import org.apache.commons.scxml2.Context;
 import org.apache.commons.scxml2.Evaluator;
+import org.apache.commons.scxml2.EvaluatorProvider;
 import org.apache.commons.scxml2.SCXMLExpressionException;
 import org.apache.commons.scxml2.env.xpath.FunctionResolver.FunctionKey;
+import org.apache.commons.scxml2.model.SCXML;
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 
@@ -48,6 +50,28 @@ public class XPathEvaluator implements E
 
     /** Serial version UID. */
     private static final long serialVersionUID = -3578920670869493294L;
+
+    private static final String SUPPORTED_DATAMODEL = "xpath";
+
+    public static class XPathEvaluatorProvider implements EvaluatorProvider {
+
+        @Override
+        public String getSupportedDatamodel() {
+            return SUPPORTED_DATAMODEL;
+        }
+
+        @Override
+        public Evaluator getEvaluator() {
+            return new XPathEvaluator();
+        }
+
+        @Override
+        public Evaluator getEvaluator(final SCXML document) {
+            return new XPathEvaluator();
+        }
+    }
+
+
     /** Pattern for recognizing the Commons SCXML Data() builtin function. */
     private static final Pattern dataFct = Pattern.compile("Data\\(");
 
@@ -61,7 +85,7 @@ public class XPathEvaluator implements E
     /**
      * No argument constructor.
      */
-    public XPathEvaluator() throws InstantiationException {
+    public XPathEvaluator() {
         fnResolver = new FunctionResolver();
         factory = XPathFactory.newInstance();
         factory.setXPathFunctionResolver(fnResolver);
@@ -73,12 +97,16 @@ public class XPathEvaluator implements E
      *
      * @param functions The user-defined XPath functions to use.
      */
-    public XPathEvaluator(final Map<FunctionKey, XPathFunction> functions)
-    throws InstantiationException {
+    public XPathEvaluator(final Map<FunctionKey, XPathFunction> functions) {
         this();
         fnResolver.addFunctions(functions);
     }
 
+    @Override
+    public String getSupportedDatamodel() {
+        return SUPPORTED_DATAMODEL;
+    }
+
     /**
      * @see Evaluator#eval(Context, String)
      */
@@ -160,14 +188,15 @@ public class XPathEvaluator implements E
     /**
      * Create dummy context node for XPath evaluation context.
      */
-    private Document getDummyContextNode() throws InstantiationException {
+    private Document getDummyContextNode() throws RuntimeException {
         try {
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setNamespaceAware(true);
             return dbf.newDocumentBuilder().newDocument();
+        } catch (RuntimeException e) {
+            throw e;
         } catch (Exception e) {
-            throw new InstantiationException("Cannot create dummy context " +
-                " node for XPath evaluator");
+            throw new RuntimeException("Cannot create dummy context node for XPath evaluator");
         }
     }
 

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLReader.java
Sat Oct 25 15:16:30 2014
@@ -270,6 +270,7 @@ public final class SCXMLReader {
     private static final String ATTR_ARRAY = "array";
     private static final String ATTR_AUTOFORWARD = "autoforward";
     private static final String ATTR_COND = "cond";
+    private static final String ATTR_DATAMODEL = "datamodel";
     private static final String ATTR_DELAY = "delay";
     private static final String ATTR_DELAYEXPR = "delayexpr";
     private static final String ATTR_EVENT = "event";
@@ -620,6 +621,7 @@ public final class SCXMLReader {
     private static void readSCXML(final XMLStreamReader reader, final Configuration configuration,
final SCXML scxml)
             throws IOException, ModelException, XMLStreamException {
 
+        scxml.setDatamodelType(readAV(reader, ATTR_DATAMODEL));
         scxml.setExmode(readAV(reader, ATTR_EXMODE));
         scxml.setInitial(readAV(reader, ATTR_INITIAL));
         scxml.setName(readAV(reader, ATTR_NAME));

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLWriter.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLWriter.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLWriter.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/SCXMLWriter.java
Sat Oct 25 15:16:30 2014
@@ -157,6 +157,7 @@ public class SCXMLWriter {
     private static final String ATTR_ARRAY = "array";
     private static final String ATTR_AUTOFORWARD = "autoforward";
     private static final String ATTR_COND = "cond";
+    private static final String ATTR_DATAMODEL = "datamodel";
     private static final String ATTR_DELAY = "delay";
     private static final String ATTR_DELAYEXPR = "delayexpr";
     private static final String ATTR_EVENT = "event";
@@ -489,6 +490,7 @@ public class SCXMLWriter {
         // Attributes
         writeAV(writer, ATTR_VERSION, scxml.getVersion());
         writeAV(writer, ATTR_INITIAL, scxml.getInitial());
+        writeAV(writer, ATTR_DATAMODEL, scxml.getDatamodelType());
         writeAV(writer, ATTR_NAME, scxml.getName());
         writeAV(writer, ATTR_PROFILE, scxml.getProfile());
         writeAV(writer, ATTR_EXMODE, scxml.getExmode());

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java?rev=1634234&r1=1634233&r2=1634234&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java (original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java Sat
Oct 25 15:16:30 2014
@@ -88,6 +88,12 @@ public class SCXML implements Serializab
      */
     private String exmode;
 
+
+    /**
+     * The datamodel (type) as specified as attribute on this document
+     */
+    private String datamodelType;
+
     /**
      * Optional property holding the data model for this SCXML document.
      * This gets merged with the root context and potentially hides any
@@ -381,5 +387,20 @@ public class SCXML implements Serializab
 		this.exmode = exmode;
 	}
 
+    /**
+     * Get the datamodel type as specified as attribute on this document
+     * @return The datamodel type of this document
+     */
+    public String getDatamodelType() {
+        return datamodelType;
+    }
+
+    /**
+     * Sets the datamodel type as specified as attribute on this document
+     * @param datamodelType The datamodel type
+     */
+    public void setDatamodelType(final String datamodelType) {
+        this.datamodelType = datamodelType;
+    }
 }
 



Mime
View raw message