Parsing OpenOffice Files with the NetBeans APIs

I wanted to be able to expand an ODP file (i.e., OpenOffice presentations) and see only the pictures within the file displayed as nodes. By default, something like that already happens in NetBeans IDE because OpenOffice files are treated like standard archives (i.e., ZIP and JAR). However, in addition to the pictures, you see all the other folders as well:

I wanted something like this, i.e., only show the pictures:

To do this, I assumed I would need to use the ODF Toolkit, as described here. That was also part of the reason why I started with the exercise in the first place. So, to parse an ODP file, I used this statement:

OdfPackage odfPackage = OdfPackage.loadPackage(file);

And the file was obtained from the FileObject, which was obtained from the DataObject that the New File Type wizard created for me, for ODP files.

However, in the end, I ended up not using the ODF Toolkit at all... because this NetBeans API code does the same thing as the line above:

FileObject archive = FileUtil.getArchiveRoot(file);

So, now I have a FileObject for the archive root, which is the ODP file. Hurray. Why? Because the NetBeans Platform understands the MIME type of every FileObject. So, first I create a node for each picture, via my ChildFactory extension class:

protected boolean createKeys(List toPopulate) {
    //Get the ODP file:
    FileObject archive = FileUtil.getArchiveRoot(obj.getPrimaryFile());
    //Get the children of the ODP file (i.e., the content):
    FileObject[] archiveKids = archive.getChildren();
    //Iterate through the content
    for (FileObject oneArchiveKid : archiveKids) {
        //Take the child called "Pictures":
        if (oneArchiveKid.getName().equals("Pictures")) {
            //Iterate through the content of the "Pictures" folder:
            FileObject[] pictureKids = oneArchiveKid.getChildren();
            //Create a new node for each picture!
            for (FileObject onePictureKid : pictureKids) {
    return true;

Therefore, I have now passed a FileObject to the constructor of my new node. That enables me to create a new action which opens the node, using its MIME type, which is an image MIME type, so that opening the file is as simple as this:

static class OpenOdpImageAction extends AbstractAction {

    private FileObject fo;

    OpenOdpImageAction(FileObject node) { = node;
        putValue(NAME, "Open");

    public void actionPerformed(ActionEvent e) {
        try {
            OpenCookie oc = (OpenCookie) DataObject.find(fo).getCookie(OpenCookie.class);
            if (oc != null) {
        } catch (DataObjectNotFoundException ex) {


FileObjects are really powerful. And now I am able to parse ODP files, thanks to the NetBeans APIs, especially thanks to FileUtil.getArchiveRoot.


Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« June 2016