1*34dd1e25SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*34dd1e25SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*34dd1e25SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*34dd1e25SAndrew Rist * distributed with this work for additional information 6*34dd1e25SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*34dd1e25SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*34dd1e25SAndrew Rist * "License"); you may not use this file except in compliance 9*34dd1e25SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*34dd1e25SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*34dd1e25SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*34dd1e25SAndrew Rist * software distributed under the License is distributed on an 15*34dd1e25SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*34dd1e25SAndrew Rist * KIND, either express or implied. See the License for the 17*34dd1e25SAndrew Rist * specific language governing permissions and limitations 18*34dd1e25SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*34dd1e25SAndrew Rist *************************************************************/ 21*34dd1e25SAndrew Rist 22*34dd1e25SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir import java.util.Vector; 25cdf0e10cSrcweir import java.util.StringTokenizer; 26cdf0e10cSrcweir 27cdf0e10cSrcweir import com.sun.star.beans.Property; 28cdf0e10cSrcweir import com.sun.star.ucb.XContent; 29cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime; 30cdf0e10cSrcweir import com.sun.star.sdbc.XRow; 31cdf0e10cSrcweir 32cdf0e10cSrcweir 33cdf0e10cSrcweir /** 34cdf0e10cSrcweir * Obtaining Property Values from a UCB Content 35cdf0e10cSrcweir */ 36cdf0e10cSrcweir public class PropertiesRetriever { 37cdf0e10cSrcweir 38cdf0e10cSrcweir /** 39cdf0e10cSrcweir * Member properties 40cdf0e10cSrcweir */ 41cdf0e10cSrcweir private Helper m_helper; 42cdf0e10cSrcweir private XContent m_content; 43cdf0e10cSrcweir private String m_contenturl = ""; 44cdf0e10cSrcweir private Vector m_propNames = new Vector(); 45cdf0e10cSrcweir 46cdf0e10cSrcweir /** 47cdf0e10cSrcweir * Constructor. 48cdf0e10cSrcweir * 49cdf0e10cSrcweir *@param String[] This construtor requires the arguments: 50cdf0e10cSrcweir * -url=... (optional) 51cdf0e10cSrcweir * -propNames=... (optional) 52cdf0e10cSrcweir * See Help (method printCmdLineUsage()). 53cdf0e10cSrcweir * Without the arguments a new connection to a 54cdf0e10cSrcweir * running office cannot created. 55cdf0e10cSrcweir *@exception java.lang.Exception 56cdf0e10cSrcweir */ PropertiesRetriever( String args[] )57cdf0e10cSrcweir public PropertiesRetriever( String args[] ) throws java.lang.Exception { 58cdf0e10cSrcweir 59cdf0e10cSrcweir // Parse arguments 60cdf0e10cSrcweir parseArguments( args ); 61cdf0e10cSrcweir 62cdf0e10cSrcweir // Init 63cdf0e10cSrcweir m_helper = new Helper( getContentURL() ); 64cdf0e10cSrcweir 65cdf0e10cSrcweir // Create UCB content 66cdf0e10cSrcweir m_content = m_helper.createUCBContent(); 67cdf0e10cSrcweir } 68cdf0e10cSrcweir 69cdf0e10cSrcweir /** 70cdf0e10cSrcweir * Get values of the properties. 71cdf0e10cSrcweir * This method requires the main and the optional arguments to be set in order to work. 72cdf0e10cSrcweir * See Constructor. 73cdf0e10cSrcweir * 74cdf0e10cSrcweir *@param Vector Properties 75cdf0e10cSrcweir *@return Vector Returns Properties values if values successfully retrieved, null otherwise 76cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 77cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 78cdf0e10cSrcweir */ getPropertyValues()79cdf0e10cSrcweir public Vector getPropertyValues() 80cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 81cdf0e10cSrcweir Vector properties = getProperties(); 82cdf0e10cSrcweir return getPropertyValues ( properties ); 83cdf0e10cSrcweir } 84cdf0e10cSrcweir 85cdf0e10cSrcweir /** 86cdf0e10cSrcweir * Get values of the properties. 87cdf0e10cSrcweir * 88cdf0e10cSrcweir *@param Vector Properties 89cdf0e10cSrcweir *@return Vector Returns Properties values if values successfully retrieved, null otherwise 90cdf0e10cSrcweir *@exception com.sun.star.ucb.CommandAbortedException 91cdf0e10cSrcweir *@exception com.sun.star.uno.Exception 92cdf0e10cSrcweir */ getPropertyValues( Vector properties )93cdf0e10cSrcweir public Vector getPropertyValues( Vector properties ) 94cdf0e10cSrcweir throws com.sun.star.ucb.CommandAbortedException, com.sun.star.uno.Exception { 95cdf0e10cSrcweir Vector m_propValues = null; 96cdf0e10cSrcweir if ( m_content != null && properties != null && !properties.isEmpty() ) { 97cdf0e10cSrcweir 98cdf0e10cSrcweir int size = properties.size(); 99cdf0e10cSrcweir 100cdf0e10cSrcweir // Fill info for the properties wanted. 101cdf0e10cSrcweir Property[] props = new Property[ size ]; 102cdf0e10cSrcweir for ( int index = 0 ; index < size; index++ ) { 103cdf0e10cSrcweir 104cdf0e10cSrcweir // Define property sequence. 105cdf0e10cSrcweir Property prop = new Property(); 106cdf0e10cSrcweir prop.Name = ( String )properties.get( index ); 107cdf0e10cSrcweir prop.Handle = -1; // n/a 108cdf0e10cSrcweir props[ index ] = prop; 109cdf0e10cSrcweir } 110cdf0e10cSrcweir 111cdf0e10cSrcweir // Execute command "getPropertyValues". 112cdf0e10cSrcweir XRow values = 113cdf0e10cSrcweir ( XRow )UnoRuntime.queryInterface( 114cdf0e10cSrcweir XRow.class, m_helper.executeCommand( m_content,"getPropertyValues", props )); 115cdf0e10cSrcweir 116cdf0e10cSrcweir m_propValues = new Vector(); 117cdf0e10cSrcweir 118cdf0e10cSrcweir /* 119cdf0e10cSrcweir Extract values from row object. Note that the 120cdf0e10cSrcweir first column is 1, not 0. 121cdf0e10cSrcweir Title: Obtain value of column 1 as string.*/ 122cdf0e10cSrcweir for ( int index = 1 ; index <= size; index++ ) { 123cdf0e10cSrcweir Object propertyValue = values.getObject( index, null ); 124cdf0e10cSrcweir if ( !values.wasNull() && !(propertyValue instanceof com.sun.star.uno.Any )) 125cdf0e10cSrcweir m_propValues.add( propertyValue ); 126cdf0e10cSrcweir else 127cdf0e10cSrcweir m_propValues.add( "[ Property not found ]" ); 128cdf0e10cSrcweir } 129cdf0e10cSrcweir } 130cdf0e10cSrcweir return m_propValues; 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir /** 134cdf0e10cSrcweir * Get connect URL. 135cdf0e10cSrcweir * 136cdf0e10cSrcweir *@return String That contains the connect URL 137cdf0e10cSrcweir */ getContentURL()138cdf0e10cSrcweir public String getContentURL() { 139cdf0e10cSrcweir return m_contenturl; 140cdf0e10cSrcweir } 141cdf0e10cSrcweir 142cdf0e10cSrcweir /** 143cdf0e10cSrcweir * Get the properties. 144cdf0e10cSrcweir * 145cdf0e10cSrcweir *@return Vector That contains the properties 146cdf0e10cSrcweir */ getProperties()147cdf0e10cSrcweir public Vector getProperties() { 148cdf0e10cSrcweir return m_propNames; 149cdf0e10cSrcweir } 150cdf0e10cSrcweir 151cdf0e10cSrcweir /** 152cdf0e10cSrcweir * Parse arguments 153cdf0e10cSrcweir * 154cdf0e10cSrcweir *@param String[] Arguments 155cdf0e10cSrcweir *@exception java.lang.Exception 156cdf0e10cSrcweir */ parseArguments( String[] args )157cdf0e10cSrcweir public void parseArguments( String[] args ) throws java.lang.Exception { 158cdf0e10cSrcweir 159cdf0e10cSrcweir for ( int i = 0; i < args.length; i++ ) { 160cdf0e10cSrcweir if ( args[i].startsWith( "-url=" )) { 161cdf0e10cSrcweir m_contenturl = args[i].substring( 5 ); 162cdf0e10cSrcweir } else if ( args[i].startsWith( "-propNames=" )) { 163cdf0e10cSrcweir StringTokenizer tok 164cdf0e10cSrcweir = new StringTokenizer( args[i].substring( 11 ), ";" ); 165cdf0e10cSrcweir 166cdf0e10cSrcweir while ( tok.hasMoreTokens() ) 167cdf0e10cSrcweir m_propNames.add( tok.nextToken() ); 168cdf0e10cSrcweir 169cdf0e10cSrcweir } else if ( args[i].startsWith( "-help" ) || 170cdf0e10cSrcweir args[i].startsWith( "-?" )) { 171cdf0e10cSrcweir printCmdLineUsage(); 172cdf0e10cSrcweir System.exit( 0 ); 173cdf0e10cSrcweir } 174cdf0e10cSrcweir } 175cdf0e10cSrcweir 176cdf0e10cSrcweir if ( m_contenturl == null || m_contenturl.equals( "" )) { 177cdf0e10cSrcweir m_contenturl = Helper.prependCurrentDirAsAbsoluteFileURL( "data/data.txt" ); 178cdf0e10cSrcweir } 179cdf0e10cSrcweir 180cdf0e10cSrcweir if ( m_propNames.size() == 0 ) { 181cdf0e10cSrcweir m_propNames.add( "Title" ); 182cdf0e10cSrcweir m_propNames.add( "IsDocument" ); 183cdf0e10cSrcweir } 184cdf0e10cSrcweir } 185cdf0e10cSrcweir 186cdf0e10cSrcweir /** 187cdf0e10cSrcweir * Print the commands options 188cdf0e10cSrcweir */ printCmdLineUsage()189cdf0e10cSrcweir public void printCmdLineUsage() { 190cdf0e10cSrcweir System.out.println( 191cdf0e10cSrcweir "Usage : PropertiesRetriever -url=... -propNames=..." ); 192cdf0e10cSrcweir System.out.println( 193cdf0e10cSrcweir "Defaults: -url=<currentdir>/data/data.txt -propNames=Title;IsDocument" ); 194cdf0e10cSrcweir System.out.println( 195cdf0e10cSrcweir "\nExample : -propNames=Title;IsFolder" ); 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir /** 199cdf0e10cSrcweir * Create a new connection with the specific args to a running office and 200cdf0e10cSrcweir * get the properties values from a resource. 201cdf0e10cSrcweir * 202cdf0e10cSrcweir *@param String[] Arguments 203cdf0e10cSrcweir */ main( String args[] )204cdf0e10cSrcweir public static void main ( String args[] ) { 205cdf0e10cSrcweir System.out.println( "\n" ); 206cdf0e10cSrcweir System.out.println( 207cdf0e10cSrcweir "--------------------------------------------------------------" ); 208cdf0e10cSrcweir System.out.println( 209cdf0e10cSrcweir "PropertiesRetriever - obtains property values from a resource." ); 210cdf0e10cSrcweir System.out.println( 211cdf0e10cSrcweir "--------------------------------------------------------------" ); 212cdf0e10cSrcweir try { 213cdf0e10cSrcweir PropertiesRetriever obtProperty = new PropertiesRetriever( args ); 214cdf0e10cSrcweir Vector properties = obtProperty.getProperties(); 215cdf0e10cSrcweir Vector propertiesValues = obtProperty.getPropertyValues( properties ); 216cdf0e10cSrcweir 217cdf0e10cSrcweir String tempPrint = "\nProperties of resource " + obtProperty.getContentURL(); 218cdf0e10cSrcweir int size = tempPrint.length(); 219cdf0e10cSrcweir System.out.println( tempPrint ); 220cdf0e10cSrcweir tempPrint = ""; 221cdf0e10cSrcweir for( int i = 0; i < size; i++ ) { 222cdf0e10cSrcweir tempPrint += "-"; 223cdf0e10cSrcweir } 224cdf0e10cSrcweir System.out.println( tempPrint ); 225cdf0e10cSrcweir 226cdf0e10cSrcweir if ( properties != null && propertiesValues != null ) { 227cdf0e10cSrcweir size = properties.size(); 228cdf0e10cSrcweir for (int index = 0; index < size ; index++ ) { 229cdf0e10cSrcweir String property = ( String )properties.get( index ); 230cdf0e10cSrcweir Object propValue = propertiesValues.get( index ); 231cdf0e10cSrcweir System.out.println( property + " : " + propValue ); 232cdf0e10cSrcweir } 233cdf0e10cSrcweir } 234cdf0e10cSrcweir } catch ( com.sun.star.ucb.CommandAbortedException e ) { 235cdf0e10cSrcweir System.out.println( "Error: " + e ); 236cdf0e10cSrcweir } catch ( com.sun.star.uno.Exception e ) { 237cdf0e10cSrcweir System.out.println( "Error: " + e ); 238cdf0e10cSrcweir } catch ( java.lang.Exception e ) { 239cdf0e10cSrcweir System.out.println( "Error: " + e ); 240cdf0e10cSrcweir } 241cdf0e10cSrcweir System.exit( 0 ); 242cdf0e10cSrcweir } 243cdf0e10cSrcweir } 244