Posts Tagged ‘Openoffice.org’
The move from OpenOffice To LibreOffice
After Sun Inc had been acquired by Oracle ; and because of some clashes between Sun’s open source projects communities and the -somewhat unfriendly- Oracle attitude towards Sun’s open source projects; OpenOffice has been forked to LibreOffice. This Effort is being Led by document foundation probably with joint efforts to come from major Linux companies Like Novel ,Ubuntu and RedHat .
For Java OpenOffice API developers , the move to Libreoffice should be straightforward. just follow these simple steps:
1- Get netbeans IDE 6.8 and then install plugin “OpenOffice API Plugin” from netbeans IDE plugins dialog.
2- Downlaod and Install Libreoffice and Libreoffice SDK.
3-In Netbeans IDE select “Tools->Options->Miscellaneous”. select Tab “OOo API Plugin”. then in fields “Openoffice.org installation” and “Openoffice.org SDK installation” browse to the folder where you installed Libreoffice and libreoffice SDK respectively as described in image below:

That’s it ! you can now start doing Java Libreoffice API development using the netbeans OpenOffice API plugin.
Runtime Introspection of OpenOffice OXT package
The OpenOffice.org Developer’s Guide defines A OpenOffice.org OXT Extension as follows :
An extension is a file intended for the distribution of code and / or data which is to be used by OpenOffice.org. The file has the file extension (formerly .uno.pkg and .zip), and it acts as a container for various items, such as libraries, JARs, configuration data, type libraries, Basic libraries, Basic dialogs, etc. Before OpenOffice.org can use any content of the extension, it needs to be installed by the Extension Manager.
An extension should also contain a description.xml and must contain a directory META-INF (all uppercase). The META-INF directory contains a manifest.xml which lists all items and their media-type.
Recently i was working on a Openoffice.org Addon in Java and at some point of the project i needed a way to extract -at runtime- some information stored in the OXT binary (in fact I was looking for the OXT Properties).
If you’re using Netbeans openoffice.org plugin, you can get the OOo Extension Properties by right-clicking your project in the projects list and selecting OXT Extension properties, it’s under the Extension Management tab.
Because I needed to dynamically query my OXT extension for some useful information stored in the “description.xml” file , I decided to write a Java class to browse and parse the OXT Extension Files and extract OXT Properties at Runtime.
The most important fields that this class must knows about are the Extension Identifier and the com.sun.start.XComponentContext Object. So it makes good sense to pass these two Objects as arguments to class constructor. (note : the complete listing of XPackageInformationParser class is down-loadable from resources section below)
/**
* constructor
* @param m_xContext
* @param extensionIdentifier
*/
public XPackageInformationParser(XComponentContext m_xContext, String extensionIdentifier) {
this.m_xContext = m_xContext;
this.extensionIdentifier = extensionIdentifier;
// some other initialization code goes here
}OpenOffice UNO API provides a class com.sun.star.deployment.XPackageInformationProvider which is the entry class to access the OXT Package Information at runtime.
The following method configures and initialize the important UNO Objects that will be used to extract information from the UNO package:
private void intitialize(){
try {
XPackageInformationProvider xPackageInformationProvider = PackageInformationProvider.get(m_xContext);
oxtLocation = xPackageInformationProvider.getPackageLocation(this.extensionIdentifier);
Object oTransformer = m_xContext.getServiceManager().createInstanceWithContext("com.sun.star.util.URLTransformer", m_xContext);
xTransformer = (XURLTransformer) UnoRuntime.queryInterface(XURLTransformer.class, oTransformer);
} catch (Exception ex) {
_logger.error(ex);
}
}this method calculates the OXT Package location path and stores it in a String variable OXTLocation. It also create a XURLTransformer Object that will be used to parse openoffice URLs.
To read all files contained within the OXT Extension Package I use the following method:
/**
* returns files list that are contained within current .oxt package at runtime
* @param dir directory to search files list (null for top level OXT directory)
* @return array of OXT files
*/
public String[] getFileList(String dir) {
com.sun.star.util.URL[] oURL = new com.sun.star.util.URL[1];
oURL[0] = new com.sun.star.util.URL();
oURL[0].Complete = (dir == null) ? oxtLocation + "/" : oxtLocation + "/" + dir;
xTransformer.parseStrict(oURL);
return new File(oURL[0].Path + oURL[0].Name).list();
}By passing a null argument to this method it will return an array of all file names at top level of the OXT Package hierarchy structure.
It’s also possible to get a Java File Object reference to a given file contained within the OXT Extension Package. The below method do this :
/**
* get file within OXT package
* @param filePath
* @return reference to OXT File
*/
public File getExtensionFile(String filePath) {
com.sun.star.util.URL[] oURL = new com.sun.star.util.URL[1];
oURL[0] = new com.sun.star.util.URL();
oURL[0].Complete = this.oxtLocation + "/" + filePath;
xTransformer.parseStrict(oURL);
return new File(oURL[0].Path + oURL[0].Name);
}To get a reference to the Java File Object of description.xml file located at top level of the OXT package , we call the method like this :
getExtensionFile(description.xml);
Now lets investigate the heart functionality of this class; which is the method that parses the OXT description.xml file and extract the OXT Properties and stores them in a Java Properties Object. The method is listed below:
/**
* parse OXT description file
*/
public void parseOXTProperties() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// For HTML, we don't want to validate without a DTD
dbf.setValidating(false);
// Ignore text elements that are completely empty:
dbf.setIgnoringElementContentWhitespace(false);
dbf.setExpandEntityReferences(true);
dbf.setCoalescing(true);
// Ensure that getLocalName() returns the HTML element name
dbf.setNamespaceAware(true);
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
return;
}
Document document = null;
File description = this.getExtensionFile(EXTENSION_DESC_FILE);
_logger.debug("got OXT description file: " + description.getAbsolutePath());
try {
document = db.parse(description);
NodeList nodes = document.getDocumentElement().getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node_i = nodes.item(i);
if (node_i.getNodeType() == Node.ELEMENT_NODE) {
Element element = ((Element) node_i);
if (element.getTagName().equals(VERSION)) {
this.properties.setProperty(VERSION, element.getAttribute(VALUE));
}
if (element.getTagName().equals(IDENTIFIER)) {
this.properties.setProperty(IDENTIFIER, element.getAttribute(VALUE));
}
if (element.getTagName().equals(DISPLAY_NAME)) {
NodeList nodeList = element.getElementsByTagName("name");
_logger.debug(nodeList.item(0).getNodeName());
this.properties.setProperty(DISPLAY_NAME, nodeList.item(0).getTextContent());
}
if (element.getTagName().equals(PUBLISHER)) {
NodeList nameNode = element.getElementsByTagName("name");
_logger.debug(nameNode.item(0).getNodeName());
this.properties.setProperty(PUBLISHER, nameNode.item(0).getTextContent());
}
}
}
} catch (SAXException ex) {
_logger.error(ex);
} catch (IOException ex) {
_logger.error(ex);
}
}This method simply uses a java DOM parser to parse file description.xml ( file resides by-default at top level of OXT package) .It extracts useful xml document’s data (such as “publisher”, “version”, “display-name” etc..) then stores them in a Java Properties Object.
(the complete listing of XPackageInformationParser class is down-loadable from resources section at top bottom of this article.)
A Test code for printing OXT Package properties at runtime would look like :
XPackageInformationParser oxtParser=new XPackageInformationParser(m_xContext,org.openoffice.example); System.out.println(oxtParser.getProperties());
you just replace string org.openoffice.example with your actual OOo Extension identifier (which you defined while creating your Addon component).
A sample output obtained by running above test code for my OXT Extension gives me the following Properties :
{display-name=OpenOffice.org Addon for SugarCRM , version=0.0.1, identifier=com.sun.star.addon.sugarcrm.OOoSugarAddOn, publisher=Othman El Moulat}
Now Of course you can do more useful things with this class by examining each file retrieved from OXT Package at runtime. for example the /META-INF/manifest.xml file contains some useful information that could be used at runtime by your Java OOo Add-on.
Resources:


