| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 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 | */ |
|---|
| 53 | public final class JavaCodeSnippets |
|---|
| 54 | { |
|---|
| 55 | private static final String JAVA_CODE_SNIPPET_XML |
|---|
| 56 | = "code-snippet-catalog.xml"; |
|---|
| 57 | 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 | { |
|---|
| 70 | if (args.length != NUMBER_OF_ARGUMENTS) |
|---|
| 71 | { |
|---|
| 72 | usage (); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | final JavaCodeSnippets jcs = new JavaCodeSnippets(); |
|---|
| 76 | |
|---|
| 77 | final File srcDir = new File(args[0]); |
|---|
| 78 | final File outDir = new File(args[1]); |
|---|
| 79 | |
|---|
| 80 | if (!srcDir.isDirectory()) |
|---|
| 81 | { |
|---|
| 82 | throw new RuntimeException("The srcDir '" + srcDir |
|---|
| 83 | + "'is not a valid directory"); |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | if (!outDir.isDirectory()) |
|---|
| 87 | { |
|---|
| 88 | throw new RuntimeException("The outDir '" + outDir |
|---|
| 89 | + "'is not a valid directory"); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | jcs.generateSnippets(srcDir, outDir); |
|---|
| 93 | jcs.writeCodeSnippetCatalog (outDir); |
|---|
| 94 | } |
|---|
| 95 | catch (Exception e) |
|---|
| 96 | { |
|---|
| 97 | e.printStackTrace(); |
|---|
| 98 | System.exit(-1); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | private void generateSnippets (File srcDir, File outDir) |
|---|
| 103 | throws FileNotFoundException, IOException |
|---|
| 104 | { |
|---|
| 105 | final File[] files = srcDir.listFiles(new FilenameFilter() |
|---|
| 106 | { |
|---|
| 107 | public boolean accept (File dir, String name) |
|---|
| 108 | { |
|---|
| 109 | boolean ret = false; |
|---|
| 110 | if (name.endsWith(".java")) |
|---|
| 111 | { |
|---|
| 112 | ret = true; |
|---|
| 113 | } |
|---|
| 114 | return ret; |
|---|
| 115 | } |
|---|
| 116 | }); |
|---|
| 117 | |
|---|
| 118 | for (int i = 0; i < files.length; i++) |
|---|
| 119 | { |
|---|
| 120 | System.out.println("Processing file '" + files[i].getName() + "'"); |
|---|
| 121 | generateSnippet(files[i], outDir); |
|---|
| 122 | } |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | private void generateSnippet (File src, File outDir) |
|---|
| 126 | throws FileNotFoundException, IOException |
|---|
| 127 | { |
|---|
| 128 | final BufferedReader in = new BufferedReader(new FileReader(src)); |
|---|
| 129 | String line = null; |
|---|
| 130 | int lineno = 0; |
|---|
| 131 | int indent = 0; |
|---|
| 132 | boolean inSnippetCode = false; |
|---|
| 133 | final StringBuffer snippet = new StringBuffer(); |
|---|
| 134 | String snippetFilename = null; |
|---|
| 135 | |
|---|
| 136 | while ((line = in.readLine()) != null) |
|---|
| 137 | { |
|---|
| 138 | ++lineno; |
|---|
| 139 | if (line.matches(".*// " + BEGIN_SNIPPET_TAG + " [a-zA-Z0-9\\.]*[ ]*")) |
|---|
| 140 | { |
|---|
| 141 | inSnippetCode = true; |
|---|
| 142 | indent = line.indexOf("//") != -1 ? line.indexOf("//") : 0; |
|---|
| 143 | snippetFilename = line.substring( |
|---|
| 144 | line.indexOf(BEGIN_SNIPPET_TAG) |
|---|
| 145 | + BEGIN_SNIPPET_TAG.length()).trim(); |
|---|
| 146 | continue; |
|---|
| 147 | } |
|---|
| 148 | else if (inSnippetCode && line.matches(".*// PAUSE SNIPPET.*")) |
|---|
| 149 | { |
|---|
| 150 | inSnippetCode = false; |
|---|
| 151 | } |
|---|
| 152 | else if (!inSnippetCode && line.matches(".*// RESUME SNIPPET.*")) |
|---|
| 153 | { |
|---|
| 154 | inSnippetCode = true; |
|---|
| 155 | continue; |
|---|
| 156 | } |
|---|
| 157 | else if (inSnippetCode && line.matches(".*// END SNIPPET.*")) |
|---|
| 158 | { |
|---|
| 159 | inSnippetCode = false; |
|---|
| 160 | final String codeSnippet = snippet.toString(); |
|---|
| 161 | System.out.println("### writing snippet: " + snippetFilename |
|---|
| 162 | + " indent: " + indent); |
|---|
| 163 | final File outFilename = new File(outDir, snippetFilename); |
|---|
| 164 | final FileOutputStream out |
|---|
| 165 | = new FileOutputStream(outFilename); |
|---|
| 166 | |
|---|
| 167 | out.write(codeSnippet.getBytes()); |
|---|
| 168 | out.close(); |
|---|
| 169 | |
|---|
| 170 | snippet.setLength(0); |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | if (inSnippetCode) |
|---|
| 174 | { |
|---|
| 175 | if (line.length() > indent) |
|---|
| 176 | { |
|---|
| 177 | line = line.substring(indent); |
|---|
| 178 | } |
|---|
| 179 | snippet.append(XmlUtil.escape(line + NEWLINE)); |
|---|
| 180 | } |
|---|
| 181 | } |
|---|
| 182 | in.close(); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /** |
|---|
| 186 | * @param codeSnippetList |
|---|
| 187 | * @param outDir |
|---|
| 188 | */ |
|---|
| 189 | private void writeCodeSnippetCatalog (File dir) |
|---|
| 190 | throws IOException |
|---|
| 191 | { |
|---|
| 192 | final File[] files = dir.listFiles(new FilenameFilter() |
|---|
| 193 | { |
|---|
| 194 | /** {@inheritDoc} */ |
|---|
| 195 | public boolean accept (File dir, String name) |
|---|
| 196 | { |
|---|
| 197 | boolean ret = false; |
|---|
| 198 | if (name.endsWith(".xml") && ! name.equals(JAVA_CODE_SNIPPET_XML)) |
|---|
| 199 | { |
|---|
| 200 | ret = true; |
|---|
| 201 | } |
|---|
| 202 | return ret; |
|---|
| 203 | } |
|---|
| 204 | }); |
|---|
| 205 | |
|---|
| 206 | final PrintStream out = new PrintStream(new FileOutputStream( |
|---|
| 207 | new File(dir, JAVA_CODE_SNIPPET_XML))); |
|---|
| 208 | |
|---|
| 209 | out.println("<?xml version='1.0' encoding='ISO-8859-1'?>"); |
|---|
| 210 | out.println(); |
|---|
| 211 | for (int i = 0; i < files.length; i++) |
|---|
| 212 | { |
|---|
| 213 | final File file = files[i]; |
|---|
| 214 | final String s = file.getName(); |
|---|
| 215 | String n = s; |
|---|
| 216 | if (s.lastIndexOf('.') != -1) |
|---|
| 217 | { |
|---|
| 218 | n = s.substring(0, s.lastIndexOf('.')); |
|---|
| 219 | } |
|---|
| 220 | out.println("<!ENTITY " + n + " SYSTEM \"" + s + "\">"); |
|---|
| 221 | |
|---|
| 222 | } |
|---|
| 223 | out.close(); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | /** |
|---|
| 227 | * Print usage message |
|---|
| 228 | */ |
|---|
| 229 | private static void usage () |
|---|
| 230 | { |
|---|
| 231 | System.out.println("Usage: java JavaCodeSnippets srcDir outDir"); |
|---|
| 232 | System.exit(-1); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|