OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

using java convert several documents

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
crrrum
Newbie
Newbie


Joined: 10 Dec 2004
Posts: 1

PostPosted: Fri Dec 10, 2004 8:04 am    Post subject: using java convert several documents Reply with quote

Hi guys,

Hoping to get some help on my code. I have a process that requires word documents to be converted to text. Currently I have some java code that opens a bridge and holds onto an XComponentLoader. Whenever a word doc needs to be converted, I use the XComponentLoader to load the document and then save to another url as a text document.

I was hoping someone could take a look at this code and tell me if I'm doing anything obviously wrong and possibly tell me if there are better ways to do this. The code generally works, but every once in a while the code refuses to let go of some objects and I have to expicitly call System.exit(0) to quit my process. I also had to synchronize the convert method... not sure if that was necessary.

Another issue is that sometimes I will have to feed corrupt word documents to the loader. Does anyone know how I can handle this without have OpenOffice choke to death?

Here it is:
import java.io.*;

import com.sun.star.comp.helper.Bootstrap;

import com.sun.star.lang.XMultiComponentFactory;

import com.sun.star.lang.XMultiServiceFactory;
import com.sun.star.bridge.BridgeExistsException;
import com.sun.star.bridge.XBridge;
import com.sun.star.bridge.XBridgeFactory;
import com.sun.star.connection.XConnection;
import com.sun.star.connection.XConnector;

//import com.sun.star.bridge.XUnoUrlResolver;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XReference;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.frame.XStorable;
import com.sun.star.beans.PropertyValue;
import com.sun.star.beans.XPropertySet;
import com.sun.star.lang.XComponent;

/**
* This <Code>WordParser</Code> attempts to use OpenOffice
* to convert word documents to text and then pass the
* text back.
*/
public class OpenOfficeWordParser implements WordParser {

private static OpenOfficeWordParser INSTANCE = null;
private static Object lock = new Object();
private String host = null;
private String port = null;
private XComponentLoader xComponentLoader = null;
private XBridge xBridge = null;
private boolean disposed = false;

private PropertyValue hiddenPropertyValue = new PropertyValue();
private PropertyValue textFilterNamePropertyValue = new PropertyValue();
private PropertyValue textFilterOptionsPropertyValue = new PropertyValue();
private PropertyValue overwritePropertyValue = new PropertyValue();

private PropertyValue[] loadPropertyValues = new PropertyValue[ 1 ];
private PropertyValue[] loadTextPropertyValues = new PropertyValue[ 3 ];

/**
* Constructor
* @throws Exception throw all
*/
private OpenOfficeWordParser() throws Exception {

/*
*-------------------------------------------------------------
* Get the host and port to communicate with OpenOffice
*-------------------------------------------------------------
*/
host = System.getProperty( "oohost" );
port = System.getProperty( "ooport" );
if ( host == null || port == null ) {
throw new Exception( "Could not find system properties oohost or ooport" );
}

init();

hiddenPropertyValue.Name = "Hidden";
hiddenPropertyValue.Value = new Boolean(true);
textFilterNamePropertyValue.Name = "FilterName";
textFilterNamePropertyValue.Value = "Text (encoded)";
textFilterOptionsPropertyValue.Name = "FilterOptions";
textFilterOptionsPropertyValue.Value = "MS_1252,LF,Times New Roman,ENGLISH (US),";
overwritePropertyValue.Name = "Overwrite";
overwritePropertyValue.Value = new Boolean(true);

// Preparing properties for loading the document
// Setting the flag for hidding the open document
loadPropertyValues[ 0 ] = hiddenPropertyValue;

// Preparing properties for loading the document
// Setting the flag for hidding the open document
loadTextPropertyValues[ 0 ] = textFilterNamePropertyValue;
loadTextPropertyValues[ 1 ] = textFilterOptionsPropertyValue;
loadTextPropertyValues[ 2 ] = hiddenPropertyValue;
}

public void init() throws Exception {
/*
*-------------------------------------------------------------
* Initialize the bridge to OpenOffice
*-------------------------------------------------------------
*/
try {
// main factory to get objects?
XMultiServiceFactory xMultiServiceFactory = Bootstrap.createSimpleServiceManager();
// get a bridge factory from the main factory
// ask UnoRuntime to cast object as XBridgeFactory class
XBridgeFactory xBridgeFactory = (XBridgeFactory)UnoRuntime.queryInterface(
XBridgeFactory.class,
xMultiServiceFactory.createInstance( "com.sun.star.bridge.BridgeFactory" )
);
// finally get our bridge
xBridge = xBridgeFactory.getBridge(
"socket,host=" + host +
",port=" + port +
";urp"
);

// that didn't work, let's try to create a bridge with a connector
if ( xBridge == null ) {
// get a connector object and then ask UnoRuntime to cast object as XConnector class
XConnector xConnector = (XConnector)UnoRuntime.queryInterface(
XConnector.class,
xMultiServiceFactory.createInstance( "com.sun.star.connection.Connector" )
);
// connect to the server
XConnection xConnection = xConnector.connect( "socket,host=" + host + ",port=" + port );
xBridge = xBridgeFactory.createBridge(
"socket,host=" + host + ",port=" + port + ";urp",
"urp",
xConnection,
null
);
}

// get service manager object from bridge then cast as XMultiComponentFactory
XMultiComponentFactory xMultiComponentFactory = ( XMultiComponentFactory ) UnoRuntime.queryInterface(
XMultiComponentFactory.class,
xBridge.getInstance( "StarOffice.ServiceManager" )
);

// Query for the XPropertySet interface.
XPropertySet xPropertySetMultiComponentFactory = ( XPropertySet )UnoRuntime.queryInterface(
XPropertySet.class,
xMultiComponentFactory );

// Get the default context from the office server.
// Query for the interface XComponentContext.
XComponentContext xComponentContext = ( XComponentContext ) UnoRuntime.queryInterface(
XComponentContext.class,
xPropertySetMultiComponentFactory.getPropertyValue( "DefaultContext" )
);

// 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 ) UnoRuntime.queryInterface(
XComponentLoader.class,
xMultiComponentFactory.createInstanceWithContext( "com.sun.star.frame.Desktop", xComponentContext )
);

disposed = false;

}
catch ( Exception e ) {
throw new Exception( "Error initializing bridge to OpenOffice", e );
}

}

/**
* get an instance of the <Code>WordParser</Code>
* @throws Exception throw all
*/
public static OpenOfficeWordParser getInstance() throws Exception {
if ( INSTANCE == null ) {
synchronized ( lock ) {
if ( INSTANCE == null ) INSTANCE = new OpenOfficeWordParser();
}
}
return INSTANCE;
}

/**
* Close our bridge to OpenOffice and finish up any other objects
* @throws Exception throw all
*/
public void finish() throws Exception {
xComponentLoader = null;
XComponent xBridgeComponent = ( XComponent ) UnoRuntime.queryInterface(
XComponent.class,
xBridge
);
xBridgeComponent.dispose();
xBridge = null;
disposed = true;
}

/**
* attempt to get a text version of a word file passed in via
* an input stream
* @param workDir directory to do conversion in
* @param in input stream
* @return converted text as string buffer
* @throws Exception throw all
*/
public synchronized StringBuffer parseWord( File workDir, InputStream in ) throws Exception {

if ( disposed ) {
try {
init();
}
catch ( Exception e ) {
throw new Exception( "Sorry, the open office connection has already been closed and could not be re-opened.", e );
}
}

/*
*-------------------------------------------------------------
* Write out the word file to a temp file
*-------------------------------------------------------------
*/
File docFile = File.createTempFile( "doc", ".doc", workDir );
docFile.deleteOnExit();
byte[] b = new byte[2048];
int blength;
FileOutputStream fileOut = null;
try {
fileOut = new FileOutputStream( docFile );
while ( ( blength = in.read( b ) ) != -1 ) {
fileOut.write( b, 0, blength );
}
}
catch ( IOException e ) {
throw new Exception( "Failed to create temporary word doc file", e );
}
finally {
if ( fileOut != null ) try { fileOut.close(); } catch ( IOException e ) {}
}

if ( disposed ) throw new Exception( "Sorry, the open office connection has already been closed." );

/*
*-------------------------------------------------------------
* Ask OpenOffice to convert the word file to a text file.
*-------------------------------------------------------------
*/
String filePath = docFile.getCanonicalPath();

String txtPath = null;
try {
txtPath = convertDocument( filePath, "swriter: Text", "txt" );
}
finally {
docFile.delete();
}

/*
*-------------------------------------------------------------
* Read in the text data from the temp file.
*-------------------------------------------------------------
*/
File outputFile = new File( txtPath );
StringBuffer buf = new StringBuffer();
FileReader textin = null;
char[] c = new char[ 2048 ];
int clength;
try {
textin = new FileReader( outputFile );
while ( ( clength = textin.read( c ) ) != -1 ) {
buf.append( c, 0, clength );
}
}
finally {
if ( textin != null ) try { textin.close(); } catch ( Exception e ) {}
}
outputFile.delete();

return buf;
}

/**
* convert one document to another type
* @param stringDocumentName path to document to convert
* @param stringConvertType type to convert document to
* @param stringExtension what extension to end the converted file with
* @return path to converted file
* @throws Exception throw all
*/
private String convertDocument( String stringDocumentName,
String stringConvertType,
String stringExtension ) throws Exception {
String stringConvertedFile = "";

// Converting the document to the favoured type
XComponent xComponentDocument = null;
try {
// Composing the URL
String stringUrl = "file:///" + stringDocumentName.replace( '\\', '/' );

// Loading the wanted document
xComponentDocument = xComponentLoader.loadComponentFromURL(
stringUrl,
"_blank",
0,
loadPropertyValues );

// failed, try to load as text document
if ( xComponentDocument == null ) {
// Loading the wanted document
xComponentDocument = xComponentLoader.loadComponentFromURL(
stringUrl,
"_blank",
0,
loadTextPropertyValues );
}

// Getting an object that will offer a simple way to store a document to a URL.
XStorable xStorable = ( XStorable ) UnoRuntime.queryInterface(
XStorable.class,
xComponentDocument );

// Preparing properties for converting the document
PropertyValue[] propertyvalue = new PropertyValue[ 2 ];
// Setting the flag for overwriting
propertyvalue[ 0 ] = overwritePropertyValue;
// Setting the filter name
propertyvalue[ 1 ] = new PropertyValue();
propertyvalue[ 1 ].Name = "FilterName";
propertyvalue[ 1 ].Value = stringConvertType;

// Appending the favoured extension to the origin document name
stringConvertedFile = stringUrl + "." + stringExtension;

// Storing and converting the document
xStorable.storeToURL( stringConvertedFile, propertyvalue );
}
finally {
if ( xComponentDocument != null ) {
try {
xComponentDocument.dispose();
}
catch ( Exception e ) {}
xComponentDocument = null;
}
}

if ( stringConvertedFile.startsWith( "file:///" ) ) {
// Truncating the beginning of the file name
stringConvertedFile = stringConvertedFile.substring( 8 );
}

// Returning the name of the converted file
return( stringConvertedFile );
}

}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group