1 /*************************************************************************
3 * $RCSfile: DataStreamRetriever.java,v $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:57:38 $
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
.OpenCommandArgument2
;
42 import com
.sun
.star
.ucb
.OpenMode
;
43 import com
.sun
.star
.ucb
.XContent
;
44 import com
.sun
.star
.io
.XActiveDataSink
;
45 import com
.sun
.star
.io
.XInputStream
;
48 * Accessing (Loading) the Content Data Stream of a UCB Document Content
50 public class DataStreamRetriever
{
55 private Helper m_helper
;
56 private XContent m_content
;
57 private String m_contenturl
= "";
62 *@param String[] This construtor requires the arguments:
64 * See Help (method printCmdLineUsage()).
65 * Without the arguments a new connection to a
66 * running office cannot created.
67 *@exception java.lang.Exception
69 public DataStreamRetriever( String args
[] ) throws java
.lang
.Exception
{
72 parseArguments( args
);
75 m_helper
= new Helper( getContentURL() );
78 m_content
= m_helper
.createUCBContent();
82 * Read the document data stream of a document content using a
83 * XActiveDataSink implementation as data sink....
85 *@return XInputStream Returns input stream if stream successfully retrieved,
87 *@exception com.sun.star.ucb.CommandAbortedException
88 *@exception com.sun.star.uno.Exception
90 public XInputStream
getDataStream()
91 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
93 XInputStream data
= null;
94 if ( m_content
!= null ) {
96 // Fill argument structure...
97 OpenCommandArgument2 arg
= new OpenCommandArgument2();
98 arg
.Mode
= OpenMode
.DOCUMENT
;
99 arg
.Priority
= 32768; // Final static for 32768
101 // Create data sink implementation object.
102 XActiveDataSink dataSink
= new MyActiveDataSink();
105 // Execute command "open". The implementation of the command will
106 // supply an XInputStream implementation to the data sink.
107 m_helper
.executeCommand( m_content
, "open", arg
);
109 // Get input stream supplied by the open command implementation.
110 data
= dataSink
.getInputStream();
118 *@return String That contains the connect URL
120 public String
getContentURL() {
127 *@param String[] Arguments
128 *@exception java.lang.Exception
130 public void parseArguments( String
[] args
) throws java
.lang
.Exception
{
132 for ( int i
= 0; i
< args
.length
; i
++ ) {
133 if ( args
[i
].startsWith( "-url=" )) {
134 m_contenturl
= args
[i
].substring( 5 );
135 } else if ( args
[i
].startsWith( "-help" ) ||
136 args
[i
].startsWith( "-?" )) {
142 if ( m_contenturl
== null || m_contenturl
.equals( "" )) {
143 m_contenturl
= Helper
.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
148 * Print the commands options
150 public void printCmdLineUsage() {
152 "Usage : DataStreamRetriever -url=..." );
154 "Defaults: -url=<currentdir>/data/data.txt" );
156 "\nExample : -url=file:///temp/my.txt" );
160 * Print Stream content.
163 *@exception com.sun.star.uno.Exception
165 public void printStream( XInputStream data
)
166 throws com
.sun
.star
.uno
.Exception
{
168 /////////////////////////////////////////////////////////////////////
169 // Read data from input stream...65536
170 /////////////////////////////////////////////////////////////////////
172 // Data buffer. Will be allocated by input stream implementation!
173 byte[][] buffer
= new byte[ 1 ][ 65536 ];
174 int read
= data
.readSomeBytes( buffer
, 65536 );
175 System
.out
.println( "Read bytes : " + read
);
176 System
.out
.println( "Read data (only first 64K displayed): ");
178 byte[] bytes
= new byte[ read
];
179 for( int i
= 0; i
< read
; i
++ ) {
180 bytes
[ i
] = buffer
[ 0 ][ i
];
182 System
.out
.println( new String(bytes
) );
184 // Process data contained in buffer.
185 read
= data
.readSomeBytes( buffer
, 65536 );
190 * Create a new connection with the specific args to a running office and
191 * access (Load) the content data stream of a UCB document content.
193 *@param String[] Arguments
195 public static void main ( String args
[] ) {
196 System
.out
.println( "\n" );
198 "-----------------------------------------------------------------------" );
200 "DataStreamRetriever - obtains the data stream from a document resource." );
202 "-----------------------------------------------------------------------" );
206 DataStreamRetriever access
= new DataStreamRetriever( args
);
207 XInputStream data
= access
.getDataStream();
208 String url
= access
.getContentURL();
209 if ( data
!= null ) {
210 String tempPrint
= "\nGetting data stream for resource " + url
+
212 int size
= tempPrint
.length();
213 System
.out
.println( tempPrint
);
215 for( int i
= 0; i
< size
; i
++ ) {
218 System
.out
.println( tempPrint
);
219 access
.printStream( data
);
222 "Getting data stream for resource " + url
+ " failed." );
224 } catch ( com
.sun
.star
.io
.NotConnectedException e
) {
225 System
.out
.println( "Error: " + e
);
226 } catch ( com
.sun
.star
.io
.BufferSizeExceededException e
) {
227 System
.out
.println( "Error: " + e
);
228 } catch ( com
.sun
.star
.io
.IOException e
) {
229 System
.out
.println( "Error: " + e
);
230 } catch ( com
.sun
.star
.ucb
.CommandAbortedException e
) {
231 System
.out
.println( "Error: " + e
);
232 } catch ( com
.sun
.star
.uno
.Exception e
) {
233 System
.out
.println( "Error: " + e
);
234 } catch ( java
.lang
.Exception e
) {
235 System
.out
.println( "Error: " + e
);