1 /*************************************************************************
3 * $RCSfile: DataStreamComposer.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:57:25 $
9 * The Contents of this file are made available subject to the terms of
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
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
.ucb
.InsertCommandArgument
;
42 import com
.sun
.star
.ucb
.XContent
;
43 import com
.sun
.star
.io
.XInputStream
;
46 * Setting (Storing) the Content Data Stream of a UCB Document Content.
48 public class DataStreamComposer
{
53 private Helper m_helper
;
54 private XContent m_content
;
55 private String m_contenturl
= "";
56 private String m_srcURL
= "";
62 *@param String[] This construtor requires the arguments:
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.
69 *@exception java.lang.Exception
71 public DataStreamComposer( String args
[] ) throws java
.lang
.Exception
{
74 parseArguments( args
);
77 m_helper
= new Helper( getContentURL() );
80 m_content
= m_helper
.createUCBContent();
84 * Write the document data stream of a document content.
85 * This method requires the main and the optional arguments to be set in order to work.
88 *@return boolean Result
89 *@exception com.sun.star.ucb.CommandAbortedException
90 *@exception com.sun.star.uno.Exception
91 *@exception java.lang.Exception
93 public boolean setDataStream()
94 throws com
.sun
.star
.ucb
.CommandAbortedException
,
95 com
.sun
.star
.uno
.Exception
,
98 String sourceURL
= getSourceURL();
99 return ( setDataStream( sourceURL
));
103 * Write the document data stream of a document content.
105 *@param String Source URL
106 *@return boolean Returns true if data stream successfully seted, false otherwise
107 *@exception com.sun.star.ucb.CommandAbortedException
108 *@exception com.sun.star.uno.Exception
109 *@exception java.lang.Exception
111 public boolean setDataStream( String sourceURL
)
112 throws com
.sun
.star
.ucb
.CommandAbortedException
,
113 com
.sun
.star
.uno
.Exception
,
114 java
.lang
.Exception
{
117 if ( sourceURL
== null || sourceURL
.equals("") ) {
118 stream
= new MyInputStream();
120 String
[] args
= new String
[ 1 ];
121 args
[ 0 ] = "-url=" + sourceURL
;
122 DataStreamRetriever access
= new DataStreamRetriever( args
);
123 stream
= access
.getDataStream();
125 return ( setDataStream( stream
));
129 * Write the document data stream of a document content...
131 *@param XInputStream Stream
132 *@return boolean Returns true if data stream successfully seted, false otherwise
133 *@exception com.sun.star.ucb.CommandAbortedException
134 *@exception com.sun.star.uno.Exception
136 public boolean setDataStream( XInputStream stream
)
137 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
139 boolean result
= false;
140 XInputStream data
= stream
;
141 if ( data
!= null && m_content
!= null ) {
143 // Fill argument structure...
144 InsertCommandArgument arg
= new InsertCommandArgument();
146 arg
.ReplaceExisting
= true;
148 // Execute command "insert".
149 m_helper
.executeCommand( m_content
, "insert", arg
);
158 *@return String That contains the source URL
160 public String
getSourceURL() {
167 *@return String That contains the connect URL
169 public String
getContentURL() {
176 *@param String[] Arguments
177 *@exception java.lang.Exception
179 public void parseArguments( String
[] args
) throws java
.lang
.Exception
{
183 for ( int i
= 0; i
< args
.length
; i
++ ) {
184 if ( args
[i
].startsWith( "-url=" )) {
185 m_contenturl
= args
[i
].substring( 5 );
186 } else if ( args
[i
].startsWith( "-srcURL=" )) {
187 m_srcURL
= args
[i
].substring( 9 );
188 } else if ( args
[i
].startsWith( "-workdir=" )) {
189 workdir
= args
[i
].substring( 9 );
190 } else if ( args
[i
].startsWith( "-help" ) ||
191 args
[i
].startsWith( "-?" )) {
197 if ( m_contenturl
== null || m_contenturl
.equals( "" )) {
198 m_contenturl
= Helper
.createTargetDataFile( workdir
);
201 if ( m_srcURL
== null || m_srcURL
.equals( "" )) {
202 m_srcURL
= Helper
.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
207 * Print the commands options
209 public void printCmdLineUsage() {
211 "Usage : DataStreamComposer -url=... -srcURL=... -workdir=..." );
213 "Defaults: -url=<workdir>/resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt -workdir=<currentdir>" );
215 "\nExample : -url=file:///temp/my.txt -srcURL=file:///temp/src.txt " );
220 * Create a new connection with the specific args to a running office and
221 * set the Content Data Stream of a UCB Document Content.
223 *@param String[] Arguments
225 public static void main ( String args
[] ) {
226 System
.out
.println( "\n" );
228 "-----------------------------------------------------------------" );
230 "DataStreamComposer - sets the data stream of a document resource." );
232 " The data stream is obtained from another (the source) document " );
234 " resource before." );
236 "-----------------------------------------------------------------" );
239 DataStreamComposer dataStream
= new DataStreamComposer( args
);
240 String sourceURL
= dataStream
.getSourceURL();
241 boolean result
= dataStream
.setDataStream( sourceURL
);
244 "\nSetting data stream succeeded.\n Source URL: " +
245 dataStream
.getSourceURL() +
247 dataStream
.getContentURL() );
250 "\nSetting data stream failed. \n Source URL: " +
251 dataStream
.getSourceURL() +
253 dataStream
.getContentURL() );
255 } catch ( com
.sun
.star
.ucb
.CommandAbortedException e
) {
256 System
.out
.println( "Error: " + e
);
257 } catch ( com
.sun
.star
.uno
.Exception e
) {
258 System
.out
.println( "Error: " + e
);
259 } catch ( java
.lang
.Exception e
) {
260 System
.out
.println( "Error: " + e
);