1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  *  The Contents of this file are made available subject to the terms of
5  *  the BSD license.
6  *
7  *  Copyright 2000, 2010 Oracle and/or its affiliates.
8  *  All rights reserved.
9  *
10  *  Redistribution and use in source and binary forms, with or without
11  *  modification, are permitted provided that the following conditions
12  *  are met:
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.
21  *
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.
33  *
34  *************************************************************************/
35 
36 import com.sun.star.ucb.InsertCommandArgument;
37 import com.sun.star.ucb.XContent;
38 import com.sun.star.io.XInputStream;
39 
40 /**
41  * Setting (Storing) the Content Data Stream of a UCB Document Content.
42  */
43 public class DataStreamComposer {
44 
45     /**
46      * Member properties
47      */
48     private  Helper    m_helper;
49     private  XContent  m_content;
50     private  String    m_contenturl    = "";
51     private  String    m_srcURL        = "";
52 
53 
54     /**
55      * Constructor.
56      *
57      *@param      args   This constructor requires the arguments:
58      *                          -url=...               (optional)
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.
64      */
DataStreamComposer( String args[] )65     public DataStreamComposer( String args[] ) throws java.lang.Exception {
66 
67         // Parse arguments
68         parseArguments( args );
69 
70         // Init
71         m_helper       = new Helper( getContentURL() );
72 
73         // Create UCB content
74         m_content      = m_helper.createUCBContent();
75     }
76 
77     /**
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.
80      * See Constructor.
81      *
82      *@return boolean   Result
83      */
setDataStream()84     public boolean setDataStream()
85         throws com.sun.star.ucb.CommandAbortedException,
86                com.sun.star.uno.Exception,
87                java.lang.Exception {
88 
89          String sourceURL         = getSourceURL();
90          return ( setDataStream( sourceURL ));
91     }
92 
93     /**
94      * Write the document data stream of a document content.
95      *
96      *@param  sourceURL    Source URL
97      *@return true if data stream successfully seted, false otherwise
98      */
setDataStream( String sourceURL )99     public boolean setDataStream( String sourceURL )
100         throws com.sun.star.ucb.CommandAbortedException,
101                com.sun.star.uno.Exception,
102                java.lang.Exception {
103 
104          XInputStream stream;
105          if ( sourceURL == null || sourceURL.equals("") )  {
106             stream = new MyInputStream();
107          } else {
108             String[] args =  new String[ 1 ];
109             args[ 0 ] = "-url=" + sourceURL;
110             DataStreamRetriever access = new DataStreamRetriever( args );
111             stream = access.getDataStream();
112          }
113          return ( setDataStream( stream ));
114     }
115 
116     /**
117      * Write the document data stream of a document content...
118      *
119      *@return boolean        Returns true if data stream successfully seted, false otherwise
120      */
setDataStream( XInputStream stream )121     public boolean setDataStream( XInputStream stream )
122         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
123 
124         boolean result = false;
125         XInputStream data = stream;
126         if ( data != null && m_content != null )  {
127 
128             // Fill argument structure...
129             InsertCommandArgument arg = new InsertCommandArgument();
130             arg.Data = data;
131             arg.ReplaceExisting = true;
132 
133             // Execute command "insert".
134             m_helper.executeCommand( m_content, "insert", arg );
135             result = true;
136         }
137         return result;
138     }
139 
140     /**
141      * Get source URL.
142      *
143      *@return String    That contains the source URL
144      */
getSourceURL()145     public String getSourceURL() {
146         return m_srcURL;
147     }
148 
149     /**
150      *  Get connect URL.
151      *
152      *@return   String    That contains the connect URL
153      */
getContentURL()154     public String getContentURL() {
155         return m_contenturl;
156     }
157 
158     /**
159      * Parse arguments
160      */
parseArguments( String[] args )161     public void parseArguments( String[] args ) throws java.lang.Exception {
162 
163         String workdir = "";
164 
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( "-?" )) {
174                 printCmdLineUsage();
175                 System.exit( 0 );
176             }
177         }
178 
179         if ( m_contenturl == null || m_contenturl.equals( "" )) {
180             m_contenturl = Helper.createTargetDataFile( workdir );
181         }
182 
183         if ( m_srcURL == null || m_srcURL.equals( "" )) {
184             m_srcURL = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" );
185         }
186     }
187 
188     /**
189      * Print the commands options
190      */
printCmdLineUsage()191     public void printCmdLineUsage() {
192         System.out.println(
193             "Usage   : DataStreamComposer -url=... -srcURL=... -workdir=..." );
194         System.out.println(
195             "Defaults: -url=<workdir>/resource-<uniquepostfix> -srcURL=<currentdir>/data/data.txt -workdir=<currentdir>" );
196         System.out.println(
197             "\nExample : -url=file:///temp/my.txt -srcURL=file:///temp/src.txt " );
198     }
199 
200 
201     /**
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.
204      */
main( String args[] )205     public static void main ( String args[] ) {
206         System.out.println( "\n" );
207         System.out.println(
208             "-----------------------------------------------------------------" );
209         System.out.println(
210             "DataStreamComposer - sets the data stream of a document resource." );
211         System.out.println(
212             " The data stream is obtained from another (the source) document " );
213         System.out.println(
214             " resource before." );
215         System.out.println(
216             "-----------------------------------------------------------------" );
217         try {
218 
219             DataStreamComposer dataStream = new DataStreamComposer( args );
220             String sourceURL         = dataStream.getSourceURL();
221             boolean result = dataStream.setDataStream( sourceURL );
222             if ( result ) {
223                 System.out.println(
224                         "\nSetting data stream succeeded.\n   Source URL: " +
225                         dataStream.getSourceURL() +
226                         "\n   Target URL: " +
227                         dataStream.getContentURL() );
228             } else {
229                 System.out.println(
230                         "\nSetting data stream failed. \n   Source URL: " +
231                         dataStream.getSourceURL() +
232                         "\n   Target URL: " +
233                         dataStream.getContentURL() );
234             }
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 );
241         }
242         System.exit( 0 );
243     }
244 }
245 
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
247