1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 import com.sun.star.ucb.OpenCommandArgument2;
25 import com.sun.star.ucb.OpenMode;
26 import com.sun.star.ucb.XContent;
27 import com.sun.star.io.XActiveDataSink;
28 import com.sun.star.io.XInputStream;
29 
30 /**
31  * Accessing (Loading) the Content Data Stream of a UCB Document Content
32  */
33 public class DataStreamRetriever {
34 
35     /**
36      * Member properties
37      */
38     private  Helper   m_helper;
39     private  XContent m_content;
40     private  String   m_contenturl    = "";
41 
42     /**
43      * Constructor.
44      *
45      *@param      String[]   This construtor requires the arguments:
46      *                          -url=... (optional)
47      *                       See Help (method printCmdLineUsage()).
48      *                       Without the arguments a new connection to a
49      *                       running office cannot created.
50      *@exception  java.lang.Exception
51      */
DataStreamRetriever( String args[] )52     public DataStreamRetriever( String args[] ) throws java.lang.Exception {
53 
54         // Parse arguments
55         parseArguments( args );
56 
57         // Init
58         m_helper       = new Helper( getContentURL() );
59 
60         // Create UCB content
61         m_content      = m_helper.createUCBContent();
62     }
63 
64     /**
65      *  Read the document data stream of a document content using a
66      *  XActiveDataSink implementation as data sink....
67      *
68      *@return     XInputStream  Returns input stream if stream successfully retrieved,
69      *                          null otherwise
70      *@exception  com.sun.star.ucb.CommandAbortedException
71      *@exception  com.sun.star.uno.Exception
72      */
getDataStream()73     public XInputStream getDataStream()
74         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
75 
76         XInputStream data = null;
77         if ( m_content != null ) {
78 
79             // Fill argument structure...
80             OpenCommandArgument2 arg = new OpenCommandArgument2();
81             arg.Mode = OpenMode.DOCUMENT;
82             arg.Priority = 32768; // Final static for 32768
83 
84             // Create data sink implementation object.
85             XActiveDataSink dataSink = new MyActiveDataSink();
86             arg.Sink = dataSink;
87 
88             // Execute command "open". The implementation of the command will
89             // supply an XInputStream implementation to the data sink.
90             m_helper.executeCommand( m_content, "open", arg );
91 
92             // Get input stream supplied by the open command implementation.
93             data = dataSink.getInputStream();
94         }
95         return data;
96     }
97 
98     /**
99      *  Get connect URL.
100      *
101      *@return   String  That contains the connect URL
102      */
getContentURL()103     public String getContentURL() {
104         return m_contenturl;
105     }
106 
107     /**
108      * Parse arguments
109      *
110      *@param      String[]   Arguments
111      *@exception  java.lang.Exception
112      */
parseArguments( String[] args )113     public void parseArguments( String[] args ) throws java.lang.Exception {
114 
115         for ( int i = 0; i < args.length; i++ ) {
116             if ( args[i].startsWith( "-url=" )) {
117                 m_contenturl    = args[i].substring( 5 );
118             } else if ( args[i].startsWith( "-help" ) ||
119                         args[i].startsWith( "-?" )) {
120                 printCmdLineUsage();
121                 System.exit( 0 );
122             }
123         }
124 
125 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
126             m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
127         }
128     }
129 
130     /**
131      * Print the commands options
132      */
printCmdLineUsage()133     public void printCmdLineUsage() {
134         System.out.println(
135             "Usage   : DataStreamRetriever -url=..." );
136         System.out.println(
137             "Defaults: -url=<currentdir>/data/data.txt" );
138         System.out.println(
139             "\nExample : -url=file:///temp/my.txt" );
140     }
141 
142     /**
143      *  Print Stream content.
144      *
145      *@param    XInputStream
146      *@exception  com.sun.star.uno.Exception
147      */
printStream( XInputStream data )148     public void printStream( XInputStream data )
149         throws com.sun.star.uno.Exception {
150 
151         /////////////////////////////////////////////////////////////////////
152         // Read data from input stream...65536
153         /////////////////////////////////////////////////////////////////////
154 
155         // Data buffer. Will be allocated by input stream implementation!
156         byte[][] buffer = new byte[ 1 ][ 65536 ];
157         int read = data.readSomeBytes( buffer, 65536 );
158         System.out.println( "Read bytes : " + read );
159         System.out.println( "Read data (only first 64K displayed): ");
160         while ( read > 0 ) {
161             byte[] bytes =  new byte[ read ];
162             for( int i = 0; i < read; i++ ) {
163                 bytes[ i ] = buffer[ 0 ][ i ];
164             }
165             System.out.println( new String(bytes) );
166 
167             // Process data contained in buffer.
168             read = data.readSomeBytes( buffer, 65536 );
169         }
170     }
171 
172     /**
173      *  Create a new connection with the specific args to a running office and
174      *  access (Load) the content data stream of a UCB document content.
175      *
176      *@param  String[]   Arguments
177      */
main( String args[] )178     public static void main ( String args[] ) {
179         System.out.println( "\n" );
180 		System.out.println(
181             "-----------------------------------------------------------------------" );
182 		System.out.println(
183             "DataStreamRetriever - obtains the data stream from a document resource." );
184 		System.out.println(
185             "-----------------------------------------------------------------------" );
186 
187         try {
188 
189             DataStreamRetriever access = new DataStreamRetriever( args );
190             XInputStream data = access.getDataStream();
191             String url = access.getContentURL();
192             if ( data != null )  {
193                 String tempPrint = "\nGetting data stream for resource " + url +
194                 " succeeded.";
195                 int size = tempPrint.length();
196                 System.out.println( tempPrint );
197                 tempPrint = "";
198                 for( int i = 0; i < size; i++ ) {
199                     tempPrint += "-";
200                 }
201                 System.out.println( tempPrint );
202                 access.printStream( data );
203             } else {
204                 System.out.println(
205                     "Getting data stream for resource " + url + " failed." );
206             }
207         } catch ( com.sun.star.io.NotConnectedException e ) {
208             System.out.println( "Error: " + e );
209         } catch ( com.sun.star.io.BufferSizeExceededException e ) {
210             System.out.println( "Error: " + e );
211         } catch ( com.sun.star.io.IOException e ) {
212             System.out.println( "Error: " + e );
213         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
214             System.out.println( "Error: " + e );
215         } catch ( com.sun.star.uno.Exception e ) {
216             System.out.println( "Error: " + e );
217         } catch ( java.lang.Exception e ) {
218             System.out.println( "Error: " + e );
219         }
220         System.exit( 0 );
221     }
222 }
223