Example usage for org.apache.commons.scxml.io SCXMLParser parse

List of usage examples for org.apache.commons.scxml.io SCXMLParser parse

Introduction

In this page you can find the example usage for org.apache.commons.scxml.io SCXMLParser parse.

Prototype

public static SCXML parse(final InputSource documentInputSource, final ErrorHandler errHandler)
        throws IOException, SAXException, ModelException 

Source Link

Document

API for standalone usage where the SCXML document is an InputSource.

Usage

From source file:com.korwe.thecore.scxml.ScxmlMessageProcessor.java

@Override
public void initialize(String sessionId) {
    super.initialize(sessionId);
    try {/*from   w w  w. j  av  a2 s.  co m*/
        String scxmlPath = CoreConfig.getInstance().getProperty("scxml_path");
        if (LOG.isDebugEnabled()) {
            LOG.debug("SCXML path = " + scxmlPath);
        }
        File scfile = new File(scxmlPath);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Absolute path: [" + scfile.getAbsolutePath() + "]");
        }

        InputSource source = new InputSource(new BufferedReader(new FileReader(scxmlPath)));

        scxml = SCXMLParser.parse(source, new SimpleErrorHandler());
        exec = new SCXMLExecutor(new JexlEvaluator(), new SimpleDispatcher(), new SimpleErrorReporter());
        exec.setStateMachine(scxml);
        exec.addListener(scxml, new SimpleSCXMLListener());
        exec.registerInvokerClass("x-coremessage", SendCoreMessageInvoker.class);

        Context context = new JexlContext();
        context.set("sessionId", sessionId);
        context.set("lastMsg", null);
        exec.setRootContext(context);

        exec.go();

    } catch (Exception e) {
        LOG.error("Failed to parse SCXML", e);
    }
}

From source file:ch.shaktipat.saraswati.internal.common.AbstractStateMachine.java

/**
 * Primary constructor, object instantiation incurs parsing cost.
 *
 * @param scxmlDocument The URL pointing to the SCXML document that
 *                      describes the "lifecycle" of the
 *                      instances of this class.
 * @param rootCtx The root context for this instance.
 * @param evaluator The expression evaluator for this instance.
 *
 * @see Context/*w w w.ja va 2s .c  o m*/
 * @see Evaluator
 */
public AbstractStateMachine(final URL scxmlDocument, final Context rootCtx, final Evaluator evaluator) {
    log = LogFactory.getLog(this.getClass());
    ErrorHandler errHandler = new SimpleErrorHandler();
    try {
        stateMachine = SCXMLParser.parse(scxmlDocument, errHandler);
    } catch (IOException ioe) {
        logError(ioe);
    } catch (SAXException sae) {
        logError(sae);
    } catch (ModelException me) {
        logError(me);
    }
    initialize(stateMachine, rootCtx, evaluator);
}

From source file:de.dfki.iui.mmds.dialogue.SiamStateMachine.java

private SCXML load(final String scxml) {

    ErrorHandler errHandler = new ErrorHandler() {
        @Override//from ww w. j av a 2s .co  m
        public void error(SAXParseException e) throws SAXException {
            System.err.println(e.getMessage());

        }

        @Override
        public void fatalError(SAXParseException e) throws SAXException {
            System.err.println(e.getMessage());

        }

        @Override
        public void warning(SAXParseException e) throws SAXException {
            System.err.println(e.getMessage());
        }
    };

    SCXML stateMachine = null;
    try {
        stateMachine = SCXMLParser.parse(new InputSource(new StringReader(scxml)), errHandler);// ca
        // );
    } catch (IOException | SAXException | ModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return stateMachine;
}

From source file:org.dishevelled.piccolo.identify.AbstractIdNode.java

/**
 * Create a new abstract id node with the specified value.
 *
 * @param value value for this id node/*from  w  w w . ja v a  2s. c o  m*/
 */
protected AbstractIdNode(final Object value) {
    this.value = value;
    iconSize = DEFAULT_ICON_SIZE;
    iconState = DEFAULT_ICON_STATE;
    iconTextDirection = DEFAULT_ICON_TEXT_DIRECTION;
    iconBundleImageNode = new IconBundleImageNode();
    nameTextNode = new NameTextNode();

    dragHandler = new DragHandler();
    mouseoverHandler = new MouseoverHandler();
    addInputEventListener(dragHandler);
    addInputEventListener(mouseoverHandler);

    if (stateMachine == null) {
        try {
            URL stateChart = getClass().getResource("stateChart.xml");
            stateMachine = SCXMLParser.parse(stateChart, new SimpleErrorHandler());
        } catch (Exception e) {
            // ignore
        }
    }
    stateMachineSupport = new StateMachineSupport(this, stateMachine);
}

From source file:org.dishevelled.piccolo.sprite.statemachine.AbstractStateMachineSprite.java

/**
 * Load the state machine resource with the specified name, if any.  Any exceptions thrown
 * will be ignored.//w w w  .  j  a  v a  2  s.  co  m
 *
 * @param cls class
 * @param name name
 * @return the state machine resource with the specified name, or <code>null</code>
 *    if no such resource exists
 */
protected static final <T> SCXML loadStateMachine(final Class<T> cls, final String name) {
    SCXML stateMachine = null;
    try {
        stateMachine = SCXMLParser.parse(cls.getResource(name), new SimpleErrorHandler());
    } catch (IOException e) {
        // ignore
    } catch (SAXException e) {
        // ignore
    } catch (ModelException e) {
        // ignore
    }
    return stateMachine;
}

From source file:org.finra.datagenerator.engine.scxml.SCXMLGapperTest.java

/**
 * Test SCXMLGapper's ability to decompose and recompose an SCXMLFrontier with none of the variables set
 *///  w ww  . ja  v  a 2s .  c  o m
@Test
public void testDecomposeAndReproduceInitial() {
    SCXMLEngine e = new SCXMLEngine();
    e.setModelByText(bigTest);

    try {
        List<PossibleState> bfs = e.bfs(1);
        PossibleState p = bfs.get(0);

        try {
            InputStream is = SCXMLEngineTest.class.getResourceAsStream("/bigtest.xml");
            SCXML model = SCXMLParser.parse(new InputSource(is), null);

            SCXMLFrontier frontier = new SCXMLFrontier(p, model);
            SCXMLGapper gapper = new SCXMLGapper();
            frontier = (SCXMLFrontier) gapper.reproduce(gapper.decompose(frontier, bigTest));

            p = frontier.getRoot();
            Assert.assertEquals(p.nextState.getId(), "start");

            Assert.assertEquals(p.variables.get("var_out_RECORD_TYPE"), "");
            Assert.assertEquals(p.variables.get("var_out_RECORD_TYPE_2"), "");
            Assert.assertEquals(p.variables.get("var_out_RECORD_TYPE_3"), "");
            Assert.assertEquals(p.variables.get("var_out_RECORD_TYPE_4"), "");
            Assert.assertEquals(p.variables.get("var_out_RECORD_TYPE_5"), "");
            Assert.assertEquals(p.variables.get("var_out_RECORD_TYPE_6"), "");
            Assert.assertEquals(p.variables.get("var_out_RECORD_TYPE_7"), "");
            Assert.assertEquals(p.variables.get("var_out_RECORD_TYPE_8"), "");
            Assert.assertEquals(p.variables.keySet().size(), 8);
        } catch (IOException | SAXException ex) {
            Assert.fail();
        }
    } catch (ModelException ex) {
        Assert.fail();
    }
}

From source file:org.finra.datagenerator.engine.scxml.SCXMLGapperTest.java

/**
 * Test SCXMLGapper's ability to decompose and recompose an SCXMLFrontier
 *///  w w w.  j  a  v  a 2s .  c o  m
@Test
public void testDecomposeAndRecompose() {
    SCXMLEngine e = new SCXMLEngine();
    e.setModelByText(bigTest);

    try {
        List<PossibleState> bfs = e.bfs(343);
        PossibleState p = bfs.get(0);

        try {
            InputStream is = SCXMLEngineTest.class.getResourceAsStream("/bigtest.xml");
            SCXML model = SCXMLParser.parse(new InputSource(is), null);

            SCXMLFrontier frontier = new SCXMLFrontier(p, model);
            SCXMLGapper gapper = new SCXMLGapper();

            PossibleState before = frontier.getRoot();
            frontier = (SCXMLFrontier) gapper.reproduce(gapper.decompose(frontier, bigTest));
            PossibleState after = frontier.getRoot();

            Assert.assertEquals(after.nextState.getId(), before.nextState.getId());

            for (String key : before.variables.keySet()) {
                Assert.assertEquals(before.variables.get(key), after.variables.get(key));
            }

            for (String key : after.variables.keySet()) {
                Assert.assertEquals(before.variables.get(key), after.variables.get(key));
            }
        } catch (IOException | SAXException ex) {
            Assert.fail();
        }
    } catch (ModelException ex) {
        Assert.fail();
    }
}

From source file:org.wso2.carbon.governance.lcm.util.LifecycleBeanPopulator.java

public static boolean deserializeLifecycleBean(OMElement configurationElement, Registry registry)
        throws Exception {
    CommonUtil.validateOMContent(configurationElement,
            CommonUtil.getLifecycleSchemaValidator(CommonUtil.getLifecycleSchemaLocation()));

    try {//ww w  .j a v a  2 s .  com
        OMElement scxmlElement = null;
        OMElement lifecycleElement = null;
        OMElement configuration = configurationElement.getFirstElement();
        String type = configuration.getAttributeValue(new QName("type"));

        if (type.equals("literal")) {
            lifecycleElement = configuration.getFirstElement();
        } else if (type.equals("resource")) {
            String resourcePath = configuration.getText();
            if (registry.resourceExists(resourcePath)) {
                Resource resource = registry.get(resourcePath);
                if (resource.getContent() != null) {
                    if (resource.getContent() instanceof String) {
                        lifecycleElement = CommonUtil.buildOMElement((String) resource.getContent());
                    } else if (resource.getContent() instanceof byte[]) {
                        lifecycleElement = CommonUtil
                                .buildOMElement(RegistryUtils.decodeBytes((byte[]) resource.getContent()));
                    } else {
                        String msg = "Could not find valid lifecycle configuration";
                        log.error(msg);
                        throw new RegistryException(msg);
                    }
                } else {
                    String msg = "Resource does not contain a valid lifecycle configuration";
                    log.error(msg);
                    throw new RegistryException(msg);
                }
            } else {
                String msg = "Resource not found at " + resourcePath;
                log.error(msg);
                throw new RegistryException(msg);
            }
        } else {
            String msg = "The type must be either literal or resource";
            log.error(msg);
            throw new RegistryException(msg);
        }
        scxmlElement = lifecycleElement.getFirstElement();

        CommonUtil.validateOMContent(scxmlElement);
        CommonUtil.validateLifeCycle(scxmlElement);

        //            Validating whether this complies to the scxml specification.
        SCXMLParser.parse(new InputSource(new CharArrayReader((scxmlElement.toString()).toCharArray())), null);

        //            Validating whether the data model is correct
        if (!CommonUtil.validateSCXMLDataModel(scxmlElement)) {
            throw new RegistryException("Failed to validate the data model. Invalid forEvent found");
        }

    } catch (RegistryException e) {
        throw e;
    } catch (Exception e) {
        String msg = e.getMessage() + ". " + "Please check whether there are any whitespaces in state names";
        log.error(msg, e);
        throw new RegistryException(msg);
    }
    return true;
}

From source file:org.wso2.carbon.governance.registry.extensions.aspects.DefaultLifeCycle.java

private void setSCXMLConfiguration(Registry registry)
        throws RegistryException, XMLStreamException, IOException, SAXException, ModelException {
    String xmlContent;//ww w  .  ja v  a2s  .  c o  m
    if (isConfigurationFromResource) {
        if (registry.resourceExists(configurationResourcePath)) {
            try {
                Resource configurationResource = registry.get(configurationResourcePath);
                xmlContent = RegistryUtils.decodeBytes((byte[]) configurationResource.getContent());
                configurationElement = AXIOMUtil.stringToOM(xmlContent);
                // to validate XML
                configurationElement.toString();
            } catch (Exception e) {
                String msg = "Invalid lifecycle configuration found at " + configurationResourcePath;
                log.error(msg);
                throw new RegistryException(msg);
            }
        } else {
            String msg = "Unable to find the lifecycle configuration from the given path: "
                    + configurationResourcePath;
            log.error(msg);
            throw new RegistryException(msg);
        }
    }

    try {
        //            We check if there is an attribute called "audit" and if it exists, what is the value of that attribute
        if (configurationElement.getAttributeValue(new QName(LifecycleConstants.AUDIT)) != null) {
            isAuditEnabled = Boolean
                    .parseBoolean(configurationElement.getAttributeValue(new QName(LifecycleConstants.AUDIT)));
        }

        //            Here we are taking the scxml element from the configuration
        OMElement scxmlElement = configurationElement.getFirstElement();
        scxml = SCXMLParser.parse(new InputSource(new CharArrayReader((scxmlElement.toString()).toCharArray())),
                null);
    } catch (Exception e) {
        String msg = "Invalid SCXML configuration found";
        log.error(msg);
        throw new RegistryException(msg);
    }
}

From source file:org.wso2.carbon.pc.core.extensions.aspects.ProcessLifeCycle.java

private void setSCXMLConfiguration(Registry registry)
        throws RegistryException, XMLStreamException, IOException, SAXException, ModelException {
    String xmlContent;/* w  ww . ja v  a2  s .co  m*/
    if (isConfigurationFromResource) {
        if (registry.resourceExists(configurationResourcePath)) {
            try {
                Resource configurationResource = registry.get(configurationResourcePath);
                xmlContent = RegistryUtils.decodeBytes((byte[]) configurationResource.getContent());
                configurationElement = AXIOMUtil.stringToOM(xmlContent);
                configurationElement.toString();
            } catch (Exception e) {
                String msg = "Invalid lifecycle configuration found at " + configurationResourcePath;
                log.error(msg, e);
                throw new RegistryException(msg);
            }
        } else {
            String msg = "Unable to find the lifecycle configuration from the given path: "
                    + configurationResourcePath;
            log.error(msg);
            throw new RegistryException(msg);
        }
    }

    try {
        if (configurationElement.getAttributeValue(new QName(LifecycleConstants.AUDIT)) != null) {
            isAuditEnabled = Boolean
                    .parseBoolean(configurationElement.getAttributeValue(new QName(LifecycleConstants.AUDIT)));
        }
        OMElement scxmlElement = configurationElement.getFirstElement();
        scxml = SCXMLParser.parse(new InputSource(new CharArrayReader((scxmlElement.toString()).toCharArray())),
                null);
    } catch (Exception e) {
        String msg = "Invalid SCXML configuration found";
        log.error(msg, e);
        throw new RegistryException(msg);
    }
}