|
| 1 | +// $Id:$ |
| 2 | +// $Id$ |
| 3 | +/* |
| 4 | +* JBoss, Home of Professional Open Source |
| 5 | +* Copyright 2008, Red Hat Middleware LLC, and individual contributors |
| 6 | +* by the @authors tag. See the copyright.txt in the distribution for a |
| 7 | +* full listing of individual contributors. |
| 8 | +* |
| 9 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | +* you may not use this file except in compliance with the License. |
| 11 | +* You may obtain a copy of the License at |
| 12 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | +package org.hibernate.javadoc; |
| 20 | + |
| 21 | +import java.io.BufferedWriter; |
| 22 | +import java.io.File; |
| 23 | +import java.io.FileWriter; |
| 24 | +import java.io.IOException; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import com.sun.javadoc.ClassDoc; |
| 30 | +import com.sun.javadoc.DocErrorReporter; |
| 31 | +import com.sun.javadoc.MethodDoc; |
| 32 | +import com.sun.javadoc.RootDoc; |
| 33 | +import com.sun.javadoc.Tag; |
| 34 | +import com.sun.tools.doclets.standard.Standard; |
| 35 | + |
| 36 | +/** |
| 37 | + * This doclet writes a report for all junit tests marked documented with <i>jsr</i>. |
| 38 | + * Tests documented with this tag are referencing sections of the Bean Validation spec they |
| 39 | + * are testing/validating. |
| 40 | + * |
| 41 | + * @author Hardy Ferentschik |
| 42 | + */ |
| 43 | +public class JSRDoclet { |
| 44 | + |
| 45 | + public static final String TAG_NAME = "jsr"; |
| 46 | + private static final String[] tableHeaders = new String[] { "Bean Validation Specification", "Class", "Method" }; |
| 47 | + private static StringBuffer out = new StringBuffer(); |
| 48 | + |
| 49 | + |
| 50 | + public static boolean start(RootDoc root) { |
| 51 | + String outDirName = readOptions( root.options() ); |
| 52 | + File outDir = new File( outDirName ); |
| 53 | + outDir.mkdirs(); |
| 54 | + File specCheckReport = new File( outDir, "index.html" ); |
| 55 | + |
| 56 | + List<JSRReference> references = processClasses( root.classes() ); |
| 57 | + Collections.sort( references ); |
| 58 | + |
| 59 | + writeHeader(); |
| 60 | + writeContents( references ); |
| 61 | + writeFooter(); |
| 62 | + |
| 63 | + |
| 64 | + try { |
| 65 | + BufferedWriter writer = new BufferedWriter( new FileWriter( specCheckReport ) ); |
| 66 | + writer.write( out.toString() ); |
| 67 | + writer.close(); |
| 68 | + } |
| 69 | + catch ( IOException e ) { |
| 70 | + System.err.println( "Error writing tck report." ); |
| 71 | + } |
| 72 | + return true; |
| 73 | + } |
| 74 | + |
| 75 | + public static int optionLength(String option) { |
| 76 | + if ( option.equals( "-d" ) ) { |
| 77 | + return 2; |
| 78 | + } |
| 79 | + else { |
| 80 | + return Standard.optionLength( option ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public static boolean validOptions(String options[][], DocErrorReporter reporter) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + private static void writeFooter() { |
| 89 | + out.append( "</body></html>" ); |
| 90 | + } |
| 91 | + |
| 92 | + private static void writeHeader() { |
| 93 | + out.append( "<html><head></head><body>" ); |
| 94 | + } |
| 95 | + |
| 96 | + private static void writeTableHeader() { |
| 97 | + out.append( "<table border=\"1\"><tr>" ); |
| 98 | + for ( String s : tableHeaders ) { |
| 99 | + out.append( "<th>" ).append( s ).append( "</th>" ); |
| 100 | + } |
| 101 | + out.append( "</tr>" ); |
| 102 | + } |
| 103 | + |
| 104 | + private static void writeTableFooter() { |
| 105 | + out.append( "</table>" ); |
| 106 | + } |
| 107 | + |
| 108 | + private static String readOptions(String[][] options) { |
| 109 | + String tagName = null; |
| 110 | + for ( String[] opt : options ) { |
| 111 | + if ( opt[0].equals( "-d" ) ) { |
| 112 | + tagName = opt[1]; |
| 113 | + } |
| 114 | + } |
| 115 | + return tagName; |
| 116 | + } |
| 117 | + |
| 118 | + private static void writeContents(List<JSRReference> references) { |
| 119 | + writeTableHeader(); |
| 120 | + for ( JSRReference reference : references ) { |
| 121 | + out.append( "<tr>" ); |
| 122 | + out.append( "<td>" ).append( reference.jsrReference ).append( "</td>" ); |
| 123 | + out.append( "<td><a href=\"" ) |
| 124 | + .append( reference.getSourceLink() ) |
| 125 | + .append( "\">" ) |
| 126 | + .append( reference.className ) |
| 127 | + .append( "</a></td>" ); |
| 128 | + out.append( "<td>" ).append( reference.methodName ).append( "</td>" ); |
| 129 | + out.append( "</tr>" ); |
| 130 | + } |
| 131 | + writeTableFooter(); |
| 132 | + } |
| 133 | + |
| 134 | + private static List<JSRReference> processClasses(ClassDoc[] classDocs) { |
| 135 | + ArrayList<JSRReference> references = new ArrayList<JSRReference>(); |
| 136 | + for ( ClassDoc aClass : classDocs ) { |
| 137 | + MethodDoc[] methods = aClass.methods(); |
| 138 | + for ( MethodDoc method : methods ) { |
| 139 | + Tag[] tags = method.tags( TAG_NAME ); |
| 140 | + if ( tags.length > 0 ) { |
| 141 | + for ( Tag tag : tags ) { |
| 142 | + JSRReference reference = new JSRReference( tag.text(), aClass.qualifiedName(), method.name() ); |
| 143 | + references.add( reference ); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + return references; |
| 149 | + } |
| 150 | + |
| 151 | + static class JSRReference implements Comparable { |
| 152 | + /** |
| 153 | + * The JSR 303 section this instance references. |
| 154 | + */ |
| 155 | + String jsrReference; |
| 156 | + |
| 157 | + /** |
| 158 | + * The name of the class which references the JSR. |
| 159 | + */ |
| 160 | + String className; |
| 161 | + |
| 162 | + /** |
| 163 | + * The method which references the JSR. |
| 164 | + */ |
| 165 | + String methodName; |
| 166 | + |
| 167 | + /** |
| 168 | + * @todo Add some validation |
| 169 | + */ |
| 170 | + JSRReference(String reference, String className, String methodName) { |
| 171 | + this.jsrReference = reference; |
| 172 | + this.className = className; |
| 173 | + this.methodName = methodName; |
| 174 | + } |
| 175 | + |
| 176 | + public String getSourceLink() { |
| 177 | + StringBuilder builder = new StringBuilder(); |
| 178 | + builder.append( "../xref-test/" ); |
| 179 | + builder.append( className.replace( '.', '/' ) ); |
| 180 | + builder.append( ".html" ); |
| 181 | + return builder.toString(); |
| 182 | + } |
| 183 | + |
| 184 | + public int compareTo(Object o) { |
| 185 | + return jsrReference.compareTo( ( ( JSRReference ) o ).jsrReference ); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments