1 package org.apache.maven.werkz.jelly; 2 3 /* ==================================================================== 4 * Licensed to the Apache Software Foundation (ASF) under one or more 5 * contributor license agreements. See the NOTICE file distributed with 6 * this work for additional information regarding copyright ownership. 7 * The ASF licenses this file to You under the Apache License, Version 2.0 8 * (the "License"); you may not use this file except in compliance with 9 * the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 * ==================================================================== 19 */ 20 21 /* 22 $Id: UseGoalsTag.java 517014 2007-03-11 21:15:50Z ltheussl $ 23 24 Copyright 2002 (C) The Werken Company. All Rights Reserved. 25 26 Redistribution and use of this software and associated documentation 27 ("Software"), with or without modification, are permitted provided 28 that the following conditions are met: 29 30 1. Redistributions of source code must retain copyright 31 statements and notices. Redistributions must also contain a 32 copy of this document. 33 34 2. Redistributions in binary form must reproduce the 35 above copyright notice, this list of conditions and the 36 following disclaimer in the documentation and/or other 37 materials provided with the distribution. 38 39 3. The name "werkz" must not be used to endorse or promote 40 products derived from this Software without prior written 41 permission of The Werken Company. For written permission, 42 please contact bob@werken.com. 43 44 4. Products derived from this Software may not be called "werkz" 45 nor may "werkz" appear in their names without prior written 46 permission of The Werken Company. "werkz" is a registered 47 trademark of The Werken Company. 48 49 5. Due credit should be given to "the werkz project" 50 ( http://werkz.werken.com/ ). 51 52 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 53 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 54 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 55 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 56 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 57 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 58 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 59 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 60 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 61 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 62 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 63 OF THE POSSIBILITY OF SUCH DAMAGE. 64 65 */ 66 67 import java.util.Iterator; 68 import java.util.Map; 69 import java.util.TreeMap; 70 71 import org.apache.commons.jelly.JellyTagException; 72 import org.apache.commons.jelly.MissingAttributeException; 73 import org.apache.commons.jelly.XMLOutput; 74 import org.apache.maven.werkz.Goal; 75 76 /** 77 * This tag outputs a sorted Map of Maps all of the goals, indexed by their prefix and their 78 * goal name. This is output to a variable. This map of maps makes it easy to navigate the 79 * available Goals. 80 * <p> 81 * So if the goals is output to a variable called 'g' then you can access a specific goal via 82 * a Jexl expression ${g.java.compile} or to find all the 'java' goals you can use ${g.java} which 83 * returns a sorted Map. 84 * 85 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> 86 * @version $Revision: 1.2 $ 87 */ 88 public class UseGoalsTag 89 extends WerkzTagSupport 90 { 91 /** the name of the variable to export */ 92 private String var; 93 94 public UseGoalsTag() 95 { 96 } 97 98 // Tag interface 99 //------------------------------------------------------------------------- 100 101 /** 102 * Evaluate the body to register all the various goals and pre/post conditions 103 * then run all the current targets 104 */ 105 public void doTag( final XMLOutput output ) 106 throws JellyTagException 107 { 108 if ( var == null ) 109 { 110 throw new MissingAttributeException( "var" ); 111 } 112 113 Map answer = createMap(); 114 115 Iterator iter = getProject().getGoals().iterator(); 116 while ( iter.hasNext() ) 117 { 118 Goal goal = (Goal) iter.next(); 119 String name = goal.getName(); 120 String prefix = name; 121 int idx = name.indexOf( ":" ); 122 if ( idx >= 0 ) 123 { 124 prefix = name.substring( 0, idx ); 125 name = name.substring( idx + 1 ); 126 } 127 else 128 { 129 name = "[default]"; 130 } 131 132 Map map = (Map) answer.get( prefix ); 133 if ( map == null ) 134 { 135 map = createMap(); 136 answer.put( prefix, map ); 137 } 138 map.put( name, goal ); 139 } 140 141 context.setVariable( var, answer ); 142 } 143 144 // Properties 145 //------------------------------------------------------------------------- 146 /** 147 * Sets the variable for which the Map of Map of goals will be exported 148 */ 149 public void setVar( String var ) 150 { 151 this.var = var; 152 } 153 154 /** 155 * Factory method to create a new sorted map 156 */ 157 protected Map createMap() 158 { 159 return new TreeMap(); 160 } 161 }