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
.OpenCommandArgument2
;
36 import com
.sun
.star
.ucb
.OpenMode
;
37 import com
.sun
.star
.ucb
.XContent
;
38 import com
.sun
.star
.io
.XActiveDataSink
;
39 import com
.sun
.star
.io
.XInputStream
;
42 * Accessing (Loading) the Content Data Stream of a UCB Document Content
44 public class DataStreamRetriever
{
49 private Helper m_helper
;
50 private XContent m_content
;
51 private String m_contenturl
= "";
56 *@param args This construtor requires the arguments:
58 * See Help (method printCmdLineUsage()).
59 * Without the arguments a new connection to a
60 * running office cannot created.
62 public DataStreamRetriever( String args
[] ) throws java
.lang
.Exception
{
65 parseArguments( args
);
68 m_helper
= new Helper( getContentURL() );
71 m_content
= m_helper
.createUCBContent();
75 * Read the document data stream of a document content using a
76 * XActiveDataSink implementation as data sink....
78 *@return XInputStream Returns input stream if stream successfully retrieved,
81 public XInputStream
getDataStream()
82 throws com
.sun
.star
.ucb
.CommandAbortedException
, com
.sun
.star
.uno
.Exception
{
84 XInputStream data
= null;
85 if ( m_content
!= null ) {
87 // Fill argument structure...
88 OpenCommandArgument2 arg
= new OpenCommandArgument2();
89 arg
.Mode
= OpenMode
.DOCUMENT
;
90 arg
.Priority
= 32768; // Final static for 32768
92 // Create data sink implementation object.
93 XActiveDataSink dataSink
= new MyActiveDataSink();
96 // Execute command "open". The implementation of the command will
97 // supply an XInputStream implementation to the data sink.
98 m_helper
.executeCommand( m_content
, "open", arg
);
100 // Get input stream supplied by the open command implementation.
101 data
= dataSink
.getInputStream();
109 *@return String That contains the connect URL
111 public String
getContentURL() {
118 *@param args Arguments
120 private void parseArguments( String
[] args
) throws java
.lang
.Exception
{
122 for ( int i
= 0; i
< args
.length
; i
++ ) {
123 if ( args
[i
].startsWith( "-url=" )) {
124 m_contenturl
= args
[i
].substring( 5 );
125 } else if ( args
[i
].startsWith( "-help" ) ||
126 args
[i
].startsWith( "-?" )) {
132 if ( m_contenturl
== null || m_contenturl
.equals( "" )) {
133 m_contenturl
= Helper
.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
138 * Print the commands options
140 private void printCmdLineUsage() {
142 "Usage : DataStreamRetriever -url=..." );
144 "Defaults: -url=<currentdir>/data/data.txt" );
146 "\nExample : -url=file:///temp/my.txt" );
150 * Print Stream content.
152 private void printStream( XInputStream data
)
153 throws com
.sun
.star
.uno
.Exception
{
156 // Read data from input stream...65536
159 // Data buffer. Will be allocated by input stream implementation!
160 byte[][] buffer
= new byte[ 1 ][ 65536 ];
161 int read
= data
.readSomeBytes( buffer
, 65536 );
162 System
.out
.println( "Read bytes : " + read
);
163 System
.out
.println( "Read data (only first 64K displayed): ");
165 byte[] bytes
= new byte[ read
];
166 for( int i
= 0; i
< read
; i
++ ) {
167 bytes
[ i
] = buffer
[ 0 ][ i
];
169 System
.out
.println( new String(bytes
) );
171 // Process data contained in buffer.
172 read
= data
.readSomeBytes( buffer
, 65536 );
177 * Create a new connection with the specific args to a running office and
178 * access (Load) the content data stream of a UCB document content.
180 public static void main ( String args
[] ) {
181 System
.out
.println( "\n" );
183 "-----------------------------------------------------------------------" );
185 "DataStreamRetriever - obtains the data stream from a document resource." );
187 "-----------------------------------------------------------------------" );
191 DataStreamRetriever access
= new DataStreamRetriever( args
);
192 XInputStream data
= access
.getDataStream();
193 String url
= access
.getContentURL();
194 if ( data
!= null ) {
195 String tempPrint
= "\nGetting data stream for resource " + url
+
197 int size
= tempPrint
.length();
198 System
.out
.println( tempPrint
);
200 for( int i
= 0; i
< size
; i
++ ) {
203 System
.out
.println( tempPrint
);
204 access
.printStream( data
);
207 "Getting data stream for resource " + url
+ " failed." );
209 } catch ( com
.sun
.star
.io
.NotConnectedException e
) {
210 System
.out
.println( "Error: " + e
);
211 } catch ( com
.sun
.star
.io
.BufferSizeExceededException e
) {
212 System
.out
.println( "Error: " + e
);
213 } catch ( com
.sun
.star
.io
.IOException e
) {
214 System
.out
.println( "Error: " + e
);
215 } catch ( com
.sun
.star
.ucb
.CommandAbortedException e
) {
216 System
.out
.println( "Error: " + e
);
217 } catch ( com
.sun
.star
.uno
.Exception e
) {
218 System
.out
.println( "Error: " + e
);
219 } catch ( java
.lang
.Exception e
) {
220 System
.out
.println( "Error: " + e
);