View Javadoc

1   package org.apache.maven.plugin.pmd;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.codehaus.plexus.util.xml.pull.XmlPullParser;
29  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
30  
31  /**
32   * Fail the build if there were any CPD violations in the source code.
33   *
34   * @since 2.0
35   * @version $Id: CpdViolationCheckMojo.html 816688 2012-05-08 15:14:44Z hboutemy $
36   * @goal cpd-check
37   * @phase verify
38   * @execute goal="cpd"
39   */
40  public class CpdViolationCheckMojo
41      extends AbstractPmdViolationCheckMojo
42  {
43  
44      /**
45       * Skip the CPD violation checks.  Most useful on the command line
46       * via "-Dcpd.skip=true".
47       *
48       * @parameter expression="${cpd.skip}" default-value="false"
49       */
50      private boolean skip;
51  
52      /** {@inheritDoc} */
53      public void execute()
54          throws MojoExecutionException, MojoFailureException
55      {
56          if ( !skip )
57          {
58              executeCheck( "cpd.xml", "duplication", "CPD duplication", 10 );
59          }
60      }
61  
62      /** {@inheritDoc} */
63      protected void printError( Map item, String severity )
64      {
65          String lines = (String) item.get( "lines" );
66  
67  
68          StringBuffer buff = new StringBuffer( 100 );
69          buff.append( "CPD " + severity + ": Found " );
70          buff.append( lines ).append( " lines of duplicated code at locations:" );
71          this.getLog().info( buff.toString() );
72  
73          buff.setLength( 0 );
74          buff.append( "    " );
75          Map file = (Map) item.get( "file" );
76          buff.append( file.get( "path" ) );
77          buff.append( " line " ).append( file.get( "line" ) );
78          this.getLog().info( buff.toString() );
79  
80          buff.setLength( 0 );
81          buff.append( "    " );
82          file = (Map) item.get( "file1" );
83          buff.append( file.get( "path" ) );
84          buff.append( " line " ).append( file.get( "line" ) );
85          this.getLog().info( buff.toString() );
86  
87          Map codefrag = (Map) item.get( "codefragment" );
88          String codefragstr = (String) codefrag.get( "text" );
89          this.getLog().debug( "CPD " + severity + ": Code Fragment " );
90          this.getLog().debug( codefragstr );
91      }
92  
93      /** {@inheritDoc} */
94      protected Map getErrorDetails( XmlPullParser xpp )
95          throws XmlPullParserException, IOException
96      {
97          int index = 0;
98          int attributeCount = 0;
99          HashMap msgs = new HashMap();
100 
101         attributeCount = xpp.getAttributeCount();
102         while ( index < attributeCount )
103         {
104             msgs.put( xpp.getAttributeName( index ), xpp.getAttributeValue( index ) );
105 
106             index++;
107         }
108 
109         int tp = xpp.next();
110         while ( tp != XmlPullParser.END_TAG )
111         {
112             // get the tag's text
113             switch ( tp )
114             {
115             case XmlPullParser.TEXT:
116                 msgs.put( "text", xpp.getText().trim() );
117                 break;
118             case XmlPullParser.START_TAG:
119                 {
120                     String nm = xpp.getName();
121                     if ( msgs.containsKey( nm ) )
122                     {
123                         int cnt = 1;
124                         while ( msgs.containsKey( nm + cnt ) )
125                         {
126                             ++cnt;
127                         }
128                         nm = nm + cnt;
129                     }
130                     msgs.put( nm, getErrorDetails( xpp ) );
131                     break;
132                 }
133             default:
134             }
135             tp = xpp.next();
136         }
137         return msgs;
138     }
139 }