private String getTitle(File f) { String result = ""; if (!f.getName().endsWith(".html")) return "not HTML"; else { try { BufferedReader reader = new BufferedReader(new FileReader(f)); String ligne = reader.readLine(); while (ligne != null && !ligne.contains("<TITLE>") && !ligne.contains("<title>")) { ligne = reader.readLine(); } reader.close(); if (ligne == null) return "No TITLE in page " + f.getName(); else { String fin = ""; String debut = ""; if (ligne.contains("</TITLE>")) { debut = "<TITLE>"; fin = "</TITLE>"; } else if (ligne.contains("</title>")) { debut = "<title>"; fin = "</title>"; } else return "No title in page " + f.getName(); int fin_index = ligne.lastIndexOf(fin); result = ligne.substring(ligne.indexOf(debut) + 7, fin_index); return result; } } catch (Exception e) { LOG.error("Error while reading file " + e); return "Error for file " + f.getName(); } } }
/** * @param guid * @throws IOException */ public void delete(int guid) throws IOException { // delete file File file = new File(Integer.toString(guid)); if (file.delete()) { System.out.println(file.getName() + " deleted"); } else { System.out.println("Failed to delete " + file.getName()); } }
protected long getTotalFileSizeSupport(File file) throws TOTorrentException { String name = file.getName(); ///////////////////////////////////////// ///////////////////////////////////// if (name.equals(".") || name.equals("..")) { return (0); } if (!file.exists()) { throw (new TOTorrentException( "TOTorrentCreate: file '" + file.getName() + "' doesn't exist", TOTorrentException.RT_FILE_NOT_FOUND)); } if (file.isFile()) { if (!ignoreFile(name)) { total_file_count++; return (file.length()); } else { return (0); } } else { File[] dir_files = file.listFiles(); if (dir_files == null) { throw (new TOTorrentException( "TOTorrentCreate: directory '" + file.getAbsolutePath() + "' returned error when listing files in it", TOTorrentException.RT_FILE_NOT_FOUND)); } long length = 0; for (int i = 0; i < dir_files.length; i++) { length += getTotalFileSizeSupport(dir_files[i]); } return (length); } }
/** Sends the user a list of the files contained in the same folder as the server. */ private void listFiles() throws IOException { File folder = new File("."); File[] listOfFiles = folder.listFiles(); for (File f : listOfFiles) { outToClient.writeBytes(f.getName() + '\n'); } }
/** * Sends the given file through this chat transport file transfer operation set. * * @param file the file to send * @return the <tt>FileTransfer</tt> object charged to transfer the file * @throws Exception if anything goes wrong */ public FileTransfer sendFile(File file) throws Exception { // If this chat transport does not support instant messaging we do // nothing here. if (!allowsFileTransfer()) return null; OperationSetFileTransfer ftOpSet = contact.getProtocolProvider().getOperationSet(OperationSetFileTransfer.class); if (FileUtils.isImage(file.getName())) { // Create a thumbnailed file if possible. OperationSetThumbnailedFileFactory tfOpSet = contact.getProtocolProvider().getOperationSet(OperationSetThumbnailedFileFactory.class); if (tfOpSet != null) { byte[] thumbnail = getFileThumbnail(file); if (thumbnail != null && thumbnail.length > 0) { file = tfOpSet.createFileWithThumbnail( file, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, "image/png", thumbnail); } } } return ftOpSet.sendFile(contact, file); }
public void resolve(Unmarshaller u) throws JAXBException { Object obj = null; try { obj = u.unmarshal(new URL(getSchemaLocation())); } catch (MalformedURLException e) { } File file = new File(getSchemaLocation()); if (!file.exists()) { if (!file.getName().startsWith("/")) { file = new File(_location.getSystemId()); file = file.getParentFile(); if (file == null) throw new JAXBException("File not found: " + getSchemaLocation()); file = new File(file, getSchemaLocation()); } } obj = u.unmarshal(file); if (obj instanceof Schema) _schema = (Schema) obj; }
private String prepareResultPagesSection() { String resultsPath; try { resultsPath = rootDirectory.getCanonicalPath() + Crawler.RESULTS_PATH_LOCAL; } catch (IOException e) { System.out.println("HTTPRequest: Error root directory" + rootDirectory.toString()); return ""; } StringBuilder result = new StringBuilder(); result.append("<div class=\"connectedDomains\"><ul>"); File resultsFolder = new File(resultsPath); if (resultsFolder.exists() && resultsFolder.isDirectory()) { File[] allFiles = resultsFolder.listFiles(); SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy-HH:mm"); for (File file : allFiles) { String filename = file.getName(); String domain = Crawler.ResultsFilenameToDomain(filename); Date creationDate = Crawler.ResultsFilenameToDate(filename); String linkFormat = domain + "-" + format.format(creationDate); result.append("<li><a href="); result.append(Crawler.RESULTS_PATH_WEB); result.append(filename); result.append(">"); result.append(linkFormat); result.append("</a></li>"); } result.append("</ul></div>"); } return result.toString(); }
/* * Set up the platform binary download and get it ready to run the tests. * This method is not intended to be called by clients, it will be called * automatically when the clients use a ReconcilerTestSuite. If the executable isn't * found on the file-system after the call, then we fail. */ public void initialize() throws Exception { initialized = false; File file = getPlatformZip(); output = getUniqueFolder(); toRemove.add(output); // for now we will exec to un-tar archives to keep the executable bits if (file.getName().toLowerCase().endsWith(".zip")) { try { FileUtils.unzipFile(file, output); } catch (IOException e) { fail("0.99", e); } } else { untar("1.0", file); } File exe = new File(output, getExeFolder() + "eclipse.exe"); if (!exe.exists()) { exe = new File(output, getExeFolder() + "eclipse"); if (!exe.exists()) fail( "Executable file: " + exe.getAbsolutePath() + "(or .exe) not found after extracting: " + file.getAbsolutePath() + " to: " + output); } initialized = true; }
private boolean directRequestToResultPages(HtmlRequest htmlRequest) { String filename = htmlRequest.requestedFile; if (filename.length() < Crawler.RESULTS_PATH_WEB.length()) { return false; } if (filename .substring(0, Crawler.RESULTS_PATH_WEB.length()) .equalsIgnoreCase(Crawler.RESULTS_PATH_WEB)) { String referrer = null; for (String line : htmlRequest.parsedRequest) { if (line.startsWith("Referer:")) { // Referer: http://xxx.xxx.xxx.xxx/ if (line.length() < 16) { return false; } String[] splitReferer = line.substring(16, line.length()).split("/"); if (splitReferer.length < 2 || splitReferer[1].equalsIgnoreCase(defaultPage.getName()) || splitReferer[1].equalsIgnoreCase(Crawler.RESULTS_PATH_WEB.replaceAll("/", "")) || splitReferer[1].equalsIgnoreCase(execResults.replaceAll("/", ""))) { return false; } } } if (referrer == null) { return true; } } return false; }
@Override public void actionPerformed(ActionEvent e) { Frame frame = getFrame(); JFileChooser chooser = new JFileChooser(); int ret = chooser.showOpenDialog(frame); if (ret != JFileChooser.APPROVE_OPTION) { return; } File f = chooser.getSelectedFile(); if (f.isFile() && f.canRead()) { Document oldDoc = getEditor().getDocument(); if (oldDoc != null) { oldDoc.removeUndoableEditListener(undoHandler); } if (elementTreePanel != null) { elementTreePanel.setEditor(null); } getEditor().setDocument(new PlainDocument()); frame.setTitle(f.getName()); Thread loader = new FileLoader(f, editor.getDocument()); loader.start(); } else { JOptionPane.showMessageDialog( getFrame(), "Could not open file: " + f, "Error opening file", JOptionPane.ERROR_MESSAGE); } }
/** * Load a file of a particular type. It's a pretty standard helper function, which uses DarwinCSV * and setCurrentCSV(...) to make file loading happen with messaging and whatnot. We can also * reset the display: just call loadFile(null). * * @param file The file to load. * @param type The type of file (see DarwinCSV's constants). */ private void loadFile(File file, short type) { // If the file was reset, reset the display and keep going. if (file == null) { mainFrame.setTitle(basicTitle); setCurrentCSV(null); return; } // Load up a new DarwinCSV and set current CSV. try { setCurrentCSV(new DarwinCSV(file, type)); } catch (IOException ex) { MessageBox.messageBox( mainFrame, "Could not read file '" + file + "'", "Unable to read file '" + file + "': " + ex); } // Set the main frame title, based on the filename and the index. mainFrame.setTitle( basicTitle + ": " + file.getName() + " (" + String.format("%,d", currentCSV.getRowIndex().getRowCount()) + " rows)"); }
// Look through the http-import directory and process all // the files there, oldest first. Note that these are actual // files, not queue elements. private void processHttpImportFiles() { File importDirFile = new File(TrialConfig.basepath + TrialConfig.httpImportDir); if (!importDirFile.exists()) return; File[] files = importDirFile.listFiles(); for (int k = 0; k < files.length; k++) { File next = files[k]; if (next.canRead() && next.canWrite()) { FileObject fileObject = FileObject.getObject(next); if (preprocess(fileObject)) { process(fileObject); if (!queueForDatabase(fileObject)) Log.message(Quarantine.file(next, processorServiceName)); else Log.message(processorServiceName + ": Processing complete: " + next.getName()); } // If the file still exists, then there must be a bug // somewhere; log it and quarantine the file so we don't // fall into an infinite loop. if (next.exists()) { logger.warn( "File still in queue after processing:\n" + next + "The file will be quarantined."); Log.message(Quarantine.file(next, processorServiceName)); } Thread.currentThread().yield(); } } }
protected static long getTorrentDataSizeFromFileOrDirSupport(File file) { String name = file.getName(); if (name.equals(".") || name.equals("..")) { return (0); } if (!file.exists()) { return (0); } if (file.isFile()) { return (file.length()); } else { File[] dir_files = file.listFiles(); long length = 0; for (int i = 0; i < dir_files.length; i++) { length += getTorrentDataSizeFromFileOrDirSupport(dir_files[i]); } return (length); } }
public int showConfirmFileOverwriteDialog(File outFile) { return JOptionPane.showConfirmDialog( this.getFrame(), "Replace existing file\n" + outFile.getName() + "?", "Overwrite Existing File?", JOptionPane.YES_NO_CANCEL_OPTION); }
String ls(String args[], boolean relative) { if (args.length < 2) throw new IllegalArgumentException( "the ${ls} macro must at least have a directory as parameter"); File dir = domain.getFile(args[1]); if (!dir.isAbsolute()) throw new IllegalArgumentException( "the ${ls} macro directory parameter is not absolute: " + dir); if (!dir.exists()) throw new IllegalArgumentException( "the ${ls} macro directory parameter does not exist: " + dir); if (!dir.isDirectory()) throw new IllegalArgumentException( "the ${ls} macro directory parameter points to a file instead of a directory: " + dir); Collection<File> files = new ArrayList<File>(new SortedList<File>(dir.listFiles())); for (int i = 2; i < args.length; i++) { Instructions filters = new Instructions(args[i]); files = filters.select(files, true); } List<String> result = new ArrayList<String>(); for (File file : files) result.add(relative ? file.getName() : file.getAbsolutePath()); return Processor.join(result, ","); }
/** * Tries to find the given file, assuming that it's missing the ".jxp" extension * * @param f File to check * @return same file if not found to be missing the .jxp, or a new File w/ the .jxp appended */ File tryNoJXP(File f) { if (f.exists()) return f; if (f.getName().indexOf(".") >= 0) return f; File temp = new File(f.toString() + ".jxp"); return temp.exists() ? temp : f; }
/* * Untar the given file in the output directory. */ private void untar(String message, File file) { String name = file.getName(); File gzFile = new File(output, name); output.mkdirs(); run(message, new String[] {"cp", file.getAbsolutePath(), gzFile.getAbsolutePath()}); run(message, new String[] {"tar", "-zpxf", gzFile.getAbsolutePath()}); gzFile.delete(); }
public String type() { if (type != null) return type; String nm = fn.getName(); if (nm.endsWith(".html")) type = "text/html; charset=iso-8859-1"; else if ((nm.indexOf('.') < 0) || nm.endsWith(".txt")) type = "text/plain; charset=iso-8859-1"; else type = "application/octet-stream"; return type; }
protected TOTorrentCreateImpl( File _torrent_base, URL _announce_url, boolean _add_other_hashes, long _piece_length) throws TOTorrentException { super(_torrent_base.getName(), _announce_url, _torrent_base.isFile()); torrent_base = _torrent_base; piece_length = _piece_length; add_other_hashes = _add_other_hashes; }
JxpSource handleFileNotFound(File f) { String name = f.getName(); if (name.endsWith(".class")) { name = name.substring(0, name.length() - 6); return getJxpServlet(name); } return null; }
private byte[] readFileForResponse(HtmlRequest htmlRequest) throws IOException { if (htmlRequest.requestedFile.equals("/") || htmlRequest.requestedFile.equals("/" + defaultPage.getName())) { fullPathForFile = rootDirectory.getCanonicalPath() + "\\" + defaultPage.getName(); return prepareDefaultPage(null); } else { fullPathForFile = rootDirectory.getCanonicalPath() + htmlRequest.requestedFile; } File file = new File(fullPathForFile); byte[] buffer = new byte[(int) file.length()]; FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); bis.read(buffer, 0, buffer.length); bis.close(); return buffer; }
/* * Add the given bundle to the given folder (do a copy). * The folder can be one of dropins, plugins or features. * If the file handle points to a directory, then do a deep copy. */ public void add(String message, String target, File file) { if (!(target.startsWith("dropins") || target.startsWith("plugins") || target.startsWith("features"))) fail( "Destination folder for resource copying should be either dropins, plugins or features."); File destinationParent = new File(output, getRootFolder() + target); destinationParent.mkdirs(); copy(message, file, new File(destinationParent, file.getName())); }
// A method for receiving files, directories or an entire tree of directories private static void readFile() { try { // Opening a socket for file receiving Socket downloadsSocket = serverSocket.accept(); // Obtaining file's name String fileName = new DataInputStream(downloadsSocket.getInputStream()).readUTF(); // Obtaining file's directory name String directoryName = new DataInputStream(downloadsSocket.getInputStream()).readUTF(); // Checking, if a file or a directory boolean isDirectory = new DataInputStream(downloadsSocket.getInputStream()).readBoolean(); // Receiving a file if (isDirectory) { new File( destinationDirectory.getName() + SYSTEM_WAY_SEPARATOR + directoryName + SYSTEM_WAY_SEPARATOR + fileName) .mkdir(); } else if (directoryName.equals("")) { Files.copy( downloadsSocket.getInputStream(), new File(destinationDirectory.getName() + SYSTEM_WAY_SEPARATOR + fileName).toPath()); } else { Files.copy( downloadsSocket.getInputStream(), new File( destinationDirectory.getName() + SYSTEM_WAY_SEPARATOR + directoryName + SYSTEM_WAY_SEPARATOR + fileName) .toPath()); } downloadsSocket.close(); } catch (FileAlreadyExistsException e) { localPrintWriter.println("File already exist."); } catch (IOException e) { e.printStackTrace(); } }
public void precompile(Collection<File> templates, File outputBaseDir, boolean purgeWhitespace) throws IOException { Context cx = Context.enter(); PrintWriter out = null; File outputFile = null; try { for (File template : templates) { outputFile = new File(outputBaseDir, template.getName() + ".js"); out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(outputFile), encoding)); LOG.info("precompile " + template + " to " + outputFile); out.print( "(function() {\n var template = Handlebars.template, " + "templates = Handlebars.templates = Handlebars.templates || {};\n"); // Rhino for Handlebars Template ScriptableObject global = cx.initStandardObjects(); InputStreamReader in = new InputStreamReader(handlebarsUrl.openStream()); cx.evaluateReader(global, in, handlebarsName, 1, null); IOUtils.closeQuietly(in); String data = FileUtils.readFileToString(template, encoding); if (purgeWhitespace) data = StringUtils.replaceEach( data, new String[] {"\n", "\r", "\t"}, new String[] {"", "", ""}); ScriptableObject.putProperty(global, "data", data); Object obj = cx.evaluateString(global, "Handlebars.precompile(String(data));", "<cmd>", 1, null); out.println( "templates['" + FilenameUtils.getBaseName(template.getName()) + "']=template(" + obj.toString() + ");"); } } finally { Context.exit(); if (out != null) out.println("})();"); IOUtils.closeQuietly(out); } }
File tryOtherExtensions(File f) { if (f.exists()) return f; if (f.getName().indexOf(".") >= 0) return f; for (int i = 0; i < JSFileLibrary._srcExtensions.length; i++) { File temp = new File(f.toString() + JSFileLibrary._srcExtensions[i]); if (temp.exists()) return temp; } return f; }
public void actionPerformed(ActionEvent e) { Frame frame = getFrame(); JFileChooser chooser = new JFileChooser(); int ret = chooser.showSaveDialog(frame); if (ret != JFileChooser.APPROVE_OPTION) { return; } File f = chooser.getSelectedFile(); frame.setTitle(f.getName()); Thread saver = new FileSaver(f, editor.getDocument()); saver.start(); }
private void addClassPathToScriptEngine(File jarFile) throws ConfigurationException { try { StringBuilder cmd = new StringBuilder(addClassPathCmd); int tagStartPos = cmd.indexOf(parameterTag); int tageEndPos = tagStartPos + parameterTag.length(); cmd.replace(tagStartPos, tageEndPos, jarFile.getAbsolutePath().replace("\\", "/")); // System.out.println("cmd " + cmd.toString()); engine.eval("add-classpath", 1, 1, cmd.toString()); // $NON-NLS-1$ } catch (Exception ex) { String msg = String.format("Failed to load class path %s", jarFile.getName()); // $NON-NLS-1$ System.err.println(msg + " " + ex); throw new ConfigurationException(msg, ex); } }
// Take a tree of files starting in a directory in a zip file // and copy them to a disk directory, recreating the tree. private int unpackZipFile( File inZipFile, String directory, String parent, boolean suppressFirstPathElement) { int count = 0; if (!inZipFile.exists()) return count; parent = parent.trim(); if (!parent.endsWith(File.separator)) parent += File.separator; if (!directory.endsWith(File.separator)) directory += File.separator; File outFile = null; try { ZipFile zipFile = new ZipFile(inZipFile); Enumeration zipEntries = zipFile.entries(); while (zipEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipEntries.nextElement(); String name = entry.getName().replace('/', File.separatorChar); if (name.startsWith(directory)) { if (suppressFirstPathElement) name = name.substring(directory.length()); outFile = new File(parent + name); // Create the directory, just in case if (name.indexOf(File.separatorChar) >= 0) { String p = name.substring(0, name.lastIndexOf(File.separatorChar) + 1); File dirFile = new File(parent + p); dirFile.mkdirs(); } if (!entry.isDirectory()) { System.out.println("Installing " + outFile); // Copy the file BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); BufferedInputStream in = new BufferedInputStream(zipFile.getInputStream(entry)); int size = 1024; int n = 0; byte[] b = new byte[size]; while ((n = in.read(b, 0, size)) != -1) out.write(b, 0, n); in.close(); out.flush(); out.close(); // Count the file count++; } } } zipFile.close(); } catch (Exception e) { System.err.println("...an error occured while installing " + outFile); e.printStackTrace(); System.err.println("Error copying " + outFile.getName() + "\n" + e.getMessage()); return -count; } System.out.println(count + " files were installed."); return count; }
private static void scanDir(File dir, String prefix, Set<String> names) throws Exception { File[] files = dir.listFiles(); if (files == null) return; for (int i = 0; i < files.length; i++) { File f = files[i]; String name = f.getName(); String p = (prefix.equals("")) ? name : prefix + "." + name; if (f.isDirectory()) scanDir(f, p, names); else if (name.endsWith(".class")) { p = p.substring(0, p.length() - 6); names.add(p); } } }
/** * Converts a dir string to a linked dir string * * @param dir the directory string (e.g. /usr/local/httpd) * @param browserLink web-path to Browser.jsp */ public static String dir2linkdir(String dir, String browserLink, int sortMode) { File f = new File(dir); StringBuffer buf = new StringBuffer(); while (f.getParentFile() != null) { if (f.canRead()) { String encPath = URLEncoder.encode(f.getAbsolutePath()); buf.insert( 0, "<a href=\"" + browserLink + "?sort=" + sortMode + "&dir=" + encPath + "\">" + conv2Html(f.getName()) + File.separator + "</a>"); } else buf.insert(0, conv2Html(f.getName()) + File.separator); f = f.getParentFile(); } if (f.canRead()) { String encPath = URLEncoder.encode(f.getAbsolutePath()); buf.insert( 0, "<a href=\"" + browserLink + "?sort=" + sortMode + "&dir=" + encPath + "\">" + conv2Html(f.getAbsolutePath()) + "</a>"); } else buf.insert(0, f.getAbsolutePath()); return buf.toString(); }