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 java.util.Vector;
25 
26 import java.io.File;
27 import java.io.FileOutputStream;
28 
29 import com.sun.star.lang.XMultiComponentFactory;
30 
31 import com.sun.star.ucb.Command;
32 import com.sun.star.ucb.XContent;
33 import com.sun.star.ucb.XContentProvider;
34 import com.sun.star.ucb.XContentIdentifier;
35 import com.sun.star.ucb.XContentIdentifierFactory;
36 import com.sun.star.ucb.XCommandProcessor;
37 
38 import com.sun.star.uno.XInterface;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XComponentContext;
41 
42 
43 /**
44  * Helper for creating a new connection with the specific args to a running office.
45  */
46 public class Helper {
47 
48     /**
49      * Member properties
50      */
51     private XInterface  m_ucb        = null;
52     private String      m_contenturl = null;
53     private static XComponentContext   m_xContext   = null;
54 
55     /**
56      *  Constructor, create a new instance of the ucb. UNO is bootstrapped and
57      *  the remote office service manger is used to create the ucb. If necessary
58      *  a new office process is started.
59      *
60      *  @exception  java.lang.Exception
61      */
Helper(String url)62     public Helper(String url) throws java.lang.Exception {
63         m_contenturl    = url;
64 
65         if (null == m_xContext ) {
66             // get the remote office component context
67             m_xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();
68             System.out.println("Connected to a running office ...");
69         }
70 
71         XMultiComponentFactory xMCF = m_xContext.getServiceManager();
72 
73         m_ucb = (XInterface)UnoRuntime.queryInterface(XInterface.class,
74             xMCF.createInstanceWithContext(
75                         "com.sun.star.ucb.UniversalContentBroker", m_xContext));
76     }
77 
78     /**
79      * Returns created identifier object for given URL..
80      *
81      *@return     XContent       Created identifier object for given URL
82      *@exception  java.lang.Exception
83      */
createUCBContent()84     public XContent createUCBContent() throws java.lang.Exception {
85         return createUCBContent( getContentURL() );
86     }
87 
88     /**
89      * Returned created identifier object for given URL.
90      *
91      *@param      String         Connect URL. Example : -url=file:///
92      *@return     XContent       Created identifier object for given URL
93      *@exception  java.lang.Exception
94      */
createUCBContent( String connectURL )95     public XContent createUCBContent( String connectURL ) throws java.lang.Exception {
96         XContent content = null;
97         if ( connectURL != null && !connectURL.equals( "" )) {
98 
99             // Obtain required UCB interfaces...
100             XContentIdentifierFactory idFactory
101                 = ( XContentIdentifierFactory )UnoRuntime.queryInterface(
102                     XContentIdentifierFactory.class, m_ucb );
103             XContentProvider provider
104                 = ( XContentProvider )UnoRuntime.queryInterface(
105                     XContentProvider.class, m_ucb );
106 
107             // Create identifier object for given URL.
108             XContentIdentifier id = idFactory.createContentIdentifier( connectURL );
109             content = provider.queryContent( id );
110         }
111         return content;
112     }
113 
114     /**
115      *  Get ucb instance.
116      *
117      *@return   XInterface  That contains the ucb  instance
118      */
getUCB()119     public XInterface getUCB() {
120         return m_ucb;
121     }
122 
123     /**
124      *  Get connect URL.
125      *
126      *@return   String  That contains the connect URL
127      */
getContentURL()128     public String getContentURL() {
129         return m_contenturl;
130     }
131 
132     /**
133      *  Executes a command.
134      *
135      *param       XInterface
136      *param       String
137      *param       Object
138      *@return     Object     The result according to the specification of the command.
139      *@exception  com.sun.star.ucb.CommandAbortedException
140      *@exception  com.sun.star.uno.Exception
141      */
executeCommand( XInterface ifc, String commandName, Object argument )142     Object executeCommand( XInterface ifc, String commandName, Object argument )
143         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception  {
144 
145         /////////////////////////////////////////////////////////////////////
146         // Obtain command processor interface from given content.
147         /////////////////////////////////////////////////////////////////////
148 
149         XCommandProcessor cmdProcessor
150             = (XCommandProcessor)UnoRuntime.queryInterface(
151                 XCommandProcessor.class, ifc );
152 
153         /////////////////////////////////////////////////////////////////////
154         // Assemble command to execute.
155         /////////////////////////////////////////////////////////////////////
156 
157         Command command = new Command();
158         command.Name     = commandName;
159         command.Handle   = -1; // not available
160         command.Argument = argument;
161 
162         // Note: throws CommandAbortedException, Exception
163         return cmdProcessor.execute( command, 0, null );
164     }
165 
getAbsoluteFileURLFromSystemPath( String systemPath )166 	public static String getAbsoluteFileURLFromSystemPath( String systemPath )
167     {
168 		try
169 		{
170 			File file = new File( systemPath );
171             String url = file.toURL().toString();
172             if ( url.charAt( 6 ) != '/' ) { // file:/xxx vs. file:///xxxx
173                 StringBuffer buf1 = new StringBuffer( "file:///" );
174                 buf1.append( url.substring( 6 ) );
175                 url = buf1.toString();
176             }
177             return url;
178 		}
179 		catch ( java.net.MalformedURLException e )
180 		{
181 			e.printStackTrace();
182 		}
183         return new String();
184     }
185 
prependCurrentDirAsAbsoluteFileURL( String relativeURL )186 	public static String prependCurrentDirAsAbsoluteFileURL( String relativeURL )
187     {
188         // get url of current dir.
189         String url = getAbsoluteFileURLFromSystemPath( "" );
190 		StringBuffer buf = new StringBuffer( url );
191         if ( !url.endsWith( File.separator ) )
192             buf.append( File.separator );
193 		buf.append( relativeURL );
194         return buf.toString();
195 	}
196 
createTargetDataFile( String workDir )197     public static String createTargetDataFile( String workDir )
198     {
199         try
200         {
201             StringBuffer buf = new StringBuffer();
202             if ( workDir != null && workDir.length() > 0 ) {
203                 buf.append( workDir );
204                 buf.append( File.separator );
205             }
206             buf.append( "resource-" );
207             buf.append( System.currentTimeMillis() );
208             File file = new File( buf.toString() );
209             String url = file.toURL().toString();
210             if ( url.charAt( 6 ) != '/' ) { // file:/xxx vs. file:///xxxx
211                 StringBuffer buf1 = new StringBuffer( "file:///" );
212                 buf1.append( url.substring( 6 ) );
213                 url = buf1.toString();
214             }
215 
216             try
217             {
218                 file.createNewFile();
219                 String content = new String(
220                     "This is the content of a sample data file." );
221                 FileOutputStream stream = new FileOutputStream( file );
222                 stream.write( content.getBytes() );
223                 stream.close();
224             }
225             catch ( java.io.IOException e )
226             {
227                 e.printStackTrace();
228             }
229 
230             return url;
231         }
232         catch ( java.net.MalformedURLException e )
233         {
234             e.printStackTrace();
235         }
236 
237         return new String();
238     }
239 }
240