List of usage examples for org.apache.commons.vfs2 FileObject resolveFile
FileObject resolveFile(String path) throws FileSystemException;
From source file:org.freedesktop.icons.DefaultIconService.java
protected Collection<IconTheme> scanBase(FileObject base) throws IOException { List<IconTheme> themes = new ArrayList<IconTheme>(); FileObject[] listDirs = listDirs(base); for (FileObject dir : listDirs) { // TODO cursor themes not supported here FileObject cursorTheme = dir.resolveFile("cursor.theme"); FileObject cursorsDir = dir.resolveFile("cursors"); if (cursorTheme.exists() || cursorsDir.exists()) { // Skip Log.debug("Skipping " + dir + " because it is a cursor theme."); continue; }/*from w ww. java 2s. c om*/ try { themes.add(new IconTheme(dir)); } catch (FileNotFoundException fnfe) { // Skip Log.debug("Skipping " + dir + " because index.theme is missing."); } catch (IOException ioe) { Log.warn("Invalid theme directory " + dir.getName().getPath() + "." + ioe.getMessage()); } } return themes; }
From source file:org.freedesktop.icons.DefaultIconService.java
FileObject lookupFallbackIcon(String iconname) throws IOException { for (FileObject base : bases.keySet()) { for (String extension : SUPPORTED_EXTENSIONS) { FileObject f = base.resolveFile(iconname + "." + extension); if (f.exists()) { return f; }//from w w w.j a va2 s . c o m } } throw new IOException("No theme or fallback icon for " + iconname + "."); }
From source file:org.freedesktop.icons.Directory.java
public Directory(IconTheme theme, String key, Properties properties) throws ParseException, IOException { this.key = key; this.theme = theme; if (!properties.containsKey(SIZE)) { throw new ParseException("Size entry is required.", 0); }/*from w ww. j a v a 2 s .c om*/ size = Integer.parseInt(properties.getProperty(SIZE)); context = properties.getProperty(CONTEXT); if (properties.containsKey(TYPE)) { String typeName = properties.getProperty(TYPE).toLowerCase(); try { type = Type.valueOf(typeName); } catch (IllegalArgumentException iae) { throw new ParseException("Invalid Type ' " + typeName + "' in " + key, 0); } } if (properties.containsKey(MAX_SIZE)) { maxSize = Integer.parseInt(properties.getProperty(MAX_SIZE)); } else { maxSize = size; } if (properties.containsKey(MIN_SIZE)) { minSize = Integer.parseInt(properties.getProperty(MIN_SIZE)); } else { minSize = size; } if (properties.containsKey(THRESHOLD)) { minSize = Integer.parseInt(properties.getProperty(THRESHOLD)); } for (FileObject base : theme.getBases()) { FileObject dirBase = base.resolveFile(getKey()); // Loop over the supported extensions so we get files in supported // extension order for (String extension : DefaultIconService.SUPPORTED_EXTENSIONS) { FileObject[] files = dirBase.findFiles(new ExtensionSelector(extension)); if (files != null) { for (FileObject file : files) { String name = file.getName().getBaseName(); int lidx = name.lastIndexOf('.'); String basename = name.substring(0, lidx); if (!cache.containsKey(basename)) { cache.put(basename, file); } } } } } }
From source file:org.freedesktop.icons.IconTheme.java
public IconTheme(FileObject... base) throws IOException { super(ICON_THEME, base); for (FileObject b : base) { FileObject f = b.resolveFile("index.theme"); if (f.exists()) { themeFile = f;/*ww w . ja v a 2s . c o m*/ break; } } if (themeFile == null) { throw new FileNotFoundException(); } }
From source file:org.freedesktop.icons.IconTheme.java
public FileObject lookupIcon(String icon, int size) throws IOException { checkLoaded();/*from w w w.j av a2 s .c o m*/ for (Directory directory : getDirectories()) { if (directory.isMatchesSize(size)) { FileObject file = directory.findIcon(icon); if (file != null) { return file; } } } int minimalSize = Integer.MAX_VALUE; FileObject closestFile = null; FileObject firstFile = null; for (Directory directory : getDirectories()) { for (String ext : DefaultIconService.SUPPORTED_EXTENSIONS) { for (FileObject base : getBases()) { FileObject file = base.resolveFile(directory.getKey() + File.separator + icon + "." + ext); int directorySizeDistance = directorySizeDistance(directory, size); if (file.exists()) { if (directorySizeDistance < minimalSize) { closestFile = file; minimalSize = directorySizeDistance; } else { if (firstFile == null) { firstFile = file; } } } } } } return closestFile == null ? firstFile : closestFile; }
From source file:org.freedesktop.mime.DefaultAliasService.java
@Override protected Collection<AliasEntry> scanBase(FileObject base) throws IOException { FileObject f = base.resolveFile("aliases"); AliasBase aliasBase = new AliasBase(); aliasBases.put(base, aliasBase);/* w ww .j a va 2 s . c o m*/ InputStream fin = f.getContent().getInputStream(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(fin)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!line.equals("") && !line.startsWith("#")) { int idx = line.indexOf(' '); if (idx == -1) { throw new IOException(f + " contains invalid data '" + line + "'."); } String mimeType = line.substring(0, idx); String alias = line.substring(idx + 1); AliasEntry entry = new AliasEntry(mimeType, alias); aliasBase.byType.put(mimeType, entry); aliasBase.byAlias.put(alias, entry); } } } finally { fin.close(); } return aliasBase.byType.values(); }
From source file:org.freedesktop.mime.DefaultGlobService.java
@Override protected Collection<GlobEntry> scanBase(FileObject base) throws IOException { FileObject f = base.resolveFile("globs"); GlobBase globBase = new GlobBase(); globBases.put(base, globBase);//from w ww . j a v a 2s . c o m InputStream fin = f.getContent().getInputStream(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(fin)); String line; while ((line = reader.readLine()) != null) { line = line.trim(); if (!line.equals("") && !line.startsWith("#")) { int idx = line.indexOf(':'); if (idx == -1) { throw new IOException(f + " contains invalid data '" + line + "'."); } String mimeType = line.substring(0, idx); String pattern = line.substring(idx + 1); // A single mime type may have several patterns GlobEntry entry = globBase.byType.get(mimeType); if (entry == null) { entry = new GlobEntry(mimeType); globBase.byType.put(mimeType, entry); } entry.addPattern(pattern); // Provide a quick lookup table for simple patterns and // explicit names if (isSimplePattern(pattern) || !isExpression(pattern)) { ArrayList<GlobEntry> entries = globBase.byPattern.get(pattern); if (entries == null) { entries = new ArrayList<GlobEntry>(); globBase.byPattern.put(pattern, entries); } if (!entries.contains(entry)) { entries.add(entry); } } } } } finally { fin.close(); } return globBase.byType.values(); }
From source file:org.freedesktop.mime.DefaultMagicService.java
@Override protected Collection<MagicEntry> scanBase(FileObject base) throws IOException { System.out.println(base);/*from ww w. j a v a 2s. c o m*/ FileObject f = base.resolveFile("magic"); MagicBase aliasBase = new MagicBase(); magicBases.put(base, aliasBase); InputStream fin = f.getContent().getInputStream(); try { byte[] buf = new byte[65536]; StringBuilder bui = new StringBuilder(); int state = -1; // -1 outside of format // 0 file header // 1 in header (priority) // 2 in header (mime type) // 3 out of header, waiting for newline // 4 waiting for indent // 5 waiting for offset // 6 waiting for value length (hi) // 7 waiting for value length (lo) // 8 reading value // 9 read value, waiting for optional mask // 10 reading mask // 11 word-size // 12 range-length // 13 done MagicEntry entry = null; Pattern pattern = null; int valueLength = 0; int bufIdx = 0; while (true) { int read = fin.read(buf); if (read == -1) { break; } for (int i = 0; i < read; i++) { byte b = buf[i]; // System.out.println((int)b + "[" + (char)b + "] = " + // state); if (state == -1) { if (b == '\n') { state = 0; if (!bui.toString().equals("MIME-Magic\0")) { throw new IOException("No MIME-Magic header"); } bui.setLength(0); } else { bui.append((char) b); } } else if (state == 0) { if (b == '[') { state = 1; } } else if (state == 1) { if (b == ':') { entry = new MagicEntry(); entry.setPriority(Integer.parseInt(bui.toString())); bui.setLength(0); state = 2; } else { bui.append((char) b); } } else if (state == 2) { if (b == ']') { entry.setMIMEType(bui.toString()); bui.setLength(0); state = 3; aliasBase.byType.put(entry.getInternalName(), entry); } else { bui.append((char) b); } } else if (state == 3) { if (b == '\n') { state = 4; pattern = new MagicEntry.Pattern(); } } else if (state == 4) { if (b == '[') { state = 1; } else if (b == '>') { state = 5; if (bui.length() > 0) { String str = bui.toString(); pattern.setIndent(Integer.parseInt(str)); bui.setLength(0); } } else { bui.append((char) b); } } else if (state == 5) { if (b == '=') { state = 6; pattern.setOffset(Long.parseLong(bui.toString())); bui.setLength(0); } else { bui.append((char) b); } } else if (state == 6) { valueLength = b << 8; state = 7; } else if (state == 7) { valueLength = valueLength | b; state = 8; pattern.setValueLength(valueLength); bufIdx = 0; } else if (state == 8) { pattern.getValue()[bufIdx++] = b; if (bufIdx == valueLength) { state = 9; } } else if (state == 9) { if (b == '&') { bufIdx = 0; state = 10; } else if (b == '~') { state = 11; } else if (b == '+') { state = 12; } else if (b == '\n') { state = 13; } } else if (state == 10) { pattern.getMask()[bufIdx++] = b; if (bufIdx == valueLength) { state = 9; } } else if (state == 11) { pattern.setWordSize(Integer.parseInt(String.valueOf((char) b))); state = 9; } else if (state == 12) { if (b == '\n') { pattern.setRangeLength(Integer.parseInt(bui.toString())); bui.setLength(0); state = 13; } else { bui.append((char) b); } } // Done if (state == 13) { entry.add(pattern); pattern = new MagicEntry.Pattern(); state = 4; } } } } finally { fin.close(); } // Sort the patterns in the base for (MagicEntry e : aliasBase.byType.values()) { Collections.sort(e); } return aliasBase.byType.values(); }
From source file:org.kalypso.commons.io.VFSUtilities.java
/** * This function copies a source file to a given destination. If no filename is given in the destination file handle, * the filename of the source is used.<br> * <br>//from ww w.j a v a2 s .com * It is tried to copy the file three times. If all three tries has failed, only then an IOException is thrown. <br> * All other exceptions are thrown normally. * * @param source * The source file. * @param destination * The destination file or path. * @param overwrite * If set, always overwrite existing and newer files */ public static void copyFileTo(final FileObject source, final FileObject destination, final boolean overwrite) throws IOException { if (source.equals(destination)) { KalypsoCommonsDebug.DEBUG.printf(Messages.getString("org.kalypso.commons.io.VFSUtilities.1"), //$NON-NLS-1$ source.getName(), destination.getName()); return; } /* Some variables for handling the errors. */ boolean success = false; int cnt = 0; while (success == false) { try { if (FileType.FOLDER.equals(source.getType())) throw new IllegalArgumentException(Messages.getString("org.kalypso.commons.io.VFSUtilities.2")); //$NON-NLS-1$ /* If the destination is only a directory, use the sources filename for the destination file. */ FileObject destinationFile = destination; if (FileType.FOLDER.equals(destination.getType())) destinationFile = destination.resolveFile(source.getName().getBaseName()); if (overwrite || !destinationFile.exists() || destinationFile.getContent().getSize() != source.getContent().getSize()) { /* Copy file. */ KalypsoCommonsDebug.DEBUG.printf("Copy file '%s' to '%s'...%n", source.getName(), //$NON-NLS-1$ destinationFile.getName()); FileUtil.copyContent(source, destinationFile); source.close(); } /* End copying of this file, because it was a success. */ success = true; } catch (final IOException e) { /* An error has occurred while copying the file. */ KalypsoCommonsDebug.DEBUG.printf("An error has occured with the message: %s%n", //$NON-NLS-1$ e.getLocalizedMessage()); /* If a certain amount (here 2) of retries was reached before, re-throw the error. */ if (cnt >= 2) { KalypsoCommonsDebug.DEBUG.printf("The second retry has failed, rethrowing the error...%n"); //$NON-NLS-1$ throw e; } /* Retry the copying of the file. */ cnt++; KalypsoCommonsDebug.DEBUG.printf("Retry: %s%n", String.valueOf(cnt)); //$NON-NLS-1$ success = false; /* Wait for some milliseconds. */ try { Thread.sleep(1000); } catch (final InterruptedException e1) { /* * Runs in the next loop then and if no error occurs then, it is ok. If an error occurs again, it is an * exception thrown on the last failed retry or it is slept again. */ } } } }
From source file:org.kalypso.commons.io.VFSUtilities.java
/** * This function will copy one directory to another one. If the destination base directory does not exist, it will be * created.// w w w . j a v a 2 s . c o m * * @param source * The source directory. * @param destination * The destination directory. * @param overwrite * If set, always overwrite existing and newer files */ public static void copyDirectoryToDirectory(final FileObject source, final FileObject destination, final boolean overwrite) throws IOException { if (!FileType.FOLDER.equals(source.getType())) throw new IllegalArgumentException( Messages.getString("org.kalypso.commons.io.VFSUtilities.3") + source.getURL()); //$NON-NLS-1$ if (destination.exists()) { if (!FileType.FOLDER.equals(destination.getType())) throw new IllegalArgumentException( Messages.getString("org.kalypso.commons.io.VFSUtilities.4") + destination.getURL()); //$NON-NLS-1$ } else { KalypsoCommonsDebug.DEBUG.printf("Creating directory '%s'...%", destination.getName()); //$NON-NLS-1$ destination.createFolder(); } final FileObject[] children = source.getChildren(); for (final FileObject child : children) { if (FileType.FILE.equals(child.getType())) { /* Need a destination file with the same name as the source file. */ final FileObject destinationFile = destination.resolveFile(child.getName().getBaseName()); /* Copy ... */ copyFileTo(child, destinationFile, overwrite); } else if (FileType.FOLDER.equals(child.getType())) { /* Need the same name for destination directory, as the source directory has. */ final FileObject destinationDir = destination.resolveFile(child.getName().getBaseName()); /* Copy ... */ KalypsoCommonsDebug.DEBUG.printf("Copy directory %s to %s ...", child.getName(), //$NON-NLS-1$ destinationDir.getName()); copyDirectoryToDirectory(child, destinationDir, overwrite); } else { KalypsoCommonsDebug.DEBUG.printf("Could not determine the file type ...%n"); //$NON-NLS-1$ } } }