LibreOffice Module xmerge (master)  1
ActiveSyncDriver.java
Go to the documentation of this file.
1 /*
2  * This file is part of the LibreOffice project.
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  *
8  * This file incorporates work covered by the following license notice:
9  *
10  * Licensed to the Apache Software Foundation (ASF) under one or more
11  * contributor license agreements. See the NOTICE file distributed
12  * with this work for additional information regarding copyright
13  * ownership. The ASF licenses this file to you under the Apache
14  * License, Version 2.0 (the "License"); you may not use this file
15  * except in compliance with the License. You may obtain a copy of
16  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
17  */
18 
19 package org.openoffice.xmerge.util;
20 
21 import java.io.File;
22 import java.io.FileOutputStream;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 
26 import java.util.StringTokenizer;
27 import java.util.NoSuchElementException;
28 
36 
37 public class ActiveSyncDriver {
38  public static void main(String[] args) {
39  if (args.length != 4) {
40  return;
41  }
42 
44 
45  try {
46  // At the moment can't really signal back to the calling DLL
47  asd.Convert(args[0], args[1], args[2], args[3]);
48  }
49  catch (Exception e) {
50  return;
51  }
52  }
53 
54  private boolean Convert(String srcMime, String dstMime, String srcFile,
55  String dstFile) throws Exception {
56  /*
57  * The classpath passed in by XMergeSync.dll should contain all of the
58  * jar files, but at the least it will contain xmerge.jar, so strip off
59  * the xmerge.jar portion and use the remainder to provide a root for
60  * the Pocket Word and Pocket Excel plugins.
61  */
62  String ooClassDir = null;
63  String strClassPath = System.getProperty("java.class.path");
64 
65  StringTokenizer st = new StringTokenizer(strClassPath, ";");
66 
67  // There should be at least one element, but just in case
68  while (st.hasMoreTokens()) {
69  String s = st.nextToken();
70 
71  if (s.endsWith("xmerge.jar")) {
72  ooClassDir = s.substring(0, s.indexOf("xmerge.jar"));
73  }
74  }
75 
76  if (ooClassDir == null) {
77  return true;
78  }
79 
80  /*
81  * The XMergeSync.dll should will have checked for the presence of the
82  * jars at the same location already.
83  *
84  * Because they can be installed separately, though, the MIME types need
85  * to be check to see which one to load.
86  */
87  File pluginJar;
88  if (srcMime.equals("staroffice/sxw") || srcMime.equals("application/x-pocket-word")) {
89  pluginJar = new File(ooClassDir + "pocketWord.jar");
90  } else {
91  if (srcMime.equals("staroffice/sxc") || srcMime.equals("application/x-pocket-excel")) {
92  pluginJar = new File(ooClassDir + "pexcel.jar");
93  } else {
94  return false;
95  }
96  }
97 
98  ConverterInfoReader cirPlugin = new ConverterInfoReader(pluginJar.toURI().toURL().toString(), false);
99 
101 
103  Convert conv = cf.getConverter(srcMime, dstMime);
104 
105  if (conv == null) {
106  return false;
107  }
108 
109  // Everything is registered so do the conversion
110  boolean bOK = true;
111  FileInputStream fis = null;
112  FileOutputStream fos = null;
113  try {
114  fis = new FileInputStream(srcFile);
115  conv.addInputStream(srcFile, fis);
116  try {
117  fos = new FileOutputStream(dstFile);
118  ConvertData dataOut = conv.convert();
119 
120  // Get the document and write it out.
121  Document doc = (Document)dataOut.getDocumentEnumeration().next();
122  doc.write(fos);
123  fos.flush();
124  } finally {
125  if (fos != null)
126  fos.close();
127  }
128  } catch (IOException e) {
129  bOK = false;
130  } catch (NullPointerException e) {
131  bOK = false;
132  } catch (ConvertException e) {
133  bOK = false;
134  } catch (NoSuchElementException e) {
135  bOK = false;
136  } finally {
137  if (fis != null)
138  fis.close();
139  }
140 
141  return bOK;
142  }
143 }
Factory that provides access to.
Provides general purpose utilities.
Provides an interface for plug-in registration.
Convert getConverter(String mimeTypeIn, String mimeTypeOut)
Returns the.
exports com.sun.star. java
boolean Convert(String srcMime, String dstMime, String srcFile, String dstFile)
Manages the converter plug-ins that are currently active.
static void addPlugIn(ConverterInfo ci)
Adds a converter plug-in to the registry.
Iterator< Object > getDocumentEnumeration()
Gets an.
void write(OutputStream os)
Writes out the.
Provides interfaces for converting between two.
Definition: Convert.java:19
void addInputStream(String name, InputStream is)
Adds an.
Definition: Convert.java:80
ConvertData convert()
Convert the input specified in calls to the.
Definition: Convert.java:181
Iterator< ConverterInfo > getConverterInfoEnumeration()
Returns an.