1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package org.openoffice.xmerge.converter.xml.xslt;
25 
26 import java.io.InputStream;
27 import java.io.IOException;
28 import java.util.Enumeration;
29 import java.io.ByteArrayOutputStream;
30 import java.io.ByteArrayInputStream;
31 
32 
33 
34 
35 import org.openoffice.xmerge.Document;
36 import org.openoffice.xmerge.ConvertData;
37 import org.openoffice.xmerge.ConvertException;
38 import org.openoffice.xmerge.DocumentDeserializer;
39 import org.openoffice.xmerge.converter.dom.DOMDocument;
40 //import org.openoffice.xmerge.converter.xml.sxw.SxwDocument;
41 import org.openoffice.xmerge.converter.xml.xslt.GenericOfficeDocument;
42 import org.openoffice.xmerge.util.Debug;
43 import org.openoffice.xmerge.util.registry.ConverterInfo;
44 
45 // Imported TraX classes
46 import javax.xml.transform.TransformerFactory;
47 import javax.xml.transform.Transformer;
48 import javax.xml.transform.dom.DOMSource;
49 //import javax.xml.transform.dom.DOMResult;
50 import javax.xml.transform.TransformerException;
51 import javax.xml.transform.TransformerConfigurationException;
52 import javax.xml.transform.stream.StreamResult;
53 import javax.xml.transform.stream.StreamSource;
54 import javax.xml.transform.URIResolver;
55 import javax.xml.transform.Source;
56 
57 
58 //
59 //import org.apache.xalan.serialize.Serializer;
60 //import org.apache.xalan.serialize.SerializerFactory;
61 //import org.apache.xalan.templates.OutputProperties;
62 
63 import javax.xml.parsers.DocumentBuilder;
64 import javax.xml.parsers.DocumentBuilderFactory;
65 
66 // Imported java classes
67 import java.io.FileNotFoundException;
68 
69 
70 /**
71  *  <p>Xslt implementation of
72  *  org.openoffice.xmerge.DocumentSerializer
73  *  for the {@link
74  *  org.openoffice.xmerge.converter.xml.xslt.PluginFactoryImpl
75  *  PluginFactoryImpl}.</p>
76  *
77  *  <p>The <code>serialize</code> method transforms the DOM
78  *  document from the given <code>Document</code> object by
79  *  means of a supplied Xsl Stylesheet.</p>
80  *
81  *  @author      Aidan Butler
82  */
83 public final class DocumentDeserializerImpl
84     implements  DocumentDeserializer,URIResolver {
85 
86     /**  A <code>ConvertData</code> object assigned to this object. */
87     private InputStream is = null;
88     private ConvertData cd = null;
89     private PluginFactoryImpl pluginFactory = null;
90 
91     /**
92      *  Constructor that assigns the given <code>ConvertData</code>
93      *  to this object.
94      *
95      *  @param  pf  A <code>PluginFactoryImpl</code> object.
96      *
97      *  @param  cd  A <code>ConvertData</code> object to read data for
98      *              the conversion process by the <code>deserialize</code>
99      *              method.
100      */
DocumentDeserializerImpl(PluginFactoryImpl pf,ConvertData cd)101     public DocumentDeserializerImpl(PluginFactoryImpl pf,ConvertData cd) {
102         this.cd = cd;
103 	pluginFactory = pf;
104     }
105 
106 
107 
108      /*
109      * This method performs the xslt transformation on the supplied <code>
110      * Document</code> and returns a <code>ByteArrayOutputStream</code> object.
111      *
112      *  Xslt transformation code
113      *
114      * @return baos A <code>ByteArrayOutputStream</code> object containing
115      *               the result of the Xslt transformation.
116      * @throws TransformerException,TransformerConfigurationException
117      *		  , FileNotFoundException,IOException
118      *
119      */
deserialize()120     public Document deserialize() throws ConvertException, IOException {
121 	log("\nFound the XSLT deserializer");
122 	Enumeration enumerate = cd.getDocumentEnumeration();
123 	org.w3c.dom.Document domDoc=null;
124 	DOMDocument docOut=null;
125 	GenericOfficeDocument doc = null;
126 	ByteArrayOutputStream baos =null;
127 	GenericOfficeDocument sxwDoc = new GenericOfficeDocument("output");
128 	while (enumerate.hasMoreElements()) {
129 		 docOut = (DOMDocument) enumerate.nextElement();
130 	}
131 	domDoc = docOut.getContentDOM();
132 	try{
133 	     baos = transform(domDoc);
134 	     sxwDoc.initContentDOM();
135 	     DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
136 	     dFactory.setNamespaceAware(true);
137 	     DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
138 	     sxwDoc.setContentDOM(dBuilder.parse(new ByteArrayInputStream(baos.toByteArray())));
139 
140 	}
141 	catch(Exception e){
142 	    System.out.println("The following error occurred:"+e);
143 	}
144 	return sxwDoc;
145     }
146 
resolve(String href,String base)147  public Source resolve(String href,String base)
148 	throws TransformerException{
149 	    //System.out.println("\nhref "+href+"\nbase "+base);
150 	    if (href !=null){
151 		if(href.equals("javax.xml.transform.dom.DOMSource")|| href.equals(""))
152 		    return null;
153 		try{
154 		    ConverterInfo ci = pluginFactory.getConverterInfo();
155 		    String newhRef ="jar:"+ci.getJarName()+"!/"+href;
156 		    //System.out.println("\n Looking For "+ newhRef);
157 		    StreamSource sheetFile= new StreamSource(newhRef);
158 		    return sheetFile;
159 		}
160 		catch (Exception e){
161 		    System.out.println("\nException in Xslt Resolver " +e);
162 		    return null;
163 		}
164 	    }
165 	    else
166 		return null;
167     }
168 
169      /*
170      * This method performs the xslt transformation on the supplied Dom Tree.
171      *
172      *  Xslt transformation code
173      *
174      * @throws TransformerException,TransformerConfigurationException
175      *		  , FileNotFoundException,IOException
176      *
177      */
transform(org.w3c.dom.Document xmlDoc)178     private ByteArrayOutputStream  transform(org.w3c.dom.Document xmlDoc)
179 	   throws TransformerException,TransformerConfigurationException
180 		  , FileNotFoundException,IOException{
181 
182 	   log("\nTransforming...");
183 	   ConverterInfo ci = pluginFactory.getConverterInfo();
184 	   ByteArrayOutputStream baos= new ByteArrayOutputStream();
185 	   try{
186 		  DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
187 		  dFactory.setNamespaceAware(true);
188 		  DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
189 
190 		  String teststr = ci.getXsltDeserial();
191 		  teststr= teststr.substring(0,6);
192 		  org.w3c.dom.Document xslDoc=null;
193 	          if ((teststr.equals("http:/"))||(teststr.equals("file:/"))
194 		                                ||(teststr.equals("jar://"))){
195 		      log(ci.getXsltDeserial());
196 		      xslDoc= dBuilder.parse(ci.getXsltDeserial());
197 
198 		  }
199 		  else{
200 		      log(ci.getJarName()+"!/"+ci.getXsltDeserial());
201 		      xslDoc = dBuilder.parse(
202 			      "jar:"+ci.getJarName()+"!/"+ci.getXsltDeserial());
203 		  }
204 
205 
206 		  DOMSource xslDomSource = new DOMSource(xslDoc);
207 		  DOMSource xmlDomSource = new DOMSource(xmlDoc);
208 
209 		   //call the tranformer using the XSL, Source and Result dom.
210 		  TransformerFactory tFactory = TransformerFactory.newInstance();
211 		  tFactory.setURIResolver(this);
212 		  Transformer transformer = tFactory.newTransformer(xslDomSource);
213 		  transformer.transform(xmlDomSource,new StreamResult(baos));
214 		  /*
215 		  // Serialize for output to standard out
216 		  Serializer serializer = SerializerFactory.getSerializer
217                                    (OutputProperties.getDefaultMethodProperties("xml"));
218 		  serializer.setOutputStream(System.out);
219 		  serializer.asDOMSerializer().serialize(xmlDomResult.getNode());
220 		  */
221 
222 		  log("\n** Transform Complete ***");
223 
224 	   }
225 	   catch (StackOverflowError sOE){
226 	          System.out.println("\nERROR : Stack Overflow Error During Transformation\n Try increasing the stack size by passing the -Xss1m option to the JRE.");
227 		  throw sOE;
228 	   }
229 	   catch(Exception e){
230 	          System.out.println("An error occured in the transformation : "+e);
231 	   }
232 	    return baos;
233      }
234 
235      /**
236      *  Sends message to the log object.
237      *
238      *  @param  str  Debug message.
239      */
log(String str)240     private void log(String str) {
241 
242         Debug.log(Debug.TRACE, str);
243     }
244 
245 }
246 
247