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