1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 /**
25  * Deleting a resource
26  */
27 public class ResourceRemover {
28 
29     /**
30      * Member properties
31      */
32     private  Helper   m_helper;
33     private  String   m_contenturl    = "";
34     private  com.sun.star.ucb.XContent m_content;
35 
36     /**
37      * Constructor.
38      *
39      *@param      String[]   This construtor requires the arguments:
40      *                          -url=...     (optional)
41      *                          -workdir=... (optional)
42      *                       See Help (method printCmdLineUsage()).
43      *                       Without the arguments a new connection to a
44      *                       running office cannot created.
45      *@exception  java.lang.Exception
46      */
ResourceRemover( String args[] )47     public ResourceRemover( String args[] ) throws java.lang.Exception {
48 
49         // Parse arguments
50         parseArguments( args );
51 
52         // Init
53         m_helper       = new Helper( getContentURL() );
54 
55         // Create UCB content
56         m_content      = m_helper.createUCBContent();
57     }
58 
59     /**
60      *  Delete resource.
61      *
62      *@return     boolean   Returns true if resource successfully deleted, false otherwise
63      *@exception  com.sun.star.ucb.CommandAbortedException
64      *@exception  com.sun.star.uno.Exception
65      */
deleteResource()66     public boolean deleteResource()
67         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
68 
69         boolean result = false;
70         if ( m_content != null ) {
71 
72             /////////////////////////////////////////////////////////////////////
73             // Destroy a resource physically...
74             /////////////////////////////////////////////////////////////////////
75 
76             Boolean deletePhysically = new Boolean( true );
77 
78             // Execute command "delete".
79             m_helper.executeCommand( m_content, "delete", deletePhysically );
80             result = true;
81         }
82         return result;
83     }
84 
85     /**
86      *  Get connect URL.
87      *
88      *@return   String    That contains the connect URL
89      */
getContentURL()90     public String getContentURL() {
91         return m_contenturl;
92     }
93 
94     /**
95      * Parse arguments
96      *
97      *@param      String[]   Arguments
98      *@exception  java.lang.Exception
99      */
parseArguments( String[] args )100     public void parseArguments( String[] args ) throws java.lang.Exception {
101 
102         String workdir = "";
103 
104         for ( int i = 0; i < args.length; i++ ) {
105             if ( args[i].startsWith( "-url=" )) {
106                 m_contenturl    = args[i].substring( 5 );
107             } else if ( args[i].startsWith( "-workdir=" )) {
108                 workdir = args[i].substring( 9 );
109             } else if ( args[i].startsWith( "-help" ) ||
110                         args[i].startsWith( "-?" )) {
111                 printCmdLineUsage();
112                 System.exit( 0 );
113             }
114          }
115 
116 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
117             m_contenturl = Helper.createTargetDataFile( workdir );
118         }
119     }
120 
121     /**
122      * Print the commands options
123      */
printCmdLineUsage()124     public void printCmdLineUsage() {
125         System.out.println(
126             "Usage   : ResourceRemover -url=... -workdir=..." );
127         System.out.println(
128             "Defaults: -url=<workdir>/resource-<uniquepostfix> -workdir=<currentdir>" );
129         System.out.println(
130             "\nExample  : -url=file:///temp/MyFile.txt \n" );
131     }
132 
133     /**
134      *  Create a new connection with the specific args to a running office and
135      *  delete a resource.
136      *
137      *@param  String[]   Arguments
138      */
main( String args[] )139     public static void main ( String args[] ) {
140 
141         System.out.println( "\n" );
142 		System.out.println(
143             "-----------------------------------------------------------------" );
144 		System.out.println(
145             "ResourceRemover - destroys a resource." );
146 		System.out.println(
147             "-----------------------------------------------------------------" );
148 
149         try {
150             ResourceRemover delete = new ResourceRemover( args );
151             boolean result = delete.deleteResource();
152             String url = delete.getContentURL();
153             if ( result )  {
154                 System.out.println(
155                         "Delete of resource " + url + " succeeded." );
156             } else  {
157                 System.out.println(
158                         "Delete of resource " + url + " failed." );
159             }
160         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
161             System.out.println( "Error: " + e );
162         } catch ( com.sun.star.uno.Exception e ) {
163             System.out.println( "Error: " + e );
164         } catch ( java.lang.Exception e ) {
165             System.out.println( "Error: " + e );
166         }
167         System.exit( 0 );
168     }
169 }
170