1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * The Contents of this file are made available subject to the terms of 4*cdf0e10cSrcweir * the BSD license. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 7*cdf0e10cSrcweir * All rights reserved. 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * Redistribution and use in source and binary forms, with or without 10*cdf0e10cSrcweir * modification, are permitted provided that the following conditions 11*cdf0e10cSrcweir * are met: 12*cdf0e10cSrcweir * 1. Redistributions of source code must retain the above copyright 13*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer. 14*cdf0e10cSrcweir * 2. Redistributions in binary form must reproduce the above copyright 15*cdf0e10cSrcweir * notice, this list of conditions and the following disclaimer in the 16*cdf0e10cSrcweir * documentation and/or other materials provided with the distribution. 17*cdf0e10cSrcweir * 3. Neither the name of Sun Microsystems, Inc. nor the names of its 18*cdf0e10cSrcweir * contributors may be used to endorse or promote products derived 19*cdf0e10cSrcweir * from this software without specific prior written permission. 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22*cdf0e10cSrcweir * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23*cdf0e10cSrcweir * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24*cdf0e10cSrcweir * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25*cdf0e10cSrcweir * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26*cdf0e10cSrcweir * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 27*cdf0e10cSrcweir * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 28*cdf0e10cSrcweir * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29*cdf0e10cSrcweir * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 30*cdf0e10cSrcweir * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 31*cdf0e10cSrcweir * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32*cdf0e10cSrcweir * 33*cdf0e10cSrcweir *************************************************************************/ 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir import com.sun.star.ucb.NameClash; 36*cdf0e10cSrcweir import com.sun.star.ucb.TransferCommandOperation; 37*cdf0e10cSrcweir import com.sun.star.ucb.GlobalTransferCommandArgument; 38*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 39*cdf0e10cSrcweir import com.sun.star.uno.XInterface; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir /** 42*cdf0e10cSrcweir * Copying, Moving and Creating Links to a Resource 43*cdf0e10cSrcweir */ 44*cdf0e10cSrcweir public class ResourceManager { 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir /** 47*cdf0e10cSrcweir * Member properties 48*cdf0e10cSrcweir */ 49*cdf0e10cSrcweir private Helper m_helper; 50*cdf0e10cSrcweir private XInterface m_ucb; 51*cdf0e10cSrcweir private String m_contenturl = ""; 52*cdf0e10cSrcweir private String m_srcURL = ""; 53*cdf0e10cSrcweir private String m_targetFolderURL = ""; 54*cdf0e10cSrcweir private String m_newTitle = ""; 55*cdf0e10cSrcweir private String m_transOperation = ""; 56*cdf0e10cSrcweir 57*cdf0e10cSrcweir /** 58*cdf0e10cSrcweir * Constructor. 59*cdf0e10cSrcweir * 60*cdf0e10cSrcweir *@param String[] This construtor requires the arguments: 61*cdf0e10cSrcweir * -url=... (optional) 62*cdf0e10cSrcweir * -targetFolderURL=... (optional) 63*cdf0e10cSrcweir * -newTitle=... (optional) 64*cdf0e10cSrcweir * -transOper=... (optional) 65*cdf0e10cSrcweir * -workdir=... (optional) 66*cdf0e10cSrcweir * See Help (method printCmdLineUsage()). 67*cdf0e10cSrcweir * Without the arguments a new connection to a 68*cdf0e10cSrcweir * running office cannot created. 69*cdf0e10cSrcweir *@exception java.lang.Exception 70*cdf0e10cSrcweir */ 71*cdf0e10cSrcweir public ResourceManager( String args[] ) throws java.lang.Exception { 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir // Parse arguments 74*cdf0e10cSrcweir parseArguments( args ); 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir // Init 77*cdf0e10cSrcweir m_helper = new Helper( getContentURL() ); 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir // Get xUCB 80*cdf0e10cSrcweir m_ucb = m_helper.getUCB(); 81*cdf0e10cSrcweir } 82*cdf0e10cSrcweir 83*cdf0e10cSrcweir /** 84*cdf0e10cSrcweir * Copy, move or create a link for a resource. 85*cdf0e10cSrcweir * This method requires the main and the optional arguments to be set in order to work. 86*cdf0e10cSrcweir * See Constructor. 87*cdf0e10cSrcweir * 88*cdf0e10cSrcweir *@return boolean Returns true if resource successfully transfered, false otherwise 89*cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 90*cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 91*cdf0e10cSrcweir */ 92*cdf0e10cSrcweir public boolean transferResource() 93*cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 94*cdf0e10cSrcweir String sourceURL = getContentURL(); // URL of the source object 95*cdf0e10cSrcweir String targetFolderURL = getTargetFolderURL(); // URL of the target folder 96*cdf0e10cSrcweir String newTitle = getNewTitle(); // New name for the resource 97*cdf0e10cSrcweir String transOperation = getTransOperation(); 98*cdf0e10cSrcweir return transferResource( sourceURL, targetFolderURL, newTitle, transOperation ); 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir 101*cdf0e10cSrcweir /** 102*cdf0e10cSrcweir * Copy, move or create a link for a resource. 103*cdf0e10cSrcweir * 104*cdf0e10cSrcweir *@param String Source URL 105*cdf0e10cSrcweir *@param String Target folder URL 106*cdf0e10cSrcweir *@param String Transfering operation (copy, move, link) 107*cdf0e10cSrcweir *@return boolean Returns true if resource successfully transfered, false otherwise 108*cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 109*cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 110*cdf0e10cSrcweir */ 111*cdf0e10cSrcweir public boolean transferResource( 112*cdf0e10cSrcweir String sourceURL, String targetFolderURL, 113*cdf0e10cSrcweir String newTitle, String transOperation ) 114*cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir boolean result = false; 117*cdf0e10cSrcweir if ( m_ucb != null && sourceURL != null && !sourceURL.equals( "" ) && 118*cdf0e10cSrcweir targetFolderURL != null && !targetFolderURL.equals( "" ) && 119*cdf0e10cSrcweir newTitle != null && transOperation != null && !transOperation.equals( "" ) && 120*cdf0e10cSrcweir ( transOperation.equals( "copy" ) || transOperation.equals( "move" ) || 121*cdf0e10cSrcweir transOperation.equals( "link" ))) { 122*cdf0e10cSrcweir 123*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 124*cdf0e10cSrcweir // Copy, move or create a link for a resource to another location... 125*cdf0e10cSrcweir ///////////////////////////////////////////////////////////////////// 126*cdf0e10cSrcweir GlobalTransferCommandArgument arg = new GlobalTransferCommandArgument(); 127*cdf0e10cSrcweir if ( transOperation.equals( "copy" )) { 128*cdf0e10cSrcweir arg.Operation = TransferCommandOperation.COPY; 129*cdf0e10cSrcweir } else if ( transOperation.equals( "move" )) { 130*cdf0e10cSrcweir arg.Operation = TransferCommandOperation.MOVE; 131*cdf0e10cSrcweir } else if ( transOperation.equals( "link" )) { 132*cdf0e10cSrcweir arg.Operation = TransferCommandOperation.LINK; 133*cdf0e10cSrcweir } 134*cdf0e10cSrcweir arg.SourceURL = sourceURL; 135*cdf0e10cSrcweir arg.TargetURL = targetFolderURL; 136*cdf0e10cSrcweir 137*cdf0e10cSrcweir // object get a new unique name 138*cdf0e10cSrcweir arg.NewTitle = newTitle; 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir // fail, if object with same name exists in target folder 141*cdf0e10cSrcweir arg.NameClash = NameClash.ERROR; 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir // Let UCB execute the command "globalTransfer". 144*cdf0e10cSrcweir m_helper.executeCommand( m_ucb, "globalTransfer", arg ); 145*cdf0e10cSrcweir result = true; 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir return result; 148*cdf0e10cSrcweir } 149*cdf0e10cSrcweir 150*cdf0e10cSrcweir /** 151*cdf0e10cSrcweir * Get connect URL. 152*cdf0e10cSrcweir * 153*cdf0e10cSrcweir *@return String That contains the connect URL 154*cdf0e10cSrcweir */ 155*cdf0e10cSrcweir public String getContentURL() { 156*cdf0e10cSrcweir return m_contenturl; 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir /** 160*cdf0e10cSrcweir * Get trasfering Operation. 161*cdf0e10cSrcweir * 162*cdf0e10cSrcweir *@return String That contains the trasfering Operation 163*cdf0e10cSrcweir */ 164*cdf0e10cSrcweir public String getTransOperation() { 165*cdf0e10cSrcweir return m_transOperation; 166*cdf0e10cSrcweir } 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir /** 169*cdf0e10cSrcweir * Get target folder URL. 170*cdf0e10cSrcweir * 171*cdf0e10cSrcweir *@return String That contains the target folder URL 172*cdf0e10cSrcweir */ 173*cdf0e10cSrcweir public String getTargetFolderURL() { 174*cdf0e10cSrcweir return m_targetFolderURL; 175*cdf0e10cSrcweir } 176*cdf0e10cSrcweir 177*cdf0e10cSrcweir /** 178*cdf0e10cSrcweir * Get new title for the resource to be transfered. 179*cdf0e10cSrcweir * 180*cdf0e10cSrcweir *@return String That contains a new title for the transfered 181*cdf0e10cSrcweir * resource. Can be empty. In this case resource 182*cdf0e10cSrcweir * will keep the title it has in the source folder. 183*cdf0e10cSrcweir */ 184*cdf0e10cSrcweir public String getNewTitle() { 185*cdf0e10cSrcweir return m_newTitle; 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir 188*cdf0e10cSrcweir /** 189*cdf0e10cSrcweir * Parse arguments 190*cdf0e10cSrcweir * 191*cdf0e10cSrcweir *@param String[] Arguments 192*cdf0e10cSrcweir *@exception java.lang.Exception 193*cdf0e10cSrcweir */ 194*cdf0e10cSrcweir public void parseArguments( String[] args ) throws java.lang.Exception { 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir String workdir = ""; 197*cdf0e10cSrcweir 198*cdf0e10cSrcweir for ( int i = 0; i < args.length; i++ ) { 199*cdf0e10cSrcweir if ( args[i].startsWith( "-url=" )) { 200*cdf0e10cSrcweir m_contenturl = args[i].substring( 5 ); 201*cdf0e10cSrcweir } else if ( args[i].startsWith( "-targetFolderURL=" )) { 202*cdf0e10cSrcweir m_targetFolderURL = args[i].substring( 17 ); 203*cdf0e10cSrcweir } else if ( args[i].startsWith( "-newTitle=" )) { 204*cdf0e10cSrcweir m_newTitle = args[i].substring( 10 ); 205*cdf0e10cSrcweir } else if ( args[i].startsWith( "-transOper=" )) { 206*cdf0e10cSrcweir m_transOperation = args[i].substring( 11 ); 207*cdf0e10cSrcweir } else if ( args[i].startsWith( "-workdir=" )) { 208*cdf0e10cSrcweir workdir = args[i].substring( 9 ); 209*cdf0e10cSrcweir } else if ( args[i].startsWith( "-help" ) || 210*cdf0e10cSrcweir args[i].startsWith( "-?" )) { 211*cdf0e10cSrcweir printCmdLineUsage(); 212*cdf0e10cSrcweir System.exit( 0 ); 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir if ( m_contenturl == null || m_contenturl.equals( "" )) { 217*cdf0e10cSrcweir m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );; 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir if ( m_targetFolderURL == null || m_targetFolderURL.equals( "" )) { 221*cdf0e10cSrcweir m_targetFolderURL = Helper.getAbsoluteFileURLFromSystemPath( workdir ); 222*cdf0e10cSrcweir } 223*cdf0e10cSrcweir 224*cdf0e10cSrcweir if ( m_newTitle == null || m_newTitle.equals( "" )) { 225*cdf0e10cSrcweir m_newTitle = "transfered-resource-" + System.currentTimeMillis(); 226*cdf0e10cSrcweir } 227*cdf0e10cSrcweir 228*cdf0e10cSrcweir if ( m_transOperation == null || m_transOperation.equals( "" )) { 229*cdf0e10cSrcweir m_transOperation = "copy"; 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir 233*cdf0e10cSrcweir /** 234*cdf0e10cSrcweir * Print the commands options 235*cdf0e10cSrcweir */ 236*cdf0e10cSrcweir public void printCmdLineUsage() { 237*cdf0e10cSrcweir System.out.println( 238*cdf0e10cSrcweir "Usage: ResourceManager -url=... -targetFolderURL=... -newTitle=... -transOper=... -workdir=..." ); 239*cdf0e10cSrcweir System.out.println( 240*cdf0e10cSrcweir "Defaults: -url=<currentdir>/data/data.txt> -targetFolderURL=<workdir> -newTitle=transfered-resource-<uniquepostfix> -transOper=copy -workdir=<currentdir>"); 241*cdf0e10cSrcweir System.out.println( 242*cdf0e10cSrcweir "\nExample : -url=file:///temp/MyFile.txt -targetFolderURL=file:///test/ -newTitle=RenamedFile.txt -transOper=copy " ); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir 245*cdf0e10cSrcweir /** 246*cdf0e10cSrcweir * Create a new connection with the specific args to a running office and 247*cdf0e10cSrcweir * copy, move or create links a resource. 248*cdf0e10cSrcweir * 249*cdf0e10cSrcweir *@param String[] Arguments 250*cdf0e10cSrcweir */ 251*cdf0e10cSrcweir public static void main ( String args[] ) { 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir System.out.println( "\n" ); 254*cdf0e10cSrcweir System.out.println( 255*cdf0e10cSrcweir "-----------------------------------------------------------------" ); 256*cdf0e10cSrcweir System.out.println( 257*cdf0e10cSrcweir "ResourceManager - copies/moves a resource." ); 258*cdf0e10cSrcweir System.out.println( 259*cdf0e10cSrcweir "-----------------------------------------------------------------" ); 260*cdf0e10cSrcweir 261*cdf0e10cSrcweir try { 262*cdf0e10cSrcweir ResourceManager transResource = new ResourceManager( args ); 263*cdf0e10cSrcweir String sourceURL = transResource.getContentURL(); 264*cdf0e10cSrcweir String targetFolderURL = transResource.getTargetFolderURL(); 265*cdf0e10cSrcweir String newTitle = transResource.getNewTitle(); 266*cdf0e10cSrcweir String transOperation = transResource.getTransOperation(); 267*cdf0e10cSrcweir boolean result = transResource.transferResource( 268*cdf0e10cSrcweir sourceURL, targetFolderURL, newTitle, transOperation ); 269*cdf0e10cSrcweir if ( result ) 270*cdf0e10cSrcweir System.out.println( "\nTransfering resource succeeded." ); 271*cdf0e10cSrcweir else 272*cdf0e10cSrcweir System.out.println( "Transfering resource failed." ); 273*cdf0e10cSrcweir 274*cdf0e10cSrcweir System.out.println( " Source URL : " + sourceURL ); 275*cdf0e10cSrcweir System.out.println( " Target Folder URL : " + targetFolderURL ); 276*cdf0e10cSrcweir System.out.println( " New name : " + newTitle ); 277*cdf0e10cSrcweir System.out.println( " Transfer Operation: " + transOperation ); 278*cdf0e10cSrcweir 279*cdf0e10cSrcweir 280*cdf0e10cSrcweir } catch ( com.sun.star.ucb.CommandAbortedException e ) { 281*cdf0e10cSrcweir System.out.println( "Error: " + e ); 282*cdf0e10cSrcweir } catch ( com.sun.star.uno.Exception e ) { 283*cdf0e10cSrcweir System.out.println( "Error: " + e ); 284*cdf0e10cSrcweir } catch ( java.lang.Exception e ) { 285*cdf0e10cSrcweir System.out.println( "Error: " + e ); 286*cdf0e10cSrcweir } 287*cdf0e10cSrcweir System.exit( 0 ); 288*cdf0e10cSrcweir } 289*cdf0e10cSrcweir } 290