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