API/Samples/Java/Office/ReadFilesFromOxt

From Apache OpenOffice Wiki
< API‎ | Samples‎ | Java
Jump to: navigation, search

This example shows how to read files that are contained within your .oxt package at runtime.

Here is some code that you can add to your project:

    private String[] getFileList() {
        try {
            XPackageInformationProvider xPackageInformationProvider = PackageInformationProvider.get(m_xContext);
            String location = xPackageInformationProvider.getPackageLocation("org.openoffice.example");
            Object oTransformer = m_xContext.getServiceManager().createInstanceWithContext("com.sun.star.util.URLTransformer", m_xContext);
            XURLTransformer xTransformer = (XURLTransformer)UnoRuntime.queryInterface(XURLTransformer.class, oTransformer);
            com.sun.star.util.URL[] oURL = new com.sun.star.util.URL[1];
            oURL[0] = new com.sun.star.util.URL();
            oURL[0].Complete = location + "/some_sub_directory";
            xTransformer.parseStrict(oURL);
            return new File(oURL[0].Path + oURL[0].Name).list();
        } catch (com.sun.star.uno.Exception ex) {
            System.out.println(ex);
            System.out.println(ex.getStackTrace());
        }
 
        return null;
    }

This method returns an array of Strings containing the files in a subdirectory of your package. Be sure to change "some_sub_directory" to the actual name of the subdirectory, or leave it blank to get a list of files at the top level. Also, change "org.openoffice.example" to the Extension Identifier of your package. If you're using Netbeans, you can get the Extension Identifier by right-clicking your project in the projects list and selecting OXT Extension properties, it's under the Extension Management tab.

If you're using Netbeans to build the .oxt file, you'll want to add something like this to your build.xml file:

    <target name="-post-uno-package">
        <zip update="true" destfile="${uno.package.name}">
            <zipfileset dir="src/some_sub_directory" includes="*" casesensitive="yes" prefix="some_sub_directory"/>
        </zip>
    </target>

This causes Netbeans to add the sub directory to your package when the .oxt file is built.

See Also

Personal tools