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