View Javadoc

1   /*
2    * Copyright 2007-2011 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.exceptions.report;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.apache.commons.lang.ObjectUtils;
23  import org.apache.commons.lang.StringUtils;
24  import org.apache.maven.doxia.sink.Sink;
25  
26  import com.sun.javadoc.ClassDoc;
27  import com.sun.javadoc.FieldDoc;
28  
29  import de.smartics.exceptions.code.NumberCode;
30  import de.smartics.exceptions.core.Code;
31  import de.smartics.exceptions.report.sort.CodeUtils;
32  import de.smartics.exceptions.report.sort.MixedCodeComparator;
33  import de.smartics.exceptions.report.sort.CodeUtils.CodeContainer;
34  import de.smartics.maven.exceptions.AbstractSinkReportGenerator;
35  import de.smartics.report.conf.ProjectConfiguration;
36  import de.smartics.report.conf.ReportData;
37  import de.smartics.report.util.JavadocUtils;
38  
39  /**
40   * Simple generator that generates with a Maven sink. This interfaces as Java
41   * 1.4 code.
42   *
43   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
44   * @version $Revision:591 $
45   */
46  public class CodeSortedSinkReportGenerator extends AbstractSinkReportGenerator
47  {
48    // ********************************* Fields *********************************
49  
50    // --- constants ------------------------------------------------------------
51  
52    // --- members --------------------------------------------------------------
53  
54    // ****************************** Initializer *******************************
55  
56    // ****************************** Constructors ******************************
57  
58    /**
59     * Default constructor.
60     */
61    public CodeSortedSinkReportGenerator()
62    {
63    }
64  
65    // ****************************** Inner Classes *****************************
66  
67    // ********************************* Methods ********************************
68  
69    // --- init -----------------------------------------------------------------
70  
71    // --- get&set --------------------------------------------------------------
72  
73    // --- business -------------------------------------------------------------
74  
75    /**
76     * Writes the content of the report data to the stream.
77     *
78     * @param output the writer to write to.
79     * @param config the project configuration to control the output.
80     * @param reportData the report information to write.
81     * @throws Exception on any problem encountered.
82     */
83    protected void write(final Object output, final ProjectConfiguration config,
84        final ReportData reportData) throws Exception // NOPMD
85    {
86      final Sink sink = toSink(output);
87      final Collection javadocs = reportData.getJavadocs();
88  
89      final List codeContainerList =
90          CodeUtils.getCodeList(config, javadocs, new MixedCodeComparator());
91  
92      writeHeader(output, config);
93      String currentComponentId = null;
94      String currentCodeMajorNumber = null;
95      boolean firstTime = true;
96      for (final Iterator i = codeContainerList.iterator(); i.hasNext();)
97      {
98        boolean headerChanged = false;
99        final CodeContainer codeContainer = (CodeContainer) i.next();
100       final ClassDoc classDoc = codeContainer.getClassDoc();
101       final FieldDoc fieldDoc = codeContainer.getFieldDoc();
102       final Code code = codeContainer.getCode();
103 
104       final String componentId = code.getComponentId();
105       if (!ObjectUtils.equals(currentComponentId, componentId))
106       {
107         if (!firstTime)
108         {
109           writeInfoSubFooter(output, config);
110           writeInfoFooter(output, config);
111         }
112         firstTime = false;
113         writeInfoHeader(output, config, componentId, classDoc);
114         currentComponentId = componentId;
115         headerChanged = true;
116       }
117 
118       if (code instanceof NumberCode)
119       {
120         final NumberCode numberCode = (NumberCode) code;
121         final String codeMajorNumber =
122             String.valueOf(numberCode.getMajorNumber());
123         if (!ObjectUtils.equals(currentCodeMajorNumber, codeMajorNumber))
124         {
125           if (!headerChanged)
126           {
127             writeInfoSubFooter(output, config);
128           }
129           writeInfoSubHeader(output, config, codeMajorNumber);
130           currentCodeMajorNumber = codeMajorNumber;
131           headerChanged = true;
132         }
133       }
134       else if (currentCodeMajorNumber != null)
135       {
136         currentCodeMajorNumber = null; // NOPMD
137         if (!headerChanged)
138         {
139           writeInfoSubHeader(output, config, "-----");
140           headerChanged = true;
141         }
142       }
143 
144       if (headerChanged)
145       {
146         final String relDir = config.getJavadocRelativeDir();
147         if (relDir != null)
148         {
149           sink.paragraph();
150 
151           final String className = classDoc.qualifiedName();
152           final String path = JavadocUtils.expandJavadocUrl(relDir, className);
153           sink.link(path);
154           sink.text(className);
155           sink.link_();
156           sink.paragraph_();
157         }
158 
159         final String message = classDoc.commentText();
160         if (!StringUtils.isBlank(message))
161         {
162           sink.paragraph();
163           sink.text(message);
164           sink.paragraph_();
165         }
166 
167         writeTableHeader(output, config);
168       }
169 
170       writeReportElementInfo(output, config, fieldDoc);
171     }
172 
173     writeInfoSubFooter(output, config);
174     writeInfoFooter(output, config);
175     writeFooter(output, config);
176   }
177 
178   // --- object basics --------------------------------------------------------
179 
180 }