1 /*************************************************************************
3 * $RCSfile: ResourceManager.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:59:15 $
9 * The Contents of this file are made available subject to the terms of
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 import com
.sun
.star
.ucb
.NameClash
;
42 import com
.sun
.star
.ucb
.TransferCommandOperation
;
43 import com
.sun
.star
.ucb
.GlobalTransferCommandArgument
;
44 import com
.sun
.star
.uno
.UnoRuntime
;
45 import com
.sun
.star
.uno
.XInterface
;
48 * Copying, Moving and Creating Links to a Resource
50 public class ResourceManager
{
55 private Helper m_helper
;
56 private XInterface m_ucb
;
57 private String m_contenturl
= "";
58 private String m_srcURL
= "";
59 private String m_targetFolderURL
= "";
60 private String m_newTitle
= "";
61 private String m_transOperation
= "";
66 *@param String[] This construtor requires the arguments:
68 * -targetFolderURL=... (optional)
69 * -newTitle=... (optional)
70 * -transOper=... (optional)
71 * -workdir=... (optional)
72 * See Help (method printCmdLineUsage()).
73 * Without the arguments a new connection to a
74 * running office cannot created.
75 *@exception java.lang.Exception
77 public ResourceManager( String args
[] ) throws java
.lang
.Exception
{
80 parseArguments( args
);
83 m_helper
= new Helper( getContentURL() );
86 m_ucb
= m_helper
.getUCB();
90 * Copy, move or create a link for a resource.
91 * This method requires the main and the optional arguments to be set in order to work.
94 *@return boolean Returns true if resource successfully transfered, false otherwise
95 *@exception com.sun.star.ucb.CommandAbortedException
96 *@exception com.sun.star.uno.Exception
98 public boolean transferResource()
99 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
100 String sourceURL
= getContentURL(); // URL of the source object
101 String targetFolderURL
= getTargetFolderURL(); // URL of the target folder
102 String newTitle
= getNewTitle(); // New name for the resource
103 String transOperation
= getTransOperation();
104 return transferResource( sourceURL
, targetFolderURL
, newTitle
, transOperation
);
108 * Copy, move or create a link for a resource.
110 *@param String Source URL
111 *@param String Target folder URL
112 *@param String Transfering operation (copy, move, link)
113 *@return boolean Returns true if resource successfully transfered, false otherwise
114 *@exception com.sun.star.ucb.CommandAbortedException
115 *@exception com.sun.star.uno.Exception
117 public boolean transferResource(
118 String sourceURL
, String targetFolderURL
,
119 String newTitle
, String transOperation
)
120 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
122 boolean result
= false;
123 if ( m_ucb
!= null && sourceURL
!= null && !sourceURL
.equals( "" ) &&
124 targetFolderURL
!= null && !targetFolderURL
.equals( "" ) &&
125 newTitle
!= null && transOperation
!= null && !transOperation
.equals( "" ) &&
126 ( transOperation
.equals( "copy" ) || transOperation
.equals( "move" ) ||
127 transOperation
.equals( "link" ))) {
129 /////////////////////////////////////////////////////////////////////
130 // Copy, move or create a link for a resource to another location...
131 /////////////////////////////////////////////////////////////////////
132 GlobalTransferCommandArgument arg
= new GlobalTransferCommandArgument();
133 if ( transOperation
.equals( "copy" )) {
134 arg
.Operation
= TransferCommandOperation
.COPY
;
135 } else if ( transOperation
.equals( "move" )) {
136 arg
.Operation
= TransferCommandOperation
.MOVE
;
137 } else if ( transOperation
.equals( "link" )) {
138 arg
.Operation
= TransferCommandOperation
.LINK
;
140 arg
.SourceURL
= sourceURL
;
141 arg
.TargetURL
= targetFolderURL
;
143 // object get a new unique name
144 arg
.NewTitle
= newTitle
;
146 // fail, if object with same name exists in target folder
147 arg
.NameClash
= NameClash
.ERROR
;
149 // Let UCB execute the command "globalTransfer".
150 m_helper
.executeCommand( m_ucb
, "globalTransfer", arg
);
159 *@return String That contains the connect URL
161 public String
getContentURL() {
166 * Get trasfering Operation.
168 *@return String That contains the trasfering Operation
170 public String
getTransOperation() {
171 return m_transOperation
;
175 * Get target folder URL.
177 *@return String That contains the target folder URL
179 public String
getTargetFolderURL() {
180 return m_targetFolderURL
;
184 * Get new title for the resource to be transfered.
186 *@return String That contains a new title for the transfered
187 * resource. Can be empty. In this case resource
188 * will keep the title it has in the source folder.
190 public String
getNewTitle() {
197 *@param String[] Arguments
198 *@exception java.lang.Exception
200 public void parseArguments( String
[] args
) throws java
.lang
.Exception
{
204 for ( int i
= 0; i
< args
.length
; i
++ ) {
205 if ( args
[i
].startsWith( "-url=" )) {
206 m_contenturl
= args
[i
].substring( 5 );
207 } else if ( args
[i
].startsWith( "-targetFolderURL=" )) {
208 m_targetFolderURL
= args
[i
].substring( 17 );
209 } else if ( args
[i
].startsWith( "-newTitle=" )) {
210 m_newTitle
= args
[i
].substring( 10 );
211 } else if ( args
[i
].startsWith( "-transOper=" )) {
212 m_transOperation
= args
[i
].substring( 11 );
213 } else if ( args
[i
].startsWith( "-workdir=" )) {
214 workdir
= args
[i
].substring( 9 );
215 } else if ( args
[i
].startsWith( "-help" ) ||
216 args
[i
].startsWith( "-?" )) {
222 if ( m_contenturl
== null || m_contenturl
.equals( "" )) {
223 m_contenturl
= Helper
.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );;
226 if ( m_targetFolderURL
== null || m_targetFolderURL
.equals( "" )) {
227 m_targetFolderURL
= Helper
.getAbsoluteFileURLFromSystemPath( workdir
);
230 if ( m_newTitle
== null || m_newTitle
.equals( "" )) {
231 m_newTitle
= "transfered-resource-" + System
.currentTimeMillis();
234 if ( m_transOperation
== null || m_transOperation
.equals( "" )) {
235 m_transOperation
= "copy";
240 * Print the commands options
242 public void printCmdLineUsage() {
244 "Usage: ResourceManager -url=... -targetFolderURL=... -newTitle=... -transOper=... -workdir=..." );
246 "Defaults: -url=<currentdir>/data/data.txt> -targetFolderURL=<workdir> -newTitle=transfered-resource-<uniquepostfix> -transOper=copy -workdir=<currentdir>");
248 "\nExample : -url=file:///temp/MyFile.txt -targetFolderURL=file:///test/ -newTitle=RenamedFile.txt -transOper=copy " );
252 * Create a new connection with the specific args to a running office and
253 * copy, move or create links a resource.
255 *@param String[] Arguments
257 public static void main ( String args
[] ) {
259 System
.out
.println( "\n" );
261 "-----------------------------------------------------------------" );
263 "ResourceManager - copies/moves a resource." );
265 "-----------------------------------------------------------------" );
268 ResourceManager transResource
= new ResourceManager( args
);
269 String sourceURL
= transResource
.getContentURL();
270 String targetFolderURL
= transResource
.getTargetFolderURL();
271 String newTitle
= transResource
.getNewTitle();
272 String transOperation
= transResource
.getTransOperation();
273 boolean result
= transResource
.transferResource(
274 sourceURL
, targetFolderURL
, newTitle
, transOperation
);
276 System
.out
.println( "\nTransfering resource succeeded." );
278 System
.out
.println( "Transfering resource failed." );
280 System
.out
.println( " Source URL : " + sourceURL
);
281 System
.out
.println( " Target Folder URL : " + targetFolderURL
);
282 System
.out
.println( " New name : " + newTitle
);
283 System
.out
.println( " Transfer Operation: " + transOperation
);
286 } catch ( com
.sun
.star
.ucb
.CommandAbortedException e
) {
287 System
.out
.println( "Error: " + e
);
288 } catch ( com
.sun
.star
.uno
.Exception e
) {
289 System
.out
.println( "Error: " + e
);
290 } catch ( java
.lang
.Exception e
) {
291 System
.out
.println( "Error: " + e
);