Thanks mikhall, it works !

I wrote a Java Class converting ODT to FlatXML using DOM in the meantime.
See bellow:

package com.versusoft.packages.ooo;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 *
 * @author vince
 */
public class OdtUtils {

    public static void ODTtoFlatXML(String fileIn, String fileOut) throws
IOException, ParserConfigurationException, SAXException,
TransformerConfigurationException, TransformerException {
        ZipFile zf = null;

        zf = new ZipFile(fileIn);

        ZipEntry metaEntry = zf.getEntry("meta.xml");
        ZipEntry stylesEntry = zf.getEntry("styles.xml");
        ZipEntry contentEntry = zf.getEntry("content.xml");
        ZipEntry settingsEntry = zf.getEntry("settings.xml");

        DocumentBuilderFactory docFactory =
DocumentBuilderFactory.newInstance();
        docFactory.setValidating(false);

        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        docBuilder.setEntityResolver(new EntityResolver() {

            public InputSource resolveEntity(java.lang.String publicId,
java.lang.String systemId)
                    throws SAXException, java.io.IOException {

                return new InputSource(new ByteArrayInputStream("<?xml
version='1.0' encoding='UTF-8'?>".getBytes()));

            }
        });


        Document doc = docBuilder.newDocument();
        Element racine = doc.createElement("office:document");


        Document metaDoc = docBuilder.parse(zf.getInputStream(metaEntry));
        Document stylesDoc =
docBuilder.parse(zf.getInputStream(stylesEntry));
        Document contentDoc =
docBuilder.parse(zf.getInputStream(contentEntry));
        Document settingsDoc =
docBuilder.parse(zf.getInputStream(settingsEntry));

        String parentPath = new File(fileIn).getParent();
        System.out.println("path=" + parentPath);

        replaceObjectContent(docBuilder, contentDoc, zf, parentPath);

        racine.setAttribute("xmlns:meta",
"urn:oasis:names:tc:opendocument:xmlns:meta:1.0");
        racine.setAttribute("xmlns:xsl", "
http://www.w3.org/1999/XSL/Transform";);
        racine.setAttribute("xmlns:office",
"urn:oasis:names:tc:opendocument:xmlns:office:1.0");
        racine.setAttribute("xmlns:style",
"urn:oasis:names:tc:opendocument:xmlns:style:1.0");
        racine.setAttribute("xmlns:text",
"urn:oasis:names:tc:opendocument:xmlns:text:1.0");
        racine.setAttribute("xmlns:table",
"urn:oasis:names:tc:opendocument:xmlns:table:1.0");
        racine.setAttribute("xmlns:draw",
"urn:oasis:names:tc:opendocument:xmlns:drawing:1.0");
        racine.setAttribute("xmlns:fo",
"urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0");
        racine.setAttribute("xmlns:meta",
"urn:oasis:names:tc:opendocument:xmlns:meta:1.0");
        racine.setAttribute("xmlns:number",
"urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0");
        racine.setAttribute("xmlns:svg",
"urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0");
        racine.setAttribute("xmlns:chart",
"urn:oasis:names:tc:opendocument:xmlns:chart:1.0");
        racine.setAttribute("xmlns:dr3d",
"urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0");
        racine.setAttribute("xmlns:form",
"urn:oasis:names:tc:opendocument:xmlns:form:1.0");
        racine.setAttribute("xmlns:script",
"urn:oasis:names:tc:opendocument:xmlns:script:1.0");
        racine.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink";);
        racine.setAttribute("xmlns:dc", "http://purl.org/dc/elements/1.1/";);
        racine.setAttribute("xmlns:math", "
http://www.w3.org/1998/Math/MathML";);
        racine.setAttribute("xmlns:dom", "http://www.w3.org/2001/xml-events
");
        racine.setAttribute("xmlns:xforms", "http://www.w3.org/2002/xforms
");
        racine.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema
");
        racine.setAttribute("xmlns:xsi", "
http://www.w3.org/2001/XMLSchema-instance";);
        racine.setAttribute("xmlns:config",
"urn:oasis:names:tc:opendocument:xmlns:config:1.0");

        NodeList nodelist = metaDoc.getDocumentElement().getChildNodes();
        for (int i = 0; i < nodelist.getLength(); i++) {
            racine.appendChild(doc.importNode(nodelist.item(i), true));
        }

        nodelist = settingsDoc.getDocumentElement().getChildNodes();
        for (int i = 0; i < nodelist.getLength(); i++) {
            racine.appendChild(doc.importNode(nodelist.item(i), true));
        }


        nodelist = stylesDoc.getDocumentElement().getChildNodes();
        for (int i = 0; i < nodelist.getLength(); i++) {
            racine.appendChild(doc.importNode(nodelist.item(i), true));
        }

        nodelist = contentDoc.getDocumentElement().getChildNodes();
        for (int i = 0; i < nodelist.getLength(); i++) {
            racine.appendChild(doc.importNode(nodelist.item(i), true));
        }


        doc.appendChild(racine);

        Transformer transformer =
TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

        StreamResult result = new StreamResult(fileOut);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);

    }

    private static void replaceObjectContent(
            DocumentBuilder docBuilder, Document contentDoc, ZipFile zf,
String parentPath) throws IOException, SAXException {

        Element root = contentDoc.getDocumentElement();
        NodeList nodelist = root.getElementsByTagName("draw:object");

        for (int i = 0; i < nodelist.getLength(); i++) {

            Node objectNode = nodelist.item(i);
            Node hrefNode =
objectNode.getAttributes().getNamedItem("xlink:href");

            String objectPath = hrefNode.getTextContent();
            System.out.println("object path=" + objectPath);

            Document objectDoc =
docBuilder.parse(zf.getInputStream(zf.getEntry(objectPath.substring(2) +
System.getProperty("file.separator") + "content.xml")));
            Node objectContentNode = objectDoc.getDocumentElement();

            String tagName = objectContentNode.getNodeName();
            System.out.println(tagName);

            if (tagName.equals("math:math")) {
                System.out.println("replacing math");

                Node newObjectNode =
contentDoc.createElement("draw:object");

newObjectNode.appendChild(contentDoc.importNode(objectContentNode, true));
                //newObjectNode.appendChild(objectContentNode);
                //contentDoc.insertBefore(objectContentNode, objectNode);
                //contentDoc.removeChild(objectNode);
                //contentDoc.replaceChild(newObjectNode, objectNode);

//replaceNode(contentDoc,objectDoc,newObjectNode.getFirstChild());

objectNode.getParentNode().replaceChild(newObjectNode,objectNode);
            }
        }

    }

    public static void main(String args[]) throws IOException {
        try {

            OdtUtils.ODTtoFlatXML(args[0], args[1]);
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        } catch (TransformerConfigurationException ex) {
            ex.printStackTrace();
        } catch (TransformerException ex) {
            ex.printStackTrace();
        }

    }
}


On Tue, Apr 8, 2008 at 9:58 AM, Mikhail Voitenko <[EMAIL PROTECTED]>
wrote:

> Hi Vincent,
>
> I assume that using of "com.sun.star.comp.Writer.XMLOasisExporter" instead
> of "com.sun.star.comp.Writer.XMLExporter" would let the ODT XML format be
> used.
>
> Best regards,
> Mikhail.
>
>
> Vincent Spiewak wrote:
>
> > Hi all
> > I want to store an ODT "Flat XML" of the current document.
> > I tried this code from "SAXEcho" project:
> >
> >
> >
> >            DocumentCollector collector = new DocumentCollector(false);
> >
> >            // get the xmlexportfilter service
> >            XMultiComponentFactory m_xMCF =
> > m_xContext.getServiceManager();
> >
> >            Object xmlExporter = null;
> >
> >            xmlExporter =
> > m_xMCF.createInstanceWithContext("com.sun.star.comp.Writer.XMLExporter",
> > m_xContext);
> >
> >            // get the interface of xmlexportfilter service
> >            XFilter xFilter = (XFilter)
> > UnoRuntime.queryInterface(XFilter.class, xmlExporter);
> >            XExporter xExporter = (XExporter)
> > UnoRuntime.queryInterface(XExporter.class, xmlExporter);
> >            XInitialization xInitialization =
> >                    (XInitialization)
> > UnoRuntime.queryInterface(XInitialization.class, xmlExporter);
> >
> >            Object args[] = new Object[1];
> >            args[0] = (com.sun.star.xml.sax.XDocumentHandler)
> > UnoRuntime.queryInterface(
> >                    com.sun.star.xml.sax.XDocumentHandler.class,
> > collector);
> >
> >            xInitialization.initialize(args);
> >
> >            // get the Desktop service
> >            Object desktop = m_xMCF.createInstanceWithContext(
> >                    "com.sun.star.frame.Desktop", m_xContext);
> >
> >            XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(
> >                    XDesktop.class, desktop);
> >
> >            // set the xcomponent
> >            xExporter.setSourceDocument(xDesktop.getCurrentComponent());
> >
> >            // start the filter
> >            xFilter.filter(new PropertyValue[0]);
> >
> >            Document root = collector.getDocument();
> >
> >            AdapterNode adapter = new AdapterNode(root, null);
> >
> >            // saving...
> >            FileOutputStream fos = new FileOutputStream(tmpUrl);
> >            DocumentDistributor.setFormatting(false);
> >            DocumentDistributor.parseNode(adapter.getNode(), fos, null);
> >            fos.close();
> >
> >
> > It works great but give a SXW Flat XML, NOT ODT.
> > Is there a similar way to export in ODT Flat XML ?
> > Instead, is there any Java Toolkit like AODL ?
> >
> >
> > ----------------------------------------------------------
> > - Vincent Spiewak - www.versusoft.com -
> > ----------------------------------------------------------
> >
> >
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>


-- 
----------------------------------------------------------
- Vincent Spiewak - www.versusoft.com -
----------------------------------------------------------

Reply via email to