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