// Read the file into a String public static String readFileToString(String filename) { try { File file = new File(filename); if (!file.exists()) { System.out.println("\nFILE DOES NOT EXIST: " + filename); } BufferedReader in = new BufferedReader(new FileReader(file)); // Create an array of characters the size of the file char[] allChars = new char[(int) file.length()]; // Read the characters into the allChars array in.read(allChars, 0, (int) file.length()); in.close(); // Convert to a string String allCharsString = new String(allChars); return allCharsString; } catch (FileNotFoundException e) { System.err.println(e); return ""; } catch (IOException e) { System.err.println(e); return ""; } }
/** * Copy a file from a source directory to a destination directory (if it is not there already). If * <code>overwrite</code> is true and the destination file already exists, overwrite it. * * @param configuration Holds the error message * @param file The name of the file to copy * @param source The source directory * @param destination The destination directory where the file needs to be copied * @param overwrite A flag to indicate whether the file in the destination directory will be * overwritten if it already exists. * @param replaceNewLine true if the newline needs to be replaced with platform- specific newline. */ public static void copyFile( Configuration configuration, String file, String source, String destination, boolean overwrite, boolean replaceNewLine) { DirectoryManager.createDirectory(configuration, destination); File destfile = new File(destination, file); if (destfile.exists() && (!overwrite)) return; try { InputStream in = Configuration.class.getResourceAsStream( source + DirectoryManager.URL_FILE_SEPARATOR + file); if (in == null) return; OutputStream out = new FileOutputStream(destfile); try { if (!replaceNewLine) { byte[] buf = new byte[2048]; int n; while ((n = in.read(buf)) > 0) out.write(buf, 0, n); } else { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out)); try { String line; while ((line = reader.readLine()) != null) { writer.write(line); writer.write(DocletConstants.NL); } } finally { reader.close(); writer.close(); } } } finally { in.close(); out.close(); } } catch (IOException ie) { ie.printStackTrace(System.err); throw new DocletAbortException(); } }
/** * Add at least the first sentence from a doc block for a package to the API. This is used by the * report generator if no comment is provided. The default source tree may not include the * package.html files, so this may be unavailable in many cases. Need to make sure that HTML tags * are not confused with XML tags. This could be done by stuffing the < character to another * string or by handling HTML in the parser. This second option is neater. Note that XML expects * all element tags to have either a closing "/>" or a matching end element tag. Due to the * difficulties of converting incorrect HTML to XHTML, the first option is used. */ public void addPkgDocumentation(RootDoc root, PackageDoc pd, int indent) { String rct = null; String filename = pd.name(); try { // See if the source path was specified as part of the // options and prepend it if it was. String srcLocation = null; String[][] options = root.options(); for (int opt = 0; opt < options.length; opt++) { if ((options[opt][0]).compareTo("-sourcepath") == 0) { srcLocation = options[opt][1]; break; } } filename = filename.replace('.', JDiff.DIR_SEP.charAt(0)); if (srcLocation != null) { // Make a relative location absolute if (srcLocation.startsWith("..")) { String curDir = System.getProperty("user.dir"); while (srcLocation.startsWith("..")) { srcLocation = srcLocation.substring(3); int idx = curDir.lastIndexOf(JDiff.DIR_SEP); curDir = curDir.substring(0, idx + 1); } srcLocation = curDir + srcLocation; } filename = srcLocation + JDiff.DIR_SEP + filename; } // Try both ".htm" and ".html" filename += JDiff.DIR_SEP + "package.htm"; File f2 = new File(filename); if (!f2.exists()) { filename += "l"; } FileInputStream f = new FileInputStream(filename); BufferedReader d = new BufferedReader(new InputStreamReader(f)); String str = d.readLine(); // Ignore everything except the lines between <body> elements boolean inBody = false; while (str != null) { if (!inBody) { if (str.toLowerCase().trim().startsWith("<body")) { inBody = true; } str = d.readLine(); // Get the next line continue; // Ignore the line } else { if (str.toLowerCase().trim().startsWith("</body")) { inBody = false; continue; // Ignore the line } } if (rct == null) rct = str + "\n"; else rct += str + "\n"; str = d.readLine(); } } catch (java.io.FileNotFoundException e) { // If it doesn't exist, that's fine if (trace) System.out.println("No package level documentation file at '" + filename + "'"); } catch (java.io.IOException e) { System.out.println("Error reading file \"" + filename + "\": " + e.getMessage()); System.exit(5); } if (rct != null) { rct = stripNonPrintingChars(rct, (Doc) pd); rct = rct.trim(); if (rct.compareTo("") != 0 && rct.indexOf(Comments.placeHolderText) == -1 && rct.indexOf("InsertOtherCommentsHere") == -1) { int idx = endOfFirstSentence(rct); if (idx == 0) return; for (int i = 0; i < indent; i++) outputFile.print(" "); outputFile.println("<doc>"); for (int i = 0; i < indent; i++) outputFile.print(" "); String firstSentence = null; if (idx == -1) firstSentence = rct; else firstSentence = rct.substring(0, idx + 1); String firstSentenceNoTags = API.stuffHTMLTags(firstSentence); outputFile.println(firstSentenceNoTags); for (int i = 0; i < indent; i++) outputFile.print(" "); outputFile.println("</doc>"); } } }