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 
19 package org.openoffice.xmerge.converter.xml.xslt;
20 
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.IOException;
24 import java.util.Iterator;
25 
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.transform.Source;
29 import javax.xml.transform.Transformer;
30 import javax.xml.transform.TransformerException;
31 import javax.xml.transform.TransformerFactory;
32 import javax.xml.transform.URIResolver;
33 import javax.xml.transform.dom.DOMSource;
34 import javax.xml.transform.stream.StreamResult;
35 import javax.xml.transform.stream.StreamSource;
36 
44 
53 public 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 {
114  ConverterInfo ci = pluginFactory.getConverterInfo();
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...");
135  ConverterInfo ci = pluginFactory.getConverterInfo();
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 }
Document deserialize()
This method performs the xslt transformation on the supplied.
Provides general purpose utilities.
static void log(int flag, String msg)
Log message based on the flag type.
Definition: Debug.java:205
Provides an interface for plug-in registration.
static final int TRACE
Trace messages.
Definition: Debug.java:46
exports com.sun.star. java
DocumentDeserializerImpl(PluginFactoryImpl pf, ConvertData cd)
Constructor that assigns the given.
Provides classes for converting basic document types to/from a.
Iterator< Object > getDocumentEnumeration()
Gets an.
Object next()
Move to next element in the sequence.
Class for storing the information about a converter plug-in.
String getJarName()
Returns the jar file name.
Provides interfaces for converting between two.
Definition: Convert.java:19
This class is used for logging debug messages.
Definition: Debug.java:39
ConverterInfo getConverterInfo()
Returns the.
ByteArrayOutputStream transform(org.w3c.dom.Document xmlDoc)
This method performs the xslt transformation on the supplied Dom Tree.
This is an interface used by the DiffAlgorithm and MergeAlgorithm to access a.
Definition: Iterator.java:27