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