cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / odk / examples / DevelopersGuide / UCB / PropertiesComposer.java
blob1ebc9cbaab79ee6abc165db528d138888dba89fb
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
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.
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.
34 *************************************************************************/
36 import java.util.ArrayList;
37 import java.util.StringTokenizer;
39 import com.sun.star.beans.PropertyValue;
40 import com.sun.star.ucb.XContent;
42 /**
43 * Setting Property Values of a UCB Content
45 public class PropertiesComposer {
47 /**
48 * Member properties
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>();
56 /**
57 * Constructor.
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.
68 public PropertiesComposer( String args[] ) throws java.lang.Exception {
70 // Parse arguments
71 parseArguments( args );
73 // Init
74 m_helper = new Helper( getContentURL() );
76 // Create UCB content
77 m_content = m_helper.createUCBContent();
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.
85 *@return Object[] Returns null or instance object of com.sun.star.uno.Any
86 * if values successfully seted, properties otherwise
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 );
95 /**
96 * Set values of the properties.
98 *@return Object[] Returns null or instance object of com.sun.star.uno.Any
99 * if values successfully seted, properties otherwise
101 public Object[] setProperties( ArrayList<String> properties, ArrayList<String> propertiesValues )
102 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception {
104 Object[] result = null;
105 if ( m_content != null && !properties.isEmpty() &&
106 !propertiesValues.isEmpty() &&
107 properties.size() == propertiesValues.size() ) {
110 **** This code is for unregistered properties. ****
112 XPropertyContainer xPropContainer
113 = (XPropertyContainer)UnoRuntime.queryInterface(
114 XPropertyContainer.class, m_content );
116 XPropertySetInfo xPropSetInfo = ( XPropertySetInfo )UnoRuntime.queryInterface(
117 XPropertySetInfo.class,
118 m_helper.executeCommand( m_content, "getPropertySetInfo", null ));
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 );
128 **** This code is for unregistered properties. ****
130 if ( !xPropSetInfo.hasPropertyByName( propName )) {
131 xPropContainer.addProperty(
132 propName, PropertyAttribute.MAYBEVOID, propValue );
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;
144 // Execute command "setPropertiesValues".
145 Object[] obj =
146 ( Object[] )m_helper.executeCommand( m_content, "setPropertyValues", props );
147 if ( obj.length == size )
148 result = obj;
150 return result;
154 * Get properties names.
156 *@return Vector That contains the properties names
158 public ArrayList<String> getProperties() {
159 return m_propNames;
163 * Get properties values.
165 *@return Vector That contains the properties values
167 public ArrayList<String> getPropertyValues() {
168 return m_propValues;
172 * Get connect URL.
174 *@return String That contains the connect URL
176 public String getContentURL() {
177 return m_contenturl;
181 * Parse arguments
183 public void parseArguments( String[] args ) throws java.lang.Exception {
185 String workdir = "";
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 ), ";" );
194 while ( tok.hasMoreTokens() )
195 m_propNames.add( tok.nextToken() );
197 } else if ( args[i].startsWith( "-propValues=" )) {
198 StringTokenizer tok
199 = new StringTokenizer( args[i].substring( 12 ), ";" );
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 );
212 if ( m_contenturl == null || m_contenturl.equals( "" )) {
213 m_contenturl = Helper.createTargetDataFile( workdir );
216 if ( m_propNames.size() == 0 ) {
217 m_propNames.add( "Title" );
220 if ( m_propValues.size() == 0 ) {
221 m_propValues.add(
222 "changed-" + m_contenturl.substring(
223 m_contenturl.lastIndexOf( "/" ) + 1 ) );
228 * Print the commands options
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" );
240 * Create a new connection with the specific args to a running office and
241 * set properties of a resource.
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 "--------------------------------------------------------" );
252 try {
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 );
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 += "-";
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." );
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 );
285 System.exit( 0 );
289 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */