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