Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / ResourceCreator.java
blob4279f94a13004bb6aa29dc4560e13ba3ed721095
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 import com.sun.star.beans.PropertyValue;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.ucb.ContentInfo;
38 import com.sun.star.ucb.InsertCommandArgument;
39 import com.sun.star.ucb.XContent;
40 import com.sun.star.io.XInputStream;
43 /**
44 * Creating a New Resource
46 public class ResourceCreator {
48 /**
49 * Member properties
51 private Helper m_helper;
52 private XContent m_content;
53 private String m_contenturl = "";
54 private String m_name = "";
55 private String m_srcURL = "";
57 /**
58 * Constructor.
60 *@param args This construtor requires the arguments:
61 * -url=... (optional)
62 * -name=... (optional)
63 * -srcURL=... (optional)
64 * -workdir=... (optional)
65 * See Help (method printCmdLineUsage()).
66 * Without the arguments a new connection to a
67 * running office cannot created.
69 public ResourceCreator( String args[] ) throws java.lang.Exception {
71 // Parse arguments
72 parseArguments( args );
73 String url = getContentURL();
75 // Init
76 m_helper = new Helper( url );
77 if ( url.startsWith( "file:///" )) {
79 // Create UCB content
80 m_content = m_helper.createUCBContent();
81 } else {
82 throw new Exception(
83 "Create new resource : parameter 'url' must contain a File URL " +
84 "pointing to the file system folder in which the new resource " +
85 "shall be created. (Example: file:///tmp/)" );
89 /**
90 * Create a new resource.
91 * This method requires the main and the optional arguments to be set in order to work.
92 * See Constructor.
94 *@return boolean Returns true if resource successfully created, false otherwise
96 public boolean createNewResource()
97 throws com.sun.star.ucb.CommandAbortedException,
98 com.sun.star.uno.Exception,
99 java.lang.Exception {
101 String sourceURL = getSourceURL();
102 String name = getName();
103 return createNewResource( sourceURL, name );
107 * Create a new resource.
109 *@param sourceURL Source resource URL
110 *@param name New resource name
111 *@return true if resource successfully created, false otherwise
113 public boolean createNewResource( String sourceURL, String name )
114 throws com.sun.star.ucb.CommandAbortedException,
115 com.sun.star.uno.Exception,
116 java.lang.Exception {
118 XInputStream stream = null;
119 if ( sourceURL == null || sourceURL.equals( "" )) {
120 stream = new MyInputStream();
121 } else {
122 String[] args = new String[ 1 ];
123 args[ 0 ] = "-url=" + sourceURL;
124 DataStreamRetriever access = new DataStreamRetriever( args );
125 stream = access.getDataStream();
127 return createNewResource( stream, name );
131 * Create a new resource.
133 *@param stream Source resource stream
134 *@param name New resource name
135 *@return true if resource successfully created, false otherwise
137 public boolean createNewResource( XInputStream stream, String name )
138 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
140 boolean result = false;
141 if ( stream != null && name != null && !name.equals( "" )) {
143 // Note: The data for info may have been obtained from
144 // property CreatableContentsInfo.
145 ContentInfo info = new ContentInfo();
146 info.Type = "application/vnd.sun.staroffice.fsys-file";
147 info.Attributes = 0;
149 // Create new, empty content (execute command "createNewContent").
150 XContent newContent = UnoRuntime.queryInterface(
151 XContent.class,
152 m_helper.executeCommand( m_content, "createNewContent", info ) );
154 if ( newContent != null ) {
157 // Set mandatory properties...
160 // Define property value sequence.
161 PropertyValue[] props = new PropertyValue[ 1 ];
162 PropertyValue prop = new PropertyValue();
163 prop.Name = "Title";
164 prop.Handle = -1; // n/a
165 prop.Value = name;
166 props[ 0 ] = prop;
168 // Execute command "setPropertyValues".
169 m_helper.executeCommand( newContent, "setPropertyValues", props );
172 // Write the new file to disk...
175 // Obtain document data for the new file.
176 XInputStream data = stream;
178 // Fill argument structure...
179 InsertCommandArgument arg = new InsertCommandArgument();
180 arg.Data = data;
181 arg.ReplaceExisting = false;
183 // Execute command "insert".
184 m_helper.executeCommand( newContent, "insert", arg );
185 result = true;
188 return result;
192 * Get new resource name.
194 *@return String That contains the name
196 public String getName() {
197 return m_name;
201 * Get source URL.
203 *@return String That contains the source URL
205 public String getSourceURL() {
206 return m_srcURL;
210 * Get connect URL.
212 *@return String That contains the connect URL
214 public String getContentURL() {
215 return m_contenturl;
219 * Parse arguments
221 public void parseArguments( String[] args ) throws java.lang.Exception {
223 String workdir = "";
225 for ( int i = 0; i < args.length; i++ ) {
226 if ( args[i].startsWith( "-url=" )) {
227 m_contenturl = args[i].substring( 5 );
228 } else if ( args[i].startsWith( "-name=" )) {
229 m_name = args[i].substring( 6 );
230 } else if ( args[i].startsWith( "-srcURL=" )) {
231 m_srcURL = args[i].substring( 8 );
232 } else if ( args[i].startsWith( "-workdir=" )) {
233 workdir = args[i].substring( 9 );
234 } else if ( args[i].startsWith( "-help" ) ||
235 args[i].startsWith( "-?" )) {
236 printCmdLineUsage();
237 System.exit( 0 );
241 if ( m_contenturl == null || m_contenturl.equals( "" )) {
242 m_contenturl = Helper.getAbsoluteFileURLFromSystemPath( workdir );
245 if ( m_name == null || m_name.equals( "" )) {
246 m_name = "created-resource-" + System.currentTimeMillis();
249 if ( m_srcURL == null || m_srcURL.equals( "" )) {
250 m_srcURL = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
255 * Print the commands options
257 public void printCmdLineUsage() {
258 System.out.println(
259 "Usage : ResourceCreator -url=... -name=... -srcURL=... -workdir=..." );
260 System.out.println(
261 "Defaults: -url=<workdir> -name=created-resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt> -workdir=<currentdir>" );
262 System.out.println(
263 "\nExample : -url=file:///home/kai/ -name=newfile.txt -srcURL=file:///home/kai/sourcefile.txt" );
267 * Create a new connection with the specific args to a running office and
268 * create a new resource.
270 public static void main ( String args[] ) {
271 System.out.println( "\n" );
272 System.out.println(
273 "-----------------------------------------------------------------------" );
274 System.out.println(
275 "ResourceCreator - creates a new file in an existing file system folder." );
276 System.out.println(
277 " (Content for the new file can be retrieved from another file)." );
278 System.out.println(
279 "-----------------------------------------------------------------------" );
280 try {
281 ResourceCreator create = new ResourceCreator( args );
282 boolean result = create.createNewResource();
283 if ( result ) {
284 System.out.println(
285 "Creation of new resource " + create.getName() + " in folder: " +
286 create.getContentURL() + " succeeded." );
287 } else {
288 System.out.println(
289 "Creation of new resource " + create.getName() + " in folder: " +
290 create.getContentURL() + " failed." );
292 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
293 System.out.println( "Error: " + e );
294 } catch ( com.sun.star.uno.Exception e ) {
295 System.out.println( "Error: " + e );
296 } catch ( java.lang.Exception e ) {
297 System.out.println( "Error: " + e );
299 System.exit( 0 );