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