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.InsertCommandArgument;
25 import com.sun.star.ucb.XContent;
26 import com.sun.star.io.XInputStream;
27 
28 /**
29  * Setting (Storing) the Content Data Stream of a UCB Document Content.
30  */
31 public class DataStreamComposer {
32 
33     /**
34      * Member properties
35      */
36     private  Helper    m_helper;
37     private  XContent  m_content;
38     private  String    m_contenturl    = "";
39     private  String    m_srcURL        = "";
40 
41 
42     /**
43      * Constructor.
44      *
45      *@param      String[]   This construtor requires the arguments:
46      *                          -url=...               (optional)
47      *                          -srcURL=...            (optional)
48      *                          -workdir=...           (optional)
49      *                       See Help (method printCmdLineUsage()).
50      *                       Without the arguments a new connection to a
51      *                       running office cannot created.
52      *@exception  java.lang.Exception
53      */
DataStreamComposer( String args[] )54     public DataStreamComposer( String args[] ) throws java.lang.Exception {
55 
56         // Parse arguments
57         parseArguments( args );
58 
59         // Init
60         m_helper       = new Helper( getContentURL() );
61 
62         // Create UCB content
63         m_content      = m_helper.createUCBContent();
64     }
65 
66     /**
67      * Write the document data stream of a document content.
68      * This method requires the main and the optional arguments to be set in order to work.
69      * See Constructor.
70      *
71      *@return boolean   Result
72      *@exception  com.sun.star.ucb.CommandAbortedException
73      *@exception  com.sun.star.uno.Exception
74      *@exception  java.lang.Exception
75      */
setDataStream()76     public boolean setDataStream()
77         throws com.sun.star.ucb.CommandAbortedException,
78                com.sun.star.uno.Exception,
79                java.lang.Exception {
80 
81          String sourceURL         = getSourceURL();
82          return ( setDataStream( sourceURL ));
83     }
84 
85     /**
86      * Write the document data stream of a document content.
87      *
88      *@param  String    Source URL
89      *@return boolean   Returns true if data stream successfully seted, false otherwise
90      *@exception  com.sun.star.ucb.CommandAbortedException
91      *@exception  com.sun.star.uno.Exception
92      *@exception  java.lang.Exception
93      */
setDataStream( String sourceURL )94     public boolean setDataStream( String sourceURL )
95         throws com.sun.star.ucb.CommandAbortedException,
96                com.sun.star.uno.Exception,
97                java.lang.Exception {
98 
99          XInputStream stream;
100          if ( sourceURL == null || sourceURL.equals("") )  {
101             stream = new MyInputStream();
102          } else {
103             String[] args =  new String[ 1 ];
104             args[ 0 ] = "-url=" + sourceURL;
105             DataStreamRetriever access = new DataStreamRetriever( args );
106             stream = access.getDataStream();
107          }
108          return ( setDataStream( stream ));
109     }
110 
111     /**
112      * Write the document data stream of a document content...
113      *
114      *@param  XInputStream   Stream
115      *@return boolean        Returns true if data stream successfully seted, false otherwise
116      *@exception  com.sun.star.ucb.CommandAbortedException
117      *@exception  com.sun.star.uno.Exception
118      */
setDataStream( XInputStream stream )119     public boolean setDataStream( XInputStream stream )
120         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
121 
122         boolean result = false;
123         XInputStream data = stream;
124         if ( data != null && m_content != null )  {
125 
126             // Fill argument structure...
127             InsertCommandArgument arg = new InsertCommandArgument();
128             arg.Data = data;
129             arg.ReplaceExisting = true;
130 
131             // Execute command "insert".
132             m_helper.executeCommand( m_content, "insert", arg );
133             result = true;
134         }
135         return result;
136     }
137 
138     /**
139      * Get source URL.
140      *
141      *@return String    That contains the source URL
142      */
getSourceURL()143     public String getSourceURL() {
144         return m_srcURL;
145     }
146 
147     /**
148      *  Get connect URL.
149      *
150      *@return   String    That contains the connect URL
151      */
getContentURL()152     public String getContentURL() {
153         return m_contenturl;
154     }
155 
156     /**
157      * Parse arguments
158      *
159      *@param      String[]   Arguments
160      *@exception  java.lang.Exception
161      */
parseArguments( String[] args )162     public void parseArguments( String[] args ) throws java.lang.Exception {
163 
164         String workdir = "";
165 
166         for ( int i = 0; i < args.length; i++ ) {
167             if ( args[i].startsWith( "-url=" )) {
168                 m_contenturl    = args[i].substring( 5 );
169             } else if ( args[i].startsWith( "-srcURL=" )) {
170                 m_srcURL = args[i].substring( 9 );
171             } else if ( args[i].startsWith( "-workdir=" )) {
172                 workdir = args[i].substring( 9 );
173             } else if ( args[i].startsWith( "-help" ) ||
174                         args[i].startsWith( "-?" )) {
175                 printCmdLineUsage();
176                 System.exit( 0 );
177             }
178         }
179 
180 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
181             m_contenturl = Helper.createTargetDataFile( workdir );
182         }
183 
184 		if ( m_srcURL == null || m_srcURL.equals( "" )) {
185             m_srcURL = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
186 		}
187 	}
188 
189     /**
190      * Print the commands options
191      */
printCmdLineUsage()192     public void printCmdLineUsage() {
193         System.out.println(
194             "Usage   : DataStreamComposer -url=... -srcURL=... -workdir=..." );
195         System.out.println(
196             "Defaults: -url=<workdir>/resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt -workdir=<currentdir>" );
197         System.out.println(
198             "\nExample : -url=file:///temp/my.txt -srcURL=file:///temp/src.txt " );
199     }
200 
201 
202     /**
203      *  Create a new connection with the specific args to a running office and
204      *  set the Content Data Stream of a UCB Document Content.
205      *
206      *@param  String[]   Arguments
207      */
main( String args[] )208     public static void main ( String args[] ) {
209         System.out.println( "\n" );
210 		System.out.println(
211             "-----------------------------------------------------------------" );
212 		System.out.println(
213             "DataStreamComposer - sets the data stream of a document resource." );
214 		System.out.println(
215             " The data stream is obtained from another (the source) document " );
216 		System.out.println(
217             " resource before." );
218 		System.out.println(
219             "-----------------------------------------------------------------" );
220         try {
221 
222             DataStreamComposer dataStream = new DataStreamComposer( args );
223             String sourceURL         = dataStream.getSourceURL();
224             boolean result = dataStream.setDataStream( sourceURL );
225             if ( result ) {
226                 System.out.println(
227                         "\nSetting data stream succeeded.\n   Source URL: " +
228                         dataStream.getSourceURL() +
229                         "\n   Target URL: " +
230                         dataStream.getContentURL() );
231             } else {
232                 System.out.println(
233                         "\nSetting data stream failed. \n   Source URL: " +
234                         dataStream.getSourceURL() +
235                         "\n   Target URL: " +
236                         dataStream.getContentURL() );
237             }
238         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
239             System.out.println( "Error: " + e );
240         } catch ( com.sun.star.uno.Exception e ) {
241             System.out.println( "Error: " + e );
242         } catch ( java.lang.Exception e ) {
243             System.out.println( "Error: " + e );
244         }
245         System.exit( 0 );
246     }
247 }
248