001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.sequencer.jpdl3;
023    
024    import java.io.InputStream;
025    import java.util.ArrayList;
026    import java.util.List;
027    import org.jbpm.graph.def.Node;
028    import org.jbpm.graph.def.ProcessDefinition;
029    import org.jbpm.graph.def.Transition;
030    import org.jbpm.graph.node.EndState;
031    import org.jbpm.graph.node.StartState;
032    
033    /**
034     * The jBPM Process definition language meta data.
035     * 
036     * @author Serge Pagop
037     */
038    public class JPDL3Metadata {
039        private String pdName;
040        private JPDL3StartStateMetadata jPDL3StartStateMetadata;
041        private JPDL3EndStateMetadata jPDL3EndStateMetadata;
042    
043        
044        private JPDL3Metadata() {
045            // prevent construction
046        }
047    
048        /**
049         * Create an instance of {@link JPDL3Metadata} with all data of a specific jpdl xml document.
050         * 
051         * @param stream - the {@link InputStream}, that represents a stream of jpdl.
052         * @return a object of {@link JPDL3Metadata}.
053         */
054        @SuppressWarnings( {"unchecked", "cast"} )
055        public static JPDL3Metadata instance( InputStream stream ) {
056            ProcessDefinition processDefinition = ProcessDefinition.parseXmlInputStream(stream);
057            if (processDefinition != null) {
058                JPDL3Metadata jplMetadata = new JPDL3Metadata();
059                if (processDefinition.getName() != null) {
060                    jplMetadata.setPdName(processDefinition.getName());
061                }
062                List<Node> nodes = (List<Node>)processDefinition.getNodes();
063                for (Node node : nodes) {
064                    if (node instanceof StartState) {
065                        StartState startState = (StartState)node;
066                        JPDL3StartStateMetadata jPDL3StartStateMetadata = new JPDL3StartStateMetadata();
067                        if (startState.getName() != null) {
068                            jPDL3StartStateMetadata.setName(startState.getName());
069                        }
070                        List<JPDL3TransitionMetadata> transitions = new ArrayList<JPDL3TransitionMetadata>();
071                        for (Transition transition : (List<Transition>)startState.getLeavingTransitions()) {
072                            JPDL3TransitionMetadata jPDL3TransitionMetadata = new JPDL3TransitionMetadata();
073                            if (transition.getName() != null) {
074                                jPDL3TransitionMetadata.setName(transition.getName());
075                            }
076                            Node toNode = transition.getTo();
077                            if (toNode != null) {
078                                jPDL3TransitionMetadata.setTo(toNode.getName());
079                            }
080                            transitions.add(jPDL3TransitionMetadata);
081                        }
082                        jPDL3StartStateMetadata.setTransitions(transitions);
083                        jplMetadata.setStartStateMetadata(jPDL3StartStateMetadata);
084                    }
085    
086                    if (node instanceof EndState) {
087                        EndState endState = (EndState)node;
088                        JPDL3EndStateMetadata jPDL3EndStateMetadata = new JPDL3EndStateMetadata();
089                        if (endState.getName() != null) {
090                            jPDL3EndStateMetadata.setName(endState.getName());
091                        }
092                        jplMetadata.setEndStateMetadata(jPDL3EndStateMetadata);
093                    }
094                }
095                return jplMetadata;
096            }
097            return null;
098        }
099    
100        /**
101         * Get the name of process definition.
102         * 
103         * @return the name of the process definition.
104         */
105        public String getPdName() {
106            return pdName;
107        }
108    
109        /**
110         * Set the name of process definition.
111         * 
112         * @param pdName - the name of process definition.
113         */
114        public void setPdName( String pdName ) {
115            this.pdName = pdName;
116        }
117    
118        /**
119         * @return the jPDL3StartStateMetadata.
120         */
121        public JPDL3StartStateMetadata getStartStateMetadata() {
122            return jPDL3StartStateMetadata;
123        }
124        
125        /**
126         * 
127         * @return the jPDL3EndStateMetadata.
128         */
129        public JPDL3EndStateMetadata getEndStateMetadata() {
130            return jPDL3EndStateMetadata;
131        }
132        
133        
134        /**
135         * @param jPDL3StartStateMetadata the jPDL3StartStateMetadata to set
136         */
137        public void setStartStateMetadata( JPDL3StartStateMetadata jPDL3StartStateMetadata ) {
138            this.jPDL3StartStateMetadata = jPDL3StartStateMetadata;
139        }
140    
141        /**
142         * @param jPDL3EndStateMetadata the jPDL3EndStateMetadata to set
143         */
144        public void setEndStateMetadata( JPDL3EndStateMetadata jPDL3EndStateMetadata ) {
145            this.jPDL3EndStateMetadata = jPDL3EndStateMetadata;
146        }
147    }