Por si alguien lo necesitaba (propiedades documentos)

XerXi
05 de Diciembre del 2007
Hola,
aquí pasteo un código (muy simple, no está revisado, pero funciona) que te devuelve las propiedades de un documento de OpenOffice y MS-Office. Para que funcione necesita tener instalado OpenOffice y unas librerías. Estas librerías estan en el SDK OpenOffice que se puede descargar de su web, de aquí descargar también la guía para desarrolladores (no lo hubiese conseguido sin ella). Código:
/*
* Proba.java
*
* Created on 26 de febrero de 2004, 12:43
*/

/**
*
* @author sbarea
*/
import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.document.XDocumentInfoSupplier;
import com.sun.star.document.XDocumentInfo;
import com.sun.star.beans.XPropertySetInfo;

/** This class opens a new or an existing office document.
*/
public class Propietats {
public static void main(String args[]) {
try {
String sConnectionString = "uno:socket,host=localhost,port=8100;urp;StarOffice.ServiceManager";


/* Bootstraps a component context with the jurt base components
registered. Component context to be granted to a component for running.
Arbitrary values can be retrieved from the context. */
XComponentContext xcomponentcontext =
com.sun.star.comp.helper.Bootstrap.createInitialComponentContext( null );

/* Gets the service manager instance to be used (or null). This method has
been added for convenience, because the service manager is a often used
object. */
XMultiComponentFactory xmulticomponentfactory =
xcomponentcontext.getServiceManager();

/* Creates an instance of the component UnoUrlResolver which
supports the services specified by the factory. */
Object objectUrlResolver =
xmulticomponentfactory.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", xcomponentcontext );

// Create a new url resolver
XUnoUrlResolver xurlresolver = ( XUnoUrlResolver )
UnoRuntime.queryInterface( XUnoUrlResolver.class,
objectUrlResolver );

// Resolves an object that is specified as follow:
// uno:<connection description>;<protocol description>;<initial object name>
Object objectInitial = xurlresolver.resolve( sConnectionString );

// Create a service manager from the initial object
xmulticomponentfactory = ( XMultiComponentFactory )
UnoRuntime.queryInterface( XMultiComponentFactory.class, objectInitial );

// Query for the XPropertySet interface.
XPropertySet xpropertysetMultiComponentFactory = ( XPropertySet )
UnoRuntime.queryInterface( XPropertySet.class, xmulticomponentfactory );

// Get the default context from the office server.
Object objectDefaultContext =
xpropertysetMultiComponentFactory.getPropertyValue( "DefaultContext" );

// Query for the interface XComponentContext.
xcomponentcontext = ( XComponentContext ) UnoRuntime.queryInterface(
XComponentContext.class, objectDefaultContext );

/* A desktop environment contains tasks with one or more
frames in which components can be loaded. Desktop is the
environment for components which can instanciate within
frames. */
XComponentLoader xcomponentloader = ( XComponentLoader )
UnoRuntime.queryInterface( XComponentLoader.class,
xmulticomponentfactory.createInstanceWithContext(
"com.sun.star.frame.Desktop", xcomponentcontext ) );

// Load a Writer document, which will be automaticly displayed
PropertyValue[] propis=new PropertyValue[1];
propis[0]=new PropertyValue();
propis[0].Name="Hidden";
propis[0].Value=new Boolean(true);

XComponent xcomponent = xcomponentloader.loadComponentFromURL(
"file:///J:/UAB2003/Agente Virtual Inteligente - Sergio Barea Cancho/ProjectOne/proba.sxw", "_blank", 0, propis);

XDocumentInfoSupplier xDocInfoSup = (XDocumentInfoSupplier) UnoRuntime.queryInterface(XDocumentInfoSupplier.class, xcomponent);
XDocumentInfo xDocInfo = xDocInfoSup.getDocumentInfo();

//Això mostra els camps de la pestanya usuari de la finestra de propietats
for(short i=0;i<xDocInfo.getUserFieldCount();i++){
System.out.println(xDocInfo.getUserFieldName(i) + ": " + xDocInfo.getUserFieldValue(i));
}
XPropertySet xProp2 = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xDocInfo);
XPropertySetInfo xInfo2=xProp2.getPropertySetInfo();
//CreationDate,ModifyDate,PrintDate,TemplateDate.
com.sun.star.beans.Property[] xPropis = xInfo2.getProperties();
for (int i=0;i<xPropis.length;i++){
String nom = xPropis[i].Name;
if ((xInfo2.hasPropertyByName(nom))){
System.out.println(nom + ": " +xProp2.getPropertyValue(nom));
}else{
System.out.println("Propietat No Disponible");
}
}

System.exit(0);
}
catch( Exception exception ) {
System.err.println( exception );
}
}
}
Si teneis alguna duda o algun comentario o mejora, por favor, no dudéis en decirmelo.
Esta funcionalidad es una de las partes de mi proyecto final de carrera, el siguiente paso es buscar todos los documentos (OpenOffice & MSOffice) de una máquina, así que si me podéis echar un cable os lo agradecería. Pues ale, voy al tema.
SALU2


Rene
05 de Diciembre del 2007
Hola, estoy intentanto probar este codigo, pero me imagino que primero debo echar andar el URE. Ahora no tengo idea de como hacerlo, asi que si tienes alguna información de ayuda te lo agradeceria mucho.

De antemano muchas gracias y saludos,

XerXi
05 de Diciembre del 2007
Perdon, en la segunda línia de código pone:
"* Proba.java"
Como véis este nombre es incorrecto, el fichero se debe llamar como la clase (es que lo cambie pero el comentario no lo modifique). El ficher se debe llamar como la clase, en este caso, "Propietats.java".
SALU2