|
What this is
Other links
The source code
/*
* Sun Public License Notice
*
* The contents of this file are subject to the Sun Public License
* Version 1.0 (the "License"). You may not use this file except in
* compliance with the License. A copy of the License is available at
* http://www.sun.com/
*
* The Original Code is NetBeans. The Initial Developer of the Original
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun
* Microsystems, Inc. All Rights Reserved.
*/
package org.netbeans.modules.vcs.profiles.commands;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.netbeans.api.queries.SharabilityQuery;
import org.netbeans.api.vcs.VcsManager;
import org.netbeans.api.vcs.commands.Command;
import org.netbeans.api.vcs.commands.CommandTask;
import org.netbeans.modules.vcscore.VcsFileSystem;
import org.netbeans.modules.vcscore.cache.CacheHandler;
import org.netbeans.modules.vcscore.cache.FileSystemCache;
import org.netbeans.modules.vcscore.caching.VcsCacheDir;
import org.netbeans.modules.vcscore.cmdline.ExecuteCommand;
import org.netbeans.modules.vcscore.cmdline.VcsAdditionalCommand;
import org.netbeans.modules.vcscore.commands.CommandDataOutputListener;
import org.netbeans.modules.vcscore.commands.CommandExecutionContext;
import org.netbeans.modules.vcscore.commands.CommandOutputListener;
import org.netbeans.modules.vcscore.commands.RegexOutputListener;
import org.netbeans.modules.vcscore.commands.TextErrorListener;
import org.netbeans.modules.vcscore.commands.VcsDescribedCommand;
import org.netbeans.modules.vcscore.util.VcsUtilities;
import org.netbeans.spi.vcs.commands.CommandSupport;
import org.openide.ErrorManager;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
/**
* A command, that checks whether the processed files and folders are sharable
* before it invokes the sub-command.
*
* @author Martin Entlicher
*/
public class SharableFilesCommand implements VcsAdditionalCommand {
public static final String LOCAL_DIR_ONLY = "-l"; // NOI18N
private CommandExecutionContext execContext;
private VcsFileSystem fileSystem;
private Hashtable vars;
/** Creates a new instance of Echo */
public SharableFilesCommand() {
}
/** Set the VCS file system to use to execute commands.
*/
public void setExecutionContext(CommandExecutionContext execContext) {
this.execContext = execContext;
if (execContext instanceof VcsFileSystem) {
fileSystem = (VcsFileSystem) execContext;
}
}
private Collection collectSharableSubfiles(Collection fileNames, Collection intermediateFolders, boolean recursive) {
Collection sharableFileNames = new ArrayList();
for (Iterator it = fileNames.iterator(); it.hasNext(); ) {
String name = (String) it.next();
File file;
if (fileSystem != null) {
file = fileSystem.getFile(name);
} else {
file = new File(name);
}
int sharability = SharabilityQuery.getSharability(file);
//System.out.println(" collectSharableSubfiles(): name '"+name+"' sharability = "+sharability);
if (sharability == SharabilityQuery.SHARABLE || file.isFile() && sharability == SharabilityQuery.UNKNOWN) {
if (file.isDirectory() && intermediateFolders != null && isEmptyDirectory(file)) {
intermediateFolders.add(name);
} else {
sharableFileNames.add(name);
}
//System.out.println("\t\t\tSHARABLE");
} else if (sharability == SharabilityQuery.MIXED || file.isDirectory() && sharability == SharabilityQuery.UNKNOWN) {
//System.out.println("\t\t\tMIXED; fo = "+file);
if (intermediateFolders != null) {
intermediateFolders.add(name);
}
addSharableSubfiles(sharableFileNames, intermediateFolders, file, name, recursive);
}
}
return sharableFileNames;
}
private void addSharableSubfiles(Collection sharableFileNames, Collection intermediateFolders, File file, String name, boolean recursive) {
//FileObject[] children = fo.getChildren();
File[] children = file.listFiles();
if (name.length() == 0 || name.length() == 1 && name.charAt(0) == '.') {
name = "";
} else {
name += "/";
}
for (int i = 0; i < children.length; i++) {
String chName = name + children[i].getName();
int sharability = SharabilityQuery.getSharability(children[i]);
boolean isDirectory = children[i].isDirectory();
//System.out.println(" addSharableSubfiles(): name '"+children[i]+"' sharability = "+sharability);
if (sharability == SharabilityQuery.SHARABLE || !isDirectory && sharability == SharabilityQuery.UNKNOWN) {
if (isDirectory && intermediateFolders != null && isEmptyDirectory(children[i])) {
intermediateFolders.add(chName);
} else {
sharableFileNames.add(chName);
}
} else if ((sharability == SharabilityQuery.MIXED || isDirectory && sharability == SharabilityQuery.UNKNOWN) && recursive) {
if (intermediateFolders != null) {
intermediateFolders.add(chName);
}
addSharableSubfiles(sharableFileNames, intermediateFolders, children[i], chName, recursive);
}
}
}
private FileObject[] getFiles(Collection fileNames, Collection diskFiles,
Pattern ignoreList, Pattern relevantList) {
if (fileSystem == null) {
for (Iterator it = fileNames.iterator(); it.hasNext(); ) {
String name = (String) it.next();
diskFiles.add(new File(name));
}
return new FileObject[0];
} else {
ArrayList fos = new ArrayList();
//FileCacheProvider cacheProvider = fileSystem.getCacheProvider();
FileSystemCache cache = null;
cache = CacheHandler.getInstance().getCache(fileSystem.getCacheIdStr());
Object locker = new Object();
/*
if (cache != null) {
//System.out.println("getting cache file ("+path+") = "+fileSystem.getFile(path));
cache.getCacheFile(new File(fileSystem.getFile(path), "testing"), CacheHandler.STRAT_DISK_OR_REFRESH_RECURS, locker);
VcsCacheDir cacheDir = (VcsCacheDir) cache.getCacheFile(fileSystem.getFile(path), CacheHandler.STRAT_DISK, locker);
}
*/
for (Iterator it = fileNames.iterator(); it.hasNext(); ) {
String path = (String) it.next();
String name = VcsUtilities.getFileNamePart(path);
if (fileSystem != null) {
String folder = VcsUtilities.getDirNamePart(path);
cache.getCacheFile(new File(fileSystem.getFile(folder), "testing"), CacheHandler.STRAT_DISK_OR_REFRESH, locker);
VcsCacheDir dir = (VcsCacheDir) cache.getCacheFile(fileSystem.getFile(folder), CacheHandler.STRAT_DISK, locker);
if (!dir.isIgnoreListSet() && fileSystem.getIgnoreListSupport() != null) {
dir.setIgnoreList(VcsUtilities.createIgnoreList(dir, dir.getFSPath(), fileSystem.getIgnoreListSupport()));
}
//Filter out files, that are ignored!!
if (dir.isIgnored(name)) continue;
}
if ((fileSystem != null && fileSystem.getFile(path).isFile()) ||
(fileSystem == null && new File(path).isFile())) {
if (ignoreList != null && ignoreList.matcher(name).matches()) {
continue;
}
if (relevantList != null && !relevantList.matcher(name).matches()) {
continue;
}
}
FileObject fo = fileSystem.findResource(path);
if (fo != null) {
fos.add(fo);
} else {
diskFiles.add(fileSystem.getFile(path));
}
}
return (FileObject[]) fos.toArray(new FileObject[0]);
}
}
private boolean isEmptyDirectory(File directory) {
File[] children = directory.listFiles();
if (children == null) return true;
FileSystemCache cache = CacheHandler.getInstance().getCache(fileSystem.getCacheIdStr());
Object locker = new Object();
cache.getCacheFile(new File(directory, "testing"), CacheHandler.STRAT_DISK_OR_REFRESH, locker);
VcsCacheDir dir = (VcsCacheDir) cache.getCacheFile(directory, CacheHandler.STRAT_DISK, locker);
for (int i = 0; i < children.length; i++) {
if (!dir.isIgnored(children[i].getName())) {
return false;
}
}
return true; // All subfiles are ignored
}
public boolean exec(Hashtable vars, String[] args,
final CommandOutputListener stdoutListener,
final CommandOutputListener stderrListener,
final CommandDataOutputListener stdoutDataListener, String dataRegex,
final CommandDataOutputListener stderrDataListener, String errorRegex) {
this.vars = vars;
int arglen = args.length;
//System.out.println("DIFF: args = "+VcsUtilities.arrayToString(args));
if (arglen < 1) {
stderrListener.outputLine("Too few arguments, expecting a name of a command "+
"and an optional name of the command that will handle intermediate mixed folders.\n"+
"Possible options: -ic
|
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.