001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 * $Id: LifeCycleStateConfiguration.java 16207 2007-04-15 11:56:45Z sfermigier $
012 */
013
014package org.nuxeo.ecm.core.lifecycle.extensions;
015
016import java.util.ArrayList;
017import java.util.Collection;
018
019import org.nuxeo.ecm.core.lifecycle.LifeCycleState;
020import org.nuxeo.ecm.core.lifecycle.impl.LifeCycleStateImpl;
021import org.w3c.dom.Element;
022import org.w3c.dom.NodeList;
023
024/**
025 * Configuration helper class for state configuration.
026 *
027 * @see org.nuxeo.ecm.core.lifecycle.extensions.LifeCycleDescriptor
028 * @see org.nuxeo.ecm.core.lifecycle.LifeCycleState
029 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
030 */
031public class LifeCycleStateConfiguration {
032
033    private static final String TAG_STATE = "state";
034
035    private static final String TAG_TRANSITION = "transition";
036
037    private static final String ATTR_STATE_NAME = "name";
038
039    private static final String ATTR_STATE_DESCRIPTION = "description";
040
041    private static final String ATTR_STATE_INITIAL = "initial";
042
043    /** The DOM element holding the states. */
044    private final Element element;
045
046    public LifeCycleStateConfiguration(Element element) {
047        this.element = element;
048    }
049
050    private static Collection<String> getAllowedTransitionsFor(Element element) {
051        Collection<String> transitions = new ArrayList<String>();
052        NodeList elements = element.getElementsByTagName(TAG_TRANSITION);
053        int len = elements.getLength();
054        for (int i = 0; i < len; i++) {
055            Element elt = (Element) elements.item(i);
056            transitions.add(elt.getTextContent());
057        }
058        return transitions;
059    }
060
061    public Collection<LifeCycleState> getStates() {
062        Collection<LifeCycleState> states = new ArrayList<LifeCycleState>();
063        NodeList elements = element.getElementsByTagName(TAG_STATE);
064        int len = elements.getLength();
065        for (int i = 0; i < len; i++) {
066            Element element = (Element) elements.item(i);
067            states.add(new LifeCycleStateImpl(element.getAttribute(ATTR_STATE_NAME),
068                    element.getAttribute(ATTR_STATE_DESCRIPTION), getAllowedTransitionsFor(element),
069                    Boolean.valueOf(element.getAttribute(ATTR_STATE_INITIAL))));
070        }
071        return states;
072    }
073
074}