Today, I wanted to get the list of the export and import filters managed by OpenOffice.org. It took me quite a long time to find out how to automatically distinguish an import filter form an export filter.

The first thing to look for is any API to get the list of the managed filters. This can be achieved using the com.sun.star.document.FilterFactory service. With this service, you have the internal names of all the filters and you can access there display name and configuration. Here is an example to get the data for the HTML filter:

Function getHtmlFilter()
  oFilterFactory = createUnoService("com.sun.star.document.FilterFactory")
    getHtmlFilter() = oFilter.getByName("HTML")
End Function

The IDL reference for the FilterFactory service is quite interesting when you can read the line:

Flags [integer] They describe the filter more specific.
(e.g. they mark it as IMPORT/EXPORT or DEFAULT filter.)

The main problem of this documentation is that the Flags property value contains an integer resulting from added constants. There is no documentation or API for these constants... Here is the start of the difficult part of the exercise. I had to look in the sources for some minutes to find the right file where these constants are defined: framework/sfx2/inc/sfx2/docfilt.hxx. There you can learn that the value for the IMPORT constant is 1 and the value for the EXPORT constant is 2.

Here is a sample of OOoBasic code to get the list of the import filters and the list of the export filters.

Sub Main()
    oFilterFactory = createUnoService("com.sun.star.document.FilterFactory")
    aNames = oFilterFactory.getElementNames()
    Dim aExports()
    Dim aImports()

    For i=0 To UBound(aNames)
        sName_i = aNames(i)
        xFilter = oFilterFactory.getByName(sName_i)

        nFlag = getFlag(xFilter)
        If nFlag mod 2 = 0 Then
            nNbExport = UBound(aExports) + 1
            ReDim Preserve aExports(nNbExport)
            aExports(nNbExport) = sName_i
        Else
            nNbImport = UBound(aImports) + 1
            ReDim Preserve aImports(nNbImport)
            aImports(nNbImport) = sName_i
        End If
    Next i

    sExports = ""
    For i=0 To UBound(aExports)
        sExports = sExports & aExports(i) & chr(10)
    Next i

    sImports = ""
    For i=0 To UBound(aImports)
        sImports = sImports & aImports(i) & chr(10)
    Next i

    sStats = "Nb Filters: " & UBound(aNames) & chr(10)
    sStats = sStats & "Nb Exports: " & UBound(aExports) & chr(10)
    sStats = sStats & "Nb Imports: " & UBound(aImports) & chr(10)

    MsgBox sStats

    msgbox sImports
    msgbox sExports

End Sub

Function getFlag(xFilter)
    getFlag() = -1

    For i=0 To UBound(xFilter)
        xProp_i = xFilter(i)
        If xProp_i.Name = "Flags" Then
            getFlag() = xProp_i.Value
        End If
    Next i
End Function

Using this discovery and the com.sun.star.document.TypeDetection service you can compute quite the same filters than the one of the standard OpenOffice.org file picker dialog.