merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / UCB / ResourceRemover.java
blob1f09a9d481bdc745f1e39f57c59a29d71c7c4855
1 /*************************************************************************
3 * $RCSfile: ResourceRemover.java,v $
5 * $Revision: 1.6 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:59:29 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
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
17 * are met:
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 /**
42 * Deleting a resource
44 public class ResourceRemover {
46 /**
47 * Member properties
49 private Helper m_helper;
50 private String m_contenturl = "";
51 private com.sun.star.ucb.XContent m_content;
53 /**
54 * Constructor.
56 *@param String[] This construtor requires the arguments:
57 * -url=... (optional)
58 * -workdir=... (optional)
59 * See Help (method printCmdLineUsage()).
60 * Without the arguments a new connection to a
61 * running office cannot created.
62 *@exception java.lang.Exception
64 public ResourceRemover( String args[] ) throws java.lang.Exception {
66 // Parse arguments
67 parseArguments( args );
69 // Init
70 m_helper = new Helper( getContentURL() );
72 // Create UCB content
73 m_content = m_helper.createUCBContent();
76 /**
77 * Delete resource.
79 *@return boolean Returns true if resource successfully deleted, false otherwise
80 *@exception com.sun.star.ucb.CommandAbortedException
81 *@exception com.sun.star.uno.Exception
83 public boolean deleteResource()
84 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
86 boolean result = false;
87 if ( m_content != null ) {
89 /////////////////////////////////////////////////////////////////////
90 // Destroy a resource physically...
91 /////////////////////////////////////////////////////////////////////
93 Boolean deletePhysically = new Boolean( true );
95 // Execute command "delete".
96 m_helper.executeCommand( m_content, "delete", deletePhysically );
97 result = true;
99 return result;
103 * Get connect URL.
105 *@return String That contains the connect URL
107 public String getContentURL() {
108 return m_contenturl;
112 * Parse arguments
114 *@param String[] Arguments
115 *@exception java.lang.Exception
117 public void parseArguments( String[] args ) throws java.lang.Exception {
119 String workdir = "";
121 for ( int i = 0; i < args.length; i++ ) {
122 if ( args[i].startsWith( "-url=" )) {
123 m_contenturl = args[i].substring( 5 );
124 } else if ( args[i].startsWith( "-workdir=" )) {
125 workdir = args[i].substring( 9 );
126 } else if ( args[i].startsWith( "-help" ) ||
127 args[i].startsWith( "-?" )) {
128 printCmdLineUsage();
129 System.exit( 0 );
133 if ( m_contenturl == null || m_contenturl.equals( "" )) {
134 m_contenturl = Helper.createTargetDataFile( workdir );
139 * Print the commands options
141 public void printCmdLineUsage() {
142 System.out.println(
143 "Usage : ResourceRemover -url=... -workdir=..." );
144 System.out.println(
145 "Defaults: -url=<workdir>/resource-<uniquepostfix> -workdir=<currentdir>" );
146 System.out.println(
147 "\nExample : -url=file:///temp/MyFile.txt \n" );
151 * Create a new connection with the specific args to a running office and
152 * delete a resource.
154 *@param String[] Arguments
156 public static void main ( String args[] ) {
158 System.out.println( "\n" );
159 System.out.println(
160 "-----------------------------------------------------------------" );
161 System.out.println(
162 "ResourceRemover - destroys a resource." );
163 System.out.println(
164 "-----------------------------------------------------------------" );
166 try {
167 ResourceRemover delete = new ResourceRemover( args );
168 boolean result = delete.deleteResource();
169 String url = delete.getContentURL();
170 if ( result ) {
171 System.out.println(
172 "Delete of resource " + url + " succeeded." );
173 } else {
174 System.out.println(
175 "Delete of resource " + url + " failed." );
177 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
178 System.out.println( "Error: " + e );
179 } catch ( com.sun.star.uno.Exception e ) {
180 System.out.println( "Error: " + e );
181 } catch ( java.lang.Exception e ) {
182 System.out.println( "Error: " + e );
184 System.exit( 0 );