LibreOffice Module xmerge (master) 1
DocumentDeserializerImpl.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 java.io.ByteArrayInputStream;
22import java.io.ByteArrayOutputStream;
23import java.io.IOException;
24import java.util.Iterator;
25
26import javax.xml.parsers.DocumentBuilder;
27import javax.xml.parsers.DocumentBuilderFactory;
28import javax.xml.transform.Source;
29import javax.xml.transform.Transformer;
30import javax.xml.transform.TransformerException;
31import javax.xml.transform.TransformerFactory;
32import javax.xml.transform.URIResolver;
33import javax.xml.transform.dom.DOMSource;
34import javax.xml.transform.stream.StreamResult;
35import javax.xml.transform.stream.StreamSource;
36
44
53public final class DocumentDeserializerImpl
54 implements DocumentDeserializer,URIResolver {
55
57 private final ConvertData cd;
59
68 this.cd = cd;
69 pluginFactory = pf;
70 }
71
81 public Document deserialize() throws ConvertException, IOException {
82 log("\nFound the XSLT deserializer");
84 org.w3c.dom.Document domDoc = null;
85 DOMDocument docOut = null;
86 ByteArrayOutputStream baos = null;
87 GenericOfficeDocument sxwDoc = new GenericOfficeDocument("output");
88 while (enumerate.hasNext()) {
89 docOut = (DOMDocument) enumerate.next();
90 }
91 if (docOut != null) {
92 try {
93 domDoc = docOut.getContentDOM();
94 baos = transform(domDoc);
95 sxwDoc.initContentDOM();
96 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
97 dFactory.setNamespaceAware(true);
98 DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
99 sxwDoc.setContentDOM(dBuilder.parse(new ByteArrayInputStream(baos.toByteArray())));
100
101 } catch (Exception e) {
102 System.out.println("The following error occurred:" + e);
103 }
104 }
105 return sxwDoc;
106 }
107
108 public Source resolve(String href, String base) throws TransformerException {
109 if (href != null) {
110 if (href.equals("javax.xml.transform.dom.DOMSource") || href.length() == 0) {
111 return null;
112 }
113 try {
115 String newhRef = "jar:" + ci.getJarName() + "!/" + href;
116 StreamSource sheetFile = new StreamSource(newhRef);
117 return sheetFile;
118 } catch (Exception e) {
119 System.out.println("\nException in Xslt Resolver " + e);
120 return null;
121 }
122 } else {
123 return null;
124 }
125 }
126
132 private ByteArrayOutputStream transform(org.w3c.dom.Document xmlDoc){
133
134 log("\nTransforming...");
136 ByteArrayOutputStream baos= new ByteArrayOutputStream();
137 try{
138 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
139 dFactory.setNamespaceAware(true);
140 DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
141
142 String teststr = ci.getXsltDeserial();
143 teststr= teststr.substring(0,6);
144 org.w3c.dom.Document xslDoc=null;
145 if ((teststr.equals("http:/"))||(teststr.equals("file:/"))
146 ||(teststr.equals("jar://"))){
147 log(ci.getXsltDeserial());
148 xslDoc= dBuilder.parse(ci.getXsltDeserial());
149
150 }
151 else{
152 log(ci.getJarName()+"!/"+ci.getXsltDeserial());
153 xslDoc = dBuilder.parse(
154 "jar:"+ci.getJarName()+"!/"+ci.getXsltDeserial());
155 }
156
157
158 DOMSource xslDomSource = new DOMSource(xslDoc);
159 DOMSource xmlDomSource = new DOMSource(xmlDoc);
160
161 //call the transformer using the XSL, Source and Result dom.
162 TransformerFactory tFactory = TransformerFactory.newInstance();
163 tFactory.setURIResolver(this);
164 Transformer transformer = tFactory.newTransformer(xslDomSource);
165 transformer.transform(xmlDomSource,new StreamResult(baos));
166
167 log("\n** Transform Complete ***");
168
169 }
170 catch (StackOverflowError sOE){
171 System.out.println("\nERROR : Stack Overflow Error During Transformation\n Try increasing the stack size by passing the -Xss1m option to the JRE.");
172 throw sOE;
173 }
174 catch(Exception e){
175 System.out.println("An error occurred in the transformation : "+e);
176 }
177 return baos;
178 }
179
185 private void log(String str) {
186
187 Debug.log(Debug.TRACE, str);
188 }
189}
ConvertData is used as a container for passing Document objects in and out of the Convert class.
Iterator< Object > getDocumentEnumeration()
Gets an Enumeration to access the Vector of Document objects.
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.
Document getContentDOM()
Return a DOM Document object of the document content file.
Xslt implementation of org.openoffice.xmerge.DocumentSerializer for the PluginFactoryImpl.
Document deserialize()
This method performs the xslt transformation on the supplied Document and returns a ByteArrayOutputSt...
final ConvertData cd
A ConvertData object assigned to this object.
ByteArrayOutputStream transform(org.w3c.dom.Document xmlDoc)
This method performs the xslt transformation on the supplied Dom Tree.
DocumentDeserializerImpl(PluginFactoryImpl pf, ConvertData cd)
Constructor that assigns the given ConvertData to this object.
This class is an implementation of OfficeDocument for the generic office format.
This class is used for logging debug messages.
Definition: Debug.java:39
static final int TRACE
Trace messages.
Definition: Debug.java:46
static void log(int flag, String msg)
Log message based on the flag type.
Definition: Debug.java:205
Class for storing the information about a converter plug-in.
String getJarName()
Returns the jar file name.
String getXsltDeserial()
Returns a String containing the xslt stylesheet URL that is to be used by the Xslt Plug-in Deserializ...
A DocumentDeserializer represents a converter that converts "Device" Document objects into the "Offic...
A Document represents any Document to be converted and the resulting Document from any conversion.
Definition: Document.java:36
This is an interface used by the DiffAlgorithm and MergeAlgorithm to access a Document.
Definition: Iterator.java:27
Object next()
Move to next element in the sequence.
@ Exception
Provides classes for converting basic document types to/from a DOMDocument object,...
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