LibreOffice Module xmerge (master) 1
DocumentSerializerImpl.java
Go to the documentation of this file.
1/*
2 * This file is part of the LibreOffice project.
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 *
8 * This file incorporates work covered by the following license notice:
9 *
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17 */
18
19package org.openoffice.xmerge.converter.xml.xslt;
20
21import org.w3c.dom.NodeList;
22import org.w3c.dom.Node;
23import org.w3c.dom.Element;
24import org.w3c.dom.*;
25
26import java.io.IOException;
27import java.io.ByteArrayOutputStream;
28import java.io.ByteArrayInputStream;
29
37
38// Imported TraX classes
39import javax.xml.transform.TransformerFactory;
40import javax.xml.transform.Transformer;
41import javax.xml.transform.stream.StreamSource;
42import javax.xml.transform.stream.StreamResult;
43import javax.xml.transform.dom.DOMSource;
44import javax.xml.transform.TransformerException;
45import javax.xml.transform.URIResolver;
46import javax.xml.transform.Source;
47
48import javax.xml.parsers.DocumentBuilder;
49import javax.xml.parsers.DocumentBuilderFactory;
50
60public final class DocumentSerializerImpl
61 implements DocumentSerializer,OfficeConstants,URIResolver {
62
65
67
77 }
78
90 public ConvertData serialize() throws ConvertException, IOException {
91 String docName = sxwDoc.getName();
92 org.w3c.dom.Document domDoc = sxwDoc.getContentDOM();
93 org.w3c.dom.Document metaDoc = sxwDoc.getMetaDOM();
94 org.w3c.dom.Document styleDoc = sxwDoc.getStyleDOM();
95 ByteArrayOutputStream baos = new ByteArrayOutputStream();
96 ConvertData cd = new ConvertData();
97 Node offnode = domDoc.getDocumentElement();
98 if (!(offnode.getNodeName()).equals("office:document")) {
99 try {
100 DocumentBuilderFactory builderFactory = DocumentBuilderFactory
101 .newInstance();
102 DocumentBuilder builder = builderFactory.newDocumentBuilder();
103 DOMImplementation domImpl = builder.getDOMImplementation();
104 DocumentType docType = domImpl.createDocumentType(
105 "office:document",
106 "-//OpenOffice.org//DTD OfficeDocument 1.0//EN", null);
107 org.w3c.dom.Document newDoc = domImpl.createDocument(
108 "http://openoffice.org/2000/office", "office:document",
109 docType);
110
111 Element rootElement = newDoc.getDocumentElement();
112 rootElement.setAttribute("xmlns:office",
113 "http://openoffice.org/2000/office");
114 rootElement.setAttribute("xmlns:style",
115 "http://openoffice.org/2000/style");
116 rootElement.setAttribute("xmlns:text",
117 "http://openoffice.org/2000/text");
118 rootElement.setAttribute("xmlns:table",
119 "http://openoffice.org/2000/table");
120
121 rootElement.setAttribute("xmlns:draw",
122 "http://openoffice.org/2000/drawing");
123 rootElement.setAttribute("xmlns:fo",
124 "http://www.w3.org/1999/XSL/Format");
125 rootElement.setAttribute("xmlns:xlink",
126 "http://www.w3.org/1999/xlink");
127 rootElement.setAttribute("xmlns:dc",
128 "http://purl.org/dc/elements/1.1/");
129 rootElement.setAttribute("xmlns:meta",
130 "http://openoffice.org/2000/meta");
131 rootElement.setAttribute("xmlns:number",
132 "http://openoffice.org/2000/datastyle");
133 rootElement.setAttribute("xmlns:svg",
134 "http://www.w3.org/2000/svg");
135 rootElement.setAttribute("xmlns:chart",
136 "http://openoffice.org/2000/chart");
137 rootElement.setAttribute("xmlns:dr3d",
138 "http://openoffice.org/2000/dr3d");
139 rootElement.setAttribute("xmlns:math",
140 "http://www.w3.org/1998/Math/MathML");
141 rootElement.setAttribute("xmlns:form",
142 "http://openoffice.org/2000/form");
143 rootElement.setAttribute("xmlns:script",
144 "http://openoffice.org/2000/script");
145 rootElement.setAttribute("xmlns:config",
146 "http://openoffice.org/2001/config");
147 rootElement.setAttribute("office:class", "text");
148 rootElement.setAttribute("office:version", "1.0");
149
150 NodeList nodeList;
151 Node tmpNode;
152 Node rootNode = rootElement;
153 if (metaDoc != null) {
154 nodeList = metaDoc.getElementsByTagName(TAG_OFFICE_META);
155 if (nodeList.getLength() > 0) {
156 tmpNode = newDoc.importNode(nodeList.item(0), true);
157 rootNode.appendChild(tmpNode);
158 }
159 }
160 if (styleDoc != null) {
161 nodeList = styleDoc.getElementsByTagName(TAG_OFFICE_STYLES);
162 if (nodeList.getLength() > 0) {
163 tmpNode = newDoc.importNode(nodeList.item(0), true);
164 rootNode.appendChild(tmpNode);
165 }
166 }
167 nodeList = domDoc
168 .getElementsByTagName(TAG_OFFICE_AUTOMATIC_STYLES);
169 if (nodeList.getLength() > 0) {
170 tmpNode = newDoc.importNode(nodeList.item(0), true);
171 rootNode.appendChild(tmpNode);
172 }
173 nodeList = domDoc.getElementsByTagName(TAG_OFFICE_BODY);
174 if (nodeList.getLength() > 0) {
175 tmpNode = newDoc.importNode(nodeList.item(0), true);
176 rootNode.appendChild(tmpNode);
177 }
178 domDoc = newDoc;
179 } catch (Exception e) {
180 System.out
181 .println("\nAn Exception occurred with Xslt Serializer"
182 + e);
183 }
184
185 }
186
187 try {
188 baos = transform(domDoc);
189 } catch (Exception e) {
190 System.out.println("\n Error with Xslt\n");
191 }
192
193 DOMDocument resultDomDoc = (DOMDocument) pluginFactory
194 .createDeviceDocument(docName,
195 new ByteArrayInputStream(baos.toByteArray()));
196 cd.addDocument(resultDomDoc);
197 return cd;
198 }
199
200 public Source resolve(String href, String base)
201 throws TransformerException {
202 if (href != null) {
203 if (href.equals("javax.xml.transform.dom.DOMSource") || href.length() == 0) {
204 return null;
205 }
206 try {
208 String newhRef = "jar:" + ci.getJarName() + "!/" + href;
209 StreamSource sheetFile = new StreamSource(newhRef);
210 return sheetFile;
211 } catch (Exception e) {
212 System.out.println("\nException in Xslt Resolver " + e);
213 return null;
214 }
215 } else {
216 return null;
217 }
218 }
219
229 private ByteArrayOutputStream transform(org.w3c.dom.Document domDoc) {
231 ByteArrayOutputStream baos = new ByteArrayOutputStream();
232 try {
233
234 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
235 dFactory.setNamespaceAware(true);
236
237 DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
238 String teststr = ci.getXsltSerial();
239
240 teststr = teststr.substring(0, 6);
241 org.w3c.dom.Document xslDoc = null;
242 if ((teststr.equals("http:/")) || (teststr.equals("file:/"))
243 || (teststr.equals("jar://"))) {
244 System.out.println(ci.getXsltSerial());
245 xslDoc = dBuilder.parse(ci.getXsltSerial());
246
247 } else {
248 xslDoc = dBuilder.parse(
249 "jar:" + ci.getJarName() + "!/" + ci.getXsltSerial());
250 }
251
252 DOMSource xslDomSource = new DOMSource(xslDoc);
253 DOMSource xmlDomSource = new DOMSource(domDoc);
254
255 //call the transformer using the XSL, Source and Result.
256 TransformerFactory tFactory = TransformerFactory.newInstance();
257 tFactory.setURIResolver(this);
258 Transformer transformer = tFactory.newTransformer(xslDomSource);
259
260 transformer.transform(xmlDomSource, new StreamResult(baos));
261 } catch (Exception e) {
262 System.out.println("An error occurred in the transformation : " + e);
263 }
264 return baos;
265 }
266}
ConvertData is used as a container for passing Document objects in and out of the Convert class.
void addDocument(Document doc)
Adds a Document to the vector.
This Exception is thrown by convert algorithms.
ConverterInfo getConverterInfo()
Returns the ConvertInfo that corresponds to this plug-in.
An implementation of Document for StarOffice documents.
Xslt implementation of org.openoffice.xmerge.DocumentSerializer for the PluginFactoryImpl.
DocumentSerializerImpl(PluginFactoryImpl pf, Document doc)
Constructor.
ByteArrayOutputStream transform(org.w3c.dom.Document domDoc)
This method performs the xsl transformation on the supplied Document and returns a DOMResult object.
final GenericOfficeDocument sxwDoc
SXW Document object that this converter processes.
ConvertData serialize()
Method to convert a Document with an xsl stylesheet.
This class is an implementation of OfficeDocument for the generic office format.
Class for storing the information about a converter plug-in.
String getJarName()
Returns the jar file name.
String getXsltSerial()
Returns a String containing the Xslt stylesheet URL that is to be used by the Xslt Plug-in Serializer...
A DocumentSerializer represents a converter that converts a "Office" Document to a "Device" Document ...
A Document represents any Document to be converted and the resulting Document from any conversion.
Definition: Document.java:36
This interface contains constants for StarOffice XML tags, attributes (StarCalc cell types,...
String TAG_OFFICE_META
Element tag for office:meta.
String TAG_OFFICE_BODY
Element tag for office:body.
String TAG_OFFICE_AUTOMATIC_STYLES
Element tag for office:automatic-styles.
String TAG_OFFICE_STYLES
Element tag for office:styles.
@ Exception
Provides classes for converting basic document types to/from a DOMDocument object,...
Document and PluginFactory implementations for XML based formats.
Provides an interface for plug-in registration.
Provides general purpose utilities.
Provides interfaces for converting between two Document formats, and supports a "merge" interface for...
Definition: Convert.java:19
DocumentType