Example usage for org.apache.commons.scxml2.io SCXMLReader read

List of usage examples for org.apache.commons.scxml2.io SCXMLReader read

Introduction

In this page you can find the example usage for org.apache.commons.scxml2.io SCXMLReader read.

Prototype

public static SCXML read(final Source scxmlSource, final Configuration configuration)
        throws IOException, ModelException, XMLStreamException 

Source Link

Document

Parse the SCXML document supplied by the given Source with the given Configuration .

Usage

From source file:org.onehippo.repository.scxml.RepositorySCXMLRegistry.java

private SCXMLDefinition readSCXMLDefinition(final String scxmlDefId, final Node scxmlDefNode)
        throws SCXMLException {
    String scxmlDefPath;// w  ww  .  j ava2  s  .co  m
    String scxmlSource;
    final List<CustomAction> customActions = new ArrayList<>();
    String className = null;

    try {
        scxmlDefPath = scxmlDefNode.getPath();
        scxmlSource = scxmlDefNode.getProperty(SCXML_SOURCE).getString();

        if (StringUtils.isBlank(scxmlSource)) {
            log.error("SCXML definition source is blank at '{}'. '{}'.", scxmlDefNode.getPath(), scxmlSource);
        }

        for (final Node actionNode : new NodeIterable(scxmlDefNode.getNodes())) {
            if (!actionNode.isNodeType(SCXML_ACTION)) {
                continue;
            }

            String namespace = actionNode.getProperty(SCXML_ACTION_NAMESPACE).getString();
            className = actionNode.getProperty(SCXML_ACTION_CLASSNAME).getString();
            @SuppressWarnings("unchecked")
            Class<? extends Action> actionClass = (Class<Action>) Thread.currentThread().getContextClassLoader()
                    .loadClass(className);
            customActions.add(new CustomAction(namespace, actionNode.getName(), actionClass));
        }
    } catch (ClassNotFoundException e) {
        throw new SCXMLException("Failed to find the custom action class: " + className, e);
    } catch (RepositoryException e) {
        throw new SCXMLException("Failed to read custom actions from repository. " + e.getLocalizedMessage(),
                e);
    } catch (Exception e) {
        throw new SCXMLException("Failed to load custom actions. " + e.getLocalizedMessage(), e);
    }

    try {
        Configuration configuration = createSCXMLReaderConfiguration(scxmlDefPath, customActions);
        SCXML scxml = SCXMLReader.read(new StreamSource(new StringReader(scxmlSource)), configuration);
        return new SCXMLDefinitionImpl(scxmlDefId, scxmlDefPath, scxml);
    } catch (IOException e) {
        throw new SCXMLException(
                "IO error while reading SCXML definition at '" + scxmlDefPath + "'. " + e.getLocalizedMessage(),
                e);
    } catch (ModelException e) {
        throw new SCXMLException(
                "Invalid SCXML model definition at '" + scxmlDefPath + "'. " + e.getLocalizedMessage(), e);
    } catch (XMLStreamException e) {
        throw new SCXMLException("Failed to read SCXML XML stream at '" + scxmlDefPath + "'. "
                + naturalizeXMLStreamExceptionMessage(e), e);
    }
}