1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 import java.util.Vector;
36 import java.util.StringTokenizer;
37 
38 import com.sun.star.beans.PropertyValue;
39 import com.sun.star.ucb.XContent;
40 import com.sun.star.uno.UnoRuntime;
41 
42 /**
43  * Setting Property Values of a UCB Content
44  */
45 public class PropertiesComposer {
46 
47     /**
48      * Member properties
49      */
50     private  Helper    m_helper;
51     private  XContent  m_content;
52     private  String    m_contenturl = "";
53     private  Vector    m_propNames          = new Vector();
54     private  Vector    m_propValues         = new Vector();
55 
56     /**
57      * Constructor.
58      *
59      *@param      String[]   This construtor requires the arguments:
60      *                          -url=...        (optional)
61      *                          -propNames=...  (optional)
62      *                          -propValues=... (optional)
63      *                          -workdir=...    (optional)
64      *                       See Help (method printCmdLineUsage()).
65      *                       Without the arguments a new connection to a
66      *                       running office cannot created.
67      *@exception  java.lang.Exception
68      */
69     public PropertiesComposer( String args[] ) throws java.lang.Exception {
70 
71         // Parse arguments
72         parseArguments( args );
73 
74         // Init
75         m_helper       = new Helper( getContentURL() );
76 
77         // Create UCB content
78         m_content      = m_helper.createUCBContent();
79     }
80 
81     /**
82      *  Set values of the properties.
83      * This method requires the main and the optional arguments to be set in order to work.
84      * See Constructor.
85      *
86      *@return Object[]  Returns null or instance object of com.sun.star.uno.Any
87      *                  if values successfully seted, properties otherwise
88      *@exception  com.sun.star.ucb.CommandAbortedException
89      *@exception  com.sun.star.uno.Exception
90      */
91     public Object[] setProperties()
92         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
93         Vector properties      = getProperties();
94         Vector propertyValues  = getPropertyValues();
95         return setProperties( properties, propertyValues );
96     }
97 
98     /**
99      *  Set values of the properties.
100      *
101      *@param  Vector    Properties
102      *@param  Vector    Properties value
103      *@return Object[]  Returns null or instance object of com.sun.star.uno.Any
104      *                  if values successfully seted, properties otherwise
105      *@exception  com.sun.star.ucb.CommandAbortedException
106      *@exception  com.sun.star.uno.Exception
107      */
108     public Object[] setProperties( Vector properties, Vector propertiesValues )
109         throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
110 
111         Object[] result = null;
112         if ( m_content != null && !properties.isEmpty() &&
113              !propertiesValues.isEmpty() &&
114              properties.size() == propertiesValues.size() ) {
115 
116             /*
117             ****     This code is for unregistered properties.     ****
118 
119             XPropertyContainer xPropContainer
120                     = (XPropertyContainer)UnoRuntime.queryInterface(
121                         XPropertyContainer.class, m_content );
122 
123             XPropertySetInfo xPropSetInfo = ( XPropertySetInfo )UnoRuntime.queryInterface(
124                     XPropertySetInfo.class,
125                     m_helper.executeCommand( m_content, "getPropertySetInfo", null ));
126             */
127 
128             int size = properties.size();
129             PropertyValue[] props = new PropertyValue[ size ];
130             for ( int index = 0 ; index < size; index++ ) {
131                 String propName  = ( String )properties.get( index );
132                 Object propValue = propertiesValues.get( index );
133 
134                 /*
135                 ****     This code is for unregistered properties.     ****
136 
137                 if ( !xPropSetInfo.hasPropertyByName( propName )) {
138                     xPropContainer.addProperty(
139                         propName, PropertyAttribute.MAYBEVOID, propValue );
140                 }
141                 */
142 
143                 // Define property sequence.
144                 PropertyValue prop = new PropertyValue();
145                 prop.Name = propName;
146                 prop.Handle = -1; // n/a
147                 prop.Value  = propValue;
148                 props[ index ] = prop;
149             }
150 
151             // Execute command "setPropertiesValues".
152             Object[] obj =
153                 ( Object[] )m_helper.executeCommand( m_content, "setPropertyValues", props );
154             if ( obj.length == size )
155                  result = obj;
156         }
157         return result;
158     }
159 
160     /**
161      *  Get properties names.
162      *
163      *@return   Vector    That contains the properties names
164      */
165     public Vector getProperties() {
166         return m_propNames;
167     }
168 
169     /**
170      *  Get properties values.
171      *
172      *@return   Vector    That contains the properties values
173      */
174     public Vector getPropertyValues() {
175         return m_propValues;
176     }
177 
178     /**
179      *  Get connect URL.
180      *
181      *@return   String    That contains the connect URL
182      */
183     public String getContentURL() {
184         return m_contenturl;
185     }
186 
187     /**
188      * Parse arguments
189      *
190      *@param      String[]   Arguments
191      *@exception  java.lang.Exception
192      */
193     public void parseArguments( String[] args ) throws java.lang.Exception {
194 
195         String workdir = "";
196 
197         for ( int i = 0; i < args.length; i++ ) {
198             if ( args[i].startsWith( "-url=" )) {
199                 m_contenturl    = args[i].substring( 5 );
200             } else if ( args[i].startsWith( "-propNames=" )) {
201                 StringTokenizer tok
202                     = new StringTokenizer( args[i].substring( 11 ), ";" );
203 
204                 while ( tok.hasMoreTokens() )
205                     m_propNames.add( tok.nextToken() );
206 
207             } else if ( args[i].startsWith( "-propValues=" )) {
208                 StringTokenizer tok
209                     = new StringTokenizer( args[i].substring( 12 ), ";" );
210 
211                 while ( tok.hasMoreTokens() )
212                     m_propValues.add( tok.nextToken() );
213             } else if ( args[i].startsWith( "-workdir=" )) {
214                 workdir = args[i].substring( 9 );
215             } else if ( args[i].startsWith( "-help" ) ||
216                         args[i].startsWith( "-?" )) {
217                 printCmdLineUsage();
218                 System.exit( 0 );
219             }
220         }
221 
222 		if ( m_contenturl == null || m_contenturl.equals( "" )) {
223             m_contenturl = Helper.createTargetDataFile( workdir );
224         }
225 
226         if ( m_propNames.size() == 0 ) {
227             m_propNames.add( "Title" );
228         }
229 
230         if ( m_propValues.size() == 0 ) {
231             m_propValues.add(
232                 "changed-" + m_contenturl.substring(
233                     m_contenturl.lastIndexOf( "/" ) + 1 ) );
234         }
235     }
236 
237     /**
238      * Print the commands options
239      */
240     public void printCmdLineUsage() {
241         System.out.println(
242             "Usage   : PropertiesComposer -url=... -propNames=... -propValues=... -workdir=..." );
243         System.out.println(
244             "Defaults: -url=<workdir>/resource-<uniquepostfix> -propNames=Title -propValues=changed-<uniquepostfix> -workdir=<currentdir>" );
245         System.out.println(
246             "\nExample : -propNames=Title;Foo -propValues=MyRenamedFile.txt;bar" );
247     }
248 
249     /**
250      *  Create a new connection with the specific args to a running office and
251      *  set properties of a resource.
252      *
253      *@param  String[]   Arguments
254      */
255     public static void main ( String args[] ) {
256         System.out.println( "\n" );
257 		System.out.println(
258             "--------------------------------------------------------" );
259 		System.out.println(
260             "PropertiesComposer - sets property values of a resource." );
261 		System.out.println(
262             "--------------------------------------------------------" );
263 
264         try {
265 
266             PropertiesComposer setProp = new PropertiesComposer( args );
267             Vector properties       = setProp.getProperties();
268             Vector propertiesValues = setProp.getPropertyValues();
269             Object[] result = setProp.setProperties( properties, propertiesValues );
270 
271             String tempPrint = "\nSetting properties of resource " + setProp.getContentURL();
272             int size = tempPrint.length();
273             System.out.println( tempPrint );
274             tempPrint = "";
275             for( int i = 0; i < size; i++ ) {
276                 tempPrint += "-";
277             }
278             System.out.println( tempPrint );
279             if ( result != null )  {
280                 for ( int index = 0; index < result.length; index++  ) {
281                     Object obj = result[ index ];
282                     if( obj == null || obj instanceof com.sun.star.uno.Any )
283                         System.out.println(
284                             "Setting property " + properties.get( index ) + " succeeded." );
285                     else
286                         System.out.println(
287                             "Setting property " + properties.get( index ) + " failed." );
288                 }
289             }
290         } catch ( com.sun.star.ucb.CommandAbortedException e ) {
291             System.out.println( "Error: " + e );
292         } catch ( com.sun.star.uno.Exception e ) {
293             System.out.println( "Error: " + e );
294         } catch ( java.lang.Exception e ) {
295             System.out.println( "Error: " + e );
296         }
297         System.exit( 0 );
298     }
299 }
300