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 import java.util.StringTokenizer;
26 
27 import com.sun.star.beans.PropertyValue;
28 import com.sun.star.ucb.XContent;
29 import com.sun.star.uno.UnoRuntime;
30 
31 /**
32  * Setting Property Values of a UCB Content
33  */
34 public class PropertiesComposer {
35 
36     /**
37      * Member properties
38      */
39     private  Helper    m_helper;
40     private  XContent  m_content;
41     private  String    m_contenturl = "";
42     private  Vector    m_propNames          = new Vector();
43     private  Vector    m_propValues         = new Vector();
44 
45     /**
46      * Constructor.
47      *
48      *@param      String[]   This construtor requires the arguments:
49      *                          -url=...        (optional)
50      *                          -propNames=...  (optional)
51      *                          -propValues=... (optional)
52      *                          -workdir=...    (optional)
53      *                       See Help (method printCmdLineUsage()).
54      *                       Without the arguments a new connection to a
55      *                       running office cannot created.
56      *@exception  java.lang.Exception
57      */
PropertiesComposer( String args[] )58     public PropertiesComposer( String args[] ) throws java.lang.Exception {
59 
60         // Parse arguments
61         parseArguments( args );
62 
63         // Init
64         m_helper       = new Helper( getContentURL() );
65 
66         // Create UCB content
67         m_content      = m_helper.createUCBContent();
68     }
69 
70     /**
71      *  Set values of the properties.
72      * This method requires the main and the optional arguments to be set in order to work.
73      * See Constructor.
74      *
75      *@return Object[]  Returns null or instance object of com.sun.star.uno.Any
76      *                  if values successfully seted, properties otherwise
77      *@exception  com.sun.star.ucb.CommandAbortedException
78      *@exception  com.sun.star.uno.Exception
79      */
setProperties()80     public Object[] setProperties()
81         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
82         Vector properties      = getProperties();
83         Vector propertyValues  = getPropertyValues();
84         return setProperties( properties, propertyValues );
85     }
86 
87     /**
88      *  Set values of the properties.
89      *
90      *@param  Vector    Properties
91      *@param  Vector    Properties value
92      *@return Object[]  Returns null or instance object of com.sun.star.uno.Any
93      *                  if values successfully seted, properties otherwise
94      *@exception  com.sun.star.ucb.CommandAbortedException
95      *@exception  com.sun.star.uno.Exception
96      */
setProperties( Vector properties, Vector propertiesValues )97     public Object[] setProperties( Vector properties, Vector propertiesValues )
98         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
99 
100         Object[] result = null;
101         if ( m_content != null && !properties.isEmpty() &&
102              !propertiesValues.isEmpty() &&
103              properties.size() == propertiesValues.size() ) {
104 
105             /*
106             ****     This code is for unregistered properties.     ****
107 
108             XPropertyContainer xPropContainer
109                     = (XPropertyContainer)UnoRuntime.queryInterface(
110                         XPropertyContainer.class, m_content );
111 
112             XPropertySetInfo xPropSetInfo = ( XPropertySetInfo )UnoRuntime.queryInterface(
113                     XPropertySetInfo.class,
114                     m_helper.executeCommand( m_content, "getPropertySetInfo", null ));
115             */
116 
117             int size = properties.size();
118             PropertyValue[] props = new PropertyValue[ size ];
119             for ( int index = 0 ; index < size; index++ ) {
120                 String propName  = ( String )properties.get( index );
121                 Object propValue = propertiesValues.get( index );
122 
123                 /*
124                 ****     This code is for unregistered properties.     ****
125 
126                 if ( !xPropSetInfo.hasPropertyByName( propName )) {
127                     xPropContainer.addProperty(
128                         propName, PropertyAttribute.MAYBEVOID, propValue );
129                 }
130                 */
131 
132                 // Define property sequence.
133                 PropertyValue prop = new PropertyValue();
134                 prop.Name = propName;
135                 prop.Handle = -1; // n/a
136                 prop.Value  = propValue;
137                 props[ index ] = prop;
138             }
139 
140             // Execute command "setPropertiesValues".
141             Object[] obj =
142                 ( Object[] )m_helper.executeCommand( m_content, "setPropertyValues", props );
143             if ( obj.length == size )
144                  result = obj;
145         }
146         return result;
147     }
148 
149     /**
150      *  Get properties names.
151      *
152      *@return   Vector    That contains the properties names
153      */
getProperties()154     public Vector getProperties() {
155         return m_propNames;
156     }
157 
158     /**
159      *  Get properties values.
160      *
161      *@return   Vector    That contains the properties values
162      */
getPropertyValues()163     public Vector getPropertyValues() {
164         return m_propValues;
165     }
166 
167     /**
168      *  Get connect URL.
169      *
170      *@return   String    That contains the connect URL
171      */
getContentURL()172     public String getContentURL() {
173         return m_contenturl;
174     }
175 
176     /**
177      * Parse arguments
178      *
179      *@param      String[]   Arguments
180      *@exception  java.lang.Exception
181      */
parseArguments( String[] args )182     public void parseArguments( String[] args ) throws java.lang.Exception {
183 
184         String workdir = "";
185 
186         for ( int i = 0; i < args.length; i++ ) {
187             if ( args[i].startsWith( "-url=" )) {
188                 m_contenturl    = args[i].substring( 5 );
189             } else if ( args[i].startsWith( "-propNames=" )) {
190                 StringTokenizer tok
191                     = new StringTokenizer( args[i].substring( 11 ), ";" );
192 
193                 while ( tok.hasMoreTokens() )
194                     m_propNames.add( tok.nextToken() );
195 
196             } else if ( args[i].startsWith( "-propValues=" )) {
197                 StringTokenizer tok
198                     = new StringTokenizer( args[i].substring( 12 ), ";" );
199 
200                 while ( tok.hasMoreTokens() )
201                     m_propValues.add( tok.nextToken() );
202             } else if ( args[i].startsWith( "-workdir=" )) {
203                 workdir = args[i].substring( 9 );
204             } else if ( args[i].startsWith( "-help" ) ||
205                         args[i].startsWith( "-?" )) {
206                 printCmdLineUsage();
207                 System.exit( 0 );
208             }
209         }
210 
211 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
212             m_contenturl = Helper.createTargetDataFile( workdir );
213         }
214 
215         if ( m_propNames.size() == 0 ) {
216             m_propNames.add( "Title" );
217         }
218 
219         if ( m_propValues.size() == 0 ) {
220             m_propValues.add(
221                 "changed-" + m_contenturl.substring(
222                     m_contenturl.lastIndexOf( "/" ) + 1 ) );
223         }
224     }
225 
226     /**
227      * Print the commands options
228      */
printCmdLineUsage()229     public void printCmdLineUsage() {
230         System.out.println(
231             "Usage   : PropertiesComposer -url=... -propNames=... -propValues=... -workdir=..." );
232         System.out.println(
233             "Defaults: -url=<workdir>/resource-<uniquepostfix> -propNames=Title -propValues=changed-<uniquepostfix> -workdir=<currentdir>" );
234         System.out.println(
235             "\nExample : -propNames=Title;Foo -propValues=MyRenamedFile.txt;bar" );
236     }
237 
238     /**
239      *  Create a new connection with the specific args to a running office and
240      *  set properties of a resource.
241      *
242      *@param  String[]   Arguments
243      */
main( String args[] )244     public static void main ( String args[] ) {
245         System.out.println( "\n" );
246 		System.out.println(
247             "--------------------------------------------------------" );
248 		System.out.println(
249             "PropertiesComposer - sets property values of a resource." );
250 		System.out.println(
251             "--------------------------------------------------------" );
252 
253         try {
254 
255             PropertiesComposer setProp = new PropertiesComposer( args );
256             Vector properties       = setProp.getProperties();
257             Vector propertiesValues = setProp.getPropertyValues();
258             Object[] result = setProp.setProperties( properties, propertiesValues );
259 
260             String tempPrint = "\nSetting properties of resource " + setProp.getContentURL();
261             int size = tempPrint.length();
262             System.out.println( tempPrint );
263             tempPrint = "";
264             for( int i = 0; i < size; i++ ) {
265                 tempPrint += "-";
266             }
267             System.out.println( tempPrint );
268             if ( result != null )  {
269                 for ( int index = 0; index < result.length; index++  ) {
270                     Object obj = result[ index ];
271                     if( obj == null || obj instanceof com.sun.star.uno.Any )
272                         System.out.println(
273                             "Setting property " + properties.get( index ) + " succeeded." );
274                     else
275                         System.out.println(
276                             "Setting property " + properties.get( index ) + " failed." );
277                 }
278             }
279         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
280             System.out.println( "Error: " + e );
281         } catch ( com.sun.star.uno.Exception e ) {
282             System.out.println( "Error: " + e );
283         } catch ( java.lang.Exception e ) {
284             System.out.println( "Error: " + e );
285         }
286         System.exit( 0 );
287     }
288 }
289