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