On Thu, 07 Sep 2006 08:56:58 +0200
Jimmy <[EMAIL PROTECTED]> wrote:

> Of course I'd be interested in some code. I see it's supposed to be 
> implemented for 2.0.4 which is due this month, am I right there?
> 
> Cheers
> Jimmy

Hi Jimmy,

I think the last question goes to the OpenOffice team. 

Here is my base class of the filter, it uses a lot of wrapper classes to
handle the javax.xml.transform.sax.* and the com.sun.star.xml.sax.*
classes.

package dk.abj.openoffice.filter;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.xml.sax.InputSource;

import com.sun.star.beans.PropertyValue;
import com.sun.star.io.XOutputStream;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lib.uno.adapter.XInputStreamToInputStreamAdapter;
import com.sun.star.lib.uno.adapter.XOutputStreamToOutputStreamAdapter;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.xml.sax.XDocumentHandler;

import dk.abj.openoffice.xml.SAXAdapter;
import dk.abj.openoffice.xml.XContentHandlerToContentHandlerAdapter;

public class DXF2CALCTemplateGeneratorFilter extends
                XContentHandlerToContentHandlerAdapter implements
                com.sun.star.xml.XImportFilter,
com.sun.star.xml.XExportFilter { public final static String SERVICE =
"dk.abj.openoffice.filter.DXF2CALCTemplateGeneratorFilter";

        public static final String EXPORT_XSL_STYLESHEET =
"templates/templategenerator/export.xsl";

        public static final String IMPORT_XSL_STYLESHEET =
"templates/templategenerator/import.xsl";

        public static final String FILTER_NAME =
"DXF2CALCTemplateGeneratorFilter";

        public DXF2CALCTemplateGeneratorFilter() {
                super(null);
        }

        public boolean exporter(PropertyValue[] props, String[] arg1)
                        throws IllegalArgumentException {

                OutputStream out = null;
                try {

                        for (int i = 0; i < props.length; i++) {
                                if
(props[i].Name.equals("OutputStream")) { XOutputStream io =
(XOutputStream) UnoRuntime .queryInterface(
                                                                        
com.sun.star.io.XOutputStream.class,
                                                                        
props[i].Value);

                                        out = new
XOutputStreamToOutputStreamAdapter(io); }
                        }

                        SAXTransformerFactory factory =
(SAXTransformerFactory) SAXTransformerFactory .newInstance();

                        InputStream in =
this.getClass().getClassLoader() .getResourceAsStream(EXPORT_XSL_STYLESHEET);

                        SAXSource src = new SAXSource(new
InputSource(in));

                        // setup the transformer with the stylessheet
                        TransformerHandler f =
factory.newTransformerHandler(src);

                        // setup the output-stream
                        f.setResult(new StreamResult(out));

                        // put the transformer as sax-event receiver
into the chain this.handler = f;
                } catch (Exception e) {
                        e.printStackTrace();
                        if (out != null) {
                                try {
                                        out.close();
                                } catch (IOException ioe) {
                                        ioe.printStackTrace();
                                }
                        }
                        throw new
IllegalArgumentException(e.toString()); }

                // all done
                return true;
        }

        public boolean importer(PropertyValue[] props, XDocumentHandler
handler, String[] arg2) throws IllegalArgumentException {
                try {
                        InputStream documentInputStream = null;
                        String filename = null;

                        // get the filter settings and the input
                        // from the mediadescriptor
                        for (int i = 0; i < props.length; i++) {
                                if (props[i].Name.equals("FileName")) {
                                        filename =
AnyConverter.toString(props[i].Value); } else if
(props[i].Name.equals("InputStream")) { com.sun.star.io.XInputStream io
= (com.sun.star.io.XInputStream)
UnoRuntime .queryInterface(com.sun.star.io.XInputStream.class,
props[i].Value);

                                        documentInputStream = new
XInputStreamToInputStreamAdapter( io);
                                }
                        }

                        if (documentInputStream == null) {
                                documentInputStream = new
FileInputStream(filename); }

                        InputStream in =
this.getClass().getClassLoader() .getResourceAsStream(IMPORT_XSL_STYLESHEET);

                        SAXSource src = new SAXSource(new
InputSource(in));

                        // create a XSLTTransformer and set the template
                        SAXTransformerFactory factory =
(SAXTransformerFactory) SAXTransformerFactory .newInstance();

                        TransformerHandler f =
factory.newTransformerHandler(src);

                        // We wrap the openoffice contenthandler to
                        // a SAXContentHandler and
                        // put the the transformer in the chain
                        SAXAdapter saxadapter = new SAXAdapter(handler);

                        // invoke the transformation
                        f.getTransformer().transform(
                                        new SAXSource(new
InputSource(documentInputStream)), new SAXResult(saxadapter));
                } catch (Exception e) {
                        e.printStackTrace();
                        throw new
IllegalArgumentException(e.toString()); }

                return true;
        }


}

The rest of the needed code is to large for the a mailing-list or a
snipped, but the project is  free available and you can get the
sources here:
http://abj.dk/dxf2calc/download/index.html

there you can find all java sources and xcu files.



Best Regards,

Simon




















---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to