|
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 the CVS Client Library. * The Initial Developer of the Original Code is Robert Greig. * Portions created by Robert Greig are Copyright (C) 2000. * All Rights Reserved. * * Contributor(s): Robert Greig. *****************************************************************************/ package org.netbeans.lib.cvsclient.command.add; import java.io.*; import java.util.*; import org.netbeans.lib.cvsclient.*; import org.netbeans.lib.cvsclient.admin.*; import org.netbeans.lib.cvsclient.command.*; import org.netbeans.lib.cvsclient.connection.*; import org.netbeans.lib.cvsclient.event.*; import org.netbeans.lib.cvsclient.request.*; /** * Adds a file or directory. * @author Robert Greig */ public class AddCommand extends BuildableCommand { /** * Constants that identify a message that creates a directory in * repository. */ private static final String DIR_ADDED = " added to the repository"; //NOI18N private static final String DIRECTORY = "Directory "; //NOI18N /** * The requests that are sent and processed. */ private List requests; /** * The argument requests that are collected and sent in the end just before the * add request. */ private final List argumentRequests = new LinkedList(); /** * The list of new directories. */ /* private HashMap newDirList; */ private final List newDirList = new LinkedList(); /** * The client services that are provided to this command. */ private ClientServices clientServices; /** * The files and/or directories to operate on. */ private File[] files; /** * Holds value of property message, (add's switch -m). */ private String message; /** * Holds value of property keywordSubst. */ private KeywordSubstitutionOptions keywordSubst; /** * Constructor. */ public AddCommand() { resetCVSCommand(); } /** * Set the files and/or directories on which to execute the command. * Sorts the paameter so that directories are first and files follow. * That way a directory and it's content will be passed correctly. * The user of the library has to specify all the files+dirs being added though. * This is just a sanity check, so that no unnessesary errors occur. */ public void setFiles(File[] files) { this.files = files; if (files == null) { return; } // sort array: directories first, files follow this.files = new File[files.length]; int dirCount = 0; int fileCount = 0; int totalCount = files.length; for (int index = 0; index < totalCount; index++) { File currentFile = files[index]; if (currentFile.isDirectory()) { this.files[dirCount] = currentFile; dirCount++; } else { this.files[totalCount - (1 + fileCount)] = currentFile; fileCount++; } } } /** * Get the files and/or directories specified for this command to operate * on. * @return the array of Files */ public File[] getFiles() { return files; } /** * @param ending - the ending part of the file's pathname.. path separator is cvs's default '/' */ public File getFileEndingWith(String ending) { String locEnding = ending.replace('\\', '/'); String localDir = getLocalDirectory().replace('\\','/'); int index = 0; for (index = 0; index < files.length; index++) { String path = files[index].getAbsolutePath(); String parentPath = files[index].getParentFile().getAbsolutePath().replace('\\', '/'); path = path.replace('\\', '/'); if ((path.endsWith(locEnding) && locEnding.indexOf('/') >= 0) || (files[index].getName().equals(locEnding) && parentPath.equals(localDir))) { return files[index]; } } return null; } /** * Getter for property message. * @return Value of property message. */ public String getMessage() { return message; } /** * Setter for property message. * @param message New value of property message. */ public void setMessage(String message) { this.message = message; } /** * Getter for property keywordSubst. * @return Value of property keywordSubst. */ public KeywordSubstitutionOptions getKeywordSubst() { return keywordSubst; } /** * Setter for property keywordSubst. * @param keywordSubst New value of property keywordSubst. */ public void setKeywordSubst(KeywordSubstitutionOptions keywordSubst) { this.keywordSubst = keywordSubst; } /** * Add requests for a particular file or directory to be added. */ protected void addRequests(File file) throws IOException, CommandException { if (file.isDirectory()) { addRequestsForDirectory(file, false); } else { addRequestsForFile(file); } } /** * Add requests for a particular directory. * @param directory the directory to add * @param adding - for the directory to be added, set to true. * used internally to recurse Directory requests. * @throws IOException if an error occurs */ private void addRequestsForDirectory(File directory, boolean recursion) throws IOException { File parentDirectory = directory.getParentFile(); String dir = recursion ? getRelativeToLocalPathInUnixStyle(directory) : getRelativeToLocalPathInUnixStyle(parentDirectory); String partPath; if (dir.equals(".")) { //NOI18N partPath = directory.getName(); } else { // trim the leading slash from the pathname we end up with // (e.g. we end up with something like \banana\foo // and this gives us banana\foo). Also replace backslashes with // forward slashes. The standard CVS server doesn't like // backslashes very much. partPath = dir + "/" + directory.getName(); //NOI18N // recursively scroll back to the localPath.. addRequestsForDirectory(parentDirectory, true); } if (recursion) { partPath = dir; } // Note that the repository file for the directory being added has not // been created yet, so we are forced to read the repository for // the parent directory and build the appropriate entry by tagging // on the directory name (called partPath here) String repository; String tag; if (recursion) { repository = clientServices.getRepositoryForDirectory( directory.getAbsolutePath()); tag = clientServices.getStickyTagForDirectory(directory); } else { repository = clientServices.getRepositoryForDirectory( parentDirectory.getAbsolutePath()); if (repository.endsWith(".")) { repository = repository.substring(0, repository.length() - 1) + directory.getName(); } else { repository = repository + "/" + directory.getName(); //NOI18N } tag = clientServices.getStickyTagForDirectory(parentDirectory); } requests.add(new DirectoryRequest(partPath, repository)); if (tag != null) { requests.add(new StickyRequest(tag)); } if (!recursion) { argumentRequests.add(new ArgumentRequest(partPath)); /* newDirList.put(partPath, repository); */ newDirList.add(new Paths(partPath, repository)); } // MK argument after Dir request.. also with the rel path from the current working dir } /** * Add requests for a particular file. */ protected void addRequestsForFile(File file) throws IOException { File directory = file.getParentFile(); String dir = getRelativeToLocalPathInUnixStyle(directory); String repository = clientServices.getRepositoryForDirectory( directory.getAbsolutePath()); requests.add(new DirectoryRequest(dir, repository)); String tag = clientServices.getStickyTagForDirectory(directory); if (tag != null) { requests.add(new StickyRequest(tag)); } Entry entry = clientServices.getEntry(file); if (entry != null) { requests.add(new EntryRequest(entry)); } else { boolean isBinary = (getKeywordSubst() == KeywordSubstitutionOptions.BINARY); requests.add(new ModifiedRequest(file, isBinary)); } if (dir.equals(".")) { //NOI18N argumentRequests.add(new ArgumentRequest(file.getName())); } else { argumentRequests.add(new ArgumentRequest(dir + "/" + file.getName())); //NOI18N } } /** * Execute a command. * @param client the client services object that provides any necessary * services to this command, including the ability to actually process * all the requests */ public void execute(ClientServices client, EventManager em) throws CommandException, AuthenticationException { if (files == null || files.length == 0) { throw new CommandException("No files have been specified for " + //NOI18N "adding.", CommandException.getLocalMessage("AddCommand.noFilesSpecified", null)); //NOI18N } client.ensureConnection(); clientServices = client; setLocalDirectory(client.getLocalPath()); /* newDirList = new HashMap(); */ newDirList.clear(); super.execute(client, em); requests = new LinkedList(); if (client.isFirstCommand()) { requests.add(new RootRequest(client.getRepository())); } // sets the message argument -m .. one for all files being sent.. String message = getMessage(); if (message != null) { message = message.trim(); } if (message != null && message.length() > 0) { addMessageRequest(message); } if (getKeywordSubst() != null && !getKeywordSubst().equals("")) { //NOI18N requests.add(new ArgumentRequest("-k" + getKeywordSubst())); //NOI18N } try { // current dir sent to server BEFORE and AFTER - kinda hack?? for (int i = 0; i < files.length; i++) { addRequests(files[i]); } // now add the request that indicates the working directory for the // command requests.add(new DirectoryRequest(".", //NOI18N client.getRepositoryForDirectory(getLocalDirectory()))); requests.addAll(argumentRequests); argumentRequests.clear(); // MK sanity check. requests.add(CommandRequest.ADD); client.processRequests(requests); } catch (CommandException ex) { throw ex; } catch (Exception ex) { throw new CommandException(ex, ex.getLocalizedMessage()); } finally { requests.clear(); } } private void addMessageRequest(String message) { requests.add(new ArgumentRequest("-m")); //NOI18N StringTokenizer token = new StringTokenizer(message, "\n", false); //NOI18N boolean first = true; while (token.hasMoreTokens()) { if (first) { requests.add(new ArgumentRequest(token.nextToken())); first = false; } else { requests.add(new ArgumentxRequest(token.nextToken())); } } } /** * This method returns how the command would look like when typed on the * command line. * Each command is responsible for constructing this information. * @returns |
... 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.