1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir /** You can find more
29*cdf0e10cSrcweir  * information on the following web page:
30*cdf0e10cSrcweir  * http://api.openoffice.org/common/ref/com/sun/star/index.html
31*cdf0e10cSrcweir  */
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir /*Java Uno Helper Classes*/
35*cdf0e10cSrcweir import com.sun.star.lib.uno.adapter.XInputStreamToInputStreamAdapter;
36*cdf0e10cSrcweir import com.sun.star.lib.uno.adapter.XOutputStreamToOutputStreamAdapter;
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir /*StarOffice/Uno Classes*/
39*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
40*cdf0e10cSrcweir import com.sun.star.lang.XServiceInfo;
41*cdf0e10cSrcweir import com.sun.star.lang.XTypeProvider;
42*cdf0e10cSrcweir import com.sun.star.uno.Type;
43*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
44*cdf0e10cSrcweir import com.sun.star.comp.loader.FactoryHelper;
45*cdf0e10cSrcweir import com.sun.star.lang.XServiceName;
46*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
47*cdf0e10cSrcweir import com.sun.star.registry.XRegistryKey;
48*cdf0e10cSrcweir import com.sun.star.frame.XConfigManager;
49*cdf0e10cSrcweir import com.sun.star.xml.sax.InputSource;
50*cdf0e10cSrcweir import com.sun.star.xml.sax.XParser;
51*cdf0e10cSrcweir import com.sun.star.io.XInputStream;
52*cdf0e10cSrcweir import com.sun.star.io.XOutputStream;
53*cdf0e10cSrcweir import com.sun.star.xml.sax.XDocumentHandler;
54*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir /* Generated from Idls */
57*cdf0e10cSrcweir import com.sun.star.xml.XImportFilter;
58*cdf0e10cSrcweir import com.sun.star.xml.XExportFilter;
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir /* XMerge Classes */
61*cdf0e10cSrcweir import org.openoffice.xmerge.util.registry.ConverterInfoReader;
62*cdf0e10cSrcweir import org.openoffice.xmerge.util.registry.ConverterInfo;
63*cdf0e10cSrcweir import org.openoffice.xmerge.util.registry.ConverterInfoMgr;
64*cdf0e10cSrcweir import org.openoffice.xmerge.Convert;
65*cdf0e10cSrcweir import org.openoffice.xmerge.ConverterFactory;
66*cdf0e10cSrcweir import org.openoffice.xmerge.Document;
67*cdf0e10cSrcweir import org.openoffice.xmerge.ConvertData;
68*cdf0e10cSrcweir import org.openoffice.xmerge.converter.xml.OfficeDocument;
69*cdf0e10cSrcweir /*Java Classes*/
70*cdf0e10cSrcweir import java.util.Enumeration;
71*cdf0e10cSrcweir import java.io.*;
72*cdf0e10cSrcweir import javax.xml.parsers.*;
73*cdf0e10cSrcweir import java.net.URI;
74*cdf0e10cSrcweir 
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir /** This outer class provides an inner class to implement the service
77*cdf0e10cSrcweir  * description and a method to instantiate the
78*cdf0e10cSrcweir  * component on demand (__getServiceFactory()).
79*cdf0e10cSrcweir  */
80*cdf0e10cSrcweir public class XMergeBridge {
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir     private static XMultiServiceFactory xMSF;
84*cdf0e10cSrcweir     private static XDocumentHandler exportDocHandler=null;
85*cdf0e10cSrcweir     private static XInputStream xInStream =null;
86*cdf0e10cSrcweir     private static XOutputStream xOutStream=null;
87*cdf0e10cSrcweir     private static String  udJarPath=null;
88*cdf0e10cSrcweir     private static XOutputStream xos = null;
89*cdf0e10cSrcweir     private static XOutputStreamToOutputStreamAdapter adaptedStream=null;
90*cdf0e10cSrcweir     private static String offMime=null;
91*cdf0e10cSrcweir     private static String sdMime=null;
92*cdf0e10cSrcweir     private static String sFileName=null;
93*cdf0e10cSrcweir     private static String sURL="";
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     //private static FileOutputStream adaptedStream =null;
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir     /** This inner class provides the component as a concrete implementation
98*cdf0e10cSrcweir      * of the service description. It implements the needed interfaces.
99*cdf0e10cSrcweir      * @implements XTypeProvider
100*cdf0e10cSrcweir      */
101*cdf0e10cSrcweir     static public class _XMergeBridge implements
102*cdf0e10cSrcweir 										       XImportFilter,
103*cdf0e10cSrcweir 	 XExportFilter,
104*cdf0e10cSrcweir         XServiceName,
105*cdf0e10cSrcweir         XServiceInfo,
106*cdf0e10cSrcweir 	XDocumentHandler,
107*cdf0e10cSrcweir         XTypeProvider {
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir         /** The component will be registered under this name.
110*cdf0e10cSrcweir          */
111*cdf0e10cSrcweir         static private final String __serviceName = "com.sun.star.documentconversion.XMergeBridge";
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         public com.sun.star.uno.Type[] getTypes() {
116*cdf0e10cSrcweir             Type[] typeReturn = {};
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir             try {
119*cdf0e10cSrcweir                 typeReturn = new Type[] {
120*cdf0e10cSrcweir                 new Type( XTypeProvider.class ),
121*cdf0e10cSrcweir                 new Type( XImportFilter.class ),
122*cdf0e10cSrcweir 		new Type( XExportFilter.class ),
123*cdf0e10cSrcweir                 new Type( XServiceName.class ),
124*cdf0e10cSrcweir                 new Type( XServiceInfo.class ) };
125*cdf0e10cSrcweir             }
126*cdf0e10cSrcweir             catch( Exception exception ) {
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir             }
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir             return( typeReturn );
131*cdf0e10cSrcweir         }
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	String getFileName(String origName)
134*cdf0e10cSrcweir 	{
135*cdf0e10cSrcweir 	    String name=null;
136*cdf0e10cSrcweir 	    if (origName !=null)
137*cdf0e10cSrcweir 	    {
138*cdf0e10cSrcweir 		if(origName.equalsIgnoreCase(""))
139*cdf0e10cSrcweir 		    name = "OutFile";
140*cdf0e10cSrcweir 		else {
141*cdf0e10cSrcweir 		    if (origName.lastIndexOf("/")>=0){
142*cdf0e10cSrcweir 			    origName=origName.substring(origName.lastIndexOf("/")+1,origName.length());
143*cdf0e10cSrcweir 		    }
144*cdf0e10cSrcweir 		    if (origName.lastIndexOf(".")>=0){
145*cdf0e10cSrcweir 			name = origName.substring(0,(origName.lastIndexOf(".")));
146*cdf0e10cSrcweir 		    }
147*cdf0e10cSrcweir 		    else{
148*cdf0e10cSrcweir 			name=origName;
149*cdf0e10cSrcweir 		    }
150*cdf0e10cSrcweir 		}
151*cdf0e10cSrcweir 	    }
152*cdf0e10cSrcweir 	    else{
153*cdf0e10cSrcweir 		name = "OutFile";
154*cdf0e10cSrcweir 	    }
155*cdf0e10cSrcweir 	    return name;
156*cdf0e10cSrcweir 	}
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	public boolean importer(com.sun.star.beans.PropertyValue[] aSourceData,
161*cdf0e10cSrcweir 				com.sun.star.xml.sax.XDocumentHandler xDocHandler,
162*cdf0e10cSrcweir 				java.lang.String[] msUserData) throws com.sun.star.uno.RuntimeException {
163*cdf0e10cSrcweir 				/*
164*cdf0e10cSrcweir 	    System.out.println("\nFound the Importer!\n");
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[0]);
167*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[1]);
168*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[2]);
169*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[3]);
170*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[4]);
171*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[5]);
172*cdf0e10cSrcweir 	    */
173*cdf0e10cSrcweir 	    sFileName="";
174*cdf0e10cSrcweir 	    sURL="";
175*cdf0e10cSrcweir 	    String sDirectory = null;
176*cdf0e10cSrcweir 	    String udConvertClass=msUserData[0];
177*cdf0e10cSrcweir 	    udJarPath=msUserData[1];
178*cdf0e10cSrcweir 	    String udImport =msUserData[2];
179*cdf0e10cSrcweir 	    String udExport =msUserData[3];
180*cdf0e10cSrcweir 	    offMime =msUserData[4];
181*cdf0e10cSrcweir 	    sdMime = msUserData[5];
182*cdf0e10cSrcweir 	    com.sun.star.io.XInputStream xis=null;
183*cdf0e10cSrcweir 	    com.sun.star.beans.PropertyValue[] pValue = aSourceData;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 	    for  (int  i = 0 ; i < pValue.length; i++)
186*cdf0e10cSrcweir 	    {
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir 		//System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 		 try{
191*cdf0e10cSrcweir 		     //System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value);
192*cdf0e10cSrcweir 		     if (pValue[i].Name.compareTo("InputStream")==0){
193*cdf0e10cSrcweir 			xis=(com.sun.star.io.XInputStream)AnyConverter.toObject(new Type(com.sun.star.io.XInputStream.class), pValue[i].Value);
194*cdf0e10cSrcweir 		     }
195*cdf0e10cSrcweir 		     if (pValue[i].Name.compareTo("FileName")==0){
196*cdf0e10cSrcweir 			 sFileName=(String)AnyConverter.toObject(new Type(java.lang.String.class), pValue[i].Value);
197*cdf0e10cSrcweir 		     }
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 		 }
200*cdf0e10cSrcweir 		 catch(com.sun.star.lang.IllegalArgumentException AnyExec){
201*cdf0e10cSrcweir 		     System.out.println("\nIllegalArgumentException "+AnyExec);
202*cdf0e10cSrcweir 		 }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir 
206*cdf0e10cSrcweir 	    }
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 	    try{
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 		Object xCfgMgrObj=xMSF.createInstance("com.sun.star.config.SpecialConfigManager");
212*cdf0e10cSrcweir 		 XConfigManager xCfgMgr = (XConfigManager) UnoRuntime.queryInterface(
213*cdf0e10cSrcweir 										    XConfigManager.class , xCfgMgrObj );
214*cdf0e10cSrcweir 		String PathString=xCfgMgr.substituteVariables("$(progurl)" );
215*cdf0e10cSrcweir 		PathString= PathString.concat("/");
216*cdf0e10cSrcweir 		udJarPath= PathString.concat(udJarPath);
217*cdf0e10cSrcweir 
218*cdf0e10cSrcweir 		Object xPipeObj=xMSF.createInstance("com.sun.star.io.Pipe");
219*cdf0e10cSrcweir 		xInStream = (XInputStream) UnoRuntime.queryInterface(
220*cdf0e10cSrcweir                         XInputStream.class , xPipeObj );
221*cdf0e10cSrcweir 	        xOutStream = (XOutputStream) UnoRuntime.queryInterface(
222*cdf0e10cSrcweir                         XOutputStream.class , xPipeObj );
223*cdf0e10cSrcweir 		convert (xis,xOutStream,false,udJarPath,sFileName,offMime,sdMime);
224*cdf0e10cSrcweir 		Object xSaxParserObj=xMSF.createInstance("com.sun.star.xml.sax.Parser");
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 		XParser xParser = (XParser) UnoRuntime.queryInterface(
227*cdf0e10cSrcweir                         XParser.class , xSaxParserObj );
228*cdf0e10cSrcweir 		if (xParser==null){
229*cdf0e10cSrcweir 		    System.out.println("\nParser creation Failed");
230*cdf0e10cSrcweir 		}
231*cdf0e10cSrcweir 		xOutStream.closeOutput();
232*cdf0e10cSrcweir 		InputSource aInput = new InputSource();
233*cdf0e10cSrcweir 		if (sFileName==null){
234*cdf0e10cSrcweir 			sFileName="";
235*cdf0e10cSrcweir 		    }
236*cdf0e10cSrcweir 		aInput.sSystemId = sFileName;
237*cdf0e10cSrcweir 		aInput.aInputStream =xInStream;
238*cdf0e10cSrcweir                 xParser.setDocumentHandler ( xDocHandler );
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 		xParser.parseStream ( aInput );
241*cdf0e10cSrcweir 		xOutStream.closeOutput();
242*cdf0e10cSrcweir 		xInStream.closeInput();
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 	    }
245*cdf0e10cSrcweir 	    catch (IOException e){
246*cdf0e10cSrcweir 		//System.out.println("XMergeBridge IO Exception "+e.getMessage());
247*cdf0e10cSrcweir 		  return false;
248*cdf0e10cSrcweir 	    }
249*cdf0e10cSrcweir 	     catch (Exception e){
250*cdf0e10cSrcweir 		//System.out.println("XMergeBridge Exception "+e+" "+e.getMessage());
251*cdf0e10cSrcweir 		return false;
252*cdf0e10cSrcweir 	    }
253*cdf0e10cSrcweir 	    return true;
254*cdf0e10cSrcweir 	}
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir        public boolean exporter(com.sun.star.beans.PropertyValue[] aSourceData,
257*cdf0e10cSrcweir 			       java.lang.String[] msUserData) throws com.sun.star.uno.RuntimeException{
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 				   /*
260*cdf0e10cSrcweir 	    System.out.println("\nFound the Exporter!\n");
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[0]);
263*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[1]);
264*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[2]);
265*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[3]);
266*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[4]);
267*cdf0e10cSrcweir 	    System.out.println("\n"+msUserData[5]);
268*cdf0e10cSrcweir 	    */
269*cdf0e10cSrcweir 	    sFileName=null;
270*cdf0e10cSrcweir 	    sURL=null;
271*cdf0e10cSrcweir 	    String sDirectory = null;
272*cdf0e10cSrcweir 	    String title=null;
273*cdf0e10cSrcweir 	    String udConvertClass=msUserData[0];
274*cdf0e10cSrcweir 	    udJarPath=msUserData[1];
275*cdf0e10cSrcweir 	    String udImport =msUserData[2];
276*cdf0e10cSrcweir 	    String udExport =msUserData[3];
277*cdf0e10cSrcweir 	    offMime =msUserData[4];
278*cdf0e10cSrcweir 	    sdMime = msUserData[5];
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir 	    com.sun.star.beans.PropertyValue[] pValue = aSourceData;
281*cdf0e10cSrcweir 	    for  (int  i = 0 ; i < pValue.length; i++)
282*cdf0e10cSrcweir 	    {
283*cdf0e10cSrcweir 
284*cdf0e10cSrcweir 		//System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value);
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir 		try{
288*cdf0e10cSrcweir 		    //System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value);
289*cdf0e10cSrcweir 		    if (pValue[i].Name.compareTo("OutputStream")==0){
290*cdf0e10cSrcweir 			xos=(com.sun.star.io.XOutputStream)AnyConverter.toObject(new Type(com.sun.star.io.XOutputStream.class), pValue[i].Value);
291*cdf0e10cSrcweir 			//  System.out.println(pValue[i].Name+" "+xos);
292*cdf0e10cSrcweir 		    }
293*cdf0e10cSrcweir 		    if (pValue[i].Name.compareTo("FileName")==0){
294*cdf0e10cSrcweir 			sFileName=(String)AnyConverter.toObject(new Type(java.lang.String.class), pValue[i].Value);
295*cdf0e10cSrcweir 			//System.out.println(pValue[i].Name+" "+sFileName);
296*cdf0e10cSrcweir 		    }
297*cdf0e10cSrcweir 		    if (pValue[i].Name.compareTo("URL")==0){
298*cdf0e10cSrcweir 			sURL=(String)AnyConverter.toObject(new Type(java.lang.String.class), pValue[i].Value);
299*cdf0e10cSrcweir 			// System.out.println("\nMediaDescriptor url "+pValue[i].Name+" "+sURL);
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 		    }
302*cdf0e10cSrcweir 		    if (pValue[i].Name.compareTo("Title")==0){
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir 			title=(String)AnyConverter.toObject(new Type(java.lang.String.class), pValue[i].Value);
305*cdf0e10cSrcweir 			//System.out.println(pValue[i].Name+" "+title);
306*cdf0e10cSrcweir 		    }
307*cdf0e10cSrcweir 		}
308*cdf0e10cSrcweir 		catch(com.sun.star.lang.IllegalArgumentException AnyExec){
309*cdf0e10cSrcweir 		     System.out.println("\nIllegalArgumentException "+AnyExec);
310*cdf0e10cSrcweir 		}
311*cdf0e10cSrcweir 	    }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir 	    if (sURL==null){
315*cdf0e10cSrcweir 		sURL="";
316*cdf0e10cSrcweir 	    }
317*cdf0e10cSrcweir 
318*cdf0e10cSrcweir 	     try{
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir 		 Object xCfgMgrObj=xMSF.createInstance("com.sun.star.config.SpecialConfigManager");
321*cdf0e10cSrcweir 		 XConfigManager xCfgMgr = (XConfigManager) UnoRuntime.queryInterface(
322*cdf0e10cSrcweir 										    XConfigManager.class , xCfgMgrObj );
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir 		String PathString=xCfgMgr.substituteVariables("$(progurl)" );
325*cdf0e10cSrcweir 		PathString= PathString.concat("/");
326*cdf0e10cSrcweir 		udJarPath= PathString.concat(udJarPath);
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir 		Object xPipeObj=xMSF.createInstance("com.sun.star.io.Pipe");
329*cdf0e10cSrcweir 		xInStream = (XInputStream) UnoRuntime.queryInterface(
330*cdf0e10cSrcweir                         XInputStream.class , xPipeObj );
331*cdf0e10cSrcweir 		xOutStream = (XOutputStream) UnoRuntime.queryInterface(
332*cdf0e10cSrcweir                         XOutputStream.class , xPipeObj );
333*cdf0e10cSrcweir 	       }
334*cdf0e10cSrcweir 	      catch (Exception e){
335*cdf0e10cSrcweir 		System.out.println("Exception "+e);
336*cdf0e10cSrcweir 		  return false;
337*cdf0e10cSrcweir 	    }
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir 	    return true;
340*cdf0e10cSrcweir        }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir 	public String replace(String origString, String origChar, String replaceChar){
343*cdf0e10cSrcweir 	       String tmp="";
344*cdf0e10cSrcweir 	       int index=origString.indexOf(origChar);
345*cdf0e10cSrcweir 	       if(index !=-1){
346*cdf0e10cSrcweir 		   while (index !=-1){
347*cdf0e10cSrcweir 		       String first =origString.substring(0,index);
348*cdf0e10cSrcweir 		       first=first.concat(replaceChar);
349*cdf0e10cSrcweir 		       tmp=tmp.concat(first);
350*cdf0e10cSrcweir 		       origString=origString.substring(index+1,origString.length());
351*cdf0e10cSrcweir 		       index=origString.indexOf(origChar);
352*cdf0e10cSrcweir 		       if(index==-1) {
353*cdf0e10cSrcweir 			   tmp=tmp.concat(origString);
354*cdf0e10cSrcweir 		       }
355*cdf0e10cSrcweir 
356*cdf0e10cSrcweir 		   }
357*cdf0e10cSrcweir 
358*cdf0e10cSrcweir 	       }
359*cdf0e10cSrcweir 	       return tmp;
360*cdf0e10cSrcweir 	}
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir 	public String needsMask(String origString){
363*cdf0e10cSrcweir 	    if (origString.indexOf("&")!=-1){
364*cdf0e10cSrcweir 		origString=replace(origString,"&","&amp;");
365*cdf0e10cSrcweir 	    }
366*cdf0e10cSrcweir 	     if (origString.indexOf("\"")!=-1){
367*cdf0e10cSrcweir 		origString=replace(origString,"\"","&quot;");
368*cdf0e10cSrcweir 	    }
369*cdf0e10cSrcweir 	    if (origString.indexOf("<")!=-1){
370*cdf0e10cSrcweir 		origString=replace(origString,"<","&lt;");
371*cdf0e10cSrcweir 	    }
372*cdf0e10cSrcweir 	    if (origString.indexOf(">")!=-1){
373*cdf0e10cSrcweir 		origString=replace(origString,">","&gt;");
374*cdf0e10cSrcweir 	    }
375*cdf0e10cSrcweir 	    return origString;
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir 	}
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir 
380*cdf0e10cSrcweir 
381*cdf0e10cSrcweir        public void  startDocument ()	{
382*cdf0e10cSrcweir 	   //System.out.println("\nStart Document!");
383*cdf0e10cSrcweir        }
384*cdf0e10cSrcweir 
385*cdf0e10cSrcweir 	public void endDocument()throws com.sun.star.uno.RuntimeException
386*cdf0e10cSrcweir 	{
387*cdf0e10cSrcweir 
388*cdf0e10cSrcweir 	    try{
389*cdf0e10cSrcweir 	    xOutStream.closeOutput();
390*cdf0e10cSrcweir 		convert (xInStream,xos,true,udJarPath,sURL,offMime,sdMime);
391*cdf0e10cSrcweir 
392*cdf0e10cSrcweir 	    }
393*cdf0e10cSrcweir 	    catch (IOException e){
394*cdf0e10cSrcweir 		//System.out.println("Exception "+e);
395*cdf0e10cSrcweir 		throw new com.sun.star.uno.RuntimeException(e.getMessage());
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir 	    }
398*cdf0e10cSrcweir 	     catch (Exception e){
399*cdf0e10cSrcweir 		//System.out.println("Exception "+e);
400*cdf0e10cSrcweir 		throw new com.sun.star.uno.RuntimeException("Xmerge Exception");
401*cdf0e10cSrcweir 
402*cdf0e10cSrcweir 	    }
403*cdf0e10cSrcweir 	}
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir 	public void startElement (String str, com.sun.star.xml.sax.XAttributeList xattribs)
408*cdf0e10cSrcweir 	{
409*cdf0e10cSrcweir 
410*cdf0e10cSrcweir 	    str="<".concat(str);
411*cdf0e10cSrcweir 	    if (xattribs !=null)
412*cdf0e10cSrcweir 	    {
413*cdf0e10cSrcweir 		str= str.concat(" ");
414*cdf0e10cSrcweir 		int len=xattribs.getLength();
415*cdf0e10cSrcweir 		for (short i=0;i<len;i++)
416*cdf0e10cSrcweir 		    {
417*cdf0e10cSrcweir 			str=str.concat(xattribs.getNameByIndex(i));
418*cdf0e10cSrcweir 			str=str.concat("=\"");
419*cdf0e10cSrcweir 			str=str.concat(needsMask(xattribs.getValueByIndex(i)));
420*cdf0e10cSrcweir 			str=str.concat("\" ");
421*cdf0e10cSrcweir 		    }
422*cdf0e10cSrcweir 	    }
423*cdf0e10cSrcweir 	    str=str.concat(">");
424*cdf0e10cSrcweir 	    // System.out.println(str);
425*cdf0e10cSrcweir 	    try{
426*cdf0e10cSrcweir 		 xOutStream.writeBytes(str.getBytes("UTF-8"));
427*cdf0e10cSrcweir 	    }
428*cdf0e10cSrcweir 	    catch (Exception e){
429*cdf0e10cSrcweir 		System.out.println("\n"+e);
430*cdf0e10cSrcweir 	    }
431*cdf0e10cSrcweir 
432*cdf0e10cSrcweir 	}
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir 	public void endElement(String str){
435*cdf0e10cSrcweir 
436*cdf0e10cSrcweir 	    str="</".concat(str);
437*cdf0e10cSrcweir 	    str=str.concat(">");
438*cdf0e10cSrcweir 	    try{
439*cdf0e10cSrcweir 		 xOutStream.writeBytes(str.getBytes("UTF-8"));
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir 	    }
442*cdf0e10cSrcweir 	    catch (Exception e){
443*cdf0e10cSrcweir 		System.out.println("\n"+e);
444*cdf0e10cSrcweir 	    }
445*cdf0e10cSrcweir 
446*cdf0e10cSrcweir 
447*cdf0e10cSrcweir 	}
448*cdf0e10cSrcweir 	public void characters(String str){
449*cdf0e10cSrcweir 	    str=needsMask(str);
450*cdf0e10cSrcweir 	    try{
451*cdf0e10cSrcweir 		 xOutStream.writeBytes(str.getBytes("UTF-8"));
452*cdf0e10cSrcweir 	    }
453*cdf0e10cSrcweir 	   catch (Exception e){
454*cdf0e10cSrcweir 	       System.out.println("\n"+e);
455*cdf0e10cSrcweir 	   }
456*cdf0e10cSrcweir 
457*cdf0e10cSrcweir 
458*cdf0e10cSrcweir 	}
459*cdf0e10cSrcweir 
460*cdf0e10cSrcweir 	public void ignorableWhitespace(String str){
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir 
463*cdf0e10cSrcweir 	}
464*cdf0e10cSrcweir        public void processingInstruction(String aTarget, String aData){
465*cdf0e10cSrcweir 
466*cdf0e10cSrcweir        }
467*cdf0e10cSrcweir 
468*cdf0e10cSrcweir 	public void setDocumentLocator(com.sun.star.xml.sax.XLocator xLocator){
469*cdf0e10cSrcweir 
470*cdf0e10cSrcweir 	}
471*cdf0e10cSrcweir 
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir 
475*cdf0e10cSrcweir 
476*cdf0e10cSrcweir 
477*cdf0e10cSrcweir 	public void convert (com.sun.star.io.XInputStream xml,com.sun.star.io.XOutputStream device,
478*cdf0e10cSrcweir              boolean convertFromOffice,String pluginUrl,String FileName,String offMime,String sdMime) throws com.sun.star.uno.RuntimeException, IOException {
479*cdf0e10cSrcweir 
480*cdf0e10cSrcweir 	     String jarName = pluginUrl;
481*cdf0e10cSrcweir 	     String name= getFileName(FileName);
482*cdf0e10cSrcweir 
483*cdf0e10cSrcweir 	     ConverterInfo converterInfo = null;
484*cdf0e10cSrcweir 	     Enumeration ciEnum= null;
485*cdf0e10cSrcweir 
486*cdf0e10cSrcweir 	     XInputStreamToInputStreamAdapter xis =new XInputStreamToInputStreamAdapter(xml);
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir 
489*cdf0e10cSrcweir 	     XOutputStreamToOutputStreamAdapter newxos =new XOutputStreamToOutputStreamAdapter(device);
490*cdf0e10cSrcweir 	     try{
491*cdf0e10cSrcweir 		 ConverterInfoReader cir = new ConverterInfoReader(jarName,false);
492*cdf0e10cSrcweir 		 ciEnum =cir.getConverterInfoEnumeration();
493*cdf0e10cSrcweir 	     }
494*cdf0e10cSrcweir 	     catch (ParserConfigurationException pexc){
495*cdf0e10cSrcweir 		  System.out.println("Error:"+pexc);
496*cdf0e10cSrcweir 	     }
497*cdf0e10cSrcweir 	      catch ( org.xml.sax.SAXException pexc){
498*cdf0e10cSrcweir 		  System.out.println("Error:"+pexc);
499*cdf0e10cSrcweir 	     }
500*cdf0e10cSrcweir 	     catch(Exception e){
501*cdf0e10cSrcweir 		 System.out.println("Error:"+e);
502*cdf0e10cSrcweir 	     }
503*cdf0e10cSrcweir 	     ConverterInfoMgr. removeByJar(jarName);
504*cdf0e10cSrcweir 	     if (convertFromOffice)
505*cdf0e10cSrcweir 	     {
506*cdf0e10cSrcweir 
507*cdf0e10cSrcweir 		 try {
508*cdf0e10cSrcweir 
509*cdf0e10cSrcweir 		     //Check to see if jar contains a plugin Impl
510*cdf0e10cSrcweir 
511*cdf0e10cSrcweir 		         ConverterInfoMgr.addPlugIn(ciEnum);
512*cdf0e10cSrcweir 			 ConverterFactory cf = new ConverterFactory();
513*cdf0e10cSrcweir 
514*cdf0e10cSrcweir 			 Convert cv = cf.getConverter(ConverterInfoMgr.findConverterInfo(sdMime,offMime),false);
515*cdf0e10cSrcweir 			 if (cv == null) {
516*cdf0e10cSrcweir 			     System.out.println("\nNo plug-in exists to convert from <staroffice/sxw> to <specified format> ");
517*cdf0e10cSrcweir 
518*cdf0e10cSrcweir 			 }
519*cdf0e10cSrcweir 			 else
520*cdf0e10cSrcweir 			 {
521*cdf0e10cSrcweir 			     cv.addInputStream(name,(InputStream)xis,false);
522*cdf0e10cSrcweir 			     ConvertData dataOut = cv.convert();
523*cdf0e10cSrcweir 
524*cdf0e10cSrcweir 			     Enumeration docEnum = dataOut.getDocumentEnumeration();
525*cdf0e10cSrcweir 
526*cdf0e10cSrcweir 			     if (docEnum.hasMoreElements()){
527*cdf0e10cSrcweir 				     Document docOut      = (Document)docEnum.nextElement();
528*cdf0e10cSrcweir 				     String fileName      = docOut.getFileName();
529*cdf0e10cSrcweir 				     docOut.write(newxos);
530*cdf0e10cSrcweir 
531*cdf0e10cSrcweir 				     newxos.flush();
532*cdf0e10cSrcweir 				     newxos.close();
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir 
535*cdf0e10cSrcweir 				     int i=1;
536*cdf0e10cSrcweir 				     while (docEnum.hasMoreElements() && sURL.startsWith("file:")) {
537*cdf0e10cSrcweir 					 //URI uri=new URI(sFileName);
538*cdf0e10cSrcweir 					 URI uri=new URI(sURL);
539*cdf0e10cSrcweir 					 String  newFileName= getPath(uri);
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir 
542*cdf0e10cSrcweir 					 //System.out.println("\nURI: "+uri.getPath());
543*cdf0e10cSrcweir 					 File newFile=null;
544*cdf0e10cSrcweir 					 if (newFileName.lastIndexOf(".")!=-1){
545*cdf0e10cSrcweir 					     newFile =new File(newFileName.substring(0,newFileName.lastIndexOf("."))+String.valueOf(i)+newFileName.substring(newFileName.lastIndexOf(".")));
546*cdf0e10cSrcweir 					 }
547*cdf0e10cSrcweir 					 else{
548*cdf0e10cSrcweir 					    newFile =new File(newFileName.concat(String.valueOf(i)));
549*cdf0e10cSrcweir 					 }
550*cdf0e10cSrcweir 
551*cdf0e10cSrcweir 					 FileOutputStream fos = new FileOutputStream(newFile);
552*cdf0e10cSrcweir 					 docOut      = (Document)docEnum.nextElement();
553*cdf0e10cSrcweir 					 fileName      = docOut.getFileName();
554*cdf0e10cSrcweir 					 docOut.write(fos);
555*cdf0e10cSrcweir 					 fos.flush();
556*cdf0e10cSrcweir 					 fos.close();
557*cdf0e10cSrcweir 					 i++;
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir 				     }
560*cdf0e10cSrcweir 
561*cdf0e10cSrcweir 			     }
562*cdf0e10cSrcweir 			 }
563*cdf0e10cSrcweir 			 ConverterInfoMgr.removeByJar(jarName);
564*cdf0e10cSrcweir 		 }
565*cdf0e10cSrcweir 		 catch (StackOverflowError sOE){
566*cdf0e10cSrcweir 		     System.out.println("\nERROR : Stack OverFlow. \n Increase of the JRE by adding the following line to the end of the javarc file \n \"-Xss1m\"\n");
567*cdf0e10cSrcweir 
568*cdf0e10cSrcweir 		 }
569*cdf0e10cSrcweir 		 catch (Exception e) {
570*cdf0e10cSrcweir 		     System.out.println("Error:"+e);
571*cdf0e10cSrcweir 		      throw new IOException("Xmerge Exception");
572*cdf0e10cSrcweir 		     }
573*cdf0e10cSrcweir 	     }
574*cdf0e10cSrcweir 	     else{
575*cdf0e10cSrcweir 
576*cdf0e10cSrcweir 		 try {
577*cdf0e10cSrcweir 		      //Check to see if jar contains a plugin Impl
578*cdf0e10cSrcweir 		         ConverterInfoMgr.addPlugIn(ciEnum);
579*cdf0e10cSrcweir 		         ConverterFactory cf = new ConverterFactory();
580*cdf0e10cSrcweir 			 Convert cv = cf.getConverter(ConverterInfoMgr.findConverterInfo(sdMime,offMime),true);
581*cdf0e10cSrcweir 			 if (cv == null) {
582*cdf0e10cSrcweir 			     System.out.println("\nNo plug-in exists to convert to <staroffice/sxw> from <specified format>");
583*cdf0e10cSrcweir 			 }
584*cdf0e10cSrcweir 			 else
585*cdf0e10cSrcweir 			 {
586*cdf0e10cSrcweir                              /*
587*cdf0e10cSrcweir                              ByteArrayOutputStream bout = new ByteArrayOutputStream();
588*cdf0e10cSrcweir                              byte[][] buf = new byte[1][4096];
589*cdf0e10cSrcweir                              int n=0;
590*cdf0e10cSrcweir                              while ((n=xml.readSomeBytes(buf, 4096))>0)
591*cdf0e10cSrcweir                                  bout.write(buf[0], 0, n);
592*cdf0e10cSrcweir                              ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
593*cdf0e10cSrcweir                              cv.addInputStream(name, bin, false);
594*cdf0e10cSrcweir                             */
595*cdf0e10cSrcweir 			     cv.addInputStream(name,(InputStream)xis,false);
596*cdf0e10cSrcweir 			     //System.out.println("\nConverting");
597*cdf0e10cSrcweir 			     ConvertData dataIn = cv.convert();
598*cdf0e10cSrcweir 			     //System.out.println("\nFinished Converting");
599*cdf0e10cSrcweir 			     Enumeration docEnum = dataIn.getDocumentEnumeration();
600*cdf0e10cSrcweir 			     while (docEnum.hasMoreElements()) {
601*cdf0e10cSrcweir 				 OfficeDocument docIn      = (OfficeDocument)docEnum.nextElement();
602*cdf0e10cSrcweir 
603*cdf0e10cSrcweir 				 docIn.write(newxos,false);
604*cdf0e10cSrcweir 			     }
605*cdf0e10cSrcweir 			     //newxos.write(-1); //EOF character
606*cdf0e10cSrcweir 						       //newxos.flush();
607*cdf0e10cSrcweir 			     newxos.close();
608*cdf0e10cSrcweir 			 }
609*cdf0e10cSrcweir 			 ConverterInfoMgr.removeByJar(jarName);
610*cdf0e10cSrcweir 		 }
611*cdf0e10cSrcweir 		 catch (StackOverflowError sOE){
612*cdf0e10cSrcweir 		      System.out.println("\nERROR : Stack OverFlow. \n Increase of the JRE by adding the following line to the end of the javarc file \n \"-Xss1m\"\n");
613*cdf0e10cSrcweir 		 }
614*cdf0e10cSrcweir 		 catch (Exception e) {
615*cdf0e10cSrcweir 		     System.out.println("Error:"+e);
616*cdf0e10cSrcweir 		     throw new IOException("Xmerge Exception");
617*cdf0e10cSrcweir 		 }
618*cdf0e10cSrcweir 
619*cdf0e10cSrcweir 
620*cdf0e10cSrcweir 	     }
621*cdf0e10cSrcweir 
622*cdf0e10cSrcweir 	}
623*cdf0e10cSrcweir 
624*cdf0e10cSrcweir         private String getPath(URI uri){
625*cdf0e10cSrcweir 	    String path = uri.getPath();
626*cdf0e10cSrcweir 	    String opSys=System.getProperty("os.name");
627*cdf0e10cSrcweir 	    if(opSys.indexOf("Windows")!=-1){
628*cdf0e10cSrcweir 		path= path.replace('/','\\');
629*cdf0e10cSrcweir 		path = path.substring(1);
630*cdf0e10cSrcweir 	    }
631*cdf0e10cSrcweir 	    return path;
632*cdf0e10cSrcweir 	}
633*cdf0e10cSrcweir 
634*cdf0e10cSrcweir 
635*cdf0e10cSrcweir 
636*cdf0e10cSrcweir 
637*cdf0e10cSrcweir 
638*cdf0e10cSrcweir 
639*cdf0e10cSrcweir 
640*cdf0e10cSrcweir         // Implement methods from interface XTypeProvider
641*cdf0e10cSrcweir         public byte[] getImplementationId() {
642*cdf0e10cSrcweir             byte[] byteReturn = {};
643*cdf0e10cSrcweir 
644*cdf0e10cSrcweir             byteReturn = new String( "" + this.hashCode() ).getBytes();
645*cdf0e10cSrcweir 
646*cdf0e10cSrcweir             return( byteReturn );
647*cdf0e10cSrcweir         }
648*cdf0e10cSrcweir 
649*cdf0e10cSrcweir         // Implement method from interface XServiceName
650*cdf0e10cSrcweir         public String getServiceName() {
651*cdf0e10cSrcweir             return( __serviceName );
652*cdf0e10cSrcweir         }
653*cdf0e10cSrcweir 
654*cdf0e10cSrcweir         // Implement methods from interface XServiceInfo
655*cdf0e10cSrcweir         public boolean supportsService(String stringServiceName) {
656*cdf0e10cSrcweir             return( stringServiceName.equals( __serviceName ) );
657*cdf0e10cSrcweir         }
658*cdf0e10cSrcweir 
659*cdf0e10cSrcweir         public String getImplementationName() {
660*cdf0e10cSrcweir             return( _XMergeBridge.class.getName() );
661*cdf0e10cSrcweir         }
662*cdf0e10cSrcweir 
663*cdf0e10cSrcweir         public String[] getSupportedServiceNames() {
664*cdf0e10cSrcweir             String[] stringSupportedServiceNames = { __serviceName };
665*cdf0e10cSrcweir             return( stringSupportedServiceNames );
666*cdf0e10cSrcweir         }
667*cdf0e10cSrcweir     }
668*cdf0e10cSrcweir 
669*cdf0e10cSrcweir     /**
670*cdf0e10cSrcweir      * Returns a factory for creating the service.
671*cdf0e10cSrcweir      * This method is called by the <code>JavaLoader</code>
672*cdf0e10cSrcweir      *
673*cdf0e10cSrcweir      * @return  returns a <code>XSingleServiceFactory</code> for creating the
674*cdf0e10cSrcweir      *          component
675*cdf0e10cSrcweir      *
676*cdf0e10cSrcweir      * @param   implName     the name of the implementation for which a
677*cdf0e10cSrcweir      *                       service is desired
678*cdf0e10cSrcweir      * @param   multiFactory the service manager to be used if needed
679*cdf0e10cSrcweir      * @param   regKey       the registryKey
680*cdf0e10cSrcweir      *
681*cdf0e10cSrcweir      * @see                  com.sun.star.comp.loader.JavaLoader
682*cdf0e10cSrcweir      */
683*cdf0e10cSrcweir     public static XSingleServiceFactory __getServiceFactory(String implName,
684*cdf0e10cSrcweir     XMultiServiceFactory multiFactory,
685*cdf0e10cSrcweir     XRegistryKey regKey) {
686*cdf0e10cSrcweir 	xMSF= multiFactory;
687*cdf0e10cSrcweir         XSingleServiceFactory xSingleServiceFactory = null;
688*cdf0e10cSrcweir         if (implName.equals(_XMergeBridge.class.getName()) ) {
689*cdf0e10cSrcweir             xSingleServiceFactory = FactoryHelper.getServiceFactory(_XMergeBridge.class,
690*cdf0e10cSrcweir             _XMergeBridge.__serviceName,
691*cdf0e10cSrcweir             multiFactory,
692*cdf0e10cSrcweir             regKey);
693*cdf0e10cSrcweir         }
694*cdf0e10cSrcweir 
695*cdf0e10cSrcweir         return xSingleServiceFactory;
696*cdf0e10cSrcweir     }
697*cdf0e10cSrcweir }
698