1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 /**
36  * Deleting a resource
37  */
38 public class ResourceRemover {
39 
40     /**
41      * Member properties
42      */
43     private  Helper   m_helper;
44     private  String   m_contenturl    = "";
45     private  com.sun.star.ucb.XContent m_content;
46 
47     /**
48      * Constructor.
49      *
50      *@param      String[]   This construtor requires the arguments:
51      *                          -url=...     (optional)
52      *                          -workdir=... (optional)
53      *                       See Help (method printCmdLineUsage()).
54      *                       Without the arguments a new connection to a
55      *                       running office cannot created.
56      *@exception  java.lang.Exception
57      */
58     public ResourceRemover( String args[] ) throws java.lang.Exception {
59 
60         // Parse arguments
61         parseArguments( args );
62 
63         // Init
64         m_helper       = new Helper( getContentURL() );
65 
66         // Create UCB content
67         m_content      = m_helper.createUCBContent();
68     }
69 
70     /**
71      *  Delete resource.
72      *
73      *@return     boolean   Returns true if resource successfully deleted, false otherwise
74      *@exception  com.sun.star.ucb.CommandAbortedException
75      *@exception  com.sun.star.uno.Exception
76      */
77     public boolean deleteResource()
78         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
79 
80         boolean result = false;
81         if ( m_content != null ) {
82 
83             /////////////////////////////////////////////////////////////////////
84             // Destroy a resource physically...
85             /////////////////////////////////////////////////////////////////////
86 
87             Boolean deletePhysically = new Boolean( true );
88 
89             // Execute command "delete".
90             m_helper.executeCommand( m_content, "delete", deletePhysically );
91             result = true;
92         }
93         return result;
94     }
95 
96     /**
97      *  Get connect URL.
98      *
99      *@return   String    That contains the connect URL
100      */
101     public String getContentURL() {
102         return m_contenturl;
103     }
104 
105     /**
106      * Parse arguments
107      *
108      *@param      String[]   Arguments
109      *@exception  java.lang.Exception
110      */
111     public void parseArguments( String[] args ) throws java.lang.Exception {
112 
113         String workdir = "";
114 
115         for ( int i = 0; i < args.length; i++ ) {
116             if ( args[i].startsWith( "-url=" )) {
117                 m_contenturl    = args[i].substring( 5 );
118             } else if ( args[i].startsWith( "-workdir=" )) {
119                 workdir = args[i].substring( 9 );
120             } else if ( args[i].startsWith( "-help" ) ||
121                         args[i].startsWith( "-?" )) {
122                 printCmdLineUsage();
123                 System.exit( 0 );
124             }
125          }
126 
127 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
128             m_contenturl = Helper.createTargetDataFile( workdir );
129         }
130     }
131 
132     /**
133      * Print the commands options
134      */
135     public void printCmdLineUsage() {
136         System.out.println(
137             "Usage   : ResourceRemover -url=... -workdir=..." );
138         System.out.println(
139             "Defaults: -url=<workdir>/resource-<uniquepostfix> -workdir=<currentdir>" );
140         System.out.println(
141             "\nExample  : -url=file:///temp/MyFile.txt \n" );
142     }
143 
144     /**
145      *  Create a new connection with the specific args to a running office and
146      *  delete a resource.
147      *
148      *@param  String[]   Arguments
149      */
150     public static void main ( String args[] ) {
151 
152         System.out.println( "\n" );
153 		System.out.println(
154             "-----------------------------------------------------------------" );
155 		System.out.println(
156             "ResourceRemover - destroys a resource." );
157 		System.out.println(
158             "-----------------------------------------------------------------" );
159 
160         try {
161             ResourceRemover delete = new ResourceRemover( args );
162             boolean result = delete.deleteResource();
163             String url = delete.getContentURL();
164             if ( result )  {
165                 System.out.println(
166                         "Delete of resource " + url + " succeeded." );
167             } else  {
168                 System.out.println(
169                         "Delete of resource " + url + " failed." );
170             }
171         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
172             System.out.println( "Error: " + e );
173         } catch ( com.sun.star.uno.Exception e ) {
174             System.out.println( "Error: " + e );
175         } catch ( java.lang.Exception e ) {
176             System.out.println( "Error: " + e );
177         }
178         System.exit( 0 );
179     }
180 }
181