/plugins/SVNPlugin/tags/1.0.0/src/ise/plugin/svn/command/Delete.java

# · Java · 159 lines · 99 code · 21 blank · 39 comment · 22 complexity · 919a59edc6c9d9a6f79ae7c74967ad54 MD5 · raw file

  1. /*
  2. Copyright (c) 2007, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the author nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package ise.plugin.svn.command;
  26. import java.io.*;
  27. import java.util.*;
  28. import org.tmatesoft.svn.cli.command.SVNCommandEventProcessor;
  29. import org.tmatesoft.svn.core.SVNCommitInfo;
  30. import org.tmatesoft.svn.core.SVNErrorCode;
  31. import org.tmatesoft.svn.core.SVNException;
  32. import org.tmatesoft.svn.core.SVNURL;
  33. import org.tmatesoft.svn.core.wc.ISVNOptions;
  34. import org.tmatesoft.svn.core.wc.SVNClientManager;
  35. import org.tmatesoft.svn.core.wc.SVNCommitClient;
  36. import org.tmatesoft.svn.core.wc.SVNEvent;
  37. import org.tmatesoft.svn.core.wc.SVNWCClient;
  38. import org.tmatesoft.svn.core.wc.SVNWCUtil;
  39. import ise.plugin.svn.data.DeleteData;
  40. import ise.plugin.svn.data.DeleteResults;
  41. public class Delete {
  42. public DeleteResults delete( DeleteData data ) throws CommandInitializationException, SVNException {
  43. SVNKit.setupLibrary();
  44. // validate data values
  45. if ( data.getPaths() == null ) {
  46. return null; // nothing to do
  47. }
  48. if ( data.getOut() == null ) {
  49. throw new CommandInitializationException( "Invalid output stream." );
  50. }
  51. if ( data.getErr() == null ) {
  52. data.setErr( data.getOut() );
  53. }
  54. // use default svn config options
  55. ISVNOptions options = SVNWCUtil.createDefaultOptions( true );
  56. // use the svnkit client manager
  57. SVNClientManager clientManager = SVNClientManager.newInstance( options, data.getUsername(), data.getDecryptedPassword() );
  58. if ( !data.pathsAreURLs() ) {
  59. // working copies, convert paths to Files
  60. List<String> paths = data.getPaths();
  61. File[] localPaths = new File[ paths.size() ];
  62. for ( int i = 0; i < paths.size(); i++ ) {
  63. localPaths[ i ] = new File( paths.get( i ) );
  64. // check for file existence?
  65. }
  66. // get a commit client
  67. SVNWCClient client = clientManager.getWCClient();
  68. // set an event handler so that messages go to the commit data streams for display
  69. client.setEventHandler( new SVNCommandEventProcessor( data.getOut(), data.getErr(), false ) );
  70. // actually do the deletes(s)
  71. PrintStream out = data.getOut();
  72. DeleteResults results = new DeleteResults();
  73. for ( String path : paths ) {
  74. try {
  75. File file = new File( path );
  76. client.doDelete( file, data.getForce(), data.getDeleteFiles(), data.getDryRun() );
  77. results.addPath( path );
  78. }
  79. catch ( Exception e ) {
  80. out.println( e.getMessage() );
  81. results.addErrorPath( path, e.getMessage() );
  82. }
  83. }
  84. out.flush();
  85. out.close();
  86. return results;
  87. }
  88. else {
  89. // remote urls, need to use a commit client
  90. SVNCommitClient client = clientManager.getCommitClient();
  91. DeleteResults results = new DeleteResults();
  92. List<String> paths = data.getPaths();
  93. SVNURL[] remotePaths = new SVNURL[ paths.size() ];
  94. for ( int i = 0; i < paths.size(); i++ ) {
  95. remotePaths[ i ] = SVNURL.parseURIDecoded( paths.get( i ) );
  96. }
  97. // set an event handler so that messages go to the commit data streams for display
  98. client.setEventHandler( new SVNCommandEventProcessor( data.getOut(), data.getErr(), false ) );
  99. // actually do the delete
  100. String commitMessage = data.getCommitMessage();
  101. if ( commitMessage == null || commitMessage.length() == 0 ) {
  102. commitMessage = "no message";
  103. }
  104. SVNCommitInfo info = client.doDelete( remotePaths, commitMessage );
  105. // handle the results
  106. PrintStream out = data.getOut();
  107. if ( info != SVNCommitInfo.NULL ) {
  108. out.println();
  109. out.println( "Deleted, revision " + info.getNewRevision() + "." );
  110. results.setRevision( info.getNewRevision() );
  111. out.flush();
  112. }
  113. else {
  114. out.println();
  115. String msg = "Delete failed";
  116. if ( info.getErrorMessage() != null ) {
  117. out.println( "Delete failed:" );
  118. out.println( info.getErrorMessage() );
  119. msg = ": " + info.getErrorMessage();
  120. }
  121. else {
  122. out.println( "Delete failed." );
  123. msg += ".";
  124. }
  125. for ( String path : data.getPaths() ) {
  126. results.addErrorPath( path, msg );
  127. }
  128. out.flush();
  129. }
  130. out.close();
  131. results.addPaths( data.getPaths() );
  132. return results;
  133. }
  134. }
  135. }