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