Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / ResourceRemover.java
blobb55bbae7fe4e956cc09b33edcb7877f1f6d384d2
1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
4 * the BSD license.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * All rights reserved.
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.
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.
33 *************************************************************************/
35 /**
36 * Deleting a resource
38 public class ResourceRemover {
40 /**
41 * Member properties
43 private Helper m_helper;
44 private String m_contenturl = "";
45 private com.sun.star.ucb.XContent m_content;
47 /**
48 * Constructor.
50 *@param args 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.
57 public ResourceRemover( String args[] ) throws java.lang.Exception {
59 // Parse arguments
60 parseArguments( args );
62 // Init
63 m_helper = new Helper( getContentURL() );
65 // Create UCB content
66 m_content = m_helper.createUCBContent();
69 /**
70 * Delete resource.
72 *@return true if resource successfully deleted, false otherwise
74 public boolean deleteResource()
75 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
77 boolean result = false;
78 if ( m_content != null ) {
81 // Destroy a resource physically...
84 Boolean deletePhysically = Boolean.TRUE;
86 // Execute command "delete".
87 m_helper.executeCommand( m_content, "delete", deletePhysically );
88 result = true;
90 return result;
93 /**
94 * Get connect URL.
96 *@return String That contains the connect URL
98 public String getContentURL() {
99 return m_contenturl;
103 * Parse arguments
105 public void parseArguments( String[] args ) throws java.lang.Exception {
107 String workdir = "";
109 for ( int i = 0; i < args.length; i++ ) {
110 if ( args[i].startsWith( "-url=" )) {
111 m_contenturl = args[i].substring( 5 );
112 } else if ( args[i].startsWith( "-workdir=" )) {
113 workdir = args[i].substring( 9 );
114 } else if ( args[i].startsWith( "-help" ) ||
115 args[i].startsWith( "-?" )) {
116 printCmdLineUsage();
117 System.exit( 0 );
121 if ( m_contenturl == null || m_contenturl.equals( "" )) {
122 m_contenturl = Helper.createTargetDataFile( workdir );
127 * Print the commands options
129 public void printCmdLineUsage() {
130 System.out.println(
131 "Usage : ResourceRemover -url=... -workdir=..." );
132 System.out.println(
133 "Defaults: -url=<workdir>/resource-<uniquepostfix> -workdir=<currentdir>" );
134 System.out.println(
135 "\nExample : -url=file:///temp/MyFile.txt \n" );
139 * Create a new connection with the specific args to a running office and
140 * delete a resource.
142 public static void main ( String args[] ) {
144 System.out.println( "\n" );
145 System.out.println(
146 "-----------------------------------------------------------------" );
147 System.out.println(
148 "ResourceRemover - destroys a resource." );
149 System.out.println(
150 "-----------------------------------------------------------------" );
152 try {
153 ResourceRemover delete = new ResourceRemover( args );
154 boolean result = delete.deleteResource();
155 String url = delete.getContentURL();
156 if ( result ) {
157 System.out.println(
158 "Delete of resource " + url + " succeeded." );
159 } else {
160 System.out.println(
161 "Delete of resource " + url + " failed." );
163 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
164 System.out.println( "Error: " + e );
165 } catch ( com.sun.star.uno.Exception e ) {
166 System.out.println( "Error: " + e );
167 } catch ( java.lang.Exception e ) {
168 System.out.println( "Error: " + e );
170 System.exit( 0 );