KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > plugin > PluginParameterException


1 package org.apache.maven.plugin;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.plugin.descriptor.MojoDescriptor;
20 import org.apache.maven.plugin.descriptor.Parameter;
21 import org.codehaus.plexus.util.StringUtils;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 public class PluginParameterException
27     extends PluginConfigurationException
28 {
29
30     private final List JavaDoc parameters;
31
32     private final MojoDescriptor mojo;
33
34     public PluginParameterException( MojoDescriptor mojo, List JavaDoc parameters )
35     {
36         super( mojo.getPluginDescriptor(),
37                "Invalid or missing parameters: " + parameters + " for mojo: " + mojo.getRoleHint() );
38
39         this.mojo = mojo;
40
41         this.parameters = parameters;
42     }
43
44     public PluginParameterException( MojoDescriptor mojo, List JavaDoc parameters, Throwable JavaDoc cause )
45     {
46         super( mojo.getPluginDescriptor(),
47                "Invalid or missing parameters: " + parameters + " for mojo: " + mojo.getRoleHint(), cause );
48
49         this.mojo = mojo;
50
51         this.parameters = parameters;
52     }
53
54     public MojoDescriptor getMojoDescriptor()
55     {
56         return mojo;
57     }
58
59     public List JavaDoc getParameters()
60     {
61         return parameters;
62     }
63
64     private static void decomposeParameterIntoUserInstructions( MojoDescriptor mojo, Parameter param,
65                                                                 StringBuffer JavaDoc messageBuffer )
66     {
67         String JavaDoc expression = param.getExpression();
68
69         if ( param.isEditable() )
70         {
71             messageBuffer.append( "inside the definition for plugin: \'" + mojo.getPluginDescriptor().getArtifactId() +
72                 "\'specify the following:\n\n<configuration>\n ...\n <" + param.getName() + ">VALUE</" +
73                 param.getName() + ">\n</configuration>" );
74
75             String JavaDoc alias = param.getAlias();
76             if ( StringUtils.isNotEmpty( alias ) && !alias.equals( param.getName() ) )
77             {
78                 messageBuffer.append(
79                     "\n\n-OR-\n\n<configuration>\n ...\n <" + alias + ">VALUE</" + alias + ">\n</configuration>\n" );
80             }
81         }
82
83         if ( StringUtils.isEmpty( expression ) )
84         {
85             messageBuffer.append( "." );
86         }
87         else
88         {
89             if ( param.isEditable() )
90             {
91                 messageBuffer.append( "\n\n-OR-\n\n" );
92             }
93
94             addParameterUsageInfo( expression, messageBuffer );
95         }
96     }
97
98     public String JavaDoc buildDiagnosticMessage()
99     {
100         StringBuffer JavaDoc messageBuffer = new StringBuffer JavaDoc();
101
102         List JavaDoc params = getParameters();
103         MojoDescriptor mojo = getMojoDescriptor();
104
105         messageBuffer.append( "One or more required plugin parameters are invalid/missing for \'" )
106             .append( mojo.getPluginDescriptor().getGoalPrefix() ).append( ":" ).append( mojo.getGoal() )
107             .append( "\'\n" );
108
109         int idx = 0;
110         for ( Iterator JavaDoc it = params.iterator(); it.hasNext(); idx++ )
111         {
112             Parameter param = (Parameter) it.next();
113
114             messageBuffer.append( "\n[" ).append( idx ).append( "] " );
115
116             decomposeParameterIntoUserInstructions( mojo, param, messageBuffer );
117
118             messageBuffer.append( "\n" );
119         }
120
121         return messageBuffer.toString();
122     }
123 }
124
Popular Tags