1 /*************************************************************************
3 * The Contents of this file are made available subject to the terms of
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
.ucb
.InsertCommandArgument
;
36 import com
.sun
.star
.ucb
.XContent
;
37 import com
.sun
.star
.io
.XInputStream
;
40 * Setting (Storing) the Content Data Stream of a UCB Document Content.
42 public class DataStreamComposer
{
47 private Helper m_helper
;
48 private XContent m_content
;
49 private String m_contenturl
= "";
50 private String m_srcURL
= "";
56 *@param args This construtor requires the arguments:
58 * -srcURL=... (optional)
59 * -workdir=... (optional)
60 * See Help (method printCmdLineUsage()).
61 * Without the arguments a new connection to a
62 * running office cannot created.
64 public DataStreamComposer( String args
[] ) throws java
.lang
.Exception
{
67 parseArguments( args
);
70 m_helper
= new Helper( getContentURL() );
73 m_content
= m_helper
.createUCBContent();
77 * Write the document data stream of a document content.
78 * This method requires the main and the optional arguments to be set in order to work.
81 *@return boolean Result
83 public boolean setDataStream()
84 throws com
.sun
.star
.ucb
.CommandAbortedException
,
85 com
.sun
.star
.uno
.Exception
,
88 String sourceURL
= getSourceURL();
89 return ( setDataStream( sourceURL
));
93 * Write the document data stream of a document content.
95 *@param sourceURL Source URL
96 *@return true if data stream successfully seted, false otherwise
98 public boolean setDataStream( String sourceURL
)
99 throws com
.sun
.star
.ucb
.CommandAbortedException
,
100 com
.sun
.star
.uno
.Exception
,
101 java
.lang
.Exception
{
104 if ( sourceURL
== null || sourceURL
.equals("") ) {
105 stream
= new MyInputStream();
107 String
[] args
= new String
[ 1 ];
108 args
[ 0 ] = "-url=" + sourceURL
;
109 DataStreamRetriever access
= new DataStreamRetriever( args
);
110 stream
= access
.getDataStream();
112 return ( setDataStream( stream
));
116 * Write the document data stream of a document content...
118 *@return boolean Returns true if data stream successfully seted, false otherwise
120 public boolean setDataStream( XInputStream stream
)
121 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
123 boolean result
= false;
124 XInputStream data
= stream
;
125 if ( data
!= null && m_content
!= null ) {
127 // Fill argument structure...
128 InsertCommandArgument arg
= new InsertCommandArgument();
130 arg
.ReplaceExisting
= true;
132 // Execute command "insert".
133 m_helper
.executeCommand( m_content
, "insert", arg
);
142 *@return String That contains the source URL
144 public String
getSourceURL() {
151 *@return String That contains the connect URL
153 public String
getContentURL() {
160 public void parseArguments( String
[] args
) throws java
.lang
.Exception
{
164 for ( int i
= 0; i
< args
.length
; i
++ ) {
165 if ( args
[i
].startsWith( "-url=" )) {
166 m_contenturl
= args
[i
].substring( 5 );
167 } else if ( args
[i
].startsWith( "-srcURL=" )) {
168 m_srcURL
= args
[i
].substring( 9 );
169 } else if ( args
[i
].startsWith( "-workdir=" )) {
170 workdir
= args
[i
].substring( 9 );
171 } else if ( args
[i
].startsWith( "-help" ) ||
172 args
[i
].startsWith( "-?" )) {
178 if ( m_contenturl
== null || m_contenturl
.equals( "" )) {
179 m_contenturl
= Helper
.createTargetDataFile( workdir
);
182 if ( m_srcURL
== null || m_srcURL
.equals( "" )) {
183 m_srcURL
= Helper
.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
188 * Print the commands options
190 public void printCmdLineUsage() {
192 "Usage : DataStreamComposer -url=... -srcURL=... -workdir=..." );
194 "Defaults: -url=<workdir>/resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt -workdir=<currentdir>" );
196 "\nExample : -url=file:///temp/my.txt -srcURL=file:///temp/src.txt " );
201 * Create a new connection with the specific args to a running office and
202 * set the Content Data Stream of a UCB Document Content.
204 public static void main ( String args
[] ) {
205 System
.out
.println( "\n" );
207 "-----------------------------------------------------------------" );
209 "DataStreamComposer - sets the data stream of a document resource." );
211 " The data stream is obtained from another (the source) document " );
213 " resource before." );
215 "-----------------------------------------------------------------" );
218 DataStreamComposer dataStream
= new DataStreamComposer( args
);
219 String sourceURL
= dataStream
.getSourceURL();
220 boolean result
= dataStream
.setDataStream( sourceURL
);
223 "\nSetting data stream succeeded.\n Source URL: " +
224 dataStream
.getSourceURL() +
226 dataStream
.getContentURL() );
229 "\nSetting data stream failed. \n Source URL: " +
230 dataStream
.getSourceURL() +
232 dataStream
.getContentURL() );
234 } catch ( com
.sun
.star
.ucb
.CommandAbortedException e
) {
235 System
.out
.println( "Error: " + e
);
236 } catch ( com
.sun
.star
.uno
.Exception e
) {
237 System
.out
.println( "Error: " + e
);
238 } catch ( java
.lang
.Exception e
) {
239 System
.out
.println( "Error: " + e
);