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
.ucb
.InsertCommandArgument
;
37 import com
.sun
.star
.ucb
.XContent
;
38 import com
.sun
.star
.io
.XInputStream
;
41 * Setting (Storing) the Content Data Stream of a UCB Document Content.
43 public class DataStreamComposer
{
48 private Helper m_helper
;
49 private XContent m_content
;
50 private String m_contenturl
= "";
51 private String m_srcURL
= "";
57 *@param args This constructor requires the arguments:
59 * -srcURL=... (optional)
60 * -workdir=... (optional)
61 * See Help (method printCmdLineUsage()).
62 * Without the arguments a new connection to a
63 * running office cannot created.
65 public DataStreamComposer( String args
[] ) throws java
.lang
.Exception
{
68 parseArguments( args
);
71 m_helper
= new Helper( getContentURL() );
74 m_content
= m_helper
.createUCBContent();
78 * Write the document data stream of a document content.
79 * This method requires the main and the optional arguments to be set in order to work.
82 *@return boolean Result
84 public boolean setDataStream()
85 throws com
.sun
.star
.ucb
.CommandAbortedException
,
86 com
.sun
.star
.uno
.Exception
,
89 String sourceURL
= getSourceURL();
90 return ( setDataStream( sourceURL
));
94 * Write the document data stream of a document content.
96 *@param sourceURL Source URL
97 *@return true if data stream successfully seted, false otherwise
99 public boolean setDataStream( String sourceURL
)
100 throws com
.sun
.star
.ucb
.CommandAbortedException
,
101 com
.sun
.star
.uno
.Exception
,
102 java
.lang
.Exception
{
105 if ( sourceURL
== null || sourceURL
.equals("") ) {
106 stream
= new MyInputStream();
108 String
[] args
= new String
[ 1 ];
109 args
[ 0 ] = "-url=" + sourceURL
;
110 DataStreamRetriever access
= new DataStreamRetriever( args
);
111 stream
= access
.getDataStream();
113 return ( setDataStream( stream
));
117 * Write the document data stream of a document content...
119 *@return boolean Returns true if data stream successfully seted, false otherwise
121 public boolean setDataStream( XInputStream stream
)
122 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
124 boolean result
= false;
125 XInputStream data
= stream
;
126 if ( data
!= null && m_content
!= null ) {
128 // Fill argument structure...
129 InsertCommandArgument arg
= new InsertCommandArgument();
131 arg
.ReplaceExisting
= true;
133 // Execute command "insert".
134 m_helper
.executeCommand( m_content
, "insert", arg
);
143 *@return String That contains the source URL
145 public String
getSourceURL() {
152 *@return String That contains the connect URL
154 public String
getContentURL() {
161 public void parseArguments( String
[] args
) throws java
.lang
.Exception
{
165 for ( int i
= 0; i
< args
.length
; i
++ ) {
166 if ( args
[i
].startsWith( "-url=" )) {
167 m_contenturl
= args
[i
].substring( 5 );
168 } else if ( args
[i
].startsWith( "-srcURL=" )) {
169 m_srcURL
= args
[i
].substring( 9 );
170 } else if ( args
[i
].startsWith( "-workdir=" )) {
171 workdir
= args
[i
].substring( 9 );
172 } else if ( args
[i
].startsWith( "-help" ) ||
173 args
[i
].startsWith( "-?" )) {
179 if ( m_contenturl
== null || m_contenturl
.equals( "" )) {
180 m_contenturl
= Helper
.createTargetDataFile( workdir
);
183 if ( m_srcURL
== null || m_srcURL
.equals( "" )) {
184 m_srcURL
= Helper
.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
189 * Print the commands options
191 public void printCmdLineUsage() {
193 "Usage : DataStreamComposer -url=... -srcURL=... -workdir=..." );
195 "Defaults: -url=<workdir>/resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt -workdir=<currentdir>" );
197 "\nExample : -url=file:///temp/my.txt -srcURL=file:///temp/src.txt " );
202 * Create a new connection with the specific args to a running office and
203 * set the Content Data Stream of a UCB Document Content.
205 public static void main ( String args
[] ) {
206 System
.out
.println( "\n" );
208 "-----------------------------------------------------------------" );
210 "DataStreamComposer - sets the data stream of a document resource." );
212 " The data stream is obtained from another (the source) document " );
214 " resource before." );
216 "-----------------------------------------------------------------" );
219 DataStreamComposer dataStream
= new DataStreamComposer( args
);
220 String sourceURL
= dataStream
.getSourceURL();
221 boolean result
= dataStream
.setDataStream( sourceURL
);
224 "\nSetting data stream succeeded.\n Source URL: " +
225 dataStream
.getSourceURL() +
227 dataStream
.getContentURL() );
230 "\nSetting data stream failed. \n Source URL: " +
231 dataStream
.getSourceURL() +
233 dataStream
.getContentURL() );
235 } catch ( com
.sun
.star
.ucb
.CommandAbortedException e
) {
236 System
.out
.println( "Error: " + e
);
237 } catch ( com
.sun
.star
.uno
.Exception e
) {
238 System
.out
.println( "Error: " + e
);
239 } catch ( java
.lang
.Exception e
) {
240 System
.out
.println( "Error: " + e
);
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */