diff options
Diffstat (limited to 'xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt')
8 files changed, 949 insertions, 0 deletions
diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/ConverterCapabilitiesImpl.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/ConverterCapabilitiesImpl.java new file mode 100644 index 000000000..64a94d350 --- /dev/null +++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/ConverterCapabilitiesImpl.java @@ -0,0 +1,74 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package org.openoffice.xmerge.converter.xml.xslt; + +import org.openoffice.xmerge.ConverterCapabilities; +import org.openoffice.xmerge.converter.xml.OfficeConstants; + +/** + * Xslt implementation of {@code ConverterCapabilities} for the {@link + * org.openoffice.xmerge.converter.xml.xslt.PluginFactoryImpl PluginFactoryImpl}. + * + * <p>Used with StarWriter XML to/from XSLT supported formats conversions. The + * {@code ConverterCapibilies} specify which "Office" {@code Document} + * tags and attributes are supported on the "Device" {@code Document} + * format.</p> + */ +public final class ConverterCapabilitiesImpl + implements ConverterCapabilities { + + public boolean canConvertTag(String tag) { + + if (OfficeConstants.TAG_OFFICE_DOCUMENT.equals(tag)) + return true; + else if (OfficeConstants.TAG_OFFICE_DOCUMENT_CONTENT.equals(tag)) + return true; + else if (OfficeConstants.TAG_OFFICE_BODY.equals(tag)) + return true; + else if (OfficeConstants.TAG_PARAGRAPH.equals(tag)) + return true; + else if (OfficeConstants.TAG_HEADING.equals(tag)) + return true; + else if (OfficeConstants.TAG_ORDERED_LIST.equals(tag)) + return true; + else if (OfficeConstants.TAG_UNORDERED_LIST.equals(tag)) + return true; + else if (OfficeConstants.TAG_LIST_ITEM.equals(tag)) + return true; + else if (OfficeConstants.TAG_LIST_HEADER.equals(tag)) + return true; + else if (OfficeConstants.TAG_SPAN.equals(tag)) + return true; + else if (OfficeConstants.TAG_HYPERLINK.equals(tag)) + return true; + else if (OfficeConstants.TAG_LINE_BREAK.equals(tag)) + return true; + else if (OfficeConstants.TAG_SPACE.equals(tag)) + return true; + else if (OfficeConstants.TAG_TAB_STOP.equals(tag)) + return true; + + return false; + } + + public boolean canConvertAttribute(String tag, String attribute) { + return OfficeConstants.TAG_SPACE.equals(tag) + && OfficeConstants.ATTRIBUTE_SPACE_COUNT.equals(attribute); + } +}
\ No newline at end of file diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/DocumentDeserializerImpl.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/DocumentDeserializerImpl.java new file mode 100644 index 000000000..c8eb43fdf --- /dev/null +++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/DocumentDeserializerImpl.java @@ -0,0 +1,189 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package org.openoffice.xmerge.converter.xml.xslt; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.util.Iterator; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.URIResolver; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; + +import org.openoffice.xmerge.ConvertData; +import org.openoffice.xmerge.ConvertException; +import org.openoffice.xmerge.Document; +import org.openoffice.xmerge.DocumentDeserializer; +import org.openoffice.xmerge.converter.dom.DOMDocument; +import org.openoffice.xmerge.util.Debug; +import org.openoffice.xmerge.util.registry.ConverterInfo; + +/** + * Xslt implementation of {@code org.openoffice.xmerge.DocumentSerializer} + * for the {@link org.openoffice.xmerge.converter.xml.xslt.PluginFactoryImpl + * PluginFactoryImpl}. + * + * <p>The {@code serialize} method transforms the DOM document from the given + * {@code Document} object by means of a supplied Xsl Stylesheet.</p> + */ +public final class DocumentDeserializerImpl + implements DocumentDeserializer,URIResolver { + + /** A {@code ConvertData} object assigned to this object. */ + private final ConvertData cd; + private final PluginFactoryImpl pluginFactory; + + /** + * Constructor that assigns the given {@code ConvertData} to this object. + * + * @param pf A {@code PluginFactoryImpl} object. + * @param cd A {@code ConvertData} object to read data for the conversion + * process by the {@code deserialize} method. + */ + public DocumentDeserializerImpl(PluginFactoryImpl pf,ConvertData cd) { + this.cd = cd; + pluginFactory = pf; + } + + /** + * This method performs the xslt transformation on the supplied + * {@code Document} and returns a {@code ByteArrayOutputStream} object. + * + * <p>Xslt transformation code.</p> + * + * @return A {@code ByteArrayOutputStream} object containing the result + * of the Xslt transformation. + */ + public Document deserialize() throws ConvertException, IOException { + log("\nFound the XSLT deserializer"); + Iterator<Object> enumerate = cd.getDocumentEnumeration(); + org.w3c.dom.Document domDoc = null; + DOMDocument docOut = null; + ByteArrayOutputStream baos = null; + GenericOfficeDocument sxwDoc = new GenericOfficeDocument("output"); + while (enumerate.hasNext()) { + docOut = (DOMDocument) enumerate.next(); + } + if (docOut != null) { + try { + domDoc = docOut.getContentDOM(); + baos = transform(domDoc); + sxwDoc.initContentDOM(); + DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); + dFactory.setNamespaceAware(true); + DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); + sxwDoc.setContentDOM(dBuilder.parse(new ByteArrayInputStream(baos.toByteArray()))); + + } catch (Exception e) { + System.out.println("The following error occurred:" + e); + } + } + return sxwDoc; + } + + public Source resolve(String href, String base) throws TransformerException { + if (href != null) { + if (href.equals("javax.xml.transform.dom.DOMSource") || href.length() == 0) { + return null; + } + try { + ConverterInfo ci = pluginFactory.getConverterInfo(); + String newhRef = "jar:" + ci.getJarName() + "!/" + href; + StreamSource sheetFile = new StreamSource(newhRef); + return sheetFile; + } catch (Exception e) { + System.out.println("\nException in Xslt Resolver " + e); + return null; + } + } else { + return null; + } + } + + /** + * This method performs the xslt transformation on the supplied Dom Tree. + * + * <p>Xslt transformation code.</p> + */ + private ByteArrayOutputStream transform(org.w3c.dom.Document xmlDoc){ + + log("\nTransforming..."); + ConverterInfo ci = pluginFactory.getConverterInfo(); + ByteArrayOutputStream baos= new ByteArrayOutputStream(); + try{ + DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); + dFactory.setNamespaceAware(true); + DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); + + String teststr = ci.getXsltDeserial(); + teststr= teststr.substring(0,6); + org.w3c.dom.Document xslDoc=null; + if ((teststr.equals("http:/"))||(teststr.equals("file:/")) + ||(teststr.equals("jar://"))){ + log(ci.getXsltDeserial()); + xslDoc= dBuilder.parse(ci.getXsltDeserial()); + + } + else{ + log(ci.getJarName()+"!/"+ci.getXsltDeserial()); + xslDoc = dBuilder.parse( + "jar:"+ci.getJarName()+"!/"+ci.getXsltDeserial()); + } + + + DOMSource xslDomSource = new DOMSource(xslDoc); + DOMSource xmlDomSource = new DOMSource(xmlDoc); + + //call the transformer using the XSL, Source and Result dom. + TransformerFactory tFactory = TransformerFactory.newInstance(); + tFactory.setURIResolver(this); + Transformer transformer = tFactory.newTransformer(xslDomSource); + transformer.transform(xmlDomSource,new StreamResult(baos)); + + log("\n** Transform Complete ***"); + + } + catch (StackOverflowError sOE){ + System.out.println("\nERROR : Stack Overflow Error During Transformation\n Try increasing the stack size by passing the -Xss1m option to the JRE."); + throw sOE; + } + catch(Exception e){ + System.out.println("An error occurred in the transformation : "+e); + } + return baos; + } + + /** + * Sends message to the log object. + * + * @param str Debug message. + */ + private void log(String str) { + + Debug.log(Debug.TRACE, str); + } +} diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/DocumentMergerImpl.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/DocumentMergerImpl.java new file mode 100644 index 000000000..b8d3ddfb9 --- /dev/null +++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/DocumentMergerImpl.java @@ -0,0 +1,80 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package org.openoffice.xmerge.converter.xml.xslt; + +import org.w3c.dom.Document; + +import org.openoffice.xmerge.DocumentMerger; +import org.openoffice.xmerge.MergeException; +import org.openoffice.xmerge.ConverterCapabilities; +import org.openoffice.xmerge.merger.DiffAlgorithm; +import org.openoffice.xmerge.merger.Difference; +import org.openoffice.xmerge.merger.NodeMergeAlgorithm; +import org.openoffice.xmerge.merger.Iterator; +import org.openoffice.xmerge.merger.diff.ParaNodeIterator; +import org.openoffice.xmerge.merger.diff.IteratorLCSAlgorithm; +import org.openoffice.xmerge.merger.merge.DocumentMerge; +import org.openoffice.xmerge.merger.merge.CharacterBaseParagraphMerge; +import org.openoffice.xmerge.util.Debug; + +/** + * Xslt implementation of {@code DocumentMerger} for the {@link + * org.openoffice.xmerge.converter.xml.xslt.PluginFactoryImpl PluginFactoryImpl}. + */ +public class DocumentMergerImpl implements DocumentMerger { + + private final ConverterCapabilities cc_; + private final org.openoffice.xmerge.Document orig; + + public DocumentMergerImpl(org.openoffice.xmerge.Document doc, ConverterCapabilities cc) { + cc_ = cc; + this.orig = doc; + } + + public void merge(org.openoffice.xmerge.Document modifiedDoc) throws MergeException { + + GenericOfficeDocument wdoc1 = (GenericOfficeDocument) orig; + GenericOfficeDocument wdoc2 = (GenericOfficeDocument) modifiedDoc; + + Document doc1 = wdoc1.getContentDOM(); + Document doc2 = wdoc2.getContentDOM(); + + Iterator i1 = new ParaNodeIterator(cc_, doc1.getDocumentElement()); + Iterator i2 = new ParaNodeIterator(cc_, doc2.getDocumentElement()); + + DiffAlgorithm diffAlgo = new IteratorLCSAlgorithm(); + + // find out the paragraph level diffs + Difference[] diffTable = diffAlgo.computeDiffs(i1, i2); + + if (Debug.isFlagSet(Debug.INFO)) { + Debug.log(Debug.INFO, "Diff Result: "); + + for (int i = 0; i < diffTable.length; i++) { + Debug.log(Debug.INFO, diffTable[i].debug()); + } + } + + // merge the paragraphs + NodeMergeAlgorithm charMerge = new CharacterBaseParagraphMerge(); + DocumentMerge docMerge = new DocumentMerge(cc_, charMerge); + + docMerge.applyDifference(i1, i2, diffTable); + } +} diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/DocumentSerializerImpl.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/DocumentSerializerImpl.java new file mode 100644 index 000000000..0d1e8d7ba --- /dev/null +++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/DocumentSerializerImpl.java @@ -0,0 +1,266 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package org.openoffice.xmerge.converter.xml.xslt; + +import org.w3c.dom.NodeList; +import org.w3c.dom.Node; +import org.w3c.dom.Element; +import org.w3c.dom.*; + +import java.io.IOException; +import java.io.ByteArrayOutputStream; +import java.io.ByteArrayInputStream; + +import org.openoffice.xmerge.Document; +import org.openoffice.xmerge.ConvertData; +import org.openoffice.xmerge.ConvertException; +import org.openoffice.xmerge.DocumentSerializer; +import org.openoffice.xmerge.converter.dom.DOMDocument; +import org.openoffice.xmerge.util.registry.ConverterInfo; +import org.openoffice.xmerge.converter.xml.OfficeConstants; + +// Imported TraX classes +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.Transformer; +import javax.xml.transform.stream.StreamSource; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.TransformerException; +import javax.xml.transform.URIResolver; +import javax.xml.transform.Source; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +/** + * Xslt implementation of {@code org.openoffice.xmerge.DocumentSerializer} + * for the {@link org.openoffice.xmerge.converter.xml.xslt.PluginFactoryImpl + * PluginFactoryImpl}. + * + * <p>The {@code serialize} method transforms the DOM document from the given + * {@code Document} object by means of a supplied Xsl Stylesheet.</p> + */ + +public final class DocumentSerializerImpl + implements DocumentSerializer,OfficeConstants,URIResolver { + + /** SXW {@code Document} object that this converter processes. */ + private final GenericOfficeDocument sxwDoc; + + private final PluginFactoryImpl pluginFactory; + + /** + * Constructor. + * + * @param pf A {@code PluginFactoryImpl}. + * @param doc A SXW {@code Document} to be converted. + */ + public DocumentSerializerImpl(PluginFactoryImpl pf,Document doc) { + pluginFactory=pf; + sxwDoc = (GenericOfficeDocument) doc; + } + + /** + * Method to convert a {@code Document} with an xsl stylesheet. + * + * <p>It creates a {@code Document} object, which is then transformed with the + * Xslt processor. A {@code ConvertData} object is constructed and returned.</p> + * + * @return A {@code ConvertData} object. + * + * @throws ConvertException If any I/O error occurs. + * @throws IOException If any I/O error occurs. + */ + public ConvertData serialize() throws ConvertException, IOException { + String docName = sxwDoc.getName(); + org.w3c.dom.Document domDoc = sxwDoc.getContentDOM(); + org.w3c.dom.Document metaDoc = sxwDoc.getMetaDOM(); + org.w3c.dom.Document styleDoc = sxwDoc.getStyleDOM(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + ConvertData cd = new ConvertData(); + Node offnode = domDoc.getDocumentElement(); + if (!(offnode.getNodeName()).equals("office:document")) { + try { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory + .newInstance(); + DocumentBuilder builder = builderFactory.newDocumentBuilder(); + DOMImplementation domImpl = builder.getDOMImplementation(); + DocumentType docType = domImpl.createDocumentType( + "office:document", + "-//OpenOffice.org//DTD OfficeDocument 1.0//EN", null); + org.w3c.dom.Document newDoc = domImpl.createDocument( + "http://openoffice.org/2000/office", "office:document", + docType); + + Element rootElement = newDoc.getDocumentElement(); + rootElement.setAttribute("xmlns:office", + "http://openoffice.org/2000/office"); + rootElement.setAttribute("xmlns:style", + "http://openoffice.org/2000/style"); + rootElement.setAttribute("xmlns:text", + "http://openoffice.org/2000/text"); + rootElement.setAttribute("xmlns:table", + "http://openoffice.org/2000/table"); + + rootElement.setAttribute("xmlns:draw", + "http://openoffice.org/2000/drawing"); + rootElement.setAttribute("xmlns:fo", + "http://www.w3.org/1999/XSL/Format"); + rootElement.setAttribute("xmlns:xlink", + "http://www.w3.org/1999/xlink"); + rootElement.setAttribute("xmlns:dc", + "http://purl.org/dc/elements/1.1/"); + rootElement.setAttribute("xmlns:meta", + "http://openoffice.org/2000/meta"); + rootElement.setAttribute("xmlns:number", + "http://openoffice.org/2000/datastyle"); + rootElement.setAttribute("xmlns:svg", + "http://www.w3.org/2000/svg"); + rootElement.setAttribute("xmlns:chart", + "http://openoffice.org/2000/chart"); + rootElement.setAttribute("xmlns:dr3d", + "http://openoffice.org/2000/dr3d"); + rootElement.setAttribute("xmlns:math", + "http://www.w3.org/1998/Math/MathML"); + rootElement.setAttribute("xmlns:form", + "http://openoffice.org/2000/form"); + rootElement.setAttribute("xmlns:script", + "http://openoffice.org/2000/script"); + rootElement.setAttribute("xmlns:config", + "http://openoffice.org/2001/config"); + rootElement.setAttribute("office:class", "text"); + rootElement.setAttribute("office:version", "1.0"); + + NodeList nodeList; + Node tmpNode; + Node rootNode = rootElement; + if (metaDoc != null) { + nodeList = metaDoc.getElementsByTagName(TAG_OFFICE_META); + if (nodeList.getLength() > 0) { + tmpNode = newDoc.importNode(nodeList.item(0), true); + rootNode.appendChild(tmpNode); + } + } + if (styleDoc != null) { + nodeList = styleDoc.getElementsByTagName(TAG_OFFICE_STYLES); + if (nodeList.getLength() > 0) { + tmpNode = newDoc.importNode(nodeList.item(0), true); + rootNode.appendChild(tmpNode); + } + } + nodeList = domDoc + .getElementsByTagName(TAG_OFFICE_AUTOMATIC_STYLES); + if (nodeList.getLength() > 0) { + tmpNode = newDoc.importNode(nodeList.item(0), true); + rootNode.appendChild(tmpNode); + } + nodeList = domDoc.getElementsByTagName(TAG_OFFICE_BODY); + if (nodeList.getLength() > 0) { + tmpNode = newDoc.importNode(nodeList.item(0), true); + rootNode.appendChild(tmpNode); + } + domDoc = newDoc; + } catch (Exception e) { + System.out + .println("\nAn Exception occurred with Xslt Serializer" + + e); + } + + } + + try { + baos = transform(domDoc); + } catch (Exception e) { + System.out.println("\n Error with Xslt\n"); + } + + DOMDocument resultDomDoc = (DOMDocument) pluginFactory + .createDeviceDocument(docName, + new ByteArrayInputStream(baos.toByteArray())); + cd.addDocument(resultDomDoc); + return cd; + } + + public Source resolve(String href, String base) + throws TransformerException { + if (href != null) { + if (href.equals("javax.xml.transform.dom.DOMSource") || href.length() == 0) { + return null; + } + try { + ConverterInfo ci = pluginFactory.getConverterInfo(); + String newhRef = "jar:" + ci.getJarName() + "!/" + href; + StreamSource sheetFile = new StreamSource(newhRef); + return sheetFile; + } catch (Exception e) { + System.out.println("\nException in Xslt Resolver " + e); + return null; + } + } else { + return null; + } + } + + /** + * This method performs the xsl transformation on the supplied + * {@code Document} and returns a {@code DOMResult} object. + * + * <p>Xslt transformation code.</p> + * + * @return A {@code ByteArrayOutputStream} object containing the result + * of the Xslt transformation. + */ + private ByteArrayOutputStream transform(org.w3c.dom.Document domDoc) { + ConverterInfo ci = pluginFactory.getConverterInfo(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try { + + DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); + dFactory.setNamespaceAware(true); + + DocumentBuilder dBuilder = dFactory.newDocumentBuilder(); + String teststr = ci.getXsltSerial(); + + teststr = teststr.substring(0, 6); + org.w3c.dom.Document xslDoc = null; + if ((teststr.equals("http:/")) || (teststr.equals("file:/")) + || (teststr.equals("jar://"))) { + System.out.println(ci.getXsltSerial()); + xslDoc = dBuilder.parse(ci.getXsltSerial()); + + } else { + xslDoc = dBuilder.parse( + "jar:" + ci.getJarName() + "!/" + ci.getXsltSerial()); + } + + DOMSource xslDomSource = new DOMSource(xslDoc); + DOMSource xmlDomSource = new DOMSource(domDoc); + + //call the transformer using the XSL, Source and Result. + TransformerFactory tFactory = TransformerFactory.newInstance(); + tFactory.setURIResolver(this); + Transformer transformer = tFactory.newTransformer(xslDomSource); + + transformer.transform(xmlDomSource, new StreamResult(baos)); + } catch (Exception e) { + System.out.println("An error occurred in the transformation : " + e); + } + return baos; + } +} diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/GenericOfficeDocument.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/GenericOfficeDocument.java new file mode 100644 index 000000000..155278016 --- /dev/null +++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/GenericOfficeDocument.java @@ -0,0 +1,81 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package org.openoffice.xmerge.converter.xml.xslt; + +import org.openoffice.xmerge.converter.xml.OfficeDocument; + +/** + * This class is an implementation of {@code OfficeDocument} for the generic + * office format. + */ +public class GenericOfficeDocument extends OfficeDocument { + + /** + * Constructor with arguments to set {@code name}. + * + * @param name The name of the {@code Document}. + */ + public GenericOfficeDocument(String name) { + super(name); + } + + /** + * Constructor with arguments to set {@code name}, the {@code namespaceAware} + * flag, and the {@code validating} flag. + * + * @param name The name of the {@code Document}. + * @param namespaceAware The value of the {@code namespaceAware} flag. + * @param validating The value of the {@code validating} flag. + */ + public GenericOfficeDocument(String name, boolean namespaceAware, boolean validating) { + + super(name, namespaceAware, validating); + } + + /** + * Returns the Office file extension for the generic format. + * + * @return The Office file extension for the generic format. + */ + @Override + protected String getFileExtension() { + return ""; + } + + /** + * Returns the Office attribute for the generic format. + * + * @return The Office attribute for the generic format. + */ + @Override + protected String getOfficeClassAttribute() { + return ""; + } + + /** + * Method to return the MIME type of the document. + * + * @return The document's MIME type. + */ + @Override + protected String getDocumentMimeType() { + /* TODO: Determine the MIME-type from the input. */ + return ""; + } +}
\ No newline at end of file diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/PluginFactoryImpl.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/PluginFactoryImpl.java new file mode 100644 index 000000000..67432ac11 --- /dev/null +++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/PluginFactoryImpl.java @@ -0,0 +1,174 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package org.openoffice.xmerge.converter.xml.xslt; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; +import java.util.Properties; + +import org.openoffice.xmerge.ConvertData; +import org.openoffice.xmerge.ConverterCapabilities; +import org.openoffice.xmerge.Document; +import org.openoffice.xmerge.DocumentDeserializer; +import org.openoffice.xmerge.DocumentDeserializerFactory; +import org.openoffice.xmerge.DocumentMerger; +import org.openoffice.xmerge.DocumentMergerFactory; +import org.openoffice.xmerge.DocumentSerializer; +import org.openoffice.xmerge.DocumentSerializerFactory; +import org.openoffice.xmerge.PluginFactory; +import org.openoffice.xmerge.converter.dom.DOMDocument; +import org.openoffice.xmerge.util.registry.ConverterInfo; + +/** + * Xslt implementation of the {@code PluginFactory}. + * + * <p>This encapsulates conversion of StarWriter XML format to and from a + * supported format.</p> + * + * <p>The superclass produces a particular {@link org.openoffice.xmerge.Document + * Document} object, i.e. {@link + * org.openoffice.xmerge.converter.xml.sxw.SxwDocument xwDocument} that the + * converters in this class work with. Thus, this class only implements the + * methods that produces the converters, i.e. {@link + * org.openoffice.xmerge.DocumentSerializer DocumentSerializer} and {@link + * org.openoffice.xmerge.DocumentDeserializer DocumentDeserializer}</p> + */ +public final class PluginFactoryImpl extends PluginFactory + implements DocumentDeserializerFactory, DocumentSerializerFactory, DocumentMergerFactory +{ + + public PluginFactoryImpl (ConverterInfo ci) { + super(ci); + } + + /** ConverterCapabilities object for this type of conversion. */ + private static final ConverterCapabilities converterCap = + new ConverterCapabilitiesImpl(); + + /** + * Returns an instance of {@code DocumentSerializerImpl}, which is an + * implementation of the {@code DocumentSerializer} interface. + * + * @param doc {@code Document} object to be converted/serialized. + * + * @return A {@code DocumentSerializerImpl} object. + */ + public DocumentSerializer createDocumentSerializer(Document doc) { + return new DocumentSerializerImpl(this,doc); + } + + /** + * Returns an instance of <code>DocumentDeserializerImpl</code>, + * which is an implementation of the <code>DocumentDeserializer</code> + * interface. + * + * @param cd {@code ConvertData} object. + * + * @return A {@code DocumentDeserializerImpl} object. + */ + public DocumentDeserializer createDocumentDeserializer(ConvertData cd) { + return new DocumentDeserializerImpl(this,cd); + } + + @Override + public org.openoffice.xmerge.Document createDeviceDocument(String str, + java.io.InputStream inputStream) throws java.io.IOException { + String ext = this.getDeviceFileExtension(); + DOMDocument domDoc = new DOMDocument(str,ext); + domDoc.read(inputStream); + return domDoc; + } + + @Override + public Document createOfficeDocument(String name, InputStream is) + throws IOException { + + // read zipped XML stream + GenericOfficeDocument doc = new GenericOfficeDocument(name); + doc.read(is); + return doc; + } + + @Override + public Document createOfficeDocument(String name, InputStream is, boolean isZip) + throws IOException { + + // read zipped XML stream + GenericOfficeDocument doc = new GenericOfficeDocument(name); + doc.read(is,isZip); + return doc; + } + + /** + * Returns a {@code String} containing the file extension of a + * {@code Document}. + * + * <p>This method uses a properties file to determine a mapping from the + * device mime in the {@code ConverterInfo} to a particular file extension. + * If a mapping is not specified, the default is ".txt".</p> + * + * @return The file extension of a {@code Document}. + */ + private String getDeviceFileExtension() { + Class<? extends PluginFactoryImpl> c = this.getClass(); + InputStream is = c.getResourceAsStream("XsltPlugin.properties"); + Properties props = new Properties(); + String ext = ".txt"; + String mimeType = null; + ConverterInfo ci = this.getConverterInfo(); + Iterator<String> enumerate = ci.getDeviceMime(); + while (enumerate.hasNext()) { + mimeType = enumerate.next(); + } + try { + props.load(is); + + String info = mimeType != null ? props.getProperty(mimeType) : null; + if (info != null) { + ext = info; + } + } catch (Exception e) { + + // It is okay for the property file to not exist. + } finally { + try { + if (is != null) { + is.close(); + } + } catch (IOException ex) { + } + } + return ext; + } + + /** + * Returns an instance of {@code DocumentMergerImpl}, which is an + * implementation of the {@code DocumentMerger} interface. + * + * @param doc {@code Document} to merge. + * + * @return A {@code DocumentMergerImpl} object. + */ + public DocumentMerger createDocumentMerger(Document doc) { + ConverterCapabilities cc = converterCap; + DocumentMergerImpl merger = new DocumentMergerImpl(doc, cc); + return merger; + } +} diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/XsltPlugin.properties b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/XsltPlugin.properties new file mode 100644 index 000000000..9e1027d63 --- /dev/null +++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/XsltPlugin.properties @@ -0,0 +1,27 @@ +# +# This file is part of the LibreOffice project. +# +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This file incorporates work covered by the following license notice: +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed +# with this work for additional information regarding copyright +# ownership. The ASF licenses this file to you under the Apache +# License, Version 2.0 (the "License"); you may not use this file +# except in compliance with the License. You may obtain a copy of +# the License at http://www.apache.org/licenses/LICENSE-2.0 . +# +# x-no-translate + +# +# XsltPlugin.properties +# + +#This file allows users to specify the mime-type to file extension mappings + +# e.g text/html=.html +text/html=.html diff --git a/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/package-info.java b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/package-info.java new file mode 100644 index 000000000..7351b158a --- /dev/null +++ b/xmerge/source/xmerge/java/org/openoffice/xmerge/converter/xml/xslt/package-info.java @@ -0,0 +1,58 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +/** + * Provides the tools for doing the conversion of StarWriter XML to and from + * supported formats, through the use of an XSLT transformation. + * + * <p>It follows the {@code org.openoffice.xmerge} framework for the conversion + * process.</p> + * + * <p>This converter does not currently support merge.</p> + * + * <h2>XSLT Transformation</h2> + * + * <p>The converter makes use of one or more XSLT style sheets, which are used + * in the DocumentSerializer and DocumentDeserializer, to perform the actual + * translations. The location of these stylesheets is extracted from the + * {@link org.openoffice.xmerge.util.registry.ConverterInfo ConverterInfo} data + * structure, and are specified using the optional converter-XSLT-serialize and + * converter-XSLT-deserialize tags in a plug-ins converter.xml file. Please + * refer to the SDK document for more information about how to implement a + * Plug-in Configuration XML File for a specific plug-in. + * A sample OpenOffice to HTML stylesheet and HTML to OpenOffice stylesheet, has + * been provided as a sample implementation. The converter also makes use of an + * XsltPlugin.properties file, which may be edited by the user to provide + * MIME-TYPE to file extension mappings. This file is used by the + * {@link org.openoffice.xmerge.converter.xml.xslt.PluginFactoryImpl + * getDeviceFileExtension} method.</p> + * + * <h2>TODO list</h2> + * + * <ol> + * <li>Expand XSLT style sheets to support more office/HTML capabilities</li> + * <li>Add support for certain character codes, such as {@literal &} which + * currently causes the transformer to break.</li> + * <li>Change the DocumentDeserializer transformer, so that the DOMResult is + * serialized using the xalan serializer and create an SxwDocument from the + * result</li> + * </ol> + * + * @see org.openoffice.xmerge.util.registry.ConverterInfo + */ +package org.openoffice.xmerge.converter.xml.xslt; |