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
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 import com
.sun
.star
.beans
.PropertyValue
;
37 import com
.sun
.star
.uno
.UnoRuntime
;
38 import com
.sun
.star
.ucb
.ContentInfo
;
39 import com
.sun
.star
.ucb
.InsertCommandArgument
;
40 import com
.sun
.star
.ucb
.XContent
;
41 import com
.sun
.star
.io
.XInputStream
;
45 * Creating a New Resource
47 public class ResourceCreator
{
52 private Helper m_helper
;
53 private XContent m_content
;
54 private String m_contenturl
= "";
55 private String m_name
= "";
56 private String m_srcURL
= "";
61 *@param args This constructor requires the arguments:
63 * -name=... (optional)
64 * -srcURL=... (optional)
65 * -workdir=... (optional)
66 * See Help (method printCmdLineUsage()).
67 * Without the arguments a new connection to a
68 * running office cannot created.
70 public ResourceCreator( String args
[] ) throws java
.lang
.Exception
{
73 parseArguments( args
);
74 String url
= getContentURL();
77 m_helper
= new Helper( url
);
78 if ( url
.startsWith( "file:///" )) {
81 m_content
= m_helper
.createUCBContent();
84 "Create new resource : parameter 'url' must contain a File URL " +
85 "pointing to the file system folder in which the new resource " +
86 "shall be created. (Example: file:///tmp/)" );
91 * Create a new resource.
92 * This method requires the main and the optional arguments to be set in order to work.
95 *@return boolean Returns true if resource successfully created, false otherwise
97 public boolean createNewResource()
98 throws com
.sun
.star
.ucb
.CommandAbortedException
,
99 com
.sun
.star
.uno
.Exception
,
100 java
.lang
.Exception
{
102 String sourceURL
= getSourceURL();
103 String name
= getName();
104 return createNewResource( sourceURL
, name
);
108 * Create a new resource.
110 *@param sourceURL Source resource URL
111 *@param name New resource name
112 *@return true if resource successfully created, false otherwise
114 public boolean createNewResource( String sourceURL
, String name
)
115 throws com
.sun
.star
.ucb
.CommandAbortedException
,
116 com
.sun
.star
.uno
.Exception
,
117 java
.lang
.Exception
{
119 XInputStream stream
= null;
120 if ( sourceURL
== null || sourceURL
.equals( "" )) {
121 stream
= new MyInputStream();
123 String
[] args
= new String
[ 1 ];
124 args
[ 0 ] = "-url=" + sourceURL
;
125 DataStreamRetriever access
= new DataStreamRetriever( args
);
126 stream
= access
.getDataStream();
128 return createNewResource( stream
, name
);
132 * Create a new resource.
134 *@param stream Source resource stream
135 *@param name New resource name
136 *@return true if resource successfully created, false otherwise
138 public boolean createNewResource( XInputStream stream
, String name
)
139 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
141 boolean result
= false;
142 if ( stream
!= null && name
!= null && !name
.equals( "" )) {
144 // Note: The data for info may have been obtained from
145 // property CreatableContentsInfo.
146 ContentInfo info
= new ContentInfo();
147 info
.Type
= "application/vnd.sun.staroffice.fsys-file";
150 // Create new, empty content (execute command "createNewContent").
151 XContent newContent
= UnoRuntime
.queryInterface(
153 m_helper
.executeCommand( m_content
, "createNewContent", info
) );
155 if ( newContent
!= null ) {
158 // Set mandatory properties...
161 // Define property value sequence.
162 PropertyValue
[] props
= new PropertyValue
[ 1 ];
163 PropertyValue prop
= new PropertyValue();
165 prop
.Handle
= -1; // n/a
169 // Execute command "setPropertyValues".
170 m_helper
.executeCommand( newContent
, "setPropertyValues", props
);
173 // Write the new file to disk...
176 // Obtain document data for the new file.
177 XInputStream data
= stream
;
179 // Fill argument structure...
180 InsertCommandArgument arg
= new InsertCommandArgument();
182 arg
.ReplaceExisting
= false;
184 // Execute command "insert".
185 m_helper
.executeCommand( newContent
, "insert", arg
);
193 * Get new resource name.
195 *@return String That contains the name
197 public String
getName() {
204 *@return String That contains the source URL
206 public String
getSourceURL() {
213 *@return String That contains the connect URL
215 public String
getContentURL() {
222 public void parseArguments( String
[] args
) throws java
.lang
.Exception
{
226 for ( int i
= 0; i
< args
.length
; i
++ ) {
227 if ( args
[i
].startsWith( "-url=" )) {
228 m_contenturl
= args
[i
].substring( 5 );
229 } else if ( args
[i
].startsWith( "-name=" )) {
230 m_name
= args
[i
].substring( 6 );
231 } else if ( args
[i
].startsWith( "-srcURL=" )) {
232 m_srcURL
= args
[i
].substring( 8 );
233 } else if ( args
[i
].startsWith( "-workdir=" )) {
234 workdir
= args
[i
].substring( 9 );
235 } else if ( args
[i
].startsWith( "-help" ) ||
236 args
[i
].startsWith( "-?" )) {
242 if ( m_contenturl
== null || m_contenturl
.equals( "" )) {
243 m_contenturl
= Helper
.getAbsoluteFileURLFromSystemPath( workdir
);
246 if ( m_name
== null || m_name
.equals( "" )) {
247 m_name
= "created-resource-" + System
.currentTimeMillis();
250 if ( m_srcURL
== null || m_srcURL
.equals( "" )) {
251 m_srcURL
= Helper
.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
256 * Print the commands options
258 public void printCmdLineUsage() {
260 "Usage : ResourceCreator -url=... -name=... -srcURL=... -workdir=..." );
262 "Defaults: -url=<workdir> -name=created-resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt> -workdir=<currentdir>" );
264 "\nExample : -url=file:///home/kai/ -name=newfile.txt -srcURL=file:///home/kai/sourcefile.txt" );
268 * Create a new connection with the specific args to a running office and
269 * create a new resource.
271 public static void main ( String args
[] ) {
272 System
.out
.println( "\n" );
274 "-----------------------------------------------------------------------" );
276 "ResourceCreator - creates a new file in an existing file system folder." );
278 " (Content for the new file can be retrieved from another file)." );
280 "-----------------------------------------------------------------------" );
282 ResourceCreator create
= new ResourceCreator( args
);
283 boolean result
= create
.createNewResource();
286 "Creation of new resource " + create
.getName() + " in folder: " +
287 create
.getContentURL() + " succeeded." );
290 "Creation of new resource " + create
.getName() + " in folder: " +
291 create
.getContentURL() + " failed." );
293 } catch ( com
.sun
.star
.ucb
.CommandAbortedException e
) {
294 System
.out
.println( "Error: " + e
);
295 } catch ( com
.sun
.star
.uno
.Exception e
) {
296 System
.out
.println( "Error: " + e
);
297 } catch ( java
.lang
.Exception e
) {
298 System
.out
.println( "Error: " + e
);
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */