/** * Copyright 2004 TermNet Merchant Services, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.apache.maven.javadoc; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import org.apache.commons.lang.StringEscapeUtils; import org.apache.commons.lang.StringUtils; /** * Converts the javadoc warnings into an xml file. * * @author Steven Caswell (steve at mungoknotwise dot com) * @version $Id$ */ public class JavadocWarningsTextToXml { private static final int ARGS_LENGTH = 3; private static final int PADDING = 10; /** * Invokes an instance of JavadocWarningsTextToXml from the * command line. The following command line arguments are expected in this * order: * *
    *
  1. input file name
  2. *
  3. output file name
  4. *
  5. output encoding
  6. *
* * @param args the command line arguments * @throws IOException if an exception occurs opening, reading, or writing * files */ public static void main(final String[] args) throws IOException { if(args.length != ARGS_LENGTH) { throw new IllegalArgumentException("Wrong number of arguments"); } JavadocWarningsTextToXml runner = new JavadocWarningsTextToXml(); int i = 0; runner.setInputFileName(args[i++]); runner.setOutputFileName(args[i++]); runner.setOutputEncoding(args[i++]); runner.build(); } private String inputFileName; private String outputFileName; private String outputEncoding; private boolean verbose = false; /** * Constructs a new instance of JavadocAudit. */ public JavadocWarningsTextToXml() { } public void setVerbose(final boolean verbose) { this.verbose = verbose; if(this.verbose()) { System.out.println("verbose is true"); } } /** * Returns the output encoding. * * @return the output encoding */ public String getOutputEncoding() { return this.outputEncoding; } /** * Returns the name of the input file. * * @return the inptu file name */ public String getInputFileName() { return this.inputFileName; } /** * Returns the name of the output file. * * @return the output file name */ public String getOutputFileName() { return this.outputFileName; } /** * Sets the name of the input file. This file is expected to be a report * generated by Javadoc. * * @param inputFileName the input file name */ public void setInputFileName(final String inputFileName) { if(this.verbose()) { System.out.println("Setting input file name: '" + inputFileName + "'"); } this.inputFileName = inputFileName; } /** * Sets the output encoding. * * @param outputEncoding the output encoding */ public void setOutputEncoding(final String outputEncoding) { this.outputEncoding = outputEncoding; } /** * Sets the output file name. * * @param outputFileName the output file name */ public void setOutputFileName(final String outputFileName) { if(this.verbose()) { System.out.println("Setting output file name: '" + outputFileName + "'"); } this.outputFileName = outputFileName; } /** * Builds the xml file from the javadoc report input. * * @throws FileNotFoundException if the input file cannot be opened for reading or the output file cannot be opened for writing * @throws IOException if an error occurs reading or writing * @throws UnsupportedEncodingException if an unknown encoding was specified */ public void build() throws FileNotFoundException, IOException, UnsupportedEncodingException { if(StringUtils.isBlank(this.inputFileName)) { throw new NullPointerException("Input file name must be specified"); } if(StringUtils.isBlank(this.outputFileName)) { throw new NullPointerException("Output file name must be specified"); } String[] lines = this.readInput(); Map fileMap = this.buildMap(lines); this.buildOutput(fileMap); } private void buildOutput(final Map fileMap) throws FileNotFoundException, UnsupportedEncodingException { File output = new File(this.outputFileName); File dir = output.getParentFile(); if(dir != null) { dir.mkdirs(); } PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(output), this.getOutputEncoding())); writeOutput(fileMap, out); out.flush(); out.close(); } private String[] readInput() throws FileNotFoundException, IOException { if(this.verbose()) { System.out.println("Reading '" + this.inputFileName + "'"); } BufferedReader reader = new BufferedReader(new FileReader(this.inputFileName)); List lines = new ArrayList(); String line = null; while( (line = reader.readLine()) != null) { // Look for lines containing the string "warning - " if(line.indexOf("warning - ") >= 0) { lines.add(line); } } reader.close(); if(this.verbose()) { System.out.println("Read " + lines.size() + " line(s) from input file"); } return (String[]) lines.toArray(new String[lines.size()]); } private Map buildMap(final String[] input) { if(this.verbose()) { System.out.println("Building map from " + input.length + " input line(s)"); } Map files = new TreeMap(); for(int i = 0; i < input.length; i++) { String line = input[i]; // Break up line into pieces int javaDocStart = line.indexOf("[javadoc]"); int fileNameStart = javaDocStart += PADDING; int warningStart = line.indexOf("warning - "); int fileNameEnd = warningStart - 1; String fileNameAndLineNumber = line.substring(fileNameStart, fileNameEnd); int lastColon = fileNameAndLineNumber.lastIndexOf(':'); int nextToLastColon = fileNameAndLineNumber.lastIndexOf(':', lastColon - 1); String fileName = fileNameAndLineNumber.substring(0, nextToLastColon); String lineNumber = fileNameAndLineNumber.substring(nextToLastColon + 1, lastColon); int msgStart = warningStart + PADDING; String msg = line.substring(msgStart); // Get the messages for the file Map fileMessages = (Map) files.get(fileName); if(fileMessages == null) { fileMessages = new TreeMap(); files.put(fileName, fileMessages); } // Get the messages for the line Set lineMessages = (Set) fileMessages.get(new Integer(lineNumber)); if(lineMessages == null) { lineMessages = new LinkedHashSet(); fileMessages.put(new Integer(lineNumber), lineMessages); } // Put the message into the line messages set lineMessages.add(msg); } return files; } private void writeOutput(final Map fileMap, final PrintWriter out) { if(this.verbose()) { System.out.println("Writing to output file '" + this.outputFileName); } out.println(""); out.println(""); for(Iterator fileMapIterator = fileMap.keySet().iterator(); fileMapIterator.hasNext();) { String fileName = (String) fileMapIterator.next(); out.println(""); Map fileMessages = (Map) fileMap.get(fileName); for(Iterator fileMessagesIterator = fileMessages.entrySet().iterator(); fileMessagesIterator.hasNext();) { Map.Entry entry = (Map.Entry) fileMessagesIterator.next(); Integer lineNumber = (Integer) entry.getKey(); Set lineMessages = (Set) entry.getValue(); for(Iterator lineMessagesIterator = lineMessages.iterator(); lineMessagesIterator.hasNext();) { String msg = (String) lineMessagesIterator.next(); out.println(""); } } out.println(""); } out.println(""); if(this.verbose()) { System.out.println("Finished writing output file"); } } private boolean verbose() { return this.verbose; } }