1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: XMergeBridge.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 * information on the following web page:
33 * http://api.openoffice.org/common/ref/com/sun/star/index.html
37 /*Java Uno Helper Classes*/
38 import com
.sun
.star
.lib
.uno
.adapter
.XInputStreamToInputStreamAdapter
;
39 import com
.sun
.star
.lib
.uno
.adapter
.XOutputStreamToOutputStreamAdapter
;
41 /*StarOffice/Uno Classes*/
42 import com
.sun
.star
.lang
.XMultiServiceFactory
;
43 import com
.sun
.star
.lang
.XServiceInfo
;
44 import com
.sun
.star
.lang
.XTypeProvider
;
45 import com
.sun
.star
.uno
.Type
;
46 import com
.sun
.star
.uno
.UnoRuntime
;
47 import com
.sun
.star
.comp
.loader
.FactoryHelper
;
48 import com
.sun
.star
.lang
.XServiceName
;
49 import com
.sun
.star
.lang
.XSingleServiceFactory
;
50 import com
.sun
.star
.registry
.XRegistryKey
;
51 import com
.sun
.star
.frame
.XConfigManager
;
52 import com
.sun
.star
.xml
.sax
.InputSource
;
53 import com
.sun
.star
.xml
.sax
.XParser
;
54 import com
.sun
.star
.io
.XInputStream
;
55 import com
.sun
.star
.io
.XOutputStream
;
56 import com
.sun
.star
.xml
.sax
.XDocumentHandler
;
57 import com
.sun
.star
.uno
.AnyConverter
;
59 /* Generated from Idls */
60 import com
.sun
.star
.xml
.XImportFilter
;
61 import com
.sun
.star
.xml
.XExportFilter
;
64 import org
.openoffice
.xmerge
.util
.registry
.ConverterInfoReader
;
65 import org
.openoffice
.xmerge
.util
.registry
.ConverterInfo
;
66 import org
.openoffice
.xmerge
.util
.registry
.ConverterInfoMgr
;
67 import org
.openoffice
.xmerge
.Convert
;
68 import org
.openoffice
.xmerge
.ConverterFactory
;
69 import org
.openoffice
.xmerge
.Document
;
70 import org
.openoffice
.xmerge
.ConvertData
;
71 import org
.openoffice
.xmerge
.converter
.xml
.OfficeDocument
;
73 import java
.util
.Enumeration
;
75 import javax
.xml
.parsers
.*;
79 /** This outer class provides an inner class to implement the service
80 * description, a method to instantiate the
81 * component on demand (__getServiceFactory()), and a method to give
82 * information about the component (__writeRegistryServiceInfo()).
84 public class XMergeBridge
{
87 private static XMultiServiceFactory xMSF
;
88 private static XDocumentHandler exportDocHandler
=null;
89 private static XInputStream xInStream
=null;
90 private static XOutputStream xOutStream
=null;
91 private static String udJarPath
=null;
92 private static XOutputStream xos
= null;
93 private static XOutputStreamToOutputStreamAdapter adaptedStream
=null;
94 private static String offMime
=null;
95 private static String sdMime
=null;
96 private static String sFileName
=null;
97 private static String sURL
="";
99 //private static FileOutputStream adaptedStream =null;
101 /** This inner class provides the component as a concrete implementation
102 * of the service description. It implements the needed interfaces.
103 * @implements XTypeProvider
105 static public class _XMergeBridge
implements
113 /** The component will be registered under this name.
115 static private final String __serviceName
= "com.sun.star.documentconversion.XMergeBridge";
119 public com
.sun
.star
.uno
.Type
[] getTypes() {
120 Type
[] typeReturn
= {};
123 typeReturn
= new Type
[] {
124 new Type( XTypeProvider
.class ),
125 new Type( XImportFilter
.class ),
126 new Type( XExportFilter
.class ),
127 new Type( XServiceName
.class ),
128 new Type( XServiceInfo
.class ) };
130 catch( Exception exception
) {
134 return( typeReturn
);
137 String
getFileName(String origName
)
142 if(origName
.equalsIgnoreCase(""))
145 if (origName
.lastIndexOf("/")>=0){
146 origName
=origName
.substring(origName
.lastIndexOf("/")+1,origName
.length());
148 if (origName
.lastIndexOf(".")>=0){
149 name
= origName
.substring(0,(origName
.lastIndexOf(".")));
164 public boolean importer(com
.sun
.star
.beans
.PropertyValue
[] aSourceData
,
165 com
.sun
.star
.xml
.sax
.XDocumentHandler xDocHandler
,
166 java
.lang
.String
[] msUserData
) throws com
.sun
.star
.uno
.RuntimeException
{
168 System.out.println("\nFound the Importer!\n");
170 System.out.println("\n"+msUserData[0]);
171 System.out.println("\n"+msUserData[1]);
172 System.out.println("\n"+msUserData[2]);
173 System.out.println("\n"+msUserData[3]);
174 System.out.println("\n"+msUserData[4]);
175 System.out.println("\n"+msUserData[5]);
179 String sDirectory
= null;
180 String udConvertClass
=msUserData
[0];
181 udJarPath
=msUserData
[1];
182 String udImport
=msUserData
[2];
183 String udExport
=msUserData
[3];
184 offMime
=msUserData
[4];
185 sdMime
= msUserData
[5];
186 com
.sun
.star
.io
.XInputStream xis
=null;
187 com
.sun
.star
.beans
.PropertyValue
[] pValue
= aSourceData
;
189 for (int i
= 0 ; i
< pValue
.length
; i
++)
192 //System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value;
195 //System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value);
196 if (pValue
[i
].Name
.compareTo("InputStream")==0){
197 xis
=(com
.sun
.star
.io
.XInputStream
)AnyConverter
.toObject(new Type(com
.sun
.star
.io
.XInputStream
.class), pValue
[i
].Value
);
199 if (pValue
[i
].Name
.compareTo("FileName")==0){
200 sFileName
=(String
)AnyConverter
.toObject(new Type(java
.lang
.String
.class), pValue
[i
].Value
);
204 catch(com
.sun
.star
.lang
.IllegalArgumentException AnyExec
){
205 System
.out
.println("\nIllegalArgumentException "+AnyExec
);
215 Object xCfgMgrObj
=xMSF
.createInstance("com.sun.star.config.SpecialConfigManager");
216 XConfigManager xCfgMgr
= (XConfigManager
) UnoRuntime
.queryInterface(
217 XConfigManager
.class , xCfgMgrObj
);
218 String PathString
=xCfgMgr
.substituteVariables("$(progurl)" );
219 PathString
= PathString
.concat("/");
220 udJarPath
= PathString
.concat(udJarPath
);
222 Object xPipeObj
=xMSF
.createInstance("com.sun.star.io.Pipe");
223 xInStream
= (XInputStream
) UnoRuntime
.queryInterface(
224 XInputStream
.class , xPipeObj
);
225 xOutStream
= (XOutputStream
) UnoRuntime
.queryInterface(
226 XOutputStream
.class , xPipeObj
);
227 convert (xis
,xOutStream
,false,udJarPath
,sFileName
,offMime
,sdMime
);
228 Object xSaxParserObj
=xMSF
.createInstance("com.sun.star.xml.sax.Parser");
230 XParser xParser
= (XParser
) UnoRuntime
.queryInterface(
231 XParser
.class , xSaxParserObj
);
233 System
.out
.println("\nParser creation Failed");
235 xOutStream
.closeOutput();
236 InputSource aInput
= new InputSource();
237 if (sFileName
==null){
240 aInput
.sSystemId
= sFileName
;
241 aInput
.aInputStream
=xInStream
;
242 xParser
.setDocumentHandler ( xDocHandler
);
244 xParser
.parseStream ( aInput
);
245 xOutStream
.closeOutput();
246 xInStream
.closeInput();
249 catch (IOException e
){
250 //System.out.println("XMergeBridge IO Exception "+e.getMessage());
254 //System.out.println("XMergeBridge Exception "+e+" "+e.getMessage());
260 public boolean exporter(com
.sun
.star
.beans
.PropertyValue
[] aSourceData
,
261 java
.lang
.String
[] msUserData
) throws com
.sun
.star
.uno
.RuntimeException
{
264 System.out.println("\nFound the Exporter!\n");
266 System.out.println("\n"+msUserData[0]);
267 System.out.println("\n"+msUserData[1]);
268 System.out.println("\n"+msUserData[2]);
269 System.out.println("\n"+msUserData[3]);
270 System.out.println("\n"+msUserData[4]);
271 System.out.println("\n"+msUserData[5]);
275 String sDirectory
= null;
277 String udConvertClass
=msUserData
[0];
278 udJarPath
=msUserData
[1];
279 String udImport
=msUserData
[2];
280 String udExport
=msUserData
[3];
281 offMime
=msUserData
[4];
282 sdMime
= msUserData
[5];
284 com
.sun
.star
.beans
.PropertyValue
[] pValue
= aSourceData
;
285 for (int i
= 0 ; i
< pValue
.length
; i
++)
288 //System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value);
292 //System.out.println("\n"+pValue[i].Name+" "+pValue[i].Value);
293 if (pValue
[i
].Name
.compareTo("OutputStream")==0){
294 xos
=(com
.sun
.star
.io
.XOutputStream
)AnyConverter
.toObject(new Type(com
.sun
.star
.io
.XOutputStream
.class), pValue
[i
].Value
);
295 // System.out.println(pValue[i].Name+" "+xos);
297 if (pValue
[i
].Name
.compareTo("FileName")==0){
298 sFileName
=(String
)AnyConverter
.toObject(new Type(java
.lang
.String
.class), pValue
[i
].Value
);
299 //System.out.println(pValue[i].Name+" "+sFileName);
301 if (pValue
[i
].Name
.compareTo("URL")==0){
302 sURL
=(String
)AnyConverter
.toObject(new Type(java
.lang
.String
.class), pValue
[i
].Value
);
303 // System.out.println("\nMediaDescriptor url "+pValue[i].Name+" "+sURL);
306 if (pValue
[i
].Name
.compareTo("Title")==0){
308 title
=(String
)AnyConverter
.toObject(new Type(java
.lang
.String
.class), pValue
[i
].Value
);
309 //System.out.println(pValue[i].Name+" "+title);
312 catch(com
.sun
.star
.lang
.IllegalArgumentException AnyExec
){
313 System
.out
.println("\nIllegalArgumentException "+AnyExec
);
324 Object xCfgMgrObj
=xMSF
.createInstance("com.sun.star.config.SpecialConfigManager");
325 XConfigManager xCfgMgr
= (XConfigManager
) UnoRuntime
.queryInterface(
326 XConfigManager
.class , xCfgMgrObj
);
328 String PathString
=xCfgMgr
.substituteVariables("$(progurl)" );
329 PathString
= PathString
.concat("/");
330 udJarPath
= PathString
.concat(udJarPath
);
332 Object xPipeObj
=xMSF
.createInstance("com.sun.star.io.Pipe");
333 xInStream
= (XInputStream
) UnoRuntime
.queryInterface(
334 XInputStream
.class , xPipeObj
);
335 xOutStream
= (XOutputStream
) UnoRuntime
.queryInterface(
336 XOutputStream
.class , xPipeObj
);
339 System
.out
.println("Exception "+e
);
346 public String
replace(String origString
, String origChar
, String replaceChar
){
348 int index
=origString
.indexOf(origChar
);
351 String first
=origString
.substring(0,index
);
352 first
=first
.concat(replaceChar
);
353 tmp
=tmp
.concat(first
);
354 origString
=origString
.substring(index
+1,origString
.length());
355 index
=origString
.indexOf(origChar
);
357 tmp
=tmp
.concat(origString
);
366 public String
needsMask(String origString
){
367 if (origString
.indexOf("&")!=-1){
368 origString
=replace(origString
,"&","&");
370 if (origString
.indexOf("\"")!=-1){
371 origString
=replace(origString
,"\"",""");
373 if (origString
.indexOf("<")!=-1){
374 origString
=replace(origString
,"<","<");
376 if (origString
.indexOf(">")!=-1){
377 origString
=replace(origString
,">",">");
385 public void startDocument () {
386 //System.out.println("\nStart Document!");
389 public void endDocument()throws com
.sun
.star
.uno
.RuntimeException
393 xOutStream
.closeOutput();
394 convert (xInStream
,xos
,true,udJarPath
,sURL
,offMime
,sdMime
);
397 catch (IOException e
){
398 //System.out.println("Exception "+e);
399 throw new com
.sun
.star
.uno
.RuntimeException(e
.getMessage());
403 //System.out.println("Exception "+e);
404 throw new com
.sun
.star
.uno
.RuntimeException("Xmerge Exception");
411 public void startElement (String str
, com
.sun
.star
.xml
.sax
.XAttributeList xattribs
)
417 str
= str
.concat(" ");
418 int len
=xattribs
.getLength();
419 for (short i
=0;i
<len
;i
++)
421 str
=str
.concat(xattribs
.getNameByIndex(i
));
422 str
=str
.concat("=\"");
423 str
=str
.concat(needsMask(xattribs
.getValueByIndex(i
)));
424 str
=str
.concat("\" ");
428 // System.out.println(str);
430 xOutStream
.writeBytes(str
.getBytes("UTF-8"));
433 System
.out
.println("\n"+e
);
438 public void endElement(String str
){
440 str
="</".concat(str
);
443 xOutStream
.writeBytes(str
.getBytes("UTF-8"));
447 System
.out
.println("\n"+e
);
452 public void characters(String str
){
455 xOutStream
.writeBytes(str
.getBytes("UTF-8"));
458 System
.out
.println("\n"+e
);
464 public void ignorableWhitespace(String str
){
468 public void processingInstruction(String aTarget
, String aData
){
472 public void setDocumentLocator(com
.sun
.star
.xml
.sax
.XLocator xLocator
){
481 public void convert (com
.sun
.star
.io
.XInputStream xml
,com
.sun
.star
.io
.XOutputStream device
,
482 boolean convertFromOffice
,String pluginUrl
,String FileName
,String offMime
,String sdMime
) throws com
.sun
.star
.uno
.RuntimeException
, IOException
{
484 String jarName
= pluginUrl
;
485 String name
= getFileName(FileName
);
487 ConverterInfo converterInfo
= null;
488 Enumeration ciEnum
= null;
490 XInputStreamToInputStreamAdapter xis
=new XInputStreamToInputStreamAdapter(xml
);
493 XOutputStreamToOutputStreamAdapter newxos
=new XOutputStreamToOutputStreamAdapter(device
);
495 ConverterInfoReader cir
= new ConverterInfoReader(jarName
,false);
496 ciEnum
=cir
.getConverterInfoEnumeration();
498 catch (ParserConfigurationException pexc
){
499 System
.out
.println("Error:"+pexc
);
501 catch ( org
.xml
.sax
.SAXException pexc
){
502 System
.out
.println("Error:"+pexc
);
505 System
.out
.println("Error:"+e
);
507 ConverterInfoMgr
. removeByJar(jarName
);
508 if (convertFromOffice
)
513 //Check to see if jar contains a plugin Impl
515 ConverterInfoMgr
.addPlugIn(ciEnum
);
516 ConverterFactory cf
= new ConverterFactory();
518 Convert cv
= cf
.getConverter(ConverterInfoMgr
.findConverterInfo(sdMime
,offMime
),false);
520 System
.out
.println("\nNo plug-in exists to convert from <staroffice/sxw> to <specified format> ");
525 cv
.addInputStream(name
,(InputStream
)xis
,false);
526 ConvertData dataOut
= cv
.convert();
528 Enumeration docEnum
= dataOut
.getDocumentEnumeration();
530 if (docEnum
.hasMoreElements()){
531 Document docOut
= (Document
)docEnum
.nextElement();
532 String fileName
= docOut
.getFileName();
533 docOut
.write(newxos
);
540 while (docEnum
.hasMoreElements() && sURL
.startsWith("file:")) {
541 //URI uri=new URI(sFileName);
542 URI uri
=new URI(sURL
);
543 String newFileName
= getPath(uri
);
546 //System.out.println("\nURI: "+uri.getPath());
548 if (newFileName
.lastIndexOf(".")!=-1){
549 newFile
=new File(newFileName
.substring(0,newFileName
.lastIndexOf("."))+String
.valueOf(i
)+newFileName
.substring(newFileName
.lastIndexOf(".")));
552 newFile
=new File(newFileName
.concat(String
.valueOf(i
)));
555 FileOutputStream fos
= new FileOutputStream(newFile
);
556 docOut
= (Document
)docEnum
.nextElement();
557 fileName
= docOut
.getFileName();
567 ConverterInfoMgr
.removeByJar(jarName
);
569 catch (StackOverflowError sOE
){
570 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");
573 catch (Exception e
) {
574 System
.out
.println("Error:"+e
);
575 throw new IOException("Xmerge Exception");
581 //Check to see if jar contains a plugin Impl
582 ConverterInfoMgr
.addPlugIn(ciEnum
);
583 ConverterFactory cf
= new ConverterFactory();
584 Convert cv
= cf
.getConverter(ConverterInfoMgr
.findConverterInfo(sdMime
,offMime
),true);
586 System
.out
.println("\nNo plug-in exists to convert to <staroffice/sxw> from <specified format>");
591 ByteArrayOutputStream bout = new ByteArrayOutputStream();
592 byte[][] buf = new byte[1][4096];
594 while ((n=xml.readSomeBytes(buf, 4096))>0)
595 bout.write(buf[0], 0, n);
596 ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
597 cv.addInputStream(name, bin, false);
599 cv
.addInputStream(name
,(InputStream
)xis
,false);
600 //System.out.println("\nConverting");
601 ConvertData dataIn
= cv
.convert();
602 //System.out.println("\nFinished Converting");
603 Enumeration docEnum
= dataIn
.getDocumentEnumeration();
604 while (docEnum
.hasMoreElements()) {
605 OfficeDocument docIn
= (OfficeDocument
)docEnum
.nextElement();
607 docIn
.write(newxos
,false);
609 //newxos.write(-1); //EOF character
613 ConverterInfoMgr
.removeByJar(jarName
);
615 catch (StackOverflowError sOE
){
616 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");
618 catch (Exception e
) {
619 System
.out
.println("Error:"+e
);
620 throw new IOException("Xmerge Exception");
628 private String
getPath(URI uri
){
629 String path
= uri
.getPath();
630 String opSys
=System
.getProperty("os.name");
631 if(opSys
.indexOf("Windows")!=-1){
632 path
= path
.replace('/','\\');
633 path
= path
.substring(1);
644 // Implement methods from interface XTypeProvider
645 public byte[] getImplementationId() {
646 byte[] byteReturn
= {};
648 byteReturn
= new String( "" + this.hashCode() ).getBytes();
650 return( byteReturn
);
653 // Implement method from interface XServiceName
654 public String
getServiceName() {
655 return( __serviceName
);
658 // Implement methods from interface XServiceInfo
659 public boolean supportsService(String stringServiceName
) {
660 return( stringServiceName
.equals( __serviceName
) );
663 public String
getImplementationName() {
664 return( _XMergeBridge
.class.getName() );
667 public String
[] getSupportedServiceNames() {
668 String
[] stringSupportedServiceNames
= { __serviceName
};
669 return( stringSupportedServiceNames
);
674 * Returns a factory for creating the service.
675 * This method is called by the <code>JavaLoader</code>
677 * @return returns a <code>XSingleServiceFactory</code> for creating the
680 * @param implName the name of the implementation for which a
682 * @param multiFactory the service manager to be used if needed
683 * @param regKey the registryKey
685 * @see com.sun.star.comp.loader.JavaLoader
687 public static XSingleServiceFactory
__getServiceFactory(String implName
,
688 XMultiServiceFactory multiFactory
,
689 XRegistryKey regKey
) {
691 XSingleServiceFactory xSingleServiceFactory
= null;
692 if (implName
.equals(_XMergeBridge
.class.getName()) ) {
693 xSingleServiceFactory
= FactoryHelper
.getServiceFactory(_XMergeBridge
.class,
694 _XMergeBridge
.__serviceName
,
699 return xSingleServiceFactory
;
703 * Writes the service information into the given registry key.
704 * This method is called by the <code>JavaLoader</code>
706 * @return returns true if the operation succeeded
707 * @param regKey the registryKey
708 * @see com.sun.star.comp.loader.JavaLoader
710 public static boolean __writeRegistryServiceInfo(XRegistryKey regKey
) {
712 return FactoryHelper
.writeRegistryServiceInfo(_XMergeBridge
.class.getName(),
713 _XMergeBridge
.__serviceName
, regKey
);