Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / ResourceRemover.java
blobe6ac8fc1cd14fad3b6ba753c0a57cb09191756f0
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 /**
37 * Deleting a resource
39 public class ResourceRemover {
41 /**
42 * Member properties
44 private Helper m_helper;
45 private String m_contenturl = "";
46 private com.sun.star.ucb.XContent m_content;
48 /**
49 * Constructor.
51 *@param args This constructor requires the arguments:
52 * -url=... (optional)
53 * -workdir=... (optional)
54 * See Help (method printCmdLineUsage()).
55 * Without the arguments a new connection to a
56 * running office cannot created.
58 public ResourceRemover( String args[] ) throws java.lang.Exception {
60 // Parse arguments
61 parseArguments( args );
63 // Init
64 m_helper = new Helper( getContentURL() );
66 // Create UCB content
67 m_content = m_helper.createUCBContent();
70 /**
71 * Delete resource.
73 *@return true if resource successfully deleted, false otherwise
75 public boolean deleteResource()
76 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
78 boolean result = false;
79 if ( m_content != null ) {
82 // Destroy a resource physically...
85 Boolean deletePhysically = Boolean.TRUE;
87 // Execute command "delete".
88 m_helper.executeCommand( m_content, "delete", deletePhysically );
89 result = true;
91 return result;
94 /**
95 * Get connect URL.
97 *@return String That contains the connect URL
99 public String getContentURL() {
100 return m_contenturl;
104 * Parse arguments
106 public void parseArguments( String[] args ) throws java.lang.Exception {
108 String workdir = "";
110 for ( int i = 0; i < args.length; i++ ) {
111 if ( args[i].startsWith( "-url=" )) {
112 m_contenturl = args[i].substring( 5 );
113 } else if ( args[i].startsWith( "-workdir=" )) {
114 workdir = args[i].substring( 9 );
115 } else if ( args[i].startsWith( "-help" ) ||
116 args[i].startsWith( "-?" )) {
117 printCmdLineUsage();
118 System.exit( 0 );
122 if ( m_contenturl == null || m_contenturl.equals( "" )) {
123 m_contenturl = Helper.createTargetDataFile( workdir );
128 * Print the commands options
130 public void printCmdLineUsage() {
131 System.out.println(
132 "Usage : ResourceRemover -url=... -workdir=..." );
133 System.out.println(
134 "Defaults: -url=<workdir>/resource-<uniquepostfix> -workdir=<currentdir>" );
135 System.out.println(
136 "\nExample : -url=file:///temp/MyFile.txt \n" );
140 * Create a new connection with the specific args to a running office and
141 * delete a resource.
143 public static void main ( String args[] ) {
145 System.out.println( "\n" );
146 System.out.println(
147 "-----------------------------------------------------------------" );
148 System.out.println(
149 "ResourceRemover - destroys a resource." );
150 System.out.println(
151 "-----------------------------------------------------------------" );
153 try {
154 ResourceRemover delete = new ResourceRemover( args );
155 boolean result = delete.deleteResource();
156 String url = delete.getContentURL();
157 if ( result ) {
158 System.out.println(
159 "Delete of resource " + url + " succeeded." );
160 } else {
161 System.out.println(
162 "Delete of resource " + url + " failed." );
164 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
165 System.out.println( "Error: " + e );
166 } catch ( com.sun.star.uno.Exception e ) {
167 System.out.println( "Error: " + e );
168 } catch ( java.lang.Exception e ) {
169 System.out.println( "Error: " + e );
171 System.exit( 0 );
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */