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.Property; 40 import com.sun.star.sdbc.XRow; 41 import com.sun.star.ucb.XContent; 42 import com.sun.star.uno.UnoRuntime; 43 44 45 /** 46 * Obtaining Property Values from a UCB Content 47 */ 48 public class PropertiesRetriever { 49 50 /** 51 * Member properties 52 */ 53 private Helper m_helper; 54 private XContent m_content; 55 private String m_contenturl = ""; 56 private ArrayList<String> m_propNames = new ArrayList<String>(); 57 58 /** 59 * Constructor. 60 * 61 *@param args This constructor requires the arguments: 62 * -url=... (optional) 63 * -propNames=... (optional) 64 * See Help (method printCmdLineUsage()). 65 * Without the arguments a new connection to a 66 * running office cannot created. 67 */ PropertiesRetriever( String args[] )68 public PropertiesRetriever( 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 * Get 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 Properties values if values successfully retrieved, null otherwise 86 */ getPropertyValues()87 public ArrayList<Object> getPropertyValues() 88 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 89 ArrayList<String> properties = getProperties(); 90 return getPropertyValues ( properties ); 91 } 92 93 /** 94 * Get values of the properties. 95 * 96 *@return Properties values if values successfully retrieved, null otherwise 97 */ getPropertyValues( ArrayList<String> properties )98 public ArrayList<Object> getPropertyValues( ArrayList<String> properties ) 99 throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 100 ArrayList<Object> m_propValues = null; 101 if ( m_content != null && properties != null && !properties.isEmpty() ) { 102 103 int size = properties.size(); 104 105 // Fill info for the properties wanted. 106 Property[] props = new Property[ size ]; 107 for ( int index = 0 ; index < size; index++ ) { 108 109 // Define property sequence. 110 Property prop = new Property(); 111 prop.Name = properties.get( index ); 112 prop.Handle = -1; // n/a 113 props[ index ] = prop; 114 } 115 116 // Execute command "getPropertyValues". 117 XRow values = 118 UnoRuntime.queryInterface( 119 XRow.class, m_helper.executeCommand( m_content,"getPropertyValues", props )); 120 121 m_propValues = new ArrayList<Object>(); 122 123 /* 124 Extract values from row object. Note that the 125 first column is 1, not 0. 126 Title: Obtain value of column 1 as string.*/ 127 for ( int index = 1 ; index <= size; index++ ) { 128 Object propertyValue = values.getObject( index, null ); 129 if ( !values.wasNull() && !(propertyValue instanceof com.sun.star.uno.Any )) 130 m_propValues.add( propertyValue ); 131 else 132 m_propValues.add( "[ Property not found ]" ); 133 } 134 } 135 return m_propValues; 136 } 137 138 /** 139 * Get connect URL. 140 * 141 *@return String That contains the connect URL 142 */ getContentURL()143 public String getContentURL() { 144 return m_contenturl; 145 } 146 147 /** 148 * Get the properties. 149 * 150 *@return Vector That contains the properties 151 */ getProperties()152 public ArrayList<String> getProperties() { 153 return m_propNames; 154 } 155 156 /** 157 * Parse arguments 158 */ parseArguments( String[] args )159 public void parseArguments( String[] args ) throws java.lang.Exception { 160 161 for ( int i = 0; i < args.length; i++ ) { 162 if ( args[i].startsWith( "-url=" )) { 163 m_contenturl = args[i].substring( 5 ); 164 } else if ( args[i].startsWith( "-propNames=" )) { 165 StringTokenizer tok 166 = new StringTokenizer( args[i].substring( 11 ), ";" ); 167 168 while ( tok.hasMoreTokens() ) 169 m_propNames.add( tok.nextToken() ); 170 171 } else if ( args[i].startsWith( "-help" ) || 172 args[i].startsWith( "-?" )) { 173 printCmdLineUsage(); 174 System.exit( 0 ); 175 } 176 } 177 178 if ( m_contenturl == null || m_contenturl.equals( "" )) { 179 m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" ); 180 } 181 182 if ( m_propNames.size() == 0 ) { 183 m_propNames.add( "Title" ); 184 m_propNames.add( "IsDocument" ); 185 } 186 } 187 188 /** 189 * Print the commands options 190 */ printCmdLineUsage()191 public void printCmdLineUsage() { 192 System.out.println( 193 "Usage : PropertiesRetriever -url=... -propNames=..." ); 194 System.out.println( 195 "Defaults: -url=<currentdir>/data/data.txt -propNames=Title;IsDocument" ); 196 System.out.println( 197 "\nExample : -propNames=Title;IsFolder" ); 198 } 199 200 /** 201 * Create a new connection with the specific args to a running office and 202 * get the properties values from a resource. 203 */ main( String args[] )204 public static void main ( String args[] ) { 205 System.out.println( "\n" ); 206 System.out.println( 207 "--------------------------------------------------------------" ); 208 System.out.println( 209 "PropertiesRetriever - obtains property values from a resource." ); 210 System.out.println( 211 "--------------------------------------------------------------" ); 212 try { 213 PropertiesRetriever obtProperty = new PropertiesRetriever( args ); 214 ArrayList<String> properties = obtProperty.getProperties(); 215 ArrayList<Object> propertiesValues = obtProperty.getPropertyValues( properties ); 216 217 String tempPrint = "\nProperties of resource " + obtProperty.getContentURL(); 218 int size = tempPrint.length(); 219 System.out.println( tempPrint ); 220 tempPrint = ""; 221 for( int i = 0; i < size; i++ ) { 222 tempPrint += "-"; 223 } 224 System.out.println( tempPrint ); 225 226 if ( properties != null && propertiesValues != null ) { 227 size = properties.size(); 228 for (int index = 0; index < size ; index++ ) { 229 String property = properties.get( index ); 230 Object propValue = propertiesValues.get( index ); 231 System.out.println( property + " : " + propValue ); 232 } 233 } 234 } catch ( com.sun.star.ucb.CommandAbortedException e ) { 235 System.out.println( "Error: " + e ); 236 } catch ( com.sun.star.uno.Exception e ) { 237 System.out.println( "Error: " + e ); 238 } catch ( java.lang.Exception e ) { 239 System.out.println( "Error: " + e ); 240 } 241 System.exit( 0 ); 242 } 243 } 244 245 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 246