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