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