/** * Expands list of files to process into full list of all files that can be found by recursively * descending directories. */ void expand(File dir, String[] files, boolean isUpdate) { if (files == null) { return; } for (int i = 0; i < files.length; i++) { File f; if (dir == null) { f = new File(files[i]); } else { f = new File(dir, files[i]); } if (f.isFile()) { if (entries.add(f)) { if (isUpdate) entryMap.put(entryName(f.getPath()), f); } } else if (f.isDirectory()) { if (entries.add(f)) { if (isUpdate) { String dirPath = f.getPath(); dirPath = (dirPath.endsWith(File.separator)) ? dirPath : (dirPath + File.separator); entryMap.put(entryName(dirPath), f); } expand(f, f.list(), isUpdate); } } else { error(formatMsg("error.nosuch.fileordir", String.valueOf(f))); ok = false; } } }
/** * Extracts next entry from JAR file, creating directories as needed. If the entry is for a * directory which doesn't exist prior to this invocation, returns that entry, otherwise returns * null. */ ZipEntry extractFile(InputStream is, ZipEntry e) throws IOException { ZipEntry rc = null; String name = e.getName(); File f = new File(e.getName().replace('/', File.separatorChar)); if (e.isDirectory()) { if (f.exists()) { if (!f.isDirectory()) { throw new IOException(formatMsg("error.create.dir", f.getPath())); } } else { if (!f.mkdirs()) { throw new IOException(formatMsg("error.create.dir", f.getPath())); } else { rc = e; } } if (vflag) { output(formatMsg("out.create", name)); } } else { if (f.getParent() != null) { File d = new File(f.getParent()); if (!d.exists() && !d.mkdirs() || !d.isDirectory()) { throw new IOException(formatMsg("error.create.dir", d.getPath())); } } try { copy(is, f); } finally { if (is instanceof ZipInputStream) ((ZipInputStream) is).closeEntry(); else is.close(); } if (vflag) { if (e.getMethod() == ZipEntry.DEFLATED) { output(formatMsg("out.inflated", name)); } else { output(formatMsg("out.extracted", name)); } } } if (!useExtractionTime) { long lastModified = e.getTime(); if (lastModified != -1) { f.setLastModified(lastModified); } } return rc; }
protected void generateJarFile(File jarFile, boolean standAlone, File dir) throws IOException { Verbose.log("Generating jar file ... " + jarFile); FileOutputStream out = new FileOutputStream(jarFile); InputStream manifestIo = getManifest(); Manifest manifest = new Manifest(manifestIo); JarOutputStream jar = new JarOutputStream(out, manifest); int rootLen = dir.getAbsolutePath().length(); int len = "java".length(); for (File javaFile : javaFiles) { String fileName = javaFile.getPath(); String classFile = fileName.substring(0, fileName.length() - len) + "class"; String className = classFile.substring(rootLen + 1); addFileToJar(className, classFile, jar); String javaName = fileName.substring(rootLen + 1); addFileToJar(javaName, fileName, jar); } if (standAlone) { Verbose.log("Adding runtime classes to the jar"); addRuntimeClasses(jar); } jar.close(); out.close(); Verbose.log("Generated jar file " + jarFile); }
/** Create a path value from a list of directories and jar files. */ String createPath(File... files) { StringBuilder sb = new StringBuilder(); for (File f : files) { if (sb.length() > 0) sb.append(File.pathSeparatorChar); sb.append(f.getPath()); } return sb.toString(); }
protected void compileTypes(File dir) throws ToolsException { Compiler compiler = new Compiler(); List<String> fileNames = new ArrayList<String>(); for (File f : javaFiles) { fileNames.add(f.getPath()); } compiler.compile(fileNames.toArray(new String[fileNames.size()]), dir); }
/** Computes the crc32 of a File. This is necessary when the ZipOutputStream is in STORED mode. */ private void crc32File(ZipEntry e, File f) throws IOException { CRC32OutputStream os = new CRC32OutputStream(); copy(f, os); if (os.n != f.length()) { throw new JarException(formatMsg("error.incorrect.length", f.getPath())); } os.updateEntry(e); }
/** Adds a new file entry to the ZIP output stream. */ void addFile(ZipOutputStream zos, File file) throws IOException { String name = file.getPath(); boolean isDir = file.isDirectory(); if (isDir) { name = name.endsWith(File.separator) ? name : (name + File.separator); } name = entryName(name); if (name.equals("") || name.equals(".") || name.equals(zname)) { return; } else if ((name.equals(MANIFEST_DIR) || name.equals(MANIFEST_NAME)) && !Mflag) { if (vflag) { output(formatMsg("out.ignore.entry", name)); } return; } long size = isDir ? 0 : file.length(); if (vflag) { out.print(formatMsg("out.adding", name)); } ZipEntry e = new ZipEntry(name); e.setTime(file.lastModified()); if (size == 0) { e.setMethod(ZipEntry.STORED); e.setSize(0); e.setCrc(0); } else if (flag0) { crc32File(e, file); } zos.putNextEntry(e); if (!isDir) { copy(file, zos); } zos.closeEntry(); /* report how much compression occurred. */ if (vflag) { size = e.getSize(); long csize = e.getCompressedSize(); out.print(formatMsg2("out.size", String.valueOf(size), String.valueOf(csize))); if (e.getMethod() == ZipEntry.DEFLATED) { long ratio = 0; if (size != 0) { ratio = ((size - csize) * 100) / size; } output(formatMsg("out.deflated", String.valueOf(ratio))); } else { output(getMsg("out.stored")); } } }
public static void unpack(File inFile) { JarOutputStream out = null; InputStream in = null; String inName = inFile.getPath(); String outName; if (inName.endsWith(".pack.gz")) { outName = inName.substring(0, inName.length() - 8); } else if (inName.endsWith(".pack")) { outName = inName.substring(0, inName.length() - 5); } else { outName = inName + ".unpacked"; } try { Pack200.Unpacker unpacker = Pack200.newUnpacker(); out = new JarOutputStream(new FileOutputStream(outName)); in = new FileInputStream(inName); if (inName.endsWith(".gz")) in = new GZIPInputStream(in); unpacker.unpack(in, out); } catch (IOException ex) { ex.printStackTrace(); } finally { if (in != null) { try { in.close(); } catch (IOException ex) { System.err.println("Error closing file: " + ex.getMessage()); } } if (out != null) { try { out.flush(); out.close(); } catch (IOException ex) { System.err.println("Error closing file: " + ex.getMessage()); } } } }
private void mapAndTocForFile(File f, File path, String underscore) { // we get the single name of the file and the urlname int under_index2 = f.getName().indexOf("_"); int point_index2 = f.getName().indexOf("."); String named = f.getName().substring(under_index2 + 1, point_index2); String tmp = path.getAbsolutePath() + "Main_pages/fr/"; String url_name = f.getPath().replace("\\", "/").substring(tmp.length()); String targetName = url_name.substring(0, url_name.lastIndexOf(".html")); // now we will add into the map and the toc print.println("<mapID target=\"" + targetName + "\" url=\"pages/" + url_name + "\"/>"); File dir_associated = new File(f.getParent(), named); if ((dir_associated.exists() && dir_associated.isDirectory()) || named.equals("objects")) { print2.println( "<tocitem text=\"" + getTitle(f) + "\" target=\"" + targetName + "\" image=\"tamicon\">"); if (dir_associated.exists()) { // Apres on fait pareil pour tous les sous fichiers File[] sub_files = dir_associated.listFiles(); for (int i = 0; i < sub_files.length; i++) { if (!sub_files[i].getName().equals("CVS") && sub_files[i].isFile()) { if (sub_files[i].getName().endsWith(".html")) mapAndTocForFile(sub_files[i], path, underscore); else { try { copyFile( sub_files[i], new File(path, "pages/" + named + "/" + sub_files[i].getName())); } catch (IOException e) { LOG.error("Error while copying normal files in help " + e); } } } } } if (named.equals("objects")) { // Specialement pour les objets on les rajoute tous File objects_dir = new File( Configuration.instance() .getTangaraPath() .getParentFile() .getAbsolutePath() .replace("\\", "/") + "/objects/"); File[] listfiles = objects_dir.listFiles(); Vector<String> list_names = new Vector<String>(); HashMap<String, String> map = new HashMap<String, String>(); for (int i = 0; i < listfiles.length; i++) { try { if (listfiles[i].getName().endsWith(".jar")) { int point_index = listfiles[i].getName().lastIndexOf("."); String name = listfiles[i].getName().substring(0, point_index); // Copy the pages in the right directory File object_dir = new File(path, "pages/" + name); object_dir.mkdir(); File object_ressource = new File( Configuration.instance() .getTangaraPath() .getParentFile() .getAbsolutePath() .replace("\\", "/") + "/objects/resources/" + name + "/Help"); if (object_ressource.exists()) { File[] list_html_object = object_ressource.listFiles(); for (int e = 0; e < list_html_object.length; e++) { if (list_html_object[e].getName().endsWith(".html")) { int under_index = list_html_object[e].getName().lastIndexOf("_"); if (underscore.equals("") && under_index == -1) copyFile( list_html_object[e], new File(path, "pages/" + name + "/" + list_html_object[e].getName())); else if (!underscore.equals("")) { if (list_html_object[e].getName().contains(underscore)) copyFile( list_html_object[e], new File(path, "pages/" + name + "/" + list_html_object[e].getName())); } } else copyFile( list_html_object[e], new File(path, "pages/" + name + "/" + list_html_object[e].getName())); } // Gets the name of the object in the selected language String name_lang = null; if (underscore.equals("")) name_lang = name; else { name_lang = getLangName(listfiles[i]); } if (name_lang != null) { list_names.add(name_lang); map.put(name_lang, name); // Add to the map file print.println( "<mapID target=\"" + name + "\" url=\"pages/" + name + "/index" + underscore + ".html\" />"); } } } } catch (Exception e2) { LOG.error("Error2 getHelp " + e2); } } // Add to the tam file Collections.sort(list_names); for (String s : list_names) { print2.println( "<tocitem text=\"" + s + "\" target=\"" + map.get(s) + "\" image=\"fileicon\" />"); } } print2.println("</tocitem>"); } else { // pas de sous fichiers print2.println( "<tocitem text=\"" + getTitle(f) + "\" target=\"" + targetName + "\" image=\"fileicon\"/>"); } File parent = new File(path, "pages/" + url_name.substring(0, url_name.lastIndexOf(named) - 3)); if (!parent.exists()) parent.mkdirs(); File in_pages = new File(path, "pages/" + url_name); try { in_pages.createNewFile(); copyFile(f, in_pages); } catch (IOException e3) { LOG.error("Error 3 getHelp " + e3 + " " + f.getName()); } }
public void run() throws Exception { // Selection of files to be compiled File absJar = createJar(new File("abs.jar").getAbsoluteFile(), "j.A"); File relJar = createJar(new File("rel.jar"), "j.R"); File absDir = createDir(new File("abs.dir").getAbsoluteFile(), "d.A"); File relDir = createDir(new File("rel.dir"), "d.R"); File absTestFile = writeFile(new File("AbsTest.java").getAbsoluteFile(), "class AbsTest { class Inner { } }"); File relTestFile = writeFile(new File("RelTest.java"), "class RelTest { class Inner { } }"); File relTest2File = writeFile(new File("p/RelTest2.java"), "package p; class RelTest2 { class Inner { } }"); // This next class references other classes that will be found on the source path // and which will therefore need to be compiled as well. File mainFile = writeFile(new File("Main.java"), "class Main { j.A ja; j.R jr; d.A da; d.R dr; }" + ""); String sourcePath = createPath(absJar, relJar, absDir, relDir); File outDir = new File("classes"); outDir.mkdirs(); String[] args = { "-sourcepath", sourcePath, "-d", outDir.getPath(), absTestFile.getPath(), relTestFile.getPath(), relTest2File.getPath(), mainFile.getPath(), }; System.err.println("compile: " + Arrays.asList(args)); StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); int rc = com.sun.tools.javac.Main.compile(args, pw); pw.close(); if (rc != 0) { System.err.println(sw.toString()); throw new Exception("unexpected exit from javac: " + rc); } Set<File> expect = getFiles( outDir, "d/A.class", "d/A$Inner.class", "d/R.class", "d/R$Inner.class", "j/A.class", "j/A$Inner.class", "j/R.class", "j/R$Inner.class", "AbsTest.class", "AbsTest$Inner.class", "RelTest.class", "RelTest$Inner.class", "p/RelTest2.class", "p/RelTest2$Inner.class", "Main.class"); Set<File> found = findFiles(outDir); if (!found.equals(expect)) { if (found.containsAll(expect)) throw new Exception("unexpected files found: " + diff(found, expect)); else if (expect.containsAll(found)) throw new Exception("expected files not found: " + diff(expect, found)); } for (File f : found) verifySourceFileAttribute(f); if (errors > 0) throw new Exception(errors + " errors occurred"); }