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: LifeCycleTransitionConfiguration.java 16046 2007-04-12 14:34:58Z fguillaume $
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.LifeCycleTransition;
020import org.nuxeo.ecm.core.lifecycle.impl.LifeCycleTransitionImpl;
021import org.w3c.dom.Element;
022import org.w3c.dom.NodeList;
023
024/**
025 * Configuration helper class for transition.
026 *
027 * @see org.nuxeo.ecm.core.lifecycle.impl.LifeCycleServiceImpl
028 * @see org.nuxeo.ecm.core.lifecycle.LifeCycleTransition
029 * @author <a href="mailto:ja@nuxeo.com">Julien Anguenot</a>
030 */
031public class LifeCycleTransitionConfiguration {
032
033    private static final String TAG_LIFECYCLE = "lifecycle";
034
035    private static final String TAG_TRANSITIONS = "transitions";
036
037    private static final String TAG_TRANSITION = "transition";
038
039    private static final String ATTR_TRANSITION_NAME = "name";
040
041    private static final String TAG_TRANSITION_DESCRIPTION = "description";
042
043    private static final String ATTR_TRANSITION_DESTINATION_STATE = "destinationState";
044
045    /** The DOM element holding the states. */
046    private final Element element;
047
048    public LifeCycleTransitionConfiguration(Element element) {
049        this.element = element;
050    }
051
052    public Collection<LifeCycleTransition> getTransitions() {
053        Collection<LifeCycleTransition> transitions = new ArrayList<LifeCycleTransition>();
054
055        NodeList transitionsElements = element.getElementsByTagName(TAG_TRANSITIONS);
056        Element transitionsElement = null;
057        if (transitionsElements.getLength() > 0) {
058            // NXP-1472 : don't get the first element, but the first one attached to <lifecycle>
059            for (int i = 0; i < transitionsElements.getLength(); i++) {
060                transitionsElement = (Element) transitionsElements.item(i);
061                if (TAG_LIFECYCLE.equals(transitionsElement.getParentNode().getNodeName())) {
062                    break;
063                }
064            }
065        } else {
066            return transitions;
067        }
068        NodeList elements = transitionsElement.getElementsByTagName(TAG_TRANSITION);
069        int len = elements.getLength();
070        for (int i = 0; i < len; i++) {
071            Element element = (Element) elements.item(i);
072
073            String name = element.getAttribute(ATTR_TRANSITION_NAME);
074            String destinationState = element.getAttribute(ATTR_TRANSITION_DESTINATION_STATE);
075            String description = "";
076
077            if (element.getElementsByTagName(TAG_TRANSITION_DESCRIPTION).getLength() > 0) {
078                description = element.getElementsByTagName(TAG_TRANSITION_DESCRIPTION).item(0).getTextContent();
079            }
080            transitions.add(new LifeCycleTransitionImpl(name, description, destinationState));
081        }
082        return transitions;
083    }
084
085}