|
| 1 | +/* |
| 2 | + * Copyright (c) 2012, Codename One and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * This code is free software; you can redistribute it and/or modify it |
| 5 | + * under the terms of the GNU General Public License version 2 only, as |
| 6 | + * published by the Free Software Foundation. Codename One designates this |
| 7 | + * particular file as subject to the "Classpath" exception as provided |
| 8 | + * by Oracle in the LICENSE file that accompanied this code. |
| 9 | + * |
| 10 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 11 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 14 | + * accompanied this code). |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License version |
| 17 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | + * |
| 20 | + * Please contact Codename One through http://www.codenameone.com/ if you |
| 21 | + * need additional information or have any questions. |
| 22 | + */ |
| 23 | + |
| 24 | +package com.codename1.tools.javadoc.sourceembed.javadocsourceembed; |
| 25 | + |
| 26 | +import java.io.BufferedReader; |
| 27 | +import java.io.BufferedWriter; |
| 28 | +import java.io.File; |
| 29 | +import java.io.FileFilter; |
| 30 | +import java.io.FileWriter; |
| 31 | +import java.io.InputStream; |
| 32 | +import java.io.InputStreamReader; |
| 33 | +import java.net.URL; |
| 34 | +import java.nio.charset.Charset; |
| 35 | +import java.nio.file.Files; |
| 36 | +import java.nio.file.Paths; |
| 37 | +import java.util.HashMap; |
| 38 | +import java.util.List; |
| 39 | +import java.util.function.Consumer; |
| 40 | +import java.util.stream.Collectors; |
| 41 | +import org.json.JSONObject; |
| 42 | + |
| 43 | +/** |
| 44 | + * <p>A trivial tool that allows embedding Java source code into JavaDoc using github gists in a way that |
| 45 | + * works nicely in the browser and in the ide...</p> |
| 46 | + * <p>To use this just use the standard gist embed code within your javadoc comments then run this tool by |
| 47 | + * supplying the source directory and a destination directory to which the modified sources should be written |
| 48 | + * then run javadoc on the modified code. Notice that you shouldn't modify the standrad code as the generated |
| 49 | + * code is a bit too "loud". |
| 50 | + * </p> |
| 51 | + * |
| 52 | + * @author Shai Almog |
| 53 | + */ |
| 54 | +public class Main { |
| 55 | + private static Charset CHARSET; |
| 56 | + private static final HashMap<String, String> gistCache = new HashMap<>(); |
| 57 | + public static void main(String[] args) throws Exception { |
| 58 | + // this accepts two arguments source directory and destination directory where the modfied files will |
| 59 | + // be written |
| 60 | + File sourceDir = new File(args[0]); |
| 61 | + File destDir = new File(args[1]); |
| 62 | + System.out.println("JavaDoc conversion " + sourceDir.getAbsolutePath() + " to " + destDir.getAbsolutePath()); |
| 63 | + if(!sourceDir.exists() || !sourceDir.isDirectory()) { |
| 64 | + System.out.println("Source directory doesn't exist"); |
| 65 | + System.exit(1); |
| 66 | + return; |
| 67 | + } |
| 68 | + CHARSET = Charset.forName("UTF-8"); |
| 69 | + directoryWalker(sourceDir, destDir); |
| 70 | + } |
| 71 | + |
| 72 | + public static void directoryWalker(File f, File destDir) throws Exception { |
| 73 | + File[] files = f.listFiles((path) -> path.isDirectory() || path.getName().endsWith(".java")); |
| 74 | + |
| 75 | + for(File ff : files) { |
| 76 | + if(ff.isDirectory()) { |
| 77 | + File destDirT = new File(destDir, ff.getName()); |
| 78 | + destDirT.mkdirs(); |
| 79 | + directoryWalker(ff, destDirT); |
| 80 | + } else { |
| 81 | + processFile(ff, new File(destDir, ff.getName())); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static void processFile(File javaSourceFile, File javaDestFile) throws Exception { |
| 87 | + System.out.println("JavaSource Processing: " + javaSourceFile.getName()); |
| 88 | + List<String> lines = Files.readAllLines(Paths.get(javaSourceFile.toURI()), CHARSET); |
| 89 | + for(int iter = 0 ; iter < lines.size() ; iter++) { |
| 90 | + String l = lines.get(iter); |
| 91 | + int position = l.indexOf("<script src=\"https://gist.github.com/"); |
| 92 | + if(position > -1) { |
| 93 | + String id = l.substring(position + 39 ); |
| 94 | + id = id.split("/")[1]; |
| 95 | + id = id.substring(0, id.indexOf('.')); |
| 96 | + String fileContent = gistCache.get(id); |
| 97 | + if(fileContent != null) { |
| 98 | + lines.add(iter + 1, fileContent); |
| 99 | + iter++; |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + URL u = new URL("https://api.github.com/gists/" + id); |
| 104 | + try(BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream(), CHARSET))) { |
| 105 | + String jsonText = br.lines().collect(Collectors.joining("\n")); |
| 106 | + JSONObject json = new JSONObject(jsonText); |
| 107 | + JSONObject filesObj = json.getJSONObject("files"); |
| 108 | + String str =""; |
| 109 | + for(String k : filesObj.keySet()) { |
| 110 | + JSONObject jsonFileEntry = filesObj.getJSONObject(k); |
| 111 | + // breaking line to fix the problem with a blank space on the first line |
| 112 | + String current = "\n" + jsonFileEntry.getString("content"); |
| 113 | + str += current; |
| 114 | + } |
| 115 | + int commentStartPos = str.indexOf("/*"); |
| 116 | + while(commentStartPos > -1) { |
| 117 | + // we just remove the comment as its pretty hard to escape it properly |
| 118 | + int commentEndPos = str.indexOf("*/"); |
| 119 | + str = str.substring(commentStartPos, commentEndPos + 1); |
| 120 | + commentStartPos = str.indexOf("/*"); |
| 121 | + } |
| 122 | + |
| 123 | + // allows things like the @Override annotation |
| 124 | + str = "<noscript><pre>{@code " + str.replace("@", "{@literal @}") + "}</pre></noscript>"; |
| 125 | + gistCache.put(id, str); |
| 126 | + lines.add(iter + 1, str); |
| 127 | + iter++; |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + try(BufferedWriter fw = new BufferedWriter(new FileWriter(javaDestFile))) { |
| 132 | + for(String l : lines) { |
| 133 | + fw.write(l); |
| 134 | + fw.write('\n'); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments