001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 * $Id: LifeCycleStateConfiguration.java 16207 2007-04-15 11:56:45Z sfermigier $
019 */
020
021package org.nuxeo.ecm.core.lifecycle.extensions;
022
023import java.util.ArrayList;
024import java.util.Collection;
025
026import org.nuxeo.ecm.core.lifecycle.LifeCycleState;
027import org.nuxeo.ecm.core.lifecycle.impl.LifeCycleStateImpl;
028import org.w3c.dom.Element;
029import org.w3c.dom.NodeList;
030
031/**
032 * Configuration helper class for state configuration.
033 *
034 * @see org.nuxeo.ecm.core.lifecycle.extensions.LifeCycleDescriptor
035 * @see org.nuxeo.ecm.core.lifecycle.LifeCycleState
036 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
037 */
038public class LifeCycleStateConfiguration {
039
040    private static final String TAG_STATE = "state";
041
042    private static final String TAG_TRANSITION = "transition";
043
044    private static final String ATTR_STATE_NAME = "name";
045
046    private static final String ATTR_STATE_DESCRIPTION = "description";
047
048    private static final String ATTR_STATE_INITIAL = "initial";
049
050    /** The DOM element holding the states. */
051    private final Element element;
052
053    public LifeCycleStateConfiguration(Element element) {
054        this.element = element;
055    }
056
057    private static Collection<String> getAllowedTransitionsFor(Element element) {
058        Collection<String> transitions = new ArrayList<String>();
059        NodeList elements = element.getElementsByTagName(TAG_TRANSITION);
060        int len = elements.getLength();
061        for (int i = 0; i < len; i++) {
062            Element elt = (Element) elements.item(i);
063            transitions.add(elt.getTextContent());
064        }
065        return transitions;
066    }
067
068    public Collection<LifeCycleState> getStates() {
069        Collection<LifeCycleState> states = new ArrayList<LifeCycleState>();
070        NodeList elements = element.getElementsByTagName(TAG_STATE);
071        int len = elements.getLength();
072        for (int i = 0; i < len; i++) {
073            Element element = (Element) elements.item(i);
074            states.add(new LifeCycleStateImpl(element.getAttribute(ATTR_STATE_NAME),
075                    element.getAttribute(ATTR_STATE_DESCRIPTION), getAllowedTransitionsFor(element),
076                    Boolean.valueOf(element.getAttribute(ATTR_STATE_INITIAL))));
077        }
078        return states;
079    }
080
081}