merge the formfield patch from ooo-build
[ooovba.git] / odk / examples / DevelopersGuide / UCB / ResourceCreator.java
blob0b12b31d61587fea59791867ce8ac4b4ef013e1d
1 /*************************************************************************
3 * $RCSfile: ResourceCreator.java,v $
5 * $Revision: 1.6 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:59:01 $
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 import com.sun.star.beans.PropertyValue;
42 import com.sun.star.uno.UnoRuntime;
43 import com.sun.star.ucb.ContentInfo;
44 import com.sun.star.ucb.InsertCommandArgument;
45 import com.sun.star.ucb.XContent;
46 import com.sun.star.ucb.XContentCreator;
47 import com.sun.star.io.XInputStream;
50 /**
51 * Creating a New Resource
53 public class ResourceCreator {
55 /**
56 * Member properties
58 private Helper m_helper;
59 private XContent m_content;
60 private String m_contenturl = "";
61 private String m_name = "";
62 private String m_srcURL = "";
64 /**
65 * Constructor.
67 *@param String[] This construtor requires the arguments:
68 * -url=... (optional)
69 * -name=... (optional)
70 * -srcURL=... (optional)
71 * -workdir=... (optional)
72 * See Help (method printCmdLineUsage()).
73 * Without the arguments a new connection to a
74 * running office cannot created.
75 *@exception java.lang.Exception
77 public ResourceCreator( String args[] ) throws java.lang.Exception {
79 // Parse arguments
80 parseArguments( args );
81 String url = getContentURL();
83 // Init
84 m_helper = new Helper( url );
85 if ( url.startsWith( "file:///" )) {
87 // Create UCB content
88 m_content = m_helper.createUCBContent();
89 } else {
90 throw new Exception(
91 "Create new resource : parameter 'url' must contain a File URL " +
92 "pointing to the file system folder in which the new resource " +
93 "shall be created. (Example: file:///tmp/)" );
97 /**
98 * Create a new resource.
99 * This method requires the main and the optional arguments to be set in order to work.
100 * See Constructor.
102 *@return boolean Returns true if resource successfully created, false otherwise
103 *@exception com.sun.star.ucb.CommandAbortedException
104 *@exception com.sun.star.uno.Exception
106 public boolean createNewResource()
107 throws com.sun.star.ucb.CommandAbortedException,
108 com.sun.star.uno.Exception,
109 java.lang.Exception {
111 String sourceURL = getSourceURL();
112 String name = getName();
113 return createNewResource( sourceURL, name );
117 * Create a new resource.
119 *@param String Source resource URL
120 *@param String New resource name
121 *@return boolean Returns true if resource successfully created, false otherwise
122 *@exception com.sun.star.ucb.CommandAbortedException
123 *@exception com.sun.star.uno.Exception
125 public boolean createNewResource( String sourceURL, String name )
126 throws com.sun.star.ucb.CommandAbortedException,
127 com.sun.star.uno.Exception,
128 java.lang.Exception {
130 XInputStream stream = null;
131 if ( sourceURL == null || sourceURL.equals( "" )) {
132 stream = new MyInputStream();
133 } else {
134 String[] args = new String[ 1 ];
135 args[ 0 ] = "-url=" + sourceURL;
136 DataStreamRetriever access = new DataStreamRetriever( args );
137 stream = access.getDataStream();
139 return createNewResource( stream, name );
143 * Create a new resource.
145 *@param XInputStream Source resource stream
146 *@param String New resource name
147 *@return boolean Returns true if resource successfully created, false otherwise
148 *@exception com.sun.star.ucb.CommandAbortedException
149 *@exception com.sun.star.uno.Exception
151 public boolean createNewResource( XInputStream stream, String name )
152 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
154 boolean result = false;
155 if ( stream != null && name != null && !name.equals( "" )) {
157 // Obtain content creator interface.
158 XContentCreator creator = ( XContentCreator )UnoRuntime.queryInterface(
159 XContentCreator.class, m_content );
161 // Note: The data for info may have been obtained using
162 // XContentCreator::queryCreatableContentsInfo().
163 ContentInfo info = new ContentInfo();
164 info.Type = "application/vnd.sun.staroffice.fsys-file";
165 info.Attributes = 0;
167 // Create new, empty content.
168 XContent newContent = creator.createNewContent( info );
169 if ( newContent != null ) {
171 /////////////////////////////////////////////////////////////////////
172 // Set mandatory properties...
173 /////////////////////////////////////////////////////////////////////
175 // Define property value sequence.
176 PropertyValue[] props = new PropertyValue[ 1 ];
177 PropertyValue prop = new PropertyValue();
178 prop.Name = "Title";
179 prop.Handle = -1; // n/a
180 prop.Value = name;
181 props[ 0 ] = prop;
183 // Execute command "setPropertyValues".
184 m_helper.executeCommand( newContent, "setPropertyValues",props );
186 /////////////////////////////////////////////////////////////////////
187 // Write the new file to disk...
188 /////////////////////////////////////////////////////////////////////
190 // Obtain document data for the new file.
191 XInputStream data = stream;
193 // Fill argument structure...
194 InsertCommandArgument arg = new InsertCommandArgument();
195 arg.Data = data;
196 arg.ReplaceExisting = false;
198 // Execute command "insert".
199 m_helper.executeCommand( newContent, "insert", arg );
200 result = true;
203 return result;
207 * Get new resource name.
209 *@return String That contains the name
211 public String getName() {
212 return m_name;
216 * Get source URL.
218 *@return String That contains the source URL
220 public String getSourceURL() {
221 return m_srcURL;
225 * Get connect URL.
227 *@return String That contains the connect URL
229 public String getContentURL() {
230 return m_contenturl;
234 * Parse arguments
236 *@param String[] Arguments
237 *@exception java.lang.Exception
239 public void parseArguments( String[] args ) throws java.lang.Exception {
241 String workdir = "";
243 for ( int i = 0; i < args.length; i++ ) {
244 if ( args[i].startsWith( "-url=" )) {
245 m_contenturl = args[i].substring( 5 );
246 } else if ( args[i].startsWith( "-name=" )) {
247 m_name = args[i].substring( 6 );
248 } else if ( args[i].startsWith( "-srcURL=" )) {
249 m_srcURL = args[i].substring( 8 );
250 } else if ( args[i].startsWith( "-workdir=" )) {
251 workdir = args[i].substring( 9 );
252 } else if ( args[i].startsWith( "-help" ) ||
253 args[i].startsWith( "-?" )) {
254 printCmdLineUsage();
255 System.exit( 0 );
259 if ( m_contenturl == null || m_contenturl.equals( "" )) {
260 m_contenturl = Helper.getAbsoluteFileURLFromSystemPath( workdir );
263 if ( m_name == null || m_name.equals( "" )) {
264 m_name = "created-resource-" + System.currentTimeMillis();
267 if ( m_srcURL == null || m_srcURL.equals( "" )) {
268 m_srcURL = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
273 * Print the commands options
275 public void printCmdLineUsage() {
276 System.out.println(
277 "Usage : ResourceCreator -url=... -name=... -srcURL=... -workdir=..." );
278 System.out.println(
279 "Defaults: -url=<workdir> -name=created-resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt> -workdir=<currentdir>" );
280 System.out.println(
281 "\nExample : -url=file:///home/kai/ -name=newfile.txt -srcURL=file:///home/kai/sourcefile.txt" );
285 * Create a new connection with the specific args to a running office and
286 * create a new resource.
288 *@param String[] Arguments
290 public static void main ( String args[] ) {
291 System.out.println( "\n" );
292 System.out.println(
293 "-----------------------------------------------------------------------" );
294 System.out.println(
295 "ResourceCreator - creates a new file in an existing file system folder." );
296 System.out.println(
297 " (Content for the new file can be retrieved from another file)." );
298 System.out.println(
299 "-----------------------------------------------------------------------" );
300 try {
301 ResourceCreator create = new ResourceCreator( args );
302 boolean result = create.createNewResource();
303 if ( result ) {
304 System.out.println(
305 "Creation of new resource " + create.getName() + " in folder: " +
306 create.getContentURL() + " succeeded." );
307 } else {
308 System.out.println(
309 "Creation of new resource " + create.getName() + " in folder: " +
310 create.getContentURL() + " failed." );
312 } catch ( com.sun.star.ucb.CommandAbortedException e ) {
313 System.out.println( "Error: " + e );
314 } catch ( com.sun.star.uno.Exception e ) {
315 System.out.println( "Error: " + e );
316 } catch ( java.lang.Exception e ) {
317 System.out.println( "Error: " + e );
319 System.exit( 0 );