Project Report: fawkez

Packagesummary org.jcoderz.guidelines

org.jcoderz.guidelines.JavaCodeSnippets

LineHitsNoteSource
1  /*
2   * $Id: JavaCodeSnippets.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.guidelines;
34  
35  
36  import java.io.BufferedReader;
37  import java.io.File;
38  import java.io.FileNotFoundException;
39  import java.io.FileOutputStream;
40  import java.io.FileReader;
41  import java.io.FilenameFilter;
42  import java.io.IOException;
43  import java.io.PrintStream;
44  
45  import org.jcoderz.commons.util.XmlUtil;
46  
47  
48  /**
49   * Cuts code samples out of java classes.
50   *
51   * @author mgriffel
52   */
530(1)public final class JavaCodeSnippets
54  {
55     private static final String JAVA_CODE_SNIPPET_XML
56             = "code-snippet-catalog.xml";
570    private static final String NEWLINE = System.getProperty("line.separator");
58     private static final String BEGIN_SNIPPET_TAG = "BEGIN SNIPPET:";
59     private static final int NUMBER_OF_ARGUMENTS = 2;
60  
61     /**
62      * Main method.
63      * @param args program args.
64      * @throws IOException File not found, read error.
65      */
66     public static void main (String[] args) throws IOException
67     {
68        try
69        {
700          if (args.length != NUMBER_OF_ARGUMENTS)
71           {
720             usage ();
73           }
74  
750          final JavaCodeSnippets jcs = new JavaCodeSnippets();
76        
770          final File srcDir = new File(args[0]);
780          final File outDir = new File(args[1]);
79        
800          if (!srcDir.isDirectory())
81           {
820             throw new RuntimeException("The srcDir '" + srcDir
83                    + "'is not a valid directory");
84           }
85  
860          if (!outDir.isDirectory())
87           {
880             throw new RuntimeException("The outDir '" + outDir
89                    + "'is not a valid directory");
90           }
91           
920          jcs.generateSnippets(srcDir, outDir);
930          jcs.writeCodeSnippetCatalog (outDir);
94        }
950       catch (Exception e)
96        {
970          e.printStackTrace();
980          System.exit(-1);
990       }
1000    }
101  
102     private void generateSnippets (File srcDir, File outDir)
103        throws FileNotFoundException, IOException
104     {
1050       final File[] files = srcDir.listFiles(new FilenameFilter()
1060(2)      {
107           public boolean accept (File dir, String name)
108           {
1090             boolean ret = false;
1100             if (name.endsWith(".java"))
111              {
1120                ret = true;
113              }
1140             return ret;
115           }
116        });
117        
1180       for (int i = 0; i < files.length; i++)
119        {
1200          System.out.println("Processing file '" + files[i].getName() + "'");
1210          generateSnippet(files[i], outDir);
122        }
1230    }
124  
125     private void generateSnippet (File src, File outDir)
126        throws FileNotFoundException, IOException
127     {
1280(3)      final BufferedReader in = new BufferedReader(new FileReader(src));
1290       String line = null;
1300       int lineno = 0;
1310       int indent = 0;
1320       boolean inSnippetCode = false;
1330       final StringBuffer snippet = new StringBuffer();
1340       String snippetFilename = null;
135  
1360       while ((line = in.readLine()) != null)
137        {
1380          ++lineno;
1390          if (line.matches(".*// " + BEGIN_SNIPPET_TAG + " [a-zA-Z0-9\\.]*[ ]*"))
140           {
1410             inSnippetCode = true;
1420             indent = line.indexOf("//") != -1 ? line.indexOf("//") : 0;
1430             snippetFilename = line.substring(
144                    line.indexOf(BEGIN_SNIPPET_TAG)
145                       + BEGIN_SNIPPET_TAG.length()).trim();
1460             continue;
147           }
1480          else if (inSnippetCode && line.matches(".*// PAUSE SNIPPET.*"))
149           {
1500             inSnippetCode = false;
151           }
1520          else if (!inSnippetCode && line.matches(".*// RESUME SNIPPET.*"))
153           {
1540             inSnippetCode = true;
1550             continue;
156           }
1570          else if (inSnippetCode && line.matches(".*// END SNIPPET.*"))
158           {
1590             inSnippetCode = false;
1600             final String codeSnippet = snippet.toString();
1610             System.out.println("### writing snippet: " + snippetFilename
162                    + " indent: " + indent);
1630             final File outFilename = new File(outDir, snippetFilename);
1640(4)(5)            final FileOutputStream out
165                    = new FileOutputStream(outFilename);
166  
1670             out.write(codeSnippet.getBytes());
1680             out.close();
169  
1700             snippet.setLength(0);
171           }
172           
1730          if (inSnippetCode)
174           {
1750             if (line.length() > indent)
176              {
1770                line = line.substring(indent);
178              }
1790             snippet.append(XmlUtil.escape(line + NEWLINE));
180           }
181        }
1820       in.close();
1830    }
184  
185     /**
186      * @param codeSnippetList
187    * @param outDir
188    */
189     private void writeCodeSnippetCatalog (File dir)
190           throws IOException
191     {
1920       final File[] files = dir.listFiles(new FilenameFilter()
1930(6)            {
194           /** {@inheritDoc} */
195           public boolean accept (File dir, String name)
196           {
1970             boolean ret = false;
1980(7)            if (name.endsWith(".xml") && ! name.equals(JAVA_CODE_SNIPPET_XML))
199              {
2000                ret = true;
201              }
2020             return ret;
203           }
204        });
205        
2060       final PrintStream out = new PrintStream(new FileOutputStream(
207                    new File(dir, JAVA_CODE_SNIPPET_XML)));
208  
2090       out.println("<?xml version='1.0' encoding='ISO-8859-1'?>");
2100       out.println();
2110       for (int i = 0; i < files.length; i++)
212        {
2130          final File file = files[i];
2140          final String s = file.getName();
2150          String n = s;
2160          if (s.lastIndexOf('.') != -1)
217           {
2180             n = s.substring(0, s.lastIndexOf('.'));
219           }
2200          out.println("<!ENTITY " + n + " SYSTEM \"" + s + "\">");
221           
222        }
2230       out.close();
2240    }
225  
226     /**
227      * Print usage message
228      */
229     private static void usage ()
230     {
2310       System.out.println("Usage: java JavaCodeSnippets srcDir outDir");
2320(8)      System.exit(-1);
2330    }
234  }

Findings in this File

f (9) Avoid printStackTrace(); use a logger call instead. main class
f (10) System.out.print is used main class
f (11) System.out.print is used main class
f (12) System.out.print is used main class
c (1) 53 : 1 Utility classes should not have a public or default constructor.
i (2) 106 : 0 The class org.jcoderz.guidelines.JavaCodeSnippets$1 could be refactored into a named _static_ inner class
i (3) 128 : 0 org.jcoderz.guidelines.JavaCodeSnippets.generateSnippet(File, File) may fail to close stream on exception
w (4) 164 : 0 Method org.jcoderz.guidelines.JavaCodeSnippets.generateSnippet(File, File) may fail to clean up stream or resource of type java.io.OutputStream
i (5) 164 : 0 org.jcoderz.guidelines.JavaCodeSnippets.generateSnippet(File, File) may fail to close stream on exception
i (6) 193 : 0 The class org.jcoderz.guidelines.JavaCodeSnippets$2 could be refactored into a named _static_ inner class
i (7) 198 : 0 method org.jcoderz.guidelines.JavaCodeSnippets$2.accept(File, String) makes literal string comparisons passing the literal as an argument
i (8) 232 : 0 org.jcoderz.guidelines.JavaCodeSnippets.usage() invokes System.exit(...), which shuts down the entire virtual machine